3 The file is used to provides some useful interfaces
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
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.
16 package org
.tianocore
.frameworkwizard
.common
;
18 import java
.awt
.Component
;
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
;
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
;
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
;
44 The class is used to provides some useful interfaces
50 // The dir user selected to create new package in
52 public static String dirForNewSpd
= null;
55 Get current date and time and format it as "yyyy-MM-dd HH:mm"
57 @return formatted current date and time
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
);
69 @return the created UUID
72 public static String
generateUuidString() {
73 return UUID
.randomUUID().toString();
77 Use current file separator in the path
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
);
90 Use Unix file separator in the path
96 public static String
convertPathToUnixType(String strPath
) {
97 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
102 Use Dos file separator in the path
108 public static String
convertPathToDosType(String strPath
) {
109 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
114 Get all system properties and output to the console
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"));
132 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
136 Generate selection items for JComboBox by input vector
139 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
141 jcb
.removeAllItems();
143 if (vector
!= null) {
144 for (int index
= 0; index
< vector
.size(); index
++) {
145 jcb
.addItem(vector
.elementAt(index
));
151 Generate selection items for JList by input vector
154 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
156 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
157 listModel
.removeAllElements();
159 if (vector
!= null) {
160 for (int index
= 0; index
< vector
.size(); index
++) {
161 listModel
.addElement(vector
.get(index
));
165 if (listModel
.size() > 0) {
166 jl
.setSelectedIndex(0);
172 Get path only from a path
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());
188 Get file name from a path
194 public static String
getFileNameOnly(String filePath
) {
195 File f
= new File(filePath
);
196 return f
.getAbsoluteFile().getName();
199 public static String
getFileNameWithoutExt(String filePath
) {
200 filePath
= getFileNameOnly(filePath
);
201 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
210 @return wholePath - commonPath
213 public static String
getRelativePath(String wholePath
, String commonPath
) {
216 i
= wholePath
.indexOf(commonPath
);
218 i
= i
+ commonPath
.length();
222 path
= wholePath
.substring(i
);
224 // remove file separator of head
226 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
227 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
229 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
230 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
233 // remove file separator of rear
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());
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());
244 // convert to UNIX format
246 path
= Tools
.convertPathToUnixType(path
);
251 Convert List ot Vector
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());
268 Convert a Vector to a String, separator with ", "
274 public static String
convertVectorToString(Vector
<String
> v
) {
276 for (int index
= 0; index
< v
.size(); index
++) {
277 s
= s
+ v
.elementAt(index
).toString() + ", ";
279 if (s
.length() > 0) {
280 s
= s
.substring(0, s
.length() - ", ".length());
286 Convert a List to a String
292 public static String
convertListToString(List list
) {
293 return Tools
.convertVectorToString(Tools
.convertListToVector(list
));
297 If the input path missing ext, append the ext to the path
304 public static String
addPathExt(String path
, int type
) {
306 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
307 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
309 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
310 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
312 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
313 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
315 if (type
== DataType
.RETURN_TYPE_TEXT
) {
316 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
318 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
319 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
321 if (path
.length() <= match
.length()) {
325 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
337 public static void showInformationMessage(String arg0
) {
338 JOptionPane
.showConfirmDialog(FrameworkWizardUI
.getInstance(), arg0
, "Info", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
342 if the string doesn't end with a file separator, append it to the string
348 public static String
addFileSeparator(String arg0
) {
349 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
350 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
356 Wrap single line long input string to multiple short line string by word
358 @param arg0 input string
359 @return wraped string
362 public static String
wrapStringByWord(String arg0
) {
363 int intMaxLength
= 40;
364 String strReturn
= "";
366 boolean isCopied
= true;
371 if (arg0
.length() <= 0) {
376 // Convert string to array by " "
378 String s
[] = arg0
.split(" ");
379 if (arg0
.indexOf(" ") == -1) {
384 // Add each string of array one by one
386 for (int index
= 0; index
< s
.length
; index
++) {
387 String ss
= s
[index
];
390 // The word length > defined line length
392 if (ss
.length() > intMaxLength
) {
394 // Finish previous line
397 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
401 // Separater to short lines
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
);
415 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
416 strTemp
= strTemp
+ " " + ss
;
419 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
420 if ((index
== s
.length
- 1) && (!ss
.equals(""))) {
421 strReturn
= strReturn
+ ss
;
431 strReturn
= strReturn
+ strTemp
;
437 public static String
convertUnicodeHexStringToString(String str
) {
439 // Handle if str is null or empty
444 if (str
.equals("")) {
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());
455 Log
.err("convertUnicodeHexStringToString", "Incorrect input string: " + str
);
461 int dec
= Integer
.parseInt(s
, 16);
463 returnString
= returnString
+ (char) (dec
);
469 Convert input string to unicode hex string
471 @param str input string
472 @return unicode hex string
475 public static String
convertStringToUnicodeHexString(String str
) {
477 // Handle if str is null or empty
482 if (str
.equals("")) {
487 // convert string to hex string
489 String hexString
= "";
490 for (int index
= 0; index
< str
.length(); index
++) {
491 int codePoint
= str
.codePointAt(index
);
492 String s
= Integer
.toHexString(codePoint
);
494 // Make the string to four length
496 if (s
.length() == 3) {
498 } else if (s
.length() == 2) {
500 } else if (s
.length() == 1) {
505 // Add the string to return hex string
507 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
513 return hexString
.trim();
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
);
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
);
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
);
544 * To reset the width of input component via container width
547 * @param containerWidth
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
;
555 c
.setSize(new java
.awt
.Dimension(newWidth
, c
.getHeight()));
560 * To reset the height of input component via container height
563 * @param containerHeight
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
;
571 c
.setSize(new java
.awt
.Dimension(c
.getWidth(), newHeight
));
576 * To reset the size of input component via container size
579 * @param containerWidth
580 * @param containerHeight
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
);
590 To adjust each column's width to meet the table's size
592 @param t the table need to be adjusted
593 @param width the new width of the table
596 public static void resizeTableColumn(JTable t
, int width
) {
598 int columnCount
= t
.getColumnCount();
599 for (int index
= 0; index
< columnCount
; index
++) {
600 t
.getColumn(t
.getColumnName(index
)).setPreferredWidth(width
/ columnCount
);
606 * To relocate the input component
609 * @param containerWidth
610 * @param spaceToRight
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
;
619 c
.setLocation(newLocationX
, c
.getLocation().y
);
624 * To relocate the input component
627 * @param containerHeight
628 * @param spaceToBottom
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
;
637 c
.setLocation(c
.getLocation().x
, newLocationY
);
642 * To relocate the input component
645 * @param containerWidth
646 * @param containerHeight
647 * @param spaceToBottom
648 * @param spaceToRight
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
);
658 Move the component to the center of screen
664 public static void centerComponent(Component c
, int width
) {
665 c
.setLocation(width
/ 2 - c
.getWidth() / 2, c
.getLocation().y
);
670 Move the component to the center of screen and adjust the y location
676 public static void centerComponent(Component c
, int width
, int containerHeight
, int preferredHeight
,
678 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
679 centerComponent(c
, width
);
683 Find the count of searchString in wholeString
690 public static int getSpecificStringCount(String wholeString
, String searchString
) {
692 count
= wholeString
.split(searchString
).length
;
697 * Check the input data is empty or not
700 * The input data which need be checked
702 * @retval true - The input data is empty
703 * @retval fals - The input data is not empty
706 public static boolean isEmpty(String strValue
) {
707 if (strValue
.length() > 0) {