]>
Commit | Line | Data |
---|---|---|
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 | |
16 | package org.tianocore.frameworkwizard.common;\r | |
17 | \r | |
18 | import java.io.File;\r | |
19 | import java.text.SimpleDateFormat;\r | |
20 | import java.util.Date;\r | |
21 | import java.util.List;\r | |
22 | import java.util.UUID;\r | |
23 | import java.util.Vector;\r | |
24 | \r | |
92e29378 | 25 | import javax.swing.DefaultListModel;\r |
a13899c5 | 26 | import javax.swing.JComboBox;\r |
92e29378 | 27 | import javax.swing.JList;\r |
a13899c5 | 28 | import javax.swing.JOptionPane;\r |
29 | \r | |
92e29378 | 30 | import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;\r |
31 | import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r | |
32 | import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r | |
33 | \r | |
a13899c5 | 34 | /**\r |
35 | The class is used to provides some useful interfaces \r | |
36 | \r | |
37 | **/\r | |
38 | public class Tools {\r | |
39 | \r | |
40 | //\r | |
41 | // The dir user selected to create new package in\r | |
42 | //\r | |
43 | public static String dirForNewSpd = null;\r | |
44 | \r | |
45 | /**\r | |
46 | Used for test\r | |
47 | \r | |
48 | @param args\r | |
49 | \r | |
50 | **/\r | |
51 | public static void main(String[] args) {\r | |
52 | System.out.println(getCurrentDateTime());\r | |
92e29378 | 53 | // Vector<String> v = new Vector<String>();\r |
54 | // Vector<String> v1 = new Vector<String>();\r | |
55 | // \r | |
56 | // v.addElement("CAC");\r | |
57 | // v1.addElement("1111");\r | |
58 | // v.addElement("1AC");\r | |
59 | // v1.addElement("2222");\r | |
60 | // v.addElement("ABC");\r | |
61 | // v1.addElement("3333");\r | |
62 | // v.addElement("0C");\r | |
63 | // v1.addElement("4444");\r | |
64 | // v.addElement("AAC");\r | |
65 | // v1.addElement("5555");\r | |
66 | // Vector<Integer> vs = new Vector<Integer>();\r | |
67 | // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);\r | |
68 | // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));\r | |
69 | // \r | |
70 | // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);\r | |
71 | // Tools.sortVectorString(v, DataType.Sort_Type_Descending);\r | |
a13899c5 | 72 | }\r |
73 | \r | |
74 | /**\r | |
75 | Get current date and time and format it as "yyyy-MM-dd HH:mm"\r | |
76 | \r | |
77 | @return formatted current date and time\r | |
78 | \r | |
79 | **/\r | |
80 | public static String getCurrentDateTime() {\r | |
81 | Date now = new Date(System.currentTimeMillis());\r | |
82 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r | |
83 | return sdf.format(now);\r | |
84 | }\r | |
85 | \r | |
86 | /**\r | |
87 | Generate a UUID\r | |
88 | \r | |
89 | @return the created UUID\r | |
90 | \r | |
91 | **/\r | |
92 | public static String generateUuidString() {\r | |
93 | return UUID.randomUUID().toString();\r | |
94 | }\r | |
95 | \r | |
96 | /**\r | |
97 | Use current file separator in the path\r | |
98 | \r | |
99 | @param strPath\r | |
100 | @return\r | |
101 | \r | |
102 | **/\r | |
103 | public static String convertPathToCurrentOsType(String strPath) {\r | |
104 | strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r | |
105 | strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r | |
106 | return strPath;\r | |
107 | }\r | |
108 | \r | |
109 | /**\r | |
110 | Use Unix file separator in the path\r | |
111 | \r | |
112 | @param strPath\r | |
113 | @return\r | |
114 | \r | |
115 | **/\r | |
116 | public static String convertPathToUnixType(String strPath) {\r | |
117 | strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);\r | |
118 | return strPath;\r | |
119 | }\r | |
120 | \r | |
121 | /**\r | |
122 | Use Dos file separator in the path\r | |
123 | \r | |
124 | @param strPath\r | |
125 | @return\r | |
126 | \r | |
127 | **/\r | |
128 | public static String convertPathToDosType(String strPath) {\r | |
129 | strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);\r | |
130 | return strPath;\r | |
131 | }\r | |
132 | \r | |
133 | /**\r | |
134 | Get all system properties and output to the console\r | |
135 | \r | |
136 | **/\r | |
137 | public static void getSystemProperties() {\r | |
138 | System.out.println(System.getProperty("java.class.version"));\r | |
139 | System.out.println(System.getProperty("java.class.path"));\r | |
140 | System.out.println(System.getProperty("java.ext.dirs"));\r | |
141 | System.out.println(System.getProperty("os.name"));\r | |
142 | System.out.println(System.getProperty("os.arch"));\r | |
143 | System.out.println(System.getProperty("os.version"));\r | |
144 | System.out.println(System.getProperty("file.separator"));\r | |
145 | System.out.println(System.getProperty("path.separator"));\r | |
146 | System.out.println(System.getProperty("line.separator"));\r | |
147 | System.out.println(System.getProperty("user.name"));\r | |
148 | System.out.println(System.getProperty("user.home"));\r | |
149 | System.out.println(System.getProperty("user.dir"));\r | |
150 | System.out.println(System.getProperty("PATH"));\r | |
151 | \r | |
152 | System.out.println(System.getenv("PROCESSOR_REVISION"));\r | |
153 | }\r | |
154 | \r | |
155 | /**\r | |
156 | Generate selection items for JComboBox by input vector\r | |
157 | \r | |
158 | **/\r | |
159 | public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {\r | |
160 | if (jcb != null) {\r | |
161 | jcb.removeAllItems();\r | |
162 | }\r | |
163 | if (vector != null) {\r | |
164 | for (int index = 0; index < vector.size(); index++) {\r | |
165 | jcb.addItem(vector.elementAt(index));\r | |
166 | }\r | |
167 | }\r | |
168 | }\r | |
92e29378 | 169 | \r |
170 | /**\r | |
171 | Generate selection items for JList by input vector\r | |
172 | \r | |
173 | **/\r | |
174 | public static void generateListByVector(JList jl, Vector<String> vector) {\r | |
175 | if (jl != null) {\r | |
176 | DefaultListModel listModel = (DefaultListModel) jl.getModel();\r | |
177 | listModel.removeAllElements();\r | |
178 | \r | |
179 | if (vector != null) {\r | |
180 | for (int index = 0; index < vector.size(); index++) {\r | |
181 | listModel.addElement(vector.get(index));\r | |
182 | }\r | |
183 | }\r | |
184 | \r | |
185 | if (listModel.size() > 0) {\r | |
186 | jl.setSelectedIndex(0);\r | |
187 | }\r | |
188 | }\r | |
189 | }\r | |
79cb6fdb | 190 | \r |
a13899c5 | 191 | /**\r |
192 | Get path only from a path\r | |
79cb6fdb | 193 | \r |
a13899c5 | 194 | @param filePath\r |
195 | @return\r | |
79cb6fdb | 196 | \r |
197 | **/\r | |
a13899c5 | 198 | public static String getFilePathOnly(String filePath) {\r |
199 | String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());\r | |
200 | if (path.endsWith(DataType.FILE_SEPARATOR)) {\r | |
201 | path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());\r | |
202 | }\r | |
79cb6fdb | 203 | \r |
a13899c5 | 204 | return path;\r |
205 | }\r | |
79cb6fdb | 206 | \r |
a13899c5 | 207 | /**\r |
208 | Get file name from a path\r | |
209 | \r | |
210 | @param filePath\r | |
211 | @return\r | |
79cb6fdb | 212 | \r |
213 | **/\r | |
a13899c5 | 214 | public static String getFileNameOnly(String filePath) {\r |
215 | File f = new File(filePath);\r | |
216 | return f.getAbsoluteFile().getName();\r | |
217 | }\r | |
79cb6fdb | 218 | \r |
a13899c5 | 219 | public static String getFileNameWithoutExt(String filePath) {\r |
220 | filePath = getFileNameOnly(filePath);\r | |
221 | filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));\r | |
222 | return filePath;\r | |
223 | }\r | |
224 | \r | |
225 | /**\r | |
226 | Get relative path\r | |
227 | \r | |
228 | @param wholePath\r | |
229 | @param commonPath\r | |
230 | @return wholePath - commonPath \r | |
231 | \r | |
232 | **/\r | |
233 | public static String getRelativePath(String wholePath, String commonPath) {\r | |
234 | String path = "";\r | |
235 | int i = 0;\r | |
236 | i = wholePath.indexOf(commonPath);\r | |
237 | if (i > -1) {\r | |
238 | i = i + commonPath.length();\r | |
239 | } else {\r | |
240 | return "";\r | |
241 | }\r | |
242 | path = wholePath.substring(i);\r | |
243 | //\r | |
244 | // remove file separator of head\r | |
245 | //\r | |
246 | if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {\r | |
247 | path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r | |
248 | }\r | |
249 | if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {\r | |
250 | path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r | |
251 | }\r | |
252 | //\r | |
253 | // remove file separator of rear\r | |
254 | //\r | |
255 | if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r | |
256 | path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r | |
257 | }\r | |
258 | if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r | |
259 | path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r | |
260 | }\r | |
261 | //\r | |
262 | // convert to UNIX format\r | |
263 | //\r | |
264 | path = Tools.convertPathToUnixType(path);\r | |
265 | return path;\r | |
266 | }\r | |
267 | \r | |
268 | /**\r | |
269 | Convert List ot Vector\r | |
270 | \r | |
271 | @param list\r | |
272 | @return\r | |
273 | \r | |
274 | **/\r | |
275 | public static Vector<String> convertListToVector(List list) {\r | |
276 | Vector<String> v = new Vector<String>();\r | |
277 | if (list != null && list.size() > 0) {\r | |
278 | for (int index = 0; index < list.size(); index++) {\r | |
279 | v.addElement(list.get(index).toString());\r | |
280 | }\r | |
281 | }\r | |
282 | return v;\r | |
283 | }\r | |
284 | \r | |
285 | /**\r | |
286 | If the input path missing ext, append the ext to the path\r | |
287 | \r | |
288 | @param path\r | |
289 | @param type\r | |
290 | @return\r | |
291 | \r | |
292 | **/\r | |
293 | public static String addPathExt(String path, int type) {\r | |
294 | String match = "";\r | |
295 | if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {\r | |
296 | match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;\r | |
297 | }\r | |
298 | if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {\r | |
299 | match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;\r | |
300 | }\r | |
301 | if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {\r | |
302 | match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;\r | |
303 | }\r | |
92e29378 | 304 | if (type == DataType.RETURN_TYPE_TEXT) {\r |
305 | match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;\r | |
306 | }\r | |
5a24e806 | 307 | if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {\r |
308 | match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;\r | |
309 | }\r | |
a13899c5 | 310 | if (path.length() <= match.length()) {\r |
311 | path = path + match;\r | |
312 | return path;\r | |
313 | }\r | |
314 | if (!(path.substring(path.length() - match.length())).equals(match)) {\r | |
315 | path = path + match;\r | |
316 | }\r | |
317 | return path;\r | |
318 | }\r | |
79cb6fdb | 319 | \r |
a13899c5 | 320 | /**\r |
321 | Show a message box\r | |
322 | \r | |
323 | @param arg0\r | |
79cb6fdb | 324 | \r |
325 | **/\r | |
a13899c5 | 326 | public static void showInformationMessage(String arg0) {\r |
327 | JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r | |
328 | }\r | |
92e29378 | 329 | \r |
330 | /**\r | |
331 | if the string doesn't end with a file separator, append it to the string\r | |
332 | \r | |
333 | @param arg0\r | |
334 | @return\r | |
335 | \r | |
336 | **/\r | |
337 | public static String addFileSeparator(String arg0) {\r | |
338 | if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {\r | |
339 | arg0 = arg0 + DataType.FILE_SEPARATOR;\r | |
340 | }\r | |
341 | return arg0;\r | |
342 | }\r | |
343 | \r | |
344 | /**\r | |
345 | Sort all elements in the vector as the specific sort type\r | |
346 | \r | |
347 | @param v The vector need to be sorted\r | |
348 | @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending\r | |
349 | \r | |
350 | **/\r | |
351 | public static void sortVectorString(Vector<String> v, int mode) {\r | |
352 | if (v != null) {\r | |
353 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
354 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
355 | if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
356 | || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
357 | String temp = v.get(indexI);\r | |
358 | v.setElementAt(v.get(indexJ), indexI);\r | |
359 | v.setElementAt(temp, indexJ);\r | |
360 | }\r | |
361 | }\r | |
362 | }\r | |
363 | }\r | |
364 | }\r | |
365 | \r | |
366 | /**\r | |
367 | Sort all elements of vector and return sorted sequence\r | |
368 | \r | |
369 | @param v The vector need to be sorted\r | |
370 | @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending\r | |
371 | @return Vector<Integer> The sorted sequence\r | |
372 | \r | |
373 | **/\r | |
374 | public static Vector<Integer> getVectorSortSequence(Vector<String> v, int mode) {\r | |
375 | Vector<Integer> vSequence = new Vector<Integer>();\r | |
376 | //\r | |
377 | // Init sequence\r | |
378 | //\r | |
379 | if (v != null) {\r | |
380 | for (int index = 0; index < v.size(); index++) {\r | |
381 | vSequence.addElement(index);\r | |
382 | }\r | |
383 | }\r | |
384 | \r | |
385 | //\r | |
386 | // sort and get new sequence\r | |
387 | //\r | |
388 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
389 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
390 | if ((v.get(indexJ).compareTo(v.get(indexI)) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
391 | || (v.get(indexI).compareTo(v.get(indexJ)) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
392 | //\r | |
393 | // Swap strings\r | |
394 | //\r | |
395 | String tempStr = v.get(indexI);\r | |
396 | v.setElementAt(v.get(indexJ), indexI);\r | |
397 | v.setElementAt(tempStr, indexJ);\r | |
398 | \r | |
399 | //\r | |
400 | // Swap sequences\r | |
401 | //\r | |
402 | int tempInt = vSequence.get(indexI);\r | |
403 | vSequence.setElementAt(vSequence.get(indexJ), indexI);\r | |
404 | vSequence.setElementAt(tempInt, indexJ);\r | |
405 | }\r | |
406 | }\r | |
407 | }\r | |
408 | \r | |
409 | return vSequence;\r | |
410 | }\r | |
411 | \r | |
412 | /**\r | |
413 | Sort all elements of vector as input sequence\r | |
414 | \r | |
415 | @param v The vector need to be sorted\r | |
416 | @param vSequence The sort sequence should be followed\r | |
417 | \r | |
418 | **/\r | |
419 | public static void sortVectorString(Vector<String> v, Vector<Integer> vSequence) {\r | |
420 | if (v != null && vSequence != null && v.size() == vSequence.size()) {\r | |
421 | Vector<String> tempV = new Vector<String>();\r | |
422 | for (int index = 0; index < v.size(); index++) {\r | |
423 | tempV.addElement(v.get(index));\r | |
424 | }\r | |
425 | for (int index = 0; index < v.size(); index++) {\r | |
426 | v.setElementAt(tempV.get(vSequence.get(index)), index);\r | |
427 | }\r | |
428 | }\r | |
429 | }\r | |
430 | \r | |
431 | /**\r | |
432 | Sort all modules\r | |
433 | \r | |
434 | @param v\r | |
435 | @param mode\r | |
436 | \r | |
437 | **/\r | |
438 | public static void sortModules(Vector<ModuleIdentification> v, int mode) {\r | |
439 | if (v != null) {\r | |
440 | //\r | |
441 | // sort by name\r | |
442 | //\r | |
443 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
444 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
445 | if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
446 | || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
447 | ModuleIdentification temp = v.get(indexI);\r | |
448 | v.setElementAt(v.get(indexJ), indexI);\r | |
449 | v.setElementAt(temp, indexJ);\r | |
450 | }\r | |
451 | }\r | |
452 | }\r | |
453 | }\r | |
454 | }\r | |
79cb6fdb | 455 | \r |
456 | /**\r | |
92e29378 | 457 | Sort all packages\r |
79cb6fdb | 458 | \r |
92e29378 | 459 | @param v\r |
460 | @param mode\r | |
79cb6fdb | 461 | \r |
462 | **/\r | |
92e29378 | 463 | public static void sortPackages(Vector<PackageIdentification> v, int mode) {\r |
464 | if (v != null) {\r | |
465 | //\r | |
466 | // sort by name\r | |
467 | //\r | |
468 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
469 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
470 | if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
471 | || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
472 | PackageIdentification temp = v.get(indexI);\r | |
473 | v.setElementAt(v.get(indexJ), indexI);\r | |
474 | v.setElementAt(temp, indexJ);\r | |
475 | }\r | |
476 | }\r | |
477 | }\r | |
79cb6fdb | 478 | }\r |
79cb6fdb | 479 | }\r |
92e29378 | 480 | \r |
481 | /**\r | |
482 | Sort all platforms\r | |
483 | \r | |
484 | @param v\r | |
485 | @param mode\r | |
486 | \r | |
487 | **/\r | |
488 | public static void sortPlatforms(Vector<PlatformIdentification> v, int mode) {\r | |
489 | if (v != null) {\r | |
490 | //\r | |
491 | // sort by name\r | |
492 | //\r | |
493 | for (int indexI = 0; indexI < v.size(); indexI++) {\r | |
494 | for (int indexJ = indexI + 1; indexJ < v.size(); indexJ++) {\r | |
495 | if ((v.get(indexJ).getName().compareTo(v.get(indexI).getName()) < 0 && mode == DataType.SORT_TYPE_ASCENDING)\r | |
496 | || (v.get(indexI).getName().compareTo(v.get(indexJ).getName()) < 0 && mode == DataType.SORT_TYPE_DESCENDING)) {\r | |
497 | PlatformIdentification temp = v.get(indexI);\r | |
498 | v.setElementAt(v.get(indexJ), indexI);\r | |
499 | v.setElementAt(temp, indexJ);\r | |
500 | }\r | |
501 | }\r | |
502 | }\r | |
503 | }\r | |
504 | }\r | |
a13899c5 | 505 | }\r |