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