]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
git-svn-id: https://edk2.svn.sourceforge.net/svnroot/edk2/trunk/edk2@671 6f19259b...
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / Tools.java
CommitLineData
a13899c5 1/** @file\r
2 \r
3 The file is used to provides some useful interfaces \r
4 \r
5 Copyright (c) 2006, Intel Corporation\r
6 All rights reserved. This program and the accompanying materials\r
7 are licensed and made available under the terms and conditions of the BSD License\r
8 which accompanies this distribution. The full text of the license may be found at\r
9 http://opensource.org/licenses/bsd-license.php\r
10 \r
11 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
12 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
13 \r
14 **/\r
15\r
16package org.tianocore.frameworkwizard.common;\r
17\r
18import java.io.File;\r
19import java.text.SimpleDateFormat;\r
20import java.util.Date;\r
21import java.util.List;\r
22import java.util.UUID;\r
23import java.util.Vector;\r
24\r
25import javax.swing.JComboBox;\r
26import javax.swing.JOptionPane;\r
27\r
28/**\r
29 The class is used to provides some useful interfaces \r
30 \r
31 **/\r
32public class Tools {\r
33\r
34 //\r
35 // The dir user selected to create new package in\r
36 //\r
37 public static String dirForNewSpd = null;\r
38\r
39 /**\r
40 Used for test\r
41 \r
42 @param args\r
43 \r
44 **/\r
45 public static void main(String[] args) {\r
46 System.out.println(getCurrentDateTime());\r
47 }\r
48\r
49 /**\r
50 Get current date and time and format it as "yyyy-MM-dd HH:mm"\r
51 \r
52 @return formatted current date and time\r
53 \r
54 **/\r
55 public static String getCurrentDateTime() {\r
56 Date now = new Date(System.currentTimeMillis());\r
57 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r
58 return sdf.format(now);\r
59 }\r
60\r
61 /**\r
62 Generate a UUID\r
63 \r
64 @return the created UUID\r
65 \r
66 **/\r
67 public static String generateUuidString() {\r
68 return UUID.randomUUID().toString();\r
69 }\r
70\r
71 /**\r
72 Use current file separator in the path\r
73 \r
74 @param strPath\r
75 @return\r
76 \r
77 **/\r
78 public static String convertPathToCurrentOsType(String strPath) {\r
79 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r
80 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r
81 return strPath;\r
82 }\r
83\r
84 /**\r
85 Use Unix file separator in the path\r
86 \r
87 @param strPath\r
88 @return\r
89 \r
90 **/\r
91 public static String convertPathToUnixType(String strPath) {\r
92 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);\r
93 return strPath;\r
94 }\r
95\r
96 /**\r
97 Use Dos file separator in the path\r
98 \r
99 @param strPath\r
100 @return\r
101 \r
102 **/\r
103 public static String convertPathToDosType(String strPath) {\r
104 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);\r
105 return strPath;\r
106 }\r
107\r
108 /**\r
109 Get all system properties and output to the console\r
110 \r
111 **/\r
112 public static void getSystemProperties() {\r
113 System.out.println(System.getProperty("java.class.version"));\r
114 System.out.println(System.getProperty("java.class.path"));\r
115 System.out.println(System.getProperty("java.ext.dirs"));\r
116 System.out.println(System.getProperty("os.name"));\r
117 System.out.println(System.getProperty("os.arch"));\r
118 System.out.println(System.getProperty("os.version"));\r
119 System.out.println(System.getProperty("file.separator"));\r
120 System.out.println(System.getProperty("path.separator"));\r
121 System.out.println(System.getProperty("line.separator"));\r
122 System.out.println(System.getProperty("user.name"));\r
123 System.out.println(System.getProperty("user.home"));\r
124 System.out.println(System.getProperty("user.dir"));\r
125 System.out.println(System.getProperty("PATH"));\r
126\r
127 System.out.println(System.getenv("PROCESSOR_REVISION"));\r
128 }\r
129\r
130 /**\r
131 Generate selection items for JComboBox by input vector\r
132 \r
133 **/\r
134 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {\r
135 if (jcb != null) {\r
136 jcb.removeAllItems();\r
137 }\r
138 if (vector != null) {\r
139 for (int index = 0; index < vector.size(); index++) {\r
140 jcb.addItem(vector.elementAt(index));\r
141 }\r
142 }\r
143 }\r
144 \r
145 /**\r
146 Get path only from a path\r
147 \r
148 @param filePath\r
149 @return\r
150 \r
151 **/\r
152 public static String getFilePathOnly(String filePath) {\r
153 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());\r
154 if (path.endsWith(DataType.FILE_SEPARATOR)) {\r
155 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());\r
156 }\r
157 \r
158 return path;\r
159 }\r
160 \r
161 \r
162 /**\r
163 Get file name from a path\r
164 \r
165 @param filePath\r
166 @return\r
167 \r
168 **/\r
169 public static String getFileNameOnly(String filePath) {\r
170 File f = new File(filePath);\r
171 return f.getAbsoluteFile().getName();\r
172 }\r
173 \r
174 public static String getFileNameWithoutExt(String filePath) {\r
175 filePath = getFileNameOnly(filePath);\r
176 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));\r
177 return filePath;\r
178 }\r
179\r
180 /**\r
181 Get relative path\r
182 \r
183 @param wholePath\r
184 @param commonPath\r
185 @return wholePath - commonPath \r
186 \r
187 **/\r
188 public static String getRelativePath(String wholePath, String commonPath) {\r
189 String path = "";\r
190 int i = 0;\r
191 i = wholePath.indexOf(commonPath);\r
192 if (i > -1) {\r
193 i = i + commonPath.length();\r
194 } else {\r
195 return "";\r
196 }\r
197 path = wholePath.substring(i);\r
198 //\r
199 // remove file separator of head\r
200 //\r
201 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {\r
202 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r
203 }\r
204 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {\r
205 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r
206 }\r
207 //\r
208 // remove file separator of rear\r
209 //\r
210 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r
211 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
212 }\r
213 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r
214 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
215 }\r
216 //\r
217 // convert to UNIX format\r
218 //\r
219 path = Tools.convertPathToUnixType(path);\r
220 return path;\r
221 }\r
222\r
223 /**\r
224 Convert List ot Vector\r
225 \r
226 @param list\r
227 @return\r
228 \r
229 **/\r
230 public static Vector<String> convertListToVector(List list) {\r
231 Vector<String> v = new Vector<String>();\r
232 if (list != null && list.size() > 0) {\r
233 for (int index = 0; index < list.size(); index++) {\r
234 v.addElement(list.get(index).toString());\r
235 }\r
236 }\r
237 return v;\r
238 }\r
239\r
240 /**\r
241 If the input path missing ext, append the ext to the path\r
242 \r
243 @param path\r
244 @param type\r
245 @return\r
246 \r
247 **/\r
248 public static String addPathExt(String path, int type) {\r
249 String match = "";\r
250 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {\r
251 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;\r
252 }\r
253 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {\r
254 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;\r
255 }\r
256 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {\r
257 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;\r
258 }\r
259 if (path.length() <= match.length()) {\r
260 path = path + match;\r
261 return path;\r
262 }\r
263 if (!(path.substring(path.length() - match.length())).equals(match)) {\r
264 path = path + match;\r
265 }\r
266 return path;\r
267 }\r
268 \r
269 /**\r
270 Show a message box\r
271 \r
272 @param arg0\r
273 \r
274 **/\r
275 public static void showInformationMessage(String arg0) {\r
276 JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r
277 }\r
278}\r