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;
338 if (arg0
.length() <= 0) {
343 // Convert string to array by " "
345 String s
[] = arg0
.split(" ");
346 if (arg0
.indexOf(" ") == -1) {
351 // Add each string of array one by one
353 for (int index
= 0; index
< s
.length
; index
++) {
354 String ss
= s
[index
];
357 // The word length > defined line length
359 if (ss
.length() > intMaxLength
) {
361 // Finish previous line
364 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
368 // Separater to short lines
370 while (ss
.length() > 0) {
371 if (ss
.length() > intMaxLength
) {
372 strReturn
= strReturn
+ s
[index
].substring(0, intMaxLength
- 1) + DataType
.UNIX_LINE_SEPARATOR
;
373 ss
= ss
.substring(intMaxLength
);
382 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
383 strTemp
= strTemp
+ " " + ss
;
386 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
387 if ((index
== s
.length
- 1) && (!ss
.equals(""))) {
388 strReturn
= strReturn
+ ss
;
398 strReturn
= strReturn
+ strTemp
;
404 public static String
convertUnicodeHexStringToString(String str
) {
406 // Handle if str is null or empty
411 if (str
.equals("")) {
415 String returnString
= "";
416 String
[] strArray
= str
.split(" ");
417 for (int index
= 0; index
< strArray
.length
; index
++) {
418 String s
= strArray
[index
];
419 if (s
.length() == 6 && s
.indexOf(DataType
.HEX_STRING_HEADER
) == 0) {
420 s
= s
.substring(DataType
.HEX_STRING_HEADER
.length());
422 Log
.err("convertUnicodeHexStringToString", "Incorrect input string: " + str
);
428 int dec
= Integer
.parseInt(s
, 16);
430 returnString
= returnString
+ (char) (dec
);
436 Convert input string to unicode hex string
438 @param str input string
439 @return unicode hex string
442 public static String
convertStringToUnicodeHexString(String str
) {
444 // Handle if str is null or empty
449 if (str
.equals("")) {
454 // convert string to hex string
456 String hexString
= "";
457 for (int index
= 0; index
< str
.length(); index
++) {
458 int codePoint
= str
.codePointAt(index
);
459 String s
= Integer
.toHexString(codePoint
);
461 // Make the string to four length
463 if (s
.length() == 3) {
465 } else if (s
.length() == 2) {
467 } else if (s
.length() == 1) {
472 // Add the string to return hex string
474 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
480 return hexString
.trim();
483 public static ModuleIdentification
getId(String path
, ModuleSurfaceArea msa
) {
484 MsaHeader head
= msa
.getMsaHeader();
485 String name
= head
.getModuleName();
486 String guid
= head
.getGuidValue();
487 String version
= head
.getVersion();
488 ModuleIdentification id
= new ModuleIdentification(name
, guid
, version
, path
);
492 public static PackageIdentification
getId(String path
, PackageSurfaceArea spd
) {
493 SpdHeader head
= spd
.getSpdHeader();
494 String name
= head
.getPackageName();
495 String guid
= head
.getGuidValue();
496 String version
= head
.getVersion();
497 PackageIdentification id
= new PackageIdentification(name
, guid
, version
, path
);
501 public static PlatformIdentification
getId(String path
, PlatformSurfaceArea fpd
) {
502 PlatformHeader head
= fpd
.getPlatformHeader();
503 String name
= head
.getPlatformName();
504 String guid
= head
.getGuidValue();
505 String version
= head
.getVersion();
506 PlatformIdentification id
= new PlatformIdentification(name
, guid
, version
, path
);
511 * To reset the width of input component via container width
514 * @param containerWidth
517 public static void resizeComponentWidth(Component c
, int containerWidth
, int preferredWidth
) {
518 int newWidth
= c
.getPreferredSize().width
+ (containerWidth
- preferredWidth
);
519 if (newWidth
< c
.getPreferredSize().width
) {
520 newWidth
= c
.getPreferredSize().width
;
522 c
.setSize(new java
.awt
.Dimension(newWidth
, c
.getHeight()));
527 * To reset the height of input component via container height
530 * @param containerHeight
533 public static void resizeComponentHeight(Component c
, int containerHeight
, int preferredHeight
) {
534 int newHeight
= c
.getPreferredSize().height
+ (containerHeight
- preferredHeight
);
535 if (newHeight
< c
.getPreferredSize().height
) {
536 newHeight
= c
.getPreferredSize().height
;
538 c
.setSize(new java
.awt
.Dimension(c
.getWidth(), newHeight
));
543 * To reset the size of input component via container size
546 * @param containerWidth
547 * @param containerHeight
550 public static void resizeComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidth
,
551 int preferredHeight
) {
552 resizeComponentWidth(c
, containerWidth
, preferredWidth
);
553 resizeComponentHeight(c
, containerHeight
, preferredHeight
);
557 * To relocate the input component
560 * @param containerWidth
561 * @param spaceToRight
564 public static void relocateComponentX(Component c
, int containerWidth
, int preferredWidth
, int spaceToRight
) {
565 int intGapToRight
= spaceToRight
+ c
.getPreferredSize().width
;
566 int newLocationX
= containerWidth
- intGapToRight
;
567 if (newLocationX
< preferredWidth
- intGapToRight
) {
568 newLocationX
= preferredWidth
- intGapToRight
;
570 c
.setLocation(newLocationX
, c
.getLocation().y
);
575 * To relocate the input component
578 * @param containerHeight
579 * @param spaceToBottom
582 public static void relocateComponentY(Component c
, int containerHeight
, int preferredHeight
, int spaceToBottom
) {
583 int intGapToBottom
= spaceToBottom
+ c
.getPreferredSize().height
;
584 int newLocationY
= containerHeight
- intGapToBottom
;
585 if (newLocationY
< preferredHeight
- spaceToBottom
) {
586 newLocationY
= preferredHeight
- spaceToBottom
;
588 c
.setLocation(c
.getLocation().x
, newLocationY
);
593 * To relocate the input component
596 * @param containerWidth
597 * @param containerHeight
598 * @param spaceToBottom
599 * @param spaceToRight
602 public static void relocateComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidht
,
603 int preferredHeight
, int spaceToRight
, int spaceToBottom
) {
604 relocateComponentX(c
, containerWidth
, preferredWidht
, spaceToRight
);
605 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
609 Move the component to the center of screen
615 public static void centerComponent(Component c
, int width
) {
616 c
.setLocation(width
/ 2 - c
.getWidth() / 2, c
.getLocation().y
);
621 Move the component to the center of screen and adjust the y location
627 public static void centerComponent(Component c
, int width
, int containerHeight
, int preferredHeight
,
629 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
630 centerComponent(c
, width
);
634 Find the count of searchString in wholeString
641 public static int getSpecificStringCount(String wholeString
, String searchString
) {
643 count
= wholeString
.split(searchString
).length
;
648 * Check the input data is empty or not
651 * The input data which need be checked
653 * @retval true - The input data is empty
654 * @retval fals - The input data is not empty
657 public static boolean isEmpty(String strValue
) {
658 if (strValue
.length() > 0) {