]>
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 | |
30 | /**\r | |
31 | The class is used to provides some useful interfaces \r | |
32 | \r | |
33 | **/\r | |
34 | public class Tools {\r | |
35 | \r | |
36 | //\r | |
37 | // The dir user selected to create new package in\r | |
38 | //\r | |
39 | public static String dirForNewSpd = null;\r | |
40 | \r | |
a13899c5 | 41 | /**\r |
42 | Get current date and time and format it as "yyyy-MM-dd HH:mm"\r | |
43 | \r | |
44 | @return formatted current date and time\r | |
45 | \r | |
46 | **/\r | |
47 | public static String getCurrentDateTime() {\r | |
48 | Date now = new Date(System.currentTimeMillis());\r | |
49 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r | |
50 | return sdf.format(now);\r | |
51 | }\r | |
52 | \r | |
53 | /**\r | |
54 | Generate a UUID\r | |
55 | \r | |
56 | @return the created UUID\r | |
57 | \r | |
58 | **/\r | |
59 | public static String generateUuidString() {\r | |
60 | return UUID.randomUUID().toString();\r | |
61 | }\r | |
62 | \r | |
63 | /**\r | |
64 | Use current file separator in the path\r | |
65 | \r | |
66 | @param strPath\r | |
67 | @return\r | |
68 | \r | |
69 | **/\r | |
70 | public static String convertPathToCurrentOsType(String strPath) {\r | |
71 | strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r | |
72 | strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r | |
73 | return strPath;\r | |
74 | }\r | |
75 | \r | |
76 | /**\r | |
77 | Use Unix file separator in the path\r | |
78 | \r | |
79 | @param strPath\r | |
80 | @return\r | |
81 | \r | |
82 | **/\r | |
83 | public static String convertPathToUnixType(String strPath) {\r | |
84 | strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);\r | |
85 | return strPath;\r | |
86 | }\r | |
87 | \r | |
88 | /**\r | |
89 | Use Dos file separator in the path\r | |
90 | \r | |
91 | @param strPath\r | |
92 | @return\r | |
93 | \r | |
94 | **/\r | |
95 | public static String convertPathToDosType(String strPath) {\r | |
96 | strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);\r | |
97 | return strPath;\r | |
98 | }\r | |
99 | \r | |
100 | /**\r | |
101 | Get all system properties and output to the console\r | |
102 | \r | |
103 | **/\r | |
104 | public static void getSystemProperties() {\r | |
105 | System.out.println(System.getProperty("java.class.version"));\r | |
106 | System.out.println(System.getProperty("java.class.path"));\r | |
107 | System.out.println(System.getProperty("java.ext.dirs"));\r | |
108 | System.out.println(System.getProperty("os.name"));\r | |
109 | System.out.println(System.getProperty("os.arch"));\r | |
110 | System.out.println(System.getProperty("os.version"));\r | |
111 | System.out.println(System.getProperty("file.separator"));\r | |
112 | System.out.println(System.getProperty("path.separator"));\r | |
113 | System.out.println(System.getProperty("line.separator"));\r | |
114 | System.out.println(System.getProperty("user.name"));\r | |
115 | System.out.println(System.getProperty("user.home"));\r | |
116 | System.out.println(System.getProperty("user.dir"));\r | |
117 | System.out.println(System.getProperty("PATH"));\r | |
118 | \r | |
119 | System.out.println(System.getenv("PROCESSOR_REVISION"));\r | |
120 | }\r | |
121 | \r | |
122 | /**\r | |
123 | Generate selection items for JComboBox by input vector\r | |
124 | \r | |
125 | **/\r | |
126 | public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {\r | |
127 | if (jcb != null) {\r | |
128 | jcb.removeAllItems();\r | |
129 | }\r | |
130 | if (vector != null) {\r | |
131 | for (int index = 0; index < vector.size(); index++) {\r | |
132 | jcb.addItem(vector.elementAt(index));\r | |
133 | }\r | |
134 | }\r | |
135 | }\r | |
9a8d6d9f | 136 | \r |
92e29378 | 137 | /**\r |
9a8d6d9f | 138 | Generate selection items for JList by input vector\r |
139 | \r | |
140 | **/\r | |
141 | public static void generateListByVector(JList jl, Vector<String> vector) {\r | |
142 | if (jl != null) {\r | |
143 | DefaultListModel listModel = (DefaultListModel) jl.getModel();\r | |
144 | listModel.removeAllElements();\r | |
145 | \r | |
146 | if (vector != null) {\r | |
147 | for (int index = 0; index < vector.size(); index++) {\r | |
148 | listModel.addElement(vector.get(index));\r | |
149 | }\r | |
150 | }\r | |
151 | \r | |
152 | if (listModel.size() > 0) {\r | |
153 | jl.setSelectedIndex(0);\r | |
154 | }\r | |
155 | }\r | |
156 | }\r | |
79cb6fdb | 157 | \r |
a13899c5 | 158 | /**\r |
159 | Get path only from a path\r | |
79cb6fdb | 160 | \r |
a13899c5 | 161 | @param filePath\r |
162 | @return\r | |
79cb6fdb | 163 | \r |
164 | **/\r | |
a13899c5 | 165 | public static String getFilePathOnly(String filePath) {\r |
166 | String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());\r | |
167 | if (path.endsWith(DataType.FILE_SEPARATOR)) {\r | |
168 | path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());\r | |
169 | }\r | |
79cb6fdb | 170 | \r |
a13899c5 | 171 | return path;\r |
172 | }\r | |
79cb6fdb | 173 | \r |
a13899c5 | 174 | /**\r |
175 | Get file name from a path\r | |
176 | \r | |
177 | @param filePath\r | |
178 | @return\r | |
79cb6fdb | 179 | \r |
180 | **/\r | |
a13899c5 | 181 | public static String getFileNameOnly(String filePath) {\r |
182 | File f = new File(filePath);\r | |
183 | return f.getAbsoluteFile().getName();\r | |
184 | }\r | |
79cb6fdb | 185 | \r |
a13899c5 | 186 | public static String getFileNameWithoutExt(String filePath) {\r |
187 | filePath = getFileNameOnly(filePath);\r | |
188 | filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));\r | |
189 | return filePath;\r | |
190 | }\r | |
191 | \r | |
192 | /**\r | |
193 | Get relative path\r | |
194 | \r | |
195 | @param wholePath\r | |
196 | @param commonPath\r | |
197 | @return wholePath - commonPath \r | |
198 | \r | |
199 | **/\r | |
200 | public static String getRelativePath(String wholePath, String commonPath) {\r | |
201 | String path = "";\r | |
202 | int i = 0;\r | |
203 | i = wholePath.indexOf(commonPath);\r | |
204 | if (i > -1) {\r | |
205 | i = i + commonPath.length();\r | |
206 | } else {\r | |
207 | return "";\r | |
208 | }\r | |
209 | path = wholePath.substring(i);\r | |
210 | //\r | |
211 | // remove file separator of head\r | |
212 | //\r | |
213 | if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {\r | |
214 | path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r | |
215 | }\r | |
216 | if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {\r | |
217 | path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r | |
218 | }\r | |
219 | //\r | |
220 | // remove file separator of rear\r | |
221 | //\r | |
222 | if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r | |
223 | path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r | |
224 | }\r | |
225 | if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r | |
226 | path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r | |
227 | }\r | |
228 | //\r | |
229 | // convert to UNIX format\r | |
230 | //\r | |
231 | path = Tools.convertPathToUnixType(path);\r | |
232 | return path;\r | |
233 | }\r | |
234 | \r | |
235 | /**\r | |
236 | Convert List ot Vector\r | |
237 | \r | |
238 | @param list\r | |
239 | @return\r | |
240 | \r | |
241 | **/\r | |
242 | public static Vector<String> convertListToVector(List list) {\r | |
243 | Vector<String> v = new Vector<String>();\r | |
244 | if (list != null && list.size() > 0) {\r | |
245 | for (int index = 0; index < list.size(); index++) {\r | |
246 | v.addElement(list.get(index).toString());\r | |
247 | }\r | |
248 | }\r | |
249 | return v;\r | |
250 | }\r | |
251 | \r | |
252 | /**\r | |
253 | If the input path missing ext, append the ext to the path\r | |
254 | \r | |
255 | @param path\r | |
256 | @param type\r | |
257 | @return\r | |
258 | \r | |
259 | **/\r | |
260 | public static String addPathExt(String path, int type) {\r | |
261 | String match = "";\r | |
262 | if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {\r | |
263 | match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;\r | |
264 | }\r | |
265 | if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {\r | |
266 | match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;\r | |
267 | }\r | |
268 | if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {\r | |
269 | match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;\r | |
270 | }\r | |
92e29378 | 271 | if (type == DataType.RETURN_TYPE_TEXT) {\r |
272 | match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;\r | |
273 | }\r | |
5a24e806 | 274 | if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {\r |
55a2762d | 275 | match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;\r |
276 | }\r | |
a13899c5 | 277 | if (path.length() <= match.length()) {\r |
278 | path = path + match;\r | |
279 | return path;\r | |
280 | }\r | |
281 | if (!(path.substring(path.length() - match.length())).equals(match)) {\r | |
282 | path = path + match;\r | |
283 | }\r | |
284 | return path;\r | |
285 | }\r | |
79cb6fdb | 286 | \r |
a13899c5 | 287 | /**\r |
288 | Show a message box\r | |
289 | \r | |
290 | @param arg0\r | |
79cb6fdb | 291 | \r |
292 | **/\r | |
a13899c5 | 293 | public static void showInformationMessage(String arg0) {\r |
a929458e | 294 | JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r |
a13899c5 | 295 | }\r |
92e29378 | 296 | \r |
297 | /**\r | |
298 | if the string doesn't end with a file separator, append it to the string\r | |
299 | \r | |
300 | @param arg0\r | |
301 | @return\r | |
302 | \r | |
303 | **/\r | |
304 | public static String addFileSeparator(String arg0) {\r | |
305 | if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {\r | |
306 | arg0 = arg0 + DataType.FILE_SEPARATOR;\r | |
307 | }\r | |
308 | return arg0;\r | |
309 | }\r | |
ed1665f2 | 310 | \r |
311 | /**\r | |
312 | Wrap single line long input string to multiple short line string by word\r | |
313 | \r | |
314 | @param arg0 input string\r | |
315 | @return wraped string\r | |
316 | \r | |
317 | **/\r | |
318 | public static String wrapStringByWord(String arg0) {\r | |
319 | int intMaxLength = 40;\r | |
320 | String strReturn = "";\r | |
321 | String strTemp = "";\r | |
322 | boolean isCopied = true;\r | |
323 | \r | |
324 | //\r | |
325 | // Convert string to array by " "\r | |
326 | //\r | |
327 | String s[] = arg0.split(" ");\r | |
328 | if (arg0.indexOf(" ") == -1) {\r | |
329 | s[0] = arg0;\r | |
330 | }\r | |
331 | \r | |
332 | //\r | |
333 | // Add each string of array one by one\r | |
334 | //\r | |
335 | for (int index = 0; index < s.length; index++) {\r | |
336 | String ss = s[index];\r | |
337 | isCopied = false;\r | |
338 | //\r | |
339 | // The word length > defined line length\r | |
340 | //\r | |
341 | if (ss.length() > intMaxLength) {\r | |
342 | //\r | |
343 | // Finish previous line\r | |
344 | //\r | |
345 | if (!isCopied) {\r | |
346 | strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;\r | |
347 | strTemp = "";\r | |
348 | }\r | |
349 | //\r | |
350 | // Separater to short lines\r | |
351 | //\r | |
352 | while (ss.length() > 0) {\r | |
353 | if (ss.length() > intMaxLength) {\r | |
354 | strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;\r | |
355 | ss = ss.substring(intMaxLength);\r | |
356 | isCopied = true;\r | |
357 | } else {\r | |
358 | strTemp = ss;\r | |
359 | ss = "";\r | |
360 | isCopied = false;\r | |
361 | }\r | |
362 | }\r | |
363 | } else {\r | |
364 | if ((strTemp + " " + ss).length() <= intMaxLength) {\r | |
365 | strTemp = strTemp + " " + ss;\r | |
366 | continue;\r | |
367 | } else {\r | |
368 | strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;\r | |
369 | strTemp = ss + " ";\r | |
370 | isCopied = true;\r | |
371 | }\r | |
372 | }\r | |
373 | }\r | |
374 | \r | |
375 | if (!isCopied) {\r | |
376 | strReturn = strReturn + strTemp;\r | |
377 | }\r | |
378 | \r | |
379 | return strReturn;\r | |
380 | }\r | |
43dc3851 | 381 | \r |
382 | public static String convertUnicodeHexStringToString(String str) {\r | |
383 | //\r | |
384 | // Handle if str is null or empty\r | |
385 | //\r | |
386 | if (str == null) {\r | |
387 | return "";\r | |
388 | }\r | |
389 | if (str.equals("")) {\r | |
390 | return "";\r | |
391 | }\r | |
392 | \r | |
393 | String returnString = "";\r | |
394 | String[] strArray = str.split(" ");\r | |
395 | for (int index = 0; index < strArray.length; index++) {\r | |
396 | String s = strArray[index];\r | |
397 | if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {\r | |
398 | s = s.substring(DataType.HEX_STRING_HEADER.length());\r | |
399 | } else {\r | |
400 | Log.err("convertUnicodeHexStringToString", "Wrong input string: " + str);\r | |
401 | continue;\r | |
402 | }\r | |
403 | //\r | |
404 | // Change hex to dec\r | |
405 | //\r | |
406 | int dec = Integer.parseInt(s, 16);\r | |
407 | \r | |
408 | returnString = returnString + (char)(dec);\r | |
409 | }\r | |
410 | return returnString;\r | |
411 | }\r | |
412 | \r | |
413 | /**\r | |
414 | Convert input string to unicode hex string\r | |
415 | \r | |
416 | @param str input string\r | |
417 | @return unicode hex string\r | |
418 | \r | |
419 | **/\r | |
420 | public static String convertStringToUnicodeHexString(String str) {\r | |
421 | //\r | |
422 | // Handle if str is null or empty\r | |
423 | //\r | |
424 | if (str == null) {\r | |
425 | return "";\r | |
426 | }\r | |
427 | if (str.equals("")) {\r | |
428 | return "";\r | |
429 | }\r | |
430 | \r | |
431 | //\r | |
432 | // convert string to hex string\r | |
433 | //\r | |
434 | String hexString = "";\r | |
435 | for (int index = 0; index < str.length(); index++) {\r | |
436 | int codePoint = str.codePointAt(index);\r | |
437 | String s = Integer.toHexString(codePoint);\r | |
438 | //\r | |
439 | // Make the string to four length\r | |
440 | //\r | |
441 | if (s.length() == 3) {\r | |
442 | s = "0" + s;\r | |
443 | } else if (s.length() == 2) {\r | |
444 | s = "00" + s;\r | |
445 | } else if (s.length() == 1) {\r | |
446 | s = "000" + s;\r | |
447 | }\r | |
448 | \r | |
449 | //\r | |
450 | // Add the string to return hex string\r | |
451 | //\r | |
452 | hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";\r | |
453 | }\r | |
454 | \r | |
455 | //\r | |
456 | // return hex string\r | |
457 | //\r | |
458 | return hexString.trim();\r | |
459 | }\r | |
a13899c5 | 460 | }\r |