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 && path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
235 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
237 if (path
.length() > 0 && path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
238 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
241 // convert to UNIX format
243 path
= Tools
.convertPathToUnixType(path
);
248 Convert List ot Vector
254 public static Vector
<String
> convertListToVector(List list
) {
255 Vector
<String
> v
= new Vector
<String
>();
256 if (list
!= null && list
.size() > 0) {
257 for (int index
= 0; index
< list
.size(); index
++) {
258 v
.addElement(list
.get(index
).toString());
265 If the input path missing ext, append the ext to the path
272 public static String
addPathExt(String path
, int type
) {
274 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
275 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
277 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
278 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
280 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
281 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
283 if (type
== DataType
.RETURN_TYPE_TEXT
) {
284 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
286 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
287 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
289 if (path
.length() <= match
.length()) {
293 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
305 public static void showInformationMessage(String arg0
) {
306 JOptionPane
.showConfirmDialog(null, arg0
, "Info", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
310 if the string doesn't end with a file separator, append it to the string
316 public static String
addFileSeparator(String arg0
) {
317 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
318 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
324 Wrap single line long input string to multiple short line string by word
326 @param arg0 input string
327 @return wraped string
330 public static String
wrapStringByWord(String arg0
) {
331 int intMaxLength
= 40;
332 String strReturn
= "";
334 boolean isCopied
= true;
339 if (arg0
.length() <= 0) {
344 // Convert string to array by " "
346 String s
[] = arg0
.split(" ");
347 if (arg0
.indexOf(" ") == -1) {
352 // Add each string of array one by one
354 for (int index
= 0; index
< s
.length
; index
++) {
355 String ss
= s
[index
];
358 // The word length > defined line length
360 if (ss
.length() > intMaxLength
) {
362 // Finish previous line
365 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
369 // Separater to short lines
371 while (ss
.length() > 0) {
372 if (ss
.length() > intMaxLength
) {
373 strReturn
= strReturn
+ s
[index
].substring(0, intMaxLength
- 1) + DataType
.UNIX_LINE_SEPARATOR
;
374 ss
= ss
.substring(intMaxLength
);
383 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
384 strTemp
= strTemp
+ " " + ss
;
387 strReturn
= strReturn
+ strTemp
+ DataType
.LINE_SEPARATOR
;
388 if ((index
== s
.length
- 1) && (!ss
.equals(""))) {
389 strReturn
= strReturn
+ ss
;
399 strReturn
= strReturn
+ strTemp
;
405 public static String
convertUnicodeHexStringToString(String str
) {
407 // Handle if str is null or empty
412 if (str
.equals("")) {
416 String returnString
= "";
417 String
[] strArray
= str
.split(" ");
418 for (int index
= 0; index
< strArray
.length
; index
++) {
419 String s
= strArray
[index
];
420 if (s
.length() == 6 && s
.indexOf(DataType
.HEX_STRING_HEADER
) == 0) {
421 s
= s
.substring(DataType
.HEX_STRING_HEADER
.length());
423 Log
.err("convertUnicodeHexStringToString", "Incorrect input string: " + str
);
429 int dec
= Integer
.parseInt(s
, 16);
431 returnString
= returnString
+ (char) (dec
);
437 Convert input string to unicode hex string
439 @param str input string
440 @return unicode hex string
443 public static String
convertStringToUnicodeHexString(String str
) {
445 // Handle if str is null or empty
450 if (str
.equals("")) {
455 // convert string to hex string
457 String hexString
= "";
458 for (int index
= 0; index
< str
.length(); index
++) {
459 int codePoint
= str
.codePointAt(index
);
460 String s
= Integer
.toHexString(codePoint
);
462 // Make the string to four length
464 if (s
.length() == 3) {
466 } else if (s
.length() == 2) {
468 } else if (s
.length() == 1) {
473 // Add the string to return hex string
475 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
481 return hexString
.trim();
484 public static ModuleIdentification
getId(String path
, ModuleSurfaceArea msa
) {
485 MsaHeader head
= msa
.getMsaHeader();
486 String name
= head
.getModuleName();
487 String guid
= head
.getGuidValue();
488 String version
= head
.getVersion();
489 ModuleIdentification id
= new ModuleIdentification(name
, guid
, version
, path
);
493 public static PackageIdentification
getId(String path
, PackageSurfaceArea spd
) {
494 SpdHeader head
= spd
.getSpdHeader();
495 String name
= head
.getPackageName();
496 String guid
= head
.getGuidValue();
497 String version
= head
.getVersion();
498 PackageIdentification id
= new PackageIdentification(name
, guid
, version
, path
);
502 public static PlatformIdentification
getId(String path
, PlatformSurfaceArea fpd
) {
503 PlatformHeader head
= fpd
.getPlatformHeader();
504 String name
= head
.getPlatformName();
505 String guid
= head
.getGuidValue();
506 String version
= head
.getVersion();
507 PlatformIdentification id
= new PlatformIdentification(name
, guid
, version
, path
);
512 * To reset the width of input component via container width
515 * @param containerWidth
518 public static void resizeComponentWidth(Component c
, int containerWidth
, int preferredWidth
) {
519 int newWidth
= c
.getPreferredSize().width
+ (containerWidth
- preferredWidth
);
520 if (newWidth
< c
.getPreferredSize().width
) {
521 newWidth
= c
.getPreferredSize().width
;
523 c
.setSize(new java
.awt
.Dimension(newWidth
, c
.getHeight()));
528 * To reset the height of input component via container height
531 * @param containerHeight
534 public static void resizeComponentHeight(Component c
, int containerHeight
, int preferredHeight
) {
535 int newHeight
= c
.getPreferredSize().height
+ (containerHeight
- preferredHeight
);
536 if (newHeight
< c
.getPreferredSize().height
) {
537 newHeight
= c
.getPreferredSize().height
;
539 c
.setSize(new java
.awt
.Dimension(c
.getWidth(), newHeight
));
544 * To reset the size of input component via container size
547 * @param containerWidth
548 * @param containerHeight
551 public static void resizeComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidth
,
552 int preferredHeight
) {
553 resizeComponentWidth(c
, containerWidth
, preferredWidth
);
554 resizeComponentHeight(c
, containerHeight
, preferredHeight
);
558 To adjust each column's width to meet the table's size
560 @param t the table need to be adjusted
561 @param width the new width of the table
564 public static void resizeTableColumn(JTable t
, int width
) {
566 int columnCount
= t
.getColumnCount();
567 for (int index
= 0; index
< columnCount
; index
++) {
568 t
.getColumn(t
.getColumnName(index
)).setPreferredWidth(width
/ columnCount
);
574 * To relocate the input component
577 * @param containerWidth
578 * @param spaceToRight
581 public static void relocateComponentX(Component c
, int containerWidth
, int preferredWidth
, int spaceToRight
) {
582 int intGapToRight
= spaceToRight
+ c
.getPreferredSize().width
;
583 int newLocationX
= containerWidth
- intGapToRight
;
584 if (newLocationX
< preferredWidth
- intGapToRight
) {
585 newLocationX
= preferredWidth
- intGapToRight
;
587 c
.setLocation(newLocationX
, c
.getLocation().y
);
592 * To relocate the input component
595 * @param containerHeight
596 * @param spaceToBottom
599 public static void relocateComponentY(Component c
, int containerHeight
, int preferredHeight
, int spaceToBottom
) {
600 int intGapToBottom
= spaceToBottom
+ c
.getPreferredSize().height
;
601 int newLocationY
= containerHeight
- intGapToBottom
;
602 if (newLocationY
< preferredHeight
- spaceToBottom
) {
603 newLocationY
= preferredHeight
- spaceToBottom
;
605 c
.setLocation(c
.getLocation().x
, newLocationY
);
610 * To relocate the input component
613 * @param containerWidth
614 * @param containerHeight
615 * @param spaceToBottom
616 * @param spaceToRight
619 public static void relocateComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidht
,
620 int preferredHeight
, int spaceToRight
, int spaceToBottom
) {
621 relocateComponentX(c
, containerWidth
, preferredWidht
, spaceToRight
);
622 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
626 Move the component to the center of screen
632 public static void centerComponent(Component c
, int width
) {
633 c
.setLocation(width
/ 2 - c
.getWidth() / 2, c
.getLocation().y
);
638 Move the component to the center of screen and adjust the y location
644 public static void centerComponent(Component c
, int width
, int containerHeight
, int preferredHeight
,
646 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
647 centerComponent(c
, width
);
651 Find the count of searchString in wholeString
658 public static int getSpecificStringCount(String wholeString
, String searchString
) {
660 count
= wholeString
.split(searchString
).length
;
665 * Check the input data is empty or not
668 * The input data which need be checked
670 * @retval true - The input data is empty
671 * @retval fals - The input data is not empty
674 public static boolean isEmpty(String strValue
) {
675 if (strValue
.length() > 0) {