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 (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
308 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
310 if (path
.length() <= match
.length()) {
314 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
326 public static void showInformationMessage(String arg0
) {
327 JOptionPane
.showConfirmDialog(null, arg0
, "Error", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
331 if the string doesn't end with a file separator, append it to the string
337 public static String
addFileSeparator(String arg0
) {
338 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
339 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
345 Sort all elements in the vector as the specific sort type
347 @param v The vector need to be sorted
348 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
351 public static void sortVectorString(Vector
<String
> v
, int mode
) {
353 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
354 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
355 if ((v
.get(indexJ
).compareTo(v
.get(indexI
)) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
356 || (v
.get(indexI
).compareTo(v
.get(indexJ
)) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
357 String temp
= v
.get(indexI
);
358 v
.setElementAt(v
.get(indexJ
), indexI
);
359 v
.setElementAt(temp
, indexJ
);
367 Sort all elements of vector and return sorted sequence
369 @param v The vector need to be sorted
370 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
371 @return Vector<Integer> The sorted sequence
374 public static Vector
<Integer
> getVectorSortSequence(Vector
<String
> v
, int mode
) {
375 Vector
<Integer
> vSequence
= new Vector
<Integer
>();
380 for (int index
= 0; index
< v
.size(); index
++) {
381 vSequence
.addElement(index
);
386 // sort and get new sequence
388 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
389 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
390 if ((v
.get(indexJ
).compareTo(v
.get(indexI
)) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
391 || (v
.get(indexI
).compareTo(v
.get(indexJ
)) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
395 String tempStr
= v
.get(indexI
);
396 v
.setElementAt(v
.get(indexJ
), indexI
);
397 v
.setElementAt(tempStr
, indexJ
);
402 int tempInt
= vSequence
.get(indexI
);
403 vSequence
.setElementAt(vSequence
.get(indexJ
), indexI
);
404 vSequence
.setElementAt(tempInt
, indexJ
);
413 Sort all elements of vector as input sequence
415 @param v The vector need to be sorted
416 @param vSequence The sort sequence should be followed
419 public static void sortVectorString(Vector
<String
> v
, Vector
<Integer
> vSequence
) {
420 if (v
!= null && vSequence
!= null && v
.size() == vSequence
.size()) {
421 Vector
<String
> tempV
= new Vector
<String
>();
422 for (int index
= 0; index
< v
.size(); index
++) {
423 tempV
.addElement(v
.get(index
));
425 for (int index
= 0; index
< v
.size(); index
++) {
426 v
.setElementAt(tempV
.get(vSequence
.get(index
)), index
);
438 public static void sortModules(Vector
<ModuleIdentification
> v
, int mode
) {
443 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
444 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
445 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
446 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
447 ModuleIdentification temp
= v
.get(indexI
);
448 v
.setElementAt(v
.get(indexJ
), indexI
);
449 v
.setElementAt(temp
, indexJ
);
463 public static void sortPackages(Vector
<PackageIdentification
> v
, int mode
) {
468 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
469 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
470 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
471 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
472 PackageIdentification temp
= v
.get(indexI
);
473 v
.setElementAt(v
.get(indexJ
), indexI
);
474 v
.setElementAt(temp
, indexJ
);
488 public static void sortPlatforms(Vector
<PlatformIdentification
> v
, int mode
) {
493 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
494 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
495 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
496 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
497 PlatformIdentification temp
= v
.get(indexI
);
498 v
.setElementAt(v
.get(indexJ
), indexI
);
499 v
.setElementAt(temp
, indexJ
);