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
.module
.Identifications
.ModuleIdentification
;
39 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
40 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
43 The class is used to provides some useful interfaces
49 // The dir user selected to create new package in
51 public static String dirForNewSpd
= null;
54 Get current date and time and format it as "yyyy-MM-dd HH:mm"
56 @return formatted current date and time
59 public static String
getCurrentDateTime() {
60 Date now
= new Date(System
.currentTimeMillis());
61 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
62 return sdf
.format(now
);
68 @return the created UUID
71 public static String
generateUuidString() {
72 return UUID
.randomUUID().toString();
76 Use current file separator in the path
82 public static String
convertPathToCurrentOsType(String strPath
) {
83 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
84 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
89 Use Unix file separator in the path
95 public static String
convertPathToUnixType(String strPath
) {
96 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
101 Use Dos file separator in the path
107 public static String
convertPathToDosType(String strPath
) {
108 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
113 Get all system properties and output to the console
116 public static void getSystemProperties() {
117 System
.out
.println(System
.getProperty("java.class.version"));
118 System
.out
.println(System
.getProperty("java.class.path"));
119 System
.out
.println(System
.getProperty("java.ext.dirs"));
120 System
.out
.println(System
.getProperty("os.name"));
121 System
.out
.println(System
.getProperty("os.arch"));
122 System
.out
.println(System
.getProperty("os.version"));
123 System
.out
.println(System
.getProperty("file.separator"));
124 System
.out
.println(System
.getProperty("path.separator"));
125 System
.out
.println(System
.getProperty("line.separator"));
126 System
.out
.println(System
.getProperty("user.name"));
127 System
.out
.println(System
.getProperty("user.home"));
128 System
.out
.println(System
.getProperty("user.dir"));
129 System
.out
.println(System
.getProperty("PATH"));
131 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
135 Generate selection items for JComboBox by input vector
138 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
140 jcb
.removeAllItems();
142 if (vector
!= null) {
143 for (int index
= 0; index
< vector
.size(); index
++) {
144 jcb
.addItem(vector
.elementAt(index
));
150 Generate selection items for JList by input vector
153 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
155 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
156 listModel
.removeAllElements();
158 if (vector
!= null) {
159 for (int index
= 0; index
< vector
.size(); index
++) {
160 listModel
.addElement(vector
.get(index
));
164 if (listModel
.size() > 0) {
165 jl
.setSelectedIndex(0);
171 Get path only from a path
177 public static String
getFilePathOnly(String filePath
) {
178 String path
= filePath
.substring(0, filePath
.length() - getFileNameOnly(filePath
).length());
179 if (path
.endsWith(DataType
.FILE_SEPARATOR
)) {
180 path
= path
.substring(0, path
.length() - DataType
.FILE_SEPARATOR
.length());
187 Get file name from a path
193 public static String
getFileNameOnly(String filePath
) {
194 File f
= new File(filePath
);
195 return f
.getAbsoluteFile().getName();
198 public static String
getFileNameWithoutExt(String filePath
) {
199 filePath
= getFileNameOnly(filePath
);
200 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
209 @return wholePath - commonPath
212 public static String
getRelativePath(String wholePath
, String commonPath
) {
215 i
= wholePath
.indexOf(commonPath
);
217 i
= i
+ commonPath
.length();
221 path
= wholePath
.substring(i
);
223 // remove file separator of head
225 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
226 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
228 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
229 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
232 // remove file separator of rear
234 if (path
.length() > 0
235 && path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
236 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
238 if (path
.length() > 0
239 && path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
240 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
243 // convert to UNIX format
245 path
= Tools
.convertPathToUnixType(path
);
250 Convert List ot Vector
256 public static Vector
<String
> convertListToVector(List list
) {
257 Vector
<String
> v
= new Vector
<String
>();
258 if (list
!= null && list
.size() > 0) {
259 for (int index
= 0; index
< list
.size(); index
++) {
260 v
.addElement(list
.get(index
).toString());
267 Convert a Vector to a String, separator with ", "
273 public static String
convertVectorToString(Vector
<String
> v
) {
275 for (int index
= 0; index
< v
.size(); index
++) {
276 s
= s
+ v
.elementAt(index
).toString() + ", ";
278 if (s
.length() > 0) {
279 s
= s
.substring(0, s
.length() - ", ".length());
285 Convert a List to a String
291 public static String
convertListToString(List list
) {
292 return Tools
.convertVectorToString(Tools
.convertListToVector(list
));
296 If the input path missing ext, append the ext to the path
303 public static String
addPathExt(String path
, int type
) {
305 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
306 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
308 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
309 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
311 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
312 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
314 if (type
== DataType
.RETURN_TYPE_TEXT
) {
315 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
317 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
318 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
320 if (path
.length() <= match
.length()) {
324 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
336 public static void showInformationMessage(String arg0
) {
337 JOptionPane
.showConfirmDialog(null, arg0
, "Info", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
341 if the string doesn't end with a file separator, append it to the string
347 public static String
addFileSeparator(String arg0
) {
348 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
349 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
355 Wrap single line long input string to multiple short line string by word
357 @param arg0 input string
358 @return wraped string
361 public static String
wrapStringByWord(String arg0
) {
362 int intMaxLength
= 40;
363 String strReturn
= "";
365 boolean isCopied
= true;
370 if (arg0
.length() <= 0) {
375 // Convert string to array by " "
377 String s
[] = arg0
.split(" ");
378 if (arg0
.indexOf(" ") == -1) {
383 // Add each string of array one by one
385 for (int index
= 0; index
< s
.length
; index
++) {
386 String ss
= s
[index
];
389 // The word length > defined line length
391 if (ss
.length() > intMaxLength
) {
393 // Finish previous line
396 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
400 // Separater to short lines
402 while (ss
.length() > 0) {
403 if (ss
.length() > intMaxLength
) {
404 strReturn
= strReturn
+ s
[index
].substring(0, intMaxLength
- 1) + DataType
.UNIX_LINE_SEPARATOR
;
405 ss
= ss
.substring(intMaxLength
);
414 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
415 strTemp
= strTemp
+ " " + ss
;
418 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
419 if ((index
== s
.length
- 1) && (!ss
.equals(""))) {
420 strReturn
= strReturn
+ ss
;
430 strReturn
= strReturn
+ strTemp
;
436 public static String
convertUnicodeHexStringToString(String str
) {
438 // Handle if str is null or empty
443 if (str
.equals("")) {
447 String returnString
= "";
448 String
[] strArray
= str
.split(" ");
449 for (int index
= 0; index
< strArray
.length
; index
++) {
450 String s
= strArray
[index
];
451 if (s
.length() == 6 && s
.indexOf(DataType
.HEX_STRING_HEADER
) == 0) {
452 s
= s
.substring(DataType
.HEX_STRING_HEADER
.length());
454 Log
.err("convertUnicodeHexStringToString", "Incorrect input string: " + str
);
460 int dec
= Integer
.parseInt(s
, 16);
462 returnString
= returnString
+ (char) (dec
);
468 Convert input string to unicode hex string
470 @param str input string
471 @return unicode hex string
474 public static String
convertStringToUnicodeHexString(String str
) {
476 // Handle if str is null or empty
481 if (str
.equals("")) {
486 // convert string to hex string
488 String hexString
= "";
489 for (int index
= 0; index
< str
.length(); index
++) {
490 int codePoint
= str
.codePointAt(index
);
491 String s
= Integer
.toHexString(codePoint
);
493 // Make the string to four length
495 if (s
.length() == 3) {
497 } else if (s
.length() == 2) {
499 } else if (s
.length() == 1) {
504 // Add the string to return hex string
506 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
512 return hexString
.trim();
515 public static ModuleIdentification
getId(String path
, ModuleSurfaceArea msa
) {
516 MsaHeader head
= msa
.getMsaHeader();
517 String name
= head
.getModuleName();
518 String guid
= head
.getGuidValue();
519 String version
= head
.getVersion();
520 ModuleIdentification id
= new ModuleIdentification(name
, guid
, version
, path
);
524 public static PackageIdentification
getId(String path
, PackageSurfaceArea spd
) {
525 SpdHeader head
= spd
.getSpdHeader();
526 String name
= head
.getPackageName();
527 String guid
= head
.getGuidValue();
528 String version
= head
.getVersion();
529 PackageIdentification id
= new PackageIdentification(name
, guid
, version
, path
);
533 public static PlatformIdentification
getId(String path
, PlatformSurfaceArea fpd
) {
534 PlatformHeader head
= fpd
.getPlatformHeader();
535 String name
= head
.getPlatformName();
536 String guid
= head
.getGuidValue();
537 String version
= head
.getVersion();
538 PlatformIdentification id
= new PlatformIdentification(name
, guid
, version
, path
);
543 * To reset the width of input component via container width
546 * @param containerWidth
549 public static void resizeComponentWidth(Component c
, int containerWidth
, int preferredWidth
) {
550 int newWidth
= c
.getPreferredSize().width
+ (containerWidth
- preferredWidth
);
551 if (newWidth
< c
.getPreferredSize().width
) {
552 newWidth
= c
.getPreferredSize().width
;
554 c
.setSize(new java
.awt
.Dimension(newWidth
, c
.getHeight()));
559 * To reset the height of input component via container height
562 * @param containerHeight
565 public static void resizeComponentHeight(Component c
, int containerHeight
, int preferredHeight
) {
566 int newHeight
= c
.getPreferredSize().height
+ (containerHeight
- preferredHeight
);
567 if (newHeight
< c
.getPreferredSize().height
) {
568 newHeight
= c
.getPreferredSize().height
;
570 c
.setSize(new java
.awt
.Dimension(c
.getWidth(), newHeight
));
575 * To reset the size of input component via container size
578 * @param containerWidth
579 * @param containerHeight
582 public static void resizeComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidth
,
583 int preferredHeight
) {
584 resizeComponentWidth(c
, containerWidth
, preferredWidth
);
585 resizeComponentHeight(c
, containerHeight
, preferredHeight
);
589 To adjust each column's width to meet the table's size
591 @param t the table need to be adjusted
592 @param width the new width of the table
595 public static void resizeTableColumn(JTable t
, int width
) {
597 int columnCount
= t
.getColumnCount();
598 for (int index
= 0; index
< columnCount
; index
++) {
599 t
.getColumn(t
.getColumnName(index
)).setPreferredWidth(width
/ columnCount
);
605 * To relocate the input component
608 * @param containerWidth
609 * @param spaceToRight
612 public static void relocateComponentX(Component c
, int containerWidth
, int preferredWidth
, int spaceToRight
) {
613 int intGapToRight
= spaceToRight
+ c
.getPreferredSize().width
;
614 int newLocationX
= containerWidth
- intGapToRight
;
615 if (newLocationX
< preferredWidth
- intGapToRight
) {
616 newLocationX
= preferredWidth
- intGapToRight
;
618 c
.setLocation(newLocationX
, c
.getLocation().y
);
623 * To relocate the input component
626 * @param containerHeight
627 * @param spaceToBottom
630 public static void relocateComponentY(Component c
, int containerHeight
, int preferredHeight
, int spaceToBottom
) {
631 int intGapToBottom
= spaceToBottom
+ c
.getPreferredSize().height
;
632 int newLocationY
= containerHeight
- intGapToBottom
;
633 if (newLocationY
< preferredHeight
- spaceToBottom
) {
634 newLocationY
= preferredHeight
- spaceToBottom
;
636 c
.setLocation(c
.getLocation().x
, newLocationY
);
641 * To relocate the input component
644 * @param containerWidth
645 * @param containerHeight
646 * @param spaceToBottom
647 * @param spaceToRight
650 public static void relocateComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidht
,
651 int preferredHeight
, int spaceToRight
, int spaceToBottom
) {
652 relocateComponentX(c
, containerWidth
, preferredWidht
, spaceToRight
);
653 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
657 Move the component to the center of screen
663 public static void centerComponent(Component c
, int width
) {
664 c
.setLocation(width
/ 2 - c
.getWidth() / 2, c
.getLocation().y
);
669 Move the component to the center of screen and adjust the y location
675 public static void centerComponent(Component c
, int width
, int containerHeight
, int preferredHeight
,
677 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
678 centerComponent(c
, width
);
682 Find the count of searchString in wholeString
689 public static int getSpecificStringCount(String wholeString
, String searchString
) {
691 count
= wholeString
.split(searchString
).length
;
696 * Check the input data is empty or not
699 * The input data which need be checked
701 * @retval true - The input data is empty
702 * @retval fals - The input data is not empty
705 public static boolean isEmpty(String strValue
) {
706 if (strValue
.length() > 0) {