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
;
30 import org
.tianocore
.frameworkwizard
.module
.Identifications
.ModuleIdentification
;
31 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
32 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
35 The class is used to provides some useful interfaces
41 // The dir user selected to create new package in
43 public static String dirForNewSpd
= null;
51 public static void main(String
[] args
) {
52 System
.out
.println(getCurrentDateTime());
53 // Vector<String> v = new Vector<String>();
54 // Vector<String> v1 = new Vector<String>();
56 // v.addElement("CAC");
57 // v1.addElement("1111");
58 // v.addElement("1AC");
59 // v1.addElement("2222");
60 // v.addElement("ABC");
61 // v1.addElement("3333");
62 // v.addElement("0C");
63 // v1.addElement("4444");
64 // v.addElement("AAC");
65 // v1.addElement("5555");
66 // Vector<Integer> vs = new Vector<Integer>();
67 // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);
68 // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));
70 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
71 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
75 Get current date and time and format it as "yyyy-MM-dd HH:mm"
77 @return formatted current date and time
80 public static String
getCurrentDateTime() {
81 Date now
= new Date(System
.currentTimeMillis());
82 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
83 return sdf
.format(now
);
89 @return the created UUID
92 public static String
generateUuidString() {
93 return UUID
.randomUUID().toString();
97 Use current file separator in the path
103 public static String
convertPathToCurrentOsType(String strPath
) {
104 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
105 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
110 Use Unix file separator in the path
116 public static String
convertPathToUnixType(String strPath
) {
117 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
122 Use Dos file separator in the path
128 public static String
convertPathToDosType(String strPath
) {
129 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
134 Get all system properties and output to the console
137 public static void getSystemProperties() {
138 System
.out
.println(System
.getProperty("java.class.version"));
139 System
.out
.println(System
.getProperty("java.class.path"));
140 System
.out
.println(System
.getProperty("java.ext.dirs"));
141 System
.out
.println(System
.getProperty("os.name"));
142 System
.out
.println(System
.getProperty("os.arch"));
143 System
.out
.println(System
.getProperty("os.version"));
144 System
.out
.println(System
.getProperty("file.separator"));
145 System
.out
.println(System
.getProperty("path.separator"));
146 System
.out
.println(System
.getProperty("line.separator"));
147 System
.out
.println(System
.getProperty("user.name"));
148 System
.out
.println(System
.getProperty("user.home"));
149 System
.out
.println(System
.getProperty("user.dir"));
150 System
.out
.println(System
.getProperty("PATH"));
152 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
156 Generate selection items for JComboBox by input vector
159 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
161 jcb
.removeAllItems();
163 if (vector
!= null) {
164 for (int index
= 0; index
< vector
.size(); index
++) {
165 jcb
.addItem(vector
.elementAt(index
));
171 Generate selection items for JList by input vector
174 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
176 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
177 listModel
.removeAllElements();
179 if (vector
!= null) {
180 for (int index
= 0; index
< vector
.size(); index
++) {
181 listModel
.addElement(vector
.get(index
));
185 if (listModel
.size() > 0) {
186 jl
.setSelectedIndex(0);
192 Get path only from a path
198 public static String
getFilePathOnly(String filePath
) {
199 String path
= filePath
.substring(0, filePath
.length() - getFileNameOnly(filePath
).length());
200 if (path
.endsWith(DataType
.FILE_SEPARATOR
)) {
201 path
= path
.substring(0, path
.length() - DataType
.FILE_SEPARATOR
.length());
208 Get file name from a path
214 public static String
getFileNameOnly(String filePath
) {
215 File f
= new File(filePath
);
216 return f
.getAbsoluteFile().getName();
219 public static String
getFileNameWithoutExt(String filePath
) {
220 filePath
= getFileNameOnly(filePath
);
221 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
230 @return wholePath - commonPath
233 public static String
getRelativePath(String wholePath
, String commonPath
) {
236 i
= wholePath
.indexOf(commonPath
);
238 i
= i
+ commonPath
.length();
242 path
= wholePath
.substring(i
);
244 // remove file separator of head
246 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
247 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
249 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
250 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
253 // remove file separator of rear
255 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
256 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
258 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
259 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
262 // convert to UNIX format
264 path
= Tools
.convertPathToUnixType(path
);
269 Convert List ot Vector
275 public static Vector
<String
> convertListToVector(List list
) {
276 Vector
<String
> v
= new Vector
<String
>();
277 if (list
!= null && list
.size() > 0) {
278 for (int index
= 0; index
< list
.size(); index
++) {
279 v
.addElement(list
.get(index
).toString());
286 If the input path missing ext, append the ext to the path
293 public static String
addPathExt(String path
, int type
) {
295 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
296 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
298 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
299 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
301 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
302 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
304 if (type
== DataType
.RETURN_TYPE_TEXT
) {
305 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
307 if (path
.length() <= match
.length()) {
311 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
323 public static void showInformationMessage(String arg0
) {
324 JOptionPane
.showConfirmDialog(null, arg0
, "Error", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
328 if the string doesn't end with a file separator, append it to the string
334 public static String
addFileSeparator(String arg0
) {
335 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
336 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
342 Sort all elements in the vector as the specific sort type
344 @param v The vector need to be sorted
345 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
348 public static void sortVectorString(Vector
<String
> v
, int mode
) {
350 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
351 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
352 if ((v
.get(indexJ
).compareTo(v
.get(indexI
)) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
353 || (v
.get(indexI
).compareTo(v
.get(indexJ
)) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
354 String temp
= v
.get(indexI
);
355 v
.setElementAt(v
.get(indexJ
), indexI
);
356 v
.setElementAt(temp
, indexJ
);
364 Sort all elements of vector and return sorted sequence
366 @param v The vector need to be sorted
367 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
368 @return Vector<Integer> The sorted sequence
371 public static Vector
<Integer
> getVectorSortSequence(Vector
<String
> v
, int mode
) {
372 Vector
<Integer
> vSequence
= new Vector
<Integer
>();
377 for (int index
= 0; index
< v
.size(); index
++) {
378 vSequence
.addElement(index
);
383 // sort and get new sequence
385 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
386 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
387 if ((v
.get(indexJ
).compareTo(v
.get(indexI
)) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
388 || (v
.get(indexI
).compareTo(v
.get(indexJ
)) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
392 String tempStr
= v
.get(indexI
);
393 v
.setElementAt(v
.get(indexJ
), indexI
);
394 v
.setElementAt(tempStr
, indexJ
);
399 int tempInt
= vSequence
.get(indexI
);
400 vSequence
.setElementAt(vSequence
.get(indexJ
), indexI
);
401 vSequence
.setElementAt(tempInt
, indexJ
);
410 Sort all elements of vector as input sequence
412 @param v The vector need to be sorted
413 @param vSequence The sort sequence should be followed
416 public static void sortVectorString(Vector
<String
> v
, Vector
<Integer
> vSequence
) {
417 if (v
!= null && vSequence
!= null && v
.size() == vSequence
.size()) {
418 Vector
<String
> tempV
= new Vector
<String
>();
419 for (int index
= 0; index
< v
.size(); index
++) {
420 tempV
.addElement(v
.get(index
));
422 for (int index
= 0; index
< v
.size(); index
++) {
423 v
.setElementAt(tempV
.get(vSequence
.get(index
)), index
);
435 public static void sortModules(Vector
<ModuleIdentification
> v
, int mode
) {
440 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
441 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
442 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
443 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
444 ModuleIdentification temp
= v
.get(indexI
);
445 v
.setElementAt(v
.get(indexJ
), indexI
);
446 v
.setElementAt(temp
, indexJ
);
460 public static void sortPackages(Vector
<PackageIdentification
> v
, int mode
) {
465 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
466 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
467 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
468 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
469 PackageIdentification temp
= v
.get(indexI
);
470 v
.setElementAt(v
.get(indexJ
), indexI
);
471 v
.setElementAt(temp
, indexJ
);
485 public static void sortPlatforms(Vector
<PlatformIdentification
> v
, int mode
) {
490 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
491 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
492 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
493 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
494 PlatformIdentification temp
= v
.get(indexI
);
495 v
.setElementAt(v
.get(indexJ
), indexI
);
496 v
.setElementAt(temp
, indexJ
);