]> git.proxmox.com Git - mirror_edk2.git/blame - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
1. Fix EDKT330 Source files in sub-directories are not added to MSA correctly
[mirror_edk2.git] / Tools / Java / 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
0915a2ce 233 if (path.length() > 0 && path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r
a13899c5 234 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
235 }\r
0915a2ce 236 if (path.length() > 0 && path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r
a13899c5 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
d933fc5f 357 strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;\r
ed1665f2 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
d933fc5f 379 strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;\r
380 if ((index == s.length - 1) && (!ss.equals(""))) {\r
381 strReturn = strReturn + ss;\r
382 } else {\r
383 strTemp = ss + " ";\r
384 }\r
ed1665f2 385 isCopied = true;\r
386 }\r
387 }\r
388 }\r
389\r
390 if (!isCopied) {\r
391 strReturn = strReturn + strTemp;\r
392 }\r
393\r
394 return strReturn;\r
395 }\r
55e83b18 396\r
43dc3851 397 public static String convertUnicodeHexStringToString(String str) {\r
398 //\r
399 // Handle if str is null or empty\r
400 //\r
401 if (str == null) {\r
402 return "";\r
403 }\r
404 if (str.equals("")) {\r
405 return "";\r
406 }\r
407\r
408 String returnString = "";\r
409 String[] strArray = str.split(" ");\r
410 for (int index = 0; index < strArray.length; index++) {\r
411 String s = strArray[index];\r
412 if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {\r
413 s = s.substring(DataType.HEX_STRING_HEADER.length());\r
414 } else {\r
af6afe48 415 Log.err("convertUnicodeHexStringToString", "Incorrect input string: " + str);\r
43dc3851 416 continue;\r
417 }\r
418 //\r
419 // Change hex to dec\r
420 //\r
421 int dec = Integer.parseInt(s, 16);\r
55e83b18 422\r
423 returnString = returnString + (char) (dec);\r
43dc3851 424 }\r
425 return returnString;\r
426 }\r
427\r
428 /**\r
429 Convert input string to unicode hex string\r
430 \r
431 @param str input string\r
432 @return unicode hex string\r
433 \r
434 **/\r
435 public static String convertStringToUnicodeHexString(String str) {\r
436 //\r
437 // Handle if str is null or empty\r
438 //\r
439 if (str == null) {\r
440 return "";\r
441 }\r
442 if (str.equals("")) {\r
443 return "";\r
444 }\r
445\r
446 //\r
447 // convert string to hex string\r
448 //\r
449 String hexString = "";\r
450 for (int index = 0; index < str.length(); index++) {\r
451 int codePoint = str.codePointAt(index);\r
452 String s = Integer.toHexString(codePoint);\r
453 //\r
454 // Make the string to four length\r
455 //\r
456 if (s.length() == 3) {\r
457 s = "0" + s;\r
458 } else if (s.length() == 2) {\r
459 s = "00" + s;\r
460 } else if (s.length() == 1) {\r
461 s = "000" + s;\r
462 }\r
463\r
464 //\r
465 // Add the string to return hex string\r
466 //\r
467 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";\r
468 }\r
469\r
470 //\r
471 // return hex string\r
472 //\r
473 return hexString.trim();\r
474 }\r
55e83b18 475\r
739c6b04 476 public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {\r
477 MsaHeader head = msa.getMsaHeader();\r
478 String name = head.getModuleName();\r
479 String guid = head.getGuidValue();\r
480 String version = head.getVersion();\r
481 ModuleIdentification id = new ModuleIdentification(name, guid, version, path);\r
482 return id;\r
483 }\r
484\r
485 public static PackageIdentification getId(String path, PackageSurfaceArea spd) {\r
486 SpdHeader head = spd.getSpdHeader();\r
487 String name = head.getPackageName();\r
488 String guid = head.getGuidValue();\r
489 String version = head.getVersion();\r
490 PackageIdentification id = new PackageIdentification(name, guid, version, path);\r
491 return id;\r
492 }\r
493\r
494 public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {\r
495 PlatformHeader head = fpd.getPlatformHeader();\r
496 String name = head.getPlatformName();\r
497 String guid = head.getGuidValue();\r
498 String version = head.getVersion();\r
499 PlatformIdentification id = new PlatformIdentification(name, guid, version, path);\r
500 return id;\r
501 }\r
55e83b18 502\r
8792f60f 503 /**\r
504 * To reset the width of input component via container width\r
505 * \r
506 * @param c\r
507 * @param containerWidth\r
508 * \r
509 */\r
510 public static void resizeComponentWidth(Component c, int containerWidth, int preferredWidth) {\r
511 int newWidth = c.getPreferredSize().width + (containerWidth - preferredWidth);\r
512 if (newWidth < c.getPreferredSize().width) {\r
513 newWidth = c.getPreferredSize().width;\r
514 }\r
515 c.setSize(new java.awt.Dimension(newWidth, c.getHeight()));\r
516 c.validate();\r
517 }\r
518\r
519 /**\r
520 * To reset the height of input component via container height\r
521 * \r
522 * @param c\r
523 * @param containerHeight\r
524 * \r
525 */\r
526 public static void resizeComponentHeight(Component c, int containerHeight, int preferredHeight) {\r
527 int newHeight = c.getPreferredSize().height + (containerHeight - preferredHeight);\r
528 if (newHeight < c.getPreferredSize().height) {\r
529 newHeight = c.getPreferredSize().height;\r
530 }\r
531 c.setSize(new java.awt.Dimension(c.getWidth(), newHeight));\r
532 c.validate();\r
533 }\r
534\r
535 /**\r
536 * To reset the size of input component via container size\r
537 * \r
538 * @param c\r
539 * @param containerWidth\r
540 * @param containerHeight\r
541 * \r
542 */\r
543 public static void resizeComponent(Component c, int containerWidth, int containerHeight, int preferredWidth,\r
55e83b18 544 int preferredHeight) {\r
8792f60f 545 resizeComponentWidth(c, containerWidth, preferredWidth);\r
546 resizeComponentHeight(c, containerHeight, preferredHeight);\r
547 }\r
548\r
549 /**\r
550 * To relocate the input component\r
551 * \r
552 * @param c\r
553 * @param containerWidth\r
554 * @param spaceToRight\r
555 * \r
556 */\r
557 public static void relocateComponentX(Component c, int containerWidth, int preferredWidth, int spaceToRight) {\r
558 int intGapToRight = spaceToRight + c.getPreferredSize().width;\r
559 int newLocationX = containerWidth - intGapToRight;\r
560 if (newLocationX < preferredWidth - intGapToRight) {\r
561 newLocationX = preferredWidth - intGapToRight;\r
562 }\r
563 c.setLocation(newLocationX, c.getLocation().y);\r
564 c.validate();\r
565 }\r
566\r
567 /**\r
568 * To relocate the input component\r
569 * \r
570 * @param c\r
571 * @param containerHeight\r
572 * @param spaceToBottom\r
573 * \r
574 */\r
575 public static void relocateComponentY(Component c, int containerHeight, int preferredHeight, int spaceToBottom) {\r
576 int intGapToBottom = spaceToBottom + c.getPreferredSize().height;\r
577 int newLocationY = containerHeight - intGapToBottom;\r
578 if (newLocationY < preferredHeight - spaceToBottom) {\r
579 newLocationY = preferredHeight - spaceToBottom;\r
580 }\r
581 c.setLocation(c.getLocation().x, newLocationY);\r
582 c.validate();\r
583 }\r
584\r
585 /**\r
586 * To relocate the input component\r
587 * \r
588 * @param c\r
589 * @param containerWidth\r
590 * @param containerHeight\r
591 * @param spaceToBottom\r
592 * @param spaceToRight\r
593 * \r
594 */\r
595 public static void relocateComponent(Component c, int containerWidth, int containerHeight, int preferredWidht,\r
55e83b18 596 int preferredHeight, int spaceToRight, int spaceToBottom) {\r
8792f60f 597 relocateComponentX(c, containerWidth, preferredWidht, spaceToRight);\r
598 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);\r
599 }\r
55e83b18 600\r
8792f60f 601 /**\r
602 Move the component to the center of screen \r
55e83b18 603 \r
8792f60f 604 @param c\r
605 @param width\r
55e83b18 606 \r
607 **/\r
8792f60f 608 public static void centerComponent(Component c, int width) {\r
609 c.setLocation(width / 2 - c.getWidth() / 2, c.getLocation().y);\r
610 c.validate();\r
611 }\r
55e83b18 612\r
8792f60f 613 /**\r
55e83b18 614 Move the component to the center of screen and adjust the y location \r
615 \r
616 @param c\r
617 @param width\r
618 \r
619 **/\r
620 public static void centerComponent(Component c, int width, int containerHeight, int preferredHeight,\r
621 int spaceToBottom) {\r
622 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);\r
623 centerComponent(c, width);\r
624 }\r
625\r
626 /**\r
627 Find the count of searchString in wholeString\r
628 \r
629 @param wholeString\r
630 @param searchString\r
631 @return\r
632\r
633 **/\r
634 public static int getSpecificStringCount(String wholeString, String searchString) {\r
635 int count = 0;\r
636 count = wholeString.split(searchString).length;\r
637 return count;\r
638 }\r
d933fc5f 639\r
3b7a9058 640 /**\r
641 * Check the input data is empty or not\r
642 * \r
643 * @param strValue\r
644 * The input data which need be checked\r
645 * \r
646 * @retval true - The input data is empty\r
647 * @retval fals - The input data is not empty\r
648 * \r
649 */\r
650 public static boolean isEmpty(String strValue) {\r
651 if (strValue.length() > 0) {\r
652 return false;\r
653 }\r
654 return true;\r
655 }\r
a13899c5 656}\r