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
;
19 import java
.text
.SimpleDateFormat
;
20 import java
.util
.Date
;
21 import java
.util
.List
;
22 import java
.util
.UUID
;
23 import java
.util
.Vector
;
25 import javax
.swing
.DefaultListModel
;
26 import javax
.swing
.JComboBox
;
27 import javax
.swing
.JList
;
28 import javax
.swing
.JOptionPane
;
31 The class is used to provides some useful interfaces
37 // The dir user selected to create new package in
39 public static String dirForNewSpd
= null;
47 public static void main(String
[] args
) {
48 System
.out
.println(getCurrentDateTime());
49 // Vector<String> v = new Vector<String>();
50 // Vector<String> v1 = new Vector<String>();
52 // v.addElement("CAC");
53 // v1.addElement("1111");
54 // v.addElement("1AC");
55 // v1.addElement("2222");
56 // v.addElement("ABC");
57 // v1.addElement("3333");
58 // v.addElement("0C");
59 // v1.addElement("4444");
60 // v.addElement("AAC");
61 // v1.addElement("5555");
62 // Vector<Integer> vs = new Vector<Integer>();
63 // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);
64 // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));
66 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
67 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
71 Get current date and time and format it as "yyyy-MM-dd HH:mm"
73 @return formatted current date and time
76 public static String
getCurrentDateTime() {
77 Date now
= new Date(System
.currentTimeMillis());
78 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
79 return sdf
.format(now
);
85 @return the created UUID
88 public static String
generateUuidString() {
89 return UUID
.randomUUID().toString();
93 Use current file separator in the path
99 public static String
convertPathToCurrentOsType(String strPath
) {
100 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
101 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
106 Use Unix file separator in the path
112 public static String
convertPathToUnixType(String strPath
) {
113 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
118 Use Dos file separator in the path
124 public static String
convertPathToDosType(String strPath
) {
125 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
130 Get all system properties and output to the console
133 public static void getSystemProperties() {
134 System
.out
.println(System
.getProperty("java.class.version"));
135 System
.out
.println(System
.getProperty("java.class.path"));
136 System
.out
.println(System
.getProperty("java.ext.dirs"));
137 System
.out
.println(System
.getProperty("os.name"));
138 System
.out
.println(System
.getProperty("os.arch"));
139 System
.out
.println(System
.getProperty("os.version"));
140 System
.out
.println(System
.getProperty("file.separator"));
141 System
.out
.println(System
.getProperty("path.separator"));
142 System
.out
.println(System
.getProperty("line.separator"));
143 System
.out
.println(System
.getProperty("user.name"));
144 System
.out
.println(System
.getProperty("user.home"));
145 System
.out
.println(System
.getProperty("user.dir"));
146 System
.out
.println(System
.getProperty("PATH"));
148 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
152 Generate selection items for JComboBox by input vector
155 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
157 jcb
.removeAllItems();
159 if (vector
!= null) {
160 for (int index
= 0; index
< vector
.size(); index
++) {
161 jcb
.addItem(vector
.elementAt(index
));
167 Generate selection items for JList by input vector
170 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
172 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
173 listModel
.removeAllElements();
175 if (vector
!= null) {
176 for (int index
= 0; index
< vector
.size(); index
++) {
177 listModel
.addElement(vector
.get(index
));
181 if (listModel
.size() > 0) {
182 jl
.setSelectedIndex(0);
188 Get path only from a path
194 public static String
getFilePathOnly(String filePath
) {
195 String path
= filePath
.substring(0, filePath
.length() - getFileNameOnly(filePath
).length());
196 if (path
.endsWith(DataType
.FILE_SEPARATOR
)) {
197 path
= path
.substring(0, path
.length() - DataType
.FILE_SEPARATOR
.length());
204 Get file name from a path
210 public static String
getFileNameOnly(String filePath
) {
211 File f
= new File(filePath
);
212 return f
.getAbsoluteFile().getName();
215 public static String
getFileNameWithoutExt(String filePath
) {
216 filePath
= getFileNameOnly(filePath
);
217 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
226 @return wholePath - commonPath
229 public static String
getRelativePath(String wholePath
, String commonPath
) {
232 i
= wholePath
.indexOf(commonPath
);
234 i
= i
+ commonPath
.length();
238 path
= wholePath
.substring(i
);
240 // remove file separator of head
242 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
243 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
245 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
246 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
249 // remove file separator of rear
251 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
252 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
254 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
255 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
258 // convert to UNIX format
260 path
= Tools
.convertPathToUnixType(path
);
265 Convert List ot Vector
271 public static Vector
<String
> convertListToVector(List list
) {
272 Vector
<String
> v
= new Vector
<String
>();
273 if (list
!= null && list
.size() > 0) {
274 for (int index
= 0; index
< list
.size(); index
++) {
275 v
.addElement(list
.get(index
).toString());
282 If the input path missing ext, append the ext to the path
289 public static String
addPathExt(String path
, int type
) {
291 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
292 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
294 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
295 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
297 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
298 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
300 if (type
== DataType
.RETURN_TYPE_TEXT
) {
301 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
303 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
304 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
306 if (path
.length() <= match
.length()) {
310 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
322 public static void showInformationMessage(String arg0
) {
323 JOptionPane
.showConfirmDialog(null, arg0
, "Error", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
327 if the string doesn't end with a file separator, append it to the string
333 public static String
addFileSeparator(String arg0
) {
334 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
335 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
341 Wrap single line long input string to multiple short line string by word
343 @param arg0 input string
344 @return wraped string
347 public static String
wrapStringByWord(String arg0
) {
348 int intMaxLength
= 40;
349 String strReturn
= "";
351 boolean isCopied
= true;
354 // Convert string to array by " "
356 String s
[] = arg0
.split(" ");
357 if (arg0
.indexOf(" ") == -1) {
362 // Add each string of array one by one
364 for (int index
= 0; index
< s
.length
; index
++) {
365 String ss
= s
[index
];
368 // The word length > defined line length
370 if (ss
.length() > intMaxLength
) {
372 // Finish previous line
375 strReturn
= strReturn
+ strTemp
+ DataType
.UNIX_LINE_SEPARATOR
;
379 // Separater to short lines
381 while (ss
.length() > 0) {
382 if (ss
.length() > intMaxLength
) {
383 strReturn
= strReturn
+ s
[index
].substring(0, intMaxLength
- 1) + DataType
.UNIX_LINE_SEPARATOR
;
384 ss
= ss
.substring(intMaxLength
);
393 if ((strTemp
+ " " + ss
).length() <= intMaxLength
) {
394 strTemp
= strTemp
+ " " + ss
;
397 strReturn
= strReturn
+ strTemp
+ DataType
.UNIX_LINE_SEPARATOR
;
405 strReturn
= strReturn
+ strTemp
;