]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
Fixed grammar in messages.
[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
8792f60f 18import java.awt.Component;\r
a13899c5 19import java.io.File;\r
20import java.text.SimpleDateFormat;\r
21import java.util.Date;\r
22import java.util.List;\r
23import java.util.UUID;\r
24import java.util.Vector;\r
25\r
92e29378 26import javax.swing.DefaultListModel;\r
a13899c5 27import javax.swing.JComboBox;\r
92e29378 28import javax.swing.JList;\r
a13899c5 29import javax.swing.JOptionPane;\r
30\r
739c6b04 31import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
32import org.tianocore.MsaHeaderDocument.MsaHeader;\r
33import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;\r
34import org.tianocore.PlatformHeaderDocument.PlatformHeader;\r
35import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;\r
36import org.tianocore.SpdHeaderDocument.SpdHeader;\r
37import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;\r
38import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
39import org.tianocore.frameworkwizard.platform.PlatformIdentification;\r
40\r
a13899c5 41/**\r
42 The class is used to provides some useful interfaces \r
43 \r
44 **/\r
45public class Tools {\r
46\r
47 //\r
48 // The dir user selected to create new package in\r
49 //\r
50 public static String dirForNewSpd = null;\r
51\r
a13899c5 52 /**\r
53 Get current date and time and format it as "yyyy-MM-dd HH:mm"\r
54 \r
55 @return formatted current date and time\r
56 \r
57 **/\r
58 public static String getCurrentDateTime() {\r
59 Date now = new Date(System.currentTimeMillis());\r
60 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r
61 return sdf.format(now);\r
62 }\r
63\r
64 /**\r
65 Generate a UUID\r
66 \r
67 @return the created UUID\r
68 \r
69 **/\r
70 public static String generateUuidString() {\r
71 return UUID.randomUUID().toString();\r
72 }\r
73\r
74 /**\r
75 Use current file separator in the path\r
76 \r
77 @param strPath\r
78 @return\r
79 \r
80 **/\r
81 public static String convertPathToCurrentOsType(String strPath) {\r
82 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r
83 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r
84 return strPath;\r
85 }\r
86\r
87 /**\r
88 Use Unix file separator in the path\r
89 \r
90 @param strPath\r
91 @return\r
92 \r
93 **/\r
94 public static String convertPathToUnixType(String strPath) {\r
95 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);\r
96 return strPath;\r
97 }\r
98\r
99 /**\r
100 Use Dos file separator in the path\r
101 \r
102 @param strPath\r
103 @return\r
104 \r
105 **/\r
106 public static String convertPathToDosType(String strPath) {\r
107 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);\r
108 return strPath;\r
109 }\r
110\r
111 /**\r
112 Get all system properties and output to the console\r
113 \r
114 **/\r
115 public static void getSystemProperties() {\r
116 System.out.println(System.getProperty("java.class.version"));\r
117 System.out.println(System.getProperty("java.class.path"));\r
118 System.out.println(System.getProperty("java.ext.dirs"));\r
119 System.out.println(System.getProperty("os.name"));\r
120 System.out.println(System.getProperty("os.arch"));\r
121 System.out.println(System.getProperty("os.version"));\r
122 System.out.println(System.getProperty("file.separator"));\r
123 System.out.println(System.getProperty("path.separator"));\r
124 System.out.println(System.getProperty("line.separator"));\r
125 System.out.println(System.getProperty("user.name"));\r
126 System.out.println(System.getProperty("user.home"));\r
127 System.out.println(System.getProperty("user.dir"));\r
128 System.out.println(System.getProperty("PATH"));\r
129\r
130 System.out.println(System.getenv("PROCESSOR_REVISION"));\r
131 }\r
132\r
133 /**\r
134 Generate selection items for JComboBox by input vector\r
135 \r
136 **/\r
137 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {\r
138 if (jcb != null) {\r
139 jcb.removeAllItems();\r
140 }\r
141 if (vector != null) {\r
142 for (int index = 0; index < vector.size(); index++) {\r
143 jcb.addItem(vector.elementAt(index));\r
144 }\r
145 }\r
146 }\r
9a8d6d9f 147\r
92e29378 148 /**\r
9a8d6d9f 149 Generate selection items for JList by input vector\r
150 \r
151 **/\r
152 public static void generateListByVector(JList jl, Vector<String> vector) {\r
153 if (jl != null) {\r
154 DefaultListModel listModel = (DefaultListModel) jl.getModel();\r
155 listModel.removeAllElements();\r
156\r
157 if (vector != null) {\r
158 for (int index = 0; index < vector.size(); index++) {\r
159 listModel.addElement(vector.get(index));\r
160 }\r
161 }\r
162\r
163 if (listModel.size() > 0) {\r
164 jl.setSelectedIndex(0);\r
165 }\r
166 }\r
167 }\r
79cb6fdb 168\r
a13899c5 169 /**\r
170 Get path only from a path\r
79cb6fdb 171 \r
a13899c5 172 @param filePath\r
173 @return\r
79cb6fdb 174 \r
175 **/\r
a13899c5 176 public static String getFilePathOnly(String filePath) {\r
177 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());\r
178 if (path.endsWith(DataType.FILE_SEPARATOR)) {\r
179 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());\r
180 }\r
79cb6fdb 181\r
a13899c5 182 return path;\r
183 }\r
79cb6fdb 184\r
a13899c5 185 /**\r
186 Get file name from a path\r
187 \r
188 @param filePath\r
189 @return\r
79cb6fdb 190 \r
191 **/\r
a13899c5 192 public static String getFileNameOnly(String filePath) {\r
193 File f = new File(filePath);\r
194 return f.getAbsoluteFile().getName();\r
195 }\r
79cb6fdb 196\r
a13899c5 197 public static String getFileNameWithoutExt(String filePath) {\r
198 filePath = getFileNameOnly(filePath);\r
199 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));\r
200 return filePath;\r
201 }\r
202\r
203 /**\r
204 Get relative path\r
205 \r
206 @param wholePath\r
207 @param commonPath\r
208 @return wholePath - commonPath \r
209 \r
210 **/\r
211 public static String getRelativePath(String wholePath, String commonPath) {\r
212 String path = "";\r
213 int i = 0;\r
214 i = wholePath.indexOf(commonPath);\r
215 if (i > -1) {\r
216 i = i + commonPath.length();\r
217 } else {\r
218 return "";\r
219 }\r
220 path = wholePath.substring(i);\r
221 //\r
222 // remove file separator of head\r
223 //\r
224 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {\r
225 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r
226 }\r
227 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {\r
228 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r
229 }\r
230 //\r
231 // remove file separator of rear\r
232 //\r
233 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r
234 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
235 }\r
236 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r
237 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
238 }\r
239 //\r
240 // convert to UNIX format\r
241 //\r
242 path = Tools.convertPathToUnixType(path);\r
243 return path;\r
244 }\r
245\r
246 /**\r
247 Convert List ot Vector\r
248 \r
249 @param list\r
250 @return\r
251 \r
252 **/\r
253 public static Vector<String> convertListToVector(List list) {\r
254 Vector<String> v = new Vector<String>();\r
255 if (list != null && list.size() > 0) {\r
256 for (int index = 0; index < list.size(); index++) {\r
257 v.addElement(list.get(index).toString());\r
258 }\r
259 }\r
260 return v;\r
261 }\r
262\r
263 /**\r
264 If the input path missing ext, append the ext to the path\r
265 \r
266 @param path\r
267 @param type\r
268 @return\r
269 \r
270 **/\r
271 public static String addPathExt(String path, int type) {\r
272 String match = "";\r
273 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {\r
274 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;\r
275 }\r
276 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {\r
277 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;\r
278 }\r
279 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {\r
280 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;\r
281 }\r
92e29378 282 if (type == DataType.RETURN_TYPE_TEXT) {\r
283 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;\r
284 }\r
5a24e806 285 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {\r
55a2762d 286 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;\r
287 }\r
a13899c5 288 if (path.length() <= match.length()) {\r
289 path = path + match;\r
290 return path;\r
291 }\r
292 if (!(path.substring(path.length() - match.length())).equals(match)) {\r
293 path = path + match;\r
294 }\r
295 return path;\r
296 }\r
79cb6fdb 297\r
a13899c5 298 /**\r
299 Show a message box\r
300 \r
301 @param arg0\r
79cb6fdb 302 \r
303 **/\r
a13899c5 304 public static void showInformationMessage(String arg0) {\r
a929458e 305 JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r
a13899c5 306 }\r
92e29378 307\r
308 /**\r
309 if the string doesn't end with a file separator, append it to the string\r
310 \r
311 @param arg0\r
312 @return\r
313 \r
314 **/\r
315 public static String addFileSeparator(String arg0) {\r
316 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {\r
317 arg0 = arg0 + DataType.FILE_SEPARATOR;\r
318 }\r
319 return arg0;\r
320 }\r
ed1665f2 321\r
322 /**\r
323 Wrap single line long input string to multiple short line string by word\r
324 \r
325 @param arg0 input string\r
326 @return wraped string\r
327 \r
328 **/\r
329 public static String wrapStringByWord(String arg0) {\r
330 int intMaxLength = 40;\r
331 String strReturn = "";\r
332 String strTemp = "";\r
333 boolean isCopied = true;\r
334\r
335 //\r
336 // Convert string to array by " "\r
337 //\r
338 String s[] = arg0.split(" ");\r
339 if (arg0.indexOf(" ") == -1) {\r
340 s[0] = arg0;\r
341 }\r
342\r
343 //\r
344 // Add each string of array one by one\r
345 //\r
346 for (int index = 0; index < s.length; index++) {\r
347 String ss = s[index];\r
348 isCopied = false;\r
349 //\r
350 // The word length > defined line length\r
351 //\r
352 if (ss.length() > intMaxLength) {\r
353 //\r
354 // Finish previous line\r
355 //\r
356 if (!isCopied) {\r
357 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;\r
358 strTemp = "";\r
359 }\r
360 //\r
361 // Separater to short lines\r
362 //\r
363 while (ss.length() > 0) {\r
364 if (ss.length() > intMaxLength) {\r
365 strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;\r
366 ss = ss.substring(intMaxLength);\r
367 isCopied = true;\r
368 } else {\r
369 strTemp = ss;\r
370 ss = "";\r
371 isCopied = false;\r
372 }\r
373 }\r
374 } else {\r
375 if ((strTemp + " " + ss).length() <= intMaxLength) {\r
376 strTemp = strTemp + " " + ss;\r
377 continue;\r
378 } else {\r
379 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;\r
380 strTemp = ss + " ";\r
381 isCopied = true;\r
382 }\r
383 }\r
384 }\r
385\r
386 if (!isCopied) {\r
387 strReturn = strReturn + strTemp;\r
388 }\r
389\r
390 return strReturn;\r
391 }\r
43dc3851 392 \r
393 public static String convertUnicodeHexStringToString(String str) {\r
394 //\r
395 // Handle if str is null or empty\r
396 //\r
397 if (str == null) {\r
398 return "";\r
399 }\r
400 if (str.equals("")) {\r
401 return "";\r
402 }\r
403\r
404 String returnString = "";\r
405 String[] strArray = str.split(" ");\r
406 for (int index = 0; index < strArray.length; index++) {\r
407 String s = strArray[index];\r
408 if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {\r
409 s = s.substring(DataType.HEX_STRING_HEADER.length());\r
410 } else {\r
af6afe48 411 Log.err("convertUnicodeHexStringToString", "Incorrect input string: " + str);\r
43dc3851 412 continue;\r
413 }\r
414 //\r
415 // Change hex to dec\r
416 //\r
417 int dec = Integer.parseInt(s, 16);\r
418 \r
419 returnString = returnString + (char)(dec);\r
420 }\r
421 return returnString;\r
422 }\r
423\r
424 /**\r
425 Convert input string to unicode hex string\r
426 \r
427 @param str input string\r
428 @return unicode hex string\r
429 \r
430 **/\r
431 public static String convertStringToUnicodeHexString(String str) {\r
432 //\r
433 // Handle if str is null or empty\r
434 //\r
435 if (str == null) {\r
436 return "";\r
437 }\r
438 if (str.equals("")) {\r
439 return "";\r
440 }\r
441\r
442 //\r
443 // convert string to hex string\r
444 //\r
445 String hexString = "";\r
446 for (int index = 0; index < str.length(); index++) {\r
447 int codePoint = str.codePointAt(index);\r
448 String s = Integer.toHexString(codePoint);\r
449 //\r
450 // Make the string to four length\r
451 //\r
452 if (s.length() == 3) {\r
453 s = "0" + s;\r
454 } else if (s.length() == 2) {\r
455 s = "00" + s;\r
456 } else if (s.length() == 1) {\r
457 s = "000" + s;\r
458 }\r
459\r
460 //\r
461 // Add the string to return hex string\r
462 //\r
463 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";\r
464 }\r
465\r
466 //\r
467 // return hex string\r
468 //\r
469 return hexString.trim();\r
470 }\r
739c6b04 471 \r
472 public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {\r
473 MsaHeader head = msa.getMsaHeader();\r
474 String name = head.getModuleName();\r
475 String guid = head.getGuidValue();\r
476 String version = head.getVersion();\r
477 ModuleIdentification id = new ModuleIdentification(name, guid, version, path);\r
478 return id;\r
479 }\r
480\r
481 public static PackageIdentification getId(String path, PackageSurfaceArea spd) {\r
482 SpdHeader head = spd.getSpdHeader();\r
483 String name = head.getPackageName();\r
484 String guid = head.getGuidValue();\r
485 String version = head.getVersion();\r
486 PackageIdentification id = new PackageIdentification(name, guid, version, path);\r
487 return id;\r
488 }\r
489\r
490 public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {\r
491 PlatformHeader head = fpd.getPlatformHeader();\r
492 String name = head.getPlatformName();\r
493 String guid = head.getGuidValue();\r
494 String version = head.getVersion();\r
495 PlatformIdentification id = new PlatformIdentification(name, guid, version, path);\r
496 return id;\r
497 }\r
8792f60f 498 \r
499 /**\r
500 * To reset the width of input component via container width\r
501 * \r
502 * @param c\r
503 * @param containerWidth\r
504 * \r
505 */\r
506 public static void resizeComponentWidth(Component c, int containerWidth, int preferredWidth) {\r
507 int newWidth = c.getPreferredSize().width + (containerWidth - preferredWidth);\r
508 if (newWidth < c.getPreferredSize().width) {\r
509 newWidth = c.getPreferredSize().width;\r
510 }\r
511 c.setSize(new java.awt.Dimension(newWidth, c.getHeight()));\r
512 c.validate();\r
513 }\r
514\r
515 /**\r
516 * To reset the height of input component via container height\r
517 * \r
518 * @param c\r
519 * @param containerHeight\r
520 * \r
521 */\r
522 public static void resizeComponentHeight(Component c, int containerHeight, int preferredHeight) {\r
523 int newHeight = c.getPreferredSize().height + (containerHeight - preferredHeight);\r
524 if (newHeight < c.getPreferredSize().height) {\r
525 newHeight = c.getPreferredSize().height;\r
526 }\r
527 c.setSize(new java.awt.Dimension(c.getWidth(), newHeight));\r
528 c.validate();\r
529 }\r
530\r
531 /**\r
532 * To reset the size of input component via container size\r
533 * \r
534 * @param c\r
535 * @param containerWidth\r
536 * @param containerHeight\r
537 * \r
538 */\r
539 public static void resizeComponent(Component c, int containerWidth, int containerHeight, int preferredWidth,\r
540 int preferredHeight) {\r
541 resizeComponentWidth(c, containerWidth, preferredWidth);\r
542 resizeComponentHeight(c, containerHeight, preferredHeight);\r
543 }\r
544\r
545 /**\r
546 * To relocate the input component\r
547 * \r
548 * @param c\r
549 * @param containerWidth\r
550 * @param spaceToRight\r
551 * \r
552 */\r
553 public static void relocateComponentX(Component c, int containerWidth, int preferredWidth, int spaceToRight) {\r
554 int intGapToRight = spaceToRight + c.getPreferredSize().width;\r
555 int newLocationX = containerWidth - intGapToRight;\r
556 if (newLocationX < preferredWidth - intGapToRight) {\r
557 newLocationX = preferredWidth - intGapToRight;\r
558 }\r
559 c.setLocation(newLocationX, c.getLocation().y);\r
560 c.validate();\r
561 }\r
562\r
563 /**\r
564 * To relocate the input component\r
565 * \r
566 * @param c\r
567 * @param containerHeight\r
568 * @param spaceToBottom\r
569 * \r
570 */\r
571 public static void relocateComponentY(Component c, int containerHeight, int preferredHeight, int spaceToBottom) {\r
572 int intGapToBottom = spaceToBottom + c.getPreferredSize().height;\r
573 int newLocationY = containerHeight - intGapToBottom;\r
574 if (newLocationY < preferredHeight - spaceToBottom) {\r
575 newLocationY = preferredHeight - spaceToBottom;\r
576 }\r
577 c.setLocation(c.getLocation().x, newLocationY);\r
578 c.validate();\r
579 }\r
580\r
581 /**\r
582 * To relocate the input component\r
583 * \r
584 * @param c\r
585 * @param containerWidth\r
586 * @param containerHeight\r
587 * @param spaceToBottom\r
588 * @param spaceToRight\r
589 * \r
590 */\r
591 public static void relocateComponent(Component c, int containerWidth, int containerHeight, int preferredWidht,\r
592 int preferredHeight, int spaceToRight, int spaceToBottom) {\r
593 relocateComponentX(c, containerWidth, preferredWidht, spaceToRight);\r
594 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);\r
595 }\r
596 \r
597 /**\r
598 Move the component to the center of screen \r
599 \r
600 @param c\r
601 @param width\r
602 \r
603 **/\r
604 public static void centerComponent(Component c, int width) {\r
605 c.setLocation(width / 2 - c.getWidth() / 2, c.getLocation().y);\r
606 c.validate();\r
607 }\r
608 \r
609 /**\r
610 Move the component to the center of screen and adjust the y location \r
611 \r
612 @param c\r
613 @param width\r
614 \r
615 **/\r
616 public static void centerComponent(Component c, int width, int containerHeight, int preferredHeight, int spaceToBottom) {\r
617 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);\r
618 centerComponent(c, width);\r
619 }\r
a13899c5 620}\r