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
.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
.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
.UNIX_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
.UNIX_LINE_SEPARATOR
;
387 strReturn
= strReturn
+ strTemp
;
393 public static String
convertUnicodeHexStringToString(String str
) {
395 // Handle if str is null or empty
400 if (str
.equals("")) {
404 String returnString
= "";
405 String
[] strArray
= str
.split(" ");
406 for (int index
= 0; index
< strArray
.length
; index
++) {
407 String s
= strArray
[index
];
408 if (s
.length() == 6 && s
.indexOf(DataType
.HEX_STRING_HEADER
) == 0) {
409 s
= s
.substring(DataType
.HEX_STRING_HEADER
.length());
411 Log
.err("convertUnicodeHexStringToString", "Incorrect input string: " + str
);
417 int dec
= Integer
.parseInt(s
, 16);
419 returnString
= returnString
+ (char) (dec
);
425 Convert input string to unicode hex string
427 @param str input string
428 @return unicode hex string
431 public static String
convertStringToUnicodeHexString(String str
) {
433 // Handle if str is null or empty
438 if (str
.equals("")) {
443 // convert string to hex string
445 String hexString
= "";
446 for (int index
= 0; index
< str
.length(); index
++) {
447 int codePoint
= str
.codePointAt(index
);
448 String s
= Integer
.toHexString(codePoint
);
450 // Make the string to four length
452 if (s
.length() == 3) {
454 } else if (s
.length() == 2) {
456 } else if (s
.length() == 1) {
461 // Add the string to return hex string
463 hexString
= hexString
+ DataType
.HEX_STRING_HEADER
+ s
+ " ";
469 return hexString
.trim();
472 public static ModuleIdentification
getId(String path
, ModuleSurfaceArea msa
) {
473 MsaHeader head
= msa
.getMsaHeader();
474 String name
= head
.getModuleName();
475 String guid
= head
.getGuidValue();
476 String version
= head
.getVersion();
477 ModuleIdentification id
= new ModuleIdentification(name
, guid
, version
, path
);
481 public static PackageIdentification
getId(String path
, PackageSurfaceArea spd
) {
482 SpdHeader head
= spd
.getSpdHeader();
483 String name
= head
.getPackageName();
484 String guid
= head
.getGuidValue();
485 String version
= head
.getVersion();
486 PackageIdentification id
= new PackageIdentification(name
, guid
, version
, path
);
490 public static PlatformIdentification
getId(String path
, PlatformSurfaceArea fpd
) {
491 PlatformHeader head
= fpd
.getPlatformHeader();
492 String name
= head
.getPlatformName();
493 String guid
= head
.getGuidValue();
494 String version
= head
.getVersion();
495 PlatformIdentification id
= new PlatformIdentification(name
, guid
, version
, path
);
500 * To reset the width of input component via container width
503 * @param containerWidth
506 public static void resizeComponentWidth(Component c
, int containerWidth
, int preferredWidth
) {
507 int newWidth
= c
.getPreferredSize().width
+ (containerWidth
- preferredWidth
);
508 if (newWidth
< c
.getPreferredSize().width
) {
509 newWidth
= c
.getPreferredSize().width
;
511 c
.setSize(new java
.awt
.Dimension(newWidth
, c
.getHeight()));
516 * To reset the height of input component via container height
519 * @param containerHeight
522 public static void resizeComponentHeight(Component c
, int containerHeight
, int preferredHeight
) {
523 int newHeight
= c
.getPreferredSize().height
+ (containerHeight
- preferredHeight
);
524 if (newHeight
< c
.getPreferredSize().height
) {
525 newHeight
= c
.getPreferredSize().height
;
527 c
.setSize(new java
.awt
.Dimension(c
.getWidth(), newHeight
));
532 * To reset the size of input component via container size
535 * @param containerWidth
536 * @param containerHeight
539 public static void resizeComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidth
,
540 int preferredHeight
) {
541 resizeComponentWidth(c
, containerWidth
, preferredWidth
);
542 resizeComponentHeight(c
, containerHeight
, preferredHeight
);
546 * To relocate the input component
549 * @param containerWidth
550 * @param spaceToRight
553 public static void relocateComponentX(Component c
, int containerWidth
, int preferredWidth
, int spaceToRight
) {
554 int intGapToRight
= spaceToRight
+ c
.getPreferredSize().width
;
555 int newLocationX
= containerWidth
- intGapToRight
;
556 if (newLocationX
< preferredWidth
- intGapToRight
) {
557 newLocationX
= preferredWidth
- intGapToRight
;
559 c
.setLocation(newLocationX
, c
.getLocation().y
);
564 * To relocate the input component
567 * @param containerHeight
568 * @param spaceToBottom
571 public static void relocateComponentY(Component c
, int containerHeight
, int preferredHeight
, int spaceToBottom
) {
572 int intGapToBottom
= spaceToBottom
+ c
.getPreferredSize().height
;
573 int newLocationY
= containerHeight
- intGapToBottom
;
574 if (newLocationY
< preferredHeight
- spaceToBottom
) {
575 newLocationY
= preferredHeight
- spaceToBottom
;
577 c
.setLocation(c
.getLocation().x
, newLocationY
);
582 * To relocate the input component
585 * @param containerWidth
586 * @param containerHeight
587 * @param spaceToBottom
588 * @param spaceToRight
591 public static void relocateComponent(Component c
, int containerWidth
, int containerHeight
, int preferredWidht
,
592 int preferredHeight
, int spaceToRight
, int spaceToBottom
) {
593 relocateComponentX(c
, containerWidth
, preferredWidht
, spaceToRight
);
594 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
598 Move the component to the center of screen
604 public static void centerComponent(Component c
, int width
) {
605 c
.setLocation(width
/ 2 - c
.getWidth() / 2, c
.getLocation().y
);
610 Move the component to the center of screen and adjust the y location
616 public static void centerComponent(Component c
, int width
, int containerHeight
, int preferredHeight
,
618 relocateComponentY(c
, containerHeight
, preferredHeight
, spaceToBottom
);
619 centerComponent(c
, width
);
623 Find the count of searchString in wholeString
630 public static int getSpecificStringCount(String wholeString
, String searchString
) {
632 count
= wholeString
.split(searchString
).length
;