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
;
31 import org
.tianocore
.ModuleSurfaceAreaDocument
.ModuleSurfaceArea
;
32 import org
.tianocore
.MsaHeaderDocument
.MsaHeader
;
33 import org
.tianocore
.PackageSurfaceAreaDocument
.PackageSurfaceArea
;
34 import org
.tianocore
.PlatformHeaderDocument
.PlatformHeader
;
35 import org
.tianocore
.PlatformSurfaceAreaDocument
.PlatformSurfaceArea
;
36 import org
.tianocore
.SpdHeaderDocument
.SpdHeader
;
37 import org
.tianocore
.frameworkwizard
.module
.Identifications
.ModuleIdentification
;
38 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
39 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
42 The class is used to provides some useful interfaces
48 // The dir user selected to create new package in
50 public static String dirForNewSpd
= null;
53 Get current date and time and format it as "yyyy-MM-dd HH:mm"
55 @return formatted current date and time
58 public static String
getCurrentDateTime() {
59 Date now
= new Date(System
.currentTimeMillis());
60 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
61 return sdf
.format(now
);
67 @return the created UUID
70 public static String
generateUuidString() {
71 return UUID
.randomUUID().toString();
75 Use current file separator in the path
81 public static String
convertPathToCurrentOsType(String strPath
) {
82 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
83 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
88 Use Unix file separator in the path
94 public static String
convertPathToUnixType(String strPath
) {
95 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
100 Use Dos file separator in the path
106 public static String
convertPathToDosType(String strPath
) {
107 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
112 Get all system properties and output to the console
115 public static void getSystemProperties() {
116 System
.out
.println(System
.getProperty("java.class.version"));
117 System
.out
.println(System
.getProperty("java.class.path"));
118 System
.out
.println(System
.getProperty("java.ext.dirs"));
119 System
.out
.println(System
.getProperty("os.name"));
120 System
.out
.println(System
.getProperty("os.arch"));
121 System
.out
.println(System
.getProperty("os.version"));
122 System
.out
.println(System
.getProperty("file.separator"));
123 System
.out
.println(System
.getProperty("path.separator"));
124 System
.out
.println(System
.getProperty("line.separator"));
125 System
.out
.println(System
.getProperty("user.name"));
126 System
.out
.println(System
.getProperty("user.home"));
127 System
.out
.println(System
.getProperty("user.dir"));
128 System
.out
.println(System
.getProperty("PATH"));
130 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
134 Generate selection items for JComboBox by input vector
137 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
139 jcb
.removeAllItems();
141 if (vector
!= null) {
142 for (int index
= 0; index
< vector
.size(); index
++) {
143 jcb
.addItem(vector
.elementAt(index
));
149 Generate selection items for JList by input vector
152 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
154 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
155 listModel
.removeAllElements();
157 if (vector
!= null) {
158 for (int index
= 0; index
< vector
.size(); index
++) {
159 listModel
.addElement(vector
.get(index
));
163 if (listModel
.size() > 0) {
164 jl
.setSelectedIndex(0);
170 Get path only from a path
176 public static String
getFilePathOnly(String filePath
) {
177 String path
= filePath
.substring(0, filePath
.length() - getFileNameOnly(filePath
).length());
178 if (path
.endsWith(DataType
.FILE_SEPARATOR
)) {
179 path
= path
.substring(0, path
.length() - DataType
.FILE_SEPARATOR
.length());
186 Get file name from a path
192 public static String
getFileNameOnly(String filePath
) {
193 File f
= new File(filePath
);
194 return f
.getAbsoluteFile().getName();
197 public static String
getFileNameWithoutExt(String filePath
) {
198 filePath
= getFileNameOnly(filePath
);
199 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
208 @return wholePath - commonPath
211 public static String
getRelativePath(String wholePath
, String commonPath
) {
214 i
= wholePath
.indexOf(commonPath
);
216 i
= i
+ commonPath
.length();
220 path
= wholePath
.substring(i
);
222 // remove file separator of head
224 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
225 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
227 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
228 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
231 // remove file separator of rear
233 if (path
.length() > 0 && path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
234 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
236 if (path
.length() > 0 && path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
237 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
240 // convert to UNIX format
242 path
= Tools
.convertPathToUnixType(path
);
247 Convert List ot Vector
253 public static Vector
<String
> convertListToVector(List list
) {
254 Vector
<String
> v
= new Vector
<String
>();
255 if (list
!= null && list
.size() > 0) {
256 for (int index
= 0; index
< list
.size(); index
++) {
257 v
.addElement(list
.get(index
).toString());
264 If the input path missing ext, append the ext to the path
271 public static String
addPathExt(String path
, int type
) {
273 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
274 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
276 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
277 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
279 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
280 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
282 if (type
== DataType
.RETURN_TYPE_TEXT
) {
283 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
285 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
286 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
288 if (path
.length() <= match
.length()) {
292 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
304 public static void showInformationMessage(String arg0
) {
305 JOptionPane
.showConfirmDialog(null, arg0
, "Info", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
309 if the string doesn't end with a file separator, append it to the string
315 public static String
addFileSeparator(String arg0
) {
316 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
317 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
323 Wrap single line long input string to multiple short line string by word
325 @param arg0 input string
326 @return wraped string
329 public static String
wrapStringByWord(String arg0
) {
330 int intMaxLength
= 40;
331 String strReturn
= "";
333 boolean isCopied
= true;
336 // Convert string to array by " "
338 String s
[] = arg0
.split(" ");
339 if (arg0
.indexOf(" ") == -1) {
344 // Add each string of array one by one
346 for (int index
= 0; index
< s
.length
; index
++) {
347 String ss
= s
[index
];
350 // The word length > defined line length
352 if (ss
.length() > intMaxLength
) {
354 // Finish previous line
357 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
361 // Separater to short lines
363 while (ss
.length() > 0) {
364 if (ss
.length() > intMaxLength
) {
365 strReturn
= strReturn
+ s
[index
].substring(0, intMaxLength
- 1) + DataType
.UNIX_LINE_SEPARATOR
;
366 ss
= ss
.substring(intMaxLength
);
375 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
376 strTemp
= strTemp
+ " " + ss
;
379 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
380 if ((index
== s
.length
- 1) && (!ss
.equals(""))) {
381 strReturn
= strReturn
+ ss
;
391 strReturn
= strReturn
+ strTemp
;
397 public static String
convertUnicodeHexStringToString(String str
) {
399 // Handle if str is null or empty
404 if (str
.equals("")) {
408 String returnString
= "";
409 String
[] strArray
= str
.split(" ");
410 for (int index
= 0; index
< strArray
.length
; index
++) {
411 String s
= strArray
[index
];
412 if (s
.length() == 6 && s
.indexOf(DataType
.HEX_STRING_HEADER
) == 0) {
413 s
= s
.substring(DataType
.HEX_STRING_HEADER
.length());
415 Log
.err("convertUnicodeHexStringToString", "Incorrect input string: " + str
);
421 int dec
= Integer
.parseInt(s
, 16);
423 returnString
= returnString
+ (char) (dec
);
429 Convert input string to unicode hex string
431 @param str input string
432 @return unicode hex string
435 public static String
convertStringToUnicodeHexString(String str
) {
437 // Handle if str is null or empty
442 if (str
.equals("")) {
447 // convert string to hex string
449 String hexString
= "";
450 for (int index
= 0; index
< str
.length(); index
++) {
451 int codePoint
= str
.codePointAt(index
);
452 String s
= Integer
.toHexString(codePoint
);
454 // Make the string to four length
456 if (s
.length() == 3) {
458 } else if (s
.length() == 2) {
460 } else if (s
.length() == 1) {
465 // Add the string to return hex string
467 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
473 return hexString
.trim();
476 public static ModuleIdentification
getId(String path
, ModuleSurfaceArea msa
) {
477 MsaHeader head
= msa
.getMsaHeader();
478 String name
= head
.getModuleName();
479 String guid
= head
.getGuidValue();
480 String version
= head
.getVersion();
481 ModuleIdentification id
= new ModuleIdentification(name
, guid
, version
, path
);
485 public static PackageIdentification
getId(String path
, PackageSurfaceArea spd
) {
486 SpdHeader head
= spd
.getSpdHeader();
487 String name
= head
.getPackageName();
488 String guid
= head
.getGuidValue();
489 String version
= head
.getVersion();
490 PackageIdentification id
= new PackageIdentification(name
, guid
, version
, path
);
494 public static PlatformIdentification
getId(String path
, PlatformSurfaceArea fpd
) {
495 PlatformHeader head
= fpd
.getPlatformHeader();
496 String name
= head
.getPlatformName();
497 String guid
= head
.getGuidValue();
498 String version
= head
.getVersion();
499 PlatformIdentification id
= new PlatformIdentification(name
, guid
, version
, path
);
504 * To reset the width of input component via container width
507 * @param containerWidth
510 public static void resizeComponentWidth(Component c
, int containerWidth
, int preferredWidth
) {
511 int newWidth
= c
.getPreferredSize().width
+ (containerWidth
- preferredWidth
);
512 if (newWidth
< c
.getPreferredSize().width
) {
513 newWidth
= c
.getPreferredSize().width
;
515 c
.setSize(new java
.awt
.Dimension(newWidth
, c
.getHeight()));
520 * To reset the height of input component via container height
523 * @param containerHeight
526 public static void resizeComponentHeight(Component c
, int containerHeight
, int preferredHeight
) {
527 int newHeight
= c
.getPreferredSize().height
+ (containerHeight
- preferredHeight
);
528 if (newHeight
< c
.getPreferredSize().height
) {
529 newHeight
= c
.getPreferredSize().height
;
531 c
.setSize(new java
.awt
.Dimension(c
.getWidth(), newHeight
));
536 * To reset the size of input component via container size
539 * @param containerWidth
540 * @param containerHeight
543 public static void resizeComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidth
,
544 int preferredHeight
) {
545 resizeComponentWidth(c
, containerWidth
, preferredWidth
);
546 resizeComponentHeight(c
, containerHeight
, preferredHeight
);
550 * To relocate the input component
553 * @param containerWidth
554 * @param spaceToRight
557 public static void relocateComponentX(Component c
, int containerWidth
, int preferredWidth
, int spaceToRight
) {
558 int intGapToRight
= spaceToRight
+ c
.getPreferredSize().width
;
559 int newLocationX
= containerWidth
- intGapToRight
;
560 if (newLocationX
< preferredWidth
- intGapToRight
) {
561 newLocationX
= preferredWidth
- intGapToRight
;
563 c
.setLocation(newLocationX
, c
.getLocation().y
);
568 * To relocate the input component
571 * @param containerHeight
572 * @param spaceToBottom
575 public static void relocateComponentY(Component c
, int containerHeight
, int preferredHeight
, int spaceToBottom
) {
576 int intGapToBottom
= spaceToBottom
+ c
.getPreferredSize().height
;
577 int newLocationY
= containerHeight
- intGapToBottom
;
578 if (newLocationY
< preferredHeight
- spaceToBottom
) {
579 newLocationY
= preferredHeight
- spaceToBottom
;
581 c
.setLocation(c
.getLocation().x
, newLocationY
);
586 * To relocate the input component
589 * @param containerWidth
590 * @param containerHeight
591 * @param spaceToBottom
592 * @param spaceToRight
595 public static void relocateComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidht
,
596 int preferredHeight
, int spaceToRight
, int spaceToBottom
) {
597 relocateComponentX(c
, containerWidth
, preferredWidht
, spaceToRight
);
598 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
602 Move the component to the center of screen
608 public static void centerComponent(Component c
, int width
) {
609 c
.setLocation(width
/ 2 - c
.getWidth() / 2, c
.getLocation().y
);
614 Move the component to the center of screen and adjust the y location
620 public static void centerComponent(Component c
, int width
, int containerHeight
, int preferredHeight
,
622 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
623 centerComponent(c
, width
);
627 Find the count of searchString in wholeString
634 public static int getSpecificStringCount(String wholeString
, String searchString
) {
636 count
= wholeString
.split(searchString
).length
;
641 * Check the input data is empty or not
644 * The input data which need be checked
646 * @retval true - The input data is empty
647 * @retval fals - The input data is not empty
650 public static boolean isEmpty(String strValue
) {
651 if (strValue
.length() > 0) {