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