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
.module
.Identifications
.PcdCoded
.PcdIdentification
;
32 import org
.tianocore
.frameworkwizard
.module
.Identifications
.PcdCoded
.PcdVector
;
33 import org
.tianocore
.frameworkwizard
.packaging
.PackageIdentification
;
34 import org
.tianocore
.frameworkwizard
.platform
.PlatformIdentification
;
37 The class is used to provides some useful interfaces
43 // The dir user selected to create new package in
45 public static String dirForNewSpd
= null;
53 public static void main(String
[] args
) {
54 System
.out
.println(getCurrentDateTime());
55 // Vector<String> v = new Vector<String>();
56 // Vector<String> v1 = new Vector<String>();
58 // v.addElement("CAC");
59 // v1.addElement("1111");
60 // v.addElement("1AC");
61 // v1.addElement("2222");
62 // v.addElement("ABC");
63 // v1.addElement("3333");
64 // v.addElement("0C");
65 // v1.addElement("4444");
66 // v.addElement("AAC");
67 // v1.addElement("5555");
68 // Vector<Integer> vs = new Vector<Integer>();
69 // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);
70 // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));
72 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
73 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
77 Get current date and time and format it as "yyyy-MM-dd HH:mm"
79 @return formatted current date and time
82 public static String
getCurrentDateTime() {
83 Date now
= new Date(System
.currentTimeMillis());
84 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy-MM-dd HH:mm");
85 return sdf
.format(now
);
91 @return the created UUID
94 public static String
generateUuidString() {
95 return UUID
.randomUUID().toString();
99 Use current file separator in the path
105 public static String
convertPathToCurrentOsType(String strPath
) {
106 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
107 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.FILE_SEPARATOR
);
112 Use Unix file separator in the path
118 public static String
convertPathToUnixType(String strPath
) {
119 strPath
= strPath
.replace(DataType
.DOS_FILE_SEPARATOR
, DataType
.UNIX_FILE_SEPARATOR
);
124 Use Dos file separator in the path
130 public static String
convertPathToDosType(String strPath
) {
131 strPath
= strPath
.replace(DataType
.UNIX_FILE_SEPARATOR
, DataType
.DOS_FILE_SEPARATOR
);
136 Get all system properties and output to the console
139 public static void getSystemProperties() {
140 System
.out
.println(System
.getProperty("java.class.version"));
141 System
.out
.println(System
.getProperty("java.class.path"));
142 System
.out
.println(System
.getProperty("java.ext.dirs"));
143 System
.out
.println(System
.getProperty("os.name"));
144 System
.out
.println(System
.getProperty("os.arch"));
145 System
.out
.println(System
.getProperty("os.version"));
146 System
.out
.println(System
.getProperty("file.separator"));
147 System
.out
.println(System
.getProperty("path.separator"));
148 System
.out
.println(System
.getProperty("line.separator"));
149 System
.out
.println(System
.getProperty("user.name"));
150 System
.out
.println(System
.getProperty("user.home"));
151 System
.out
.println(System
.getProperty("user.dir"));
152 System
.out
.println(System
.getProperty("PATH"));
154 System
.out
.println(System
.getenv("PROCESSOR_REVISION"));
158 Generate selection items for JComboBox by input vector
161 public static void generateComboBoxByVector(JComboBox jcb
, Vector
<String
> vector
) {
163 jcb
.removeAllItems();
165 if (vector
!= null) {
166 for (int index
= 0; index
< vector
.size(); index
++) {
167 jcb
.addItem(vector
.elementAt(index
));
173 Generate selection items for JList by input vector
176 public static void generateListByVector(JList jl
, Vector
<String
> vector
) {
178 DefaultListModel listModel
= (DefaultListModel
) jl
.getModel();
179 listModel
.removeAllElements();
181 if (vector
!= null) {
182 for (int index
= 0; index
< vector
.size(); index
++) {
183 listModel
.addElement(vector
.get(index
));
187 if (listModel
.size() > 0) {
188 jl
.setSelectedIndex(0);
194 Get path only from a path
200 public static String
getFilePathOnly(String filePath
) {
201 String path
= filePath
.substring(0, filePath
.length() - getFileNameOnly(filePath
).length());
202 if (path
.endsWith(DataType
.FILE_SEPARATOR
)) {
203 path
= path
.substring(0, path
.length() - DataType
.FILE_SEPARATOR
.length());
210 Get file name from a path
216 public static String
getFileNameOnly(String filePath
) {
217 File f
= new File(filePath
);
218 return f
.getAbsoluteFile().getName();
221 public static String
getFileNameWithoutExt(String filePath
) {
222 filePath
= getFileNameOnly(filePath
);
223 filePath
= filePath
.substring(0, filePath
.lastIndexOf(DataType
.FILE_EXT_SEPARATOR
));
232 @return wholePath - commonPath
235 public static String
getRelativePath(String wholePath
, String commonPath
) {
238 i
= wholePath
.indexOf(commonPath
);
240 i
= i
+ commonPath
.length();
244 path
= wholePath
.substring(i
);
246 // remove file separator of head
248 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == 0) {
249 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
251 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == 0) {
252 path
= path
.substring(0 + DataType
.DOS_FILE_SEPARATOR
.length());
255 // remove file separator of rear
257 if (path
.indexOf(DataType
.DOS_FILE_SEPARATOR
) == path
.length() - DataType
.DOS_FILE_SEPARATOR
.length()) {
258 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
260 if (path
.indexOf(DataType
.UNIX_FILE_SEPARATOR
) == path
.length() - DataType
.UNIX_FILE_SEPARATOR
.length()) {
261 path
= path
.substring(0, path
.length() - DataType
.DOS_FILE_SEPARATOR
.length());
264 // convert to UNIX format
266 path
= Tools
.convertPathToUnixType(path
);
271 Convert List ot Vector
277 public static Vector
<String
> convertListToVector(List list
) {
278 Vector
<String
> v
= new Vector
<String
>();
279 if (list
!= null && list
.size() > 0) {
280 for (int index
= 0; index
< list
.size(); index
++) {
281 v
.addElement(list
.get(index
).toString());
288 If the input path missing ext, append the ext to the path
295 public static String
addPathExt(String path
, int type
) {
297 if (type
== DataType
.RETURN_TYPE_MODULE_SURFACE_AREA
) {
298 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.MODULE_SURFACE_AREA_EXT
;
300 if (type
== DataType
.RETURN_TYPE_PACKAGE_SURFACE_AREA
) {
301 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PACKAGE_SURFACE_AREA_EXT
;
303 if (type
== DataType
.RETURN_TYPE_PLATFORM_SURFACE_AREA
) {
304 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.PLATFORM_SURFACE_AREA_EXT
;
306 if (type
== DataType
.RETURN_TYPE_TEXT
) {
307 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.TEXT_FILE_EXT
;
309 if (type
== DataType
.RETURN_TYPE_FAR_SURFACE_AREA
) {
310 match
= DataType
.FILE_EXT_SEPARATOR
+ DataType
.FAR_SURFACE_AREA_EXT
;
312 if (path
.length() <= match
.length()) {
316 if (!(path
.substring(path
.length() - match
.length())).equals(match
)) {
328 public static void showInformationMessage(String arg0
) {
329 JOptionPane
.showConfirmDialog(null, arg0
, "Error", JOptionPane
.DEFAULT_OPTION
, JOptionPane
.INFORMATION_MESSAGE
);
333 if the string doesn't end with a file separator, append it to the string
339 public static String
addFileSeparator(String arg0
) {
340 if (!arg0
.endsWith(DataType
.FILE_SEPARATOR
)) {
341 arg0
= arg0
+ DataType
.FILE_SEPARATOR
;
347 Sort all elements in the vector as the specific sort type
349 @param v The vector need to be sorted
350 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
353 public static void sortVectorString(Vector
<String
> v
, int mode
) {
355 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
356 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
357 if ((v
.get(indexJ
).compareTo(v
.get(indexI
)) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
358 || (v
.get(indexI
).compareTo(v
.get(indexJ
)) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
359 String temp
= v
.get(indexI
);
360 v
.setElementAt(v
.get(indexJ
), indexI
);
361 v
.setElementAt(temp
, indexJ
);
369 Sort all elements of vector and return sorted sequence
371 @param v The vector need to be sorted
372 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
373 @return Vector<Integer> The sorted sequence
376 public static Vector
<Integer
> getVectorSortSequence(Vector
<String
> v
, int mode
) {
377 Vector
<Integer
> vSequence
= new Vector
<Integer
>();
382 for (int index
= 0; index
< v
.size(); index
++) {
383 vSequence
.addElement(index
);
388 // sort and get new sequence
390 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
391 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
392 if ((v
.get(indexJ
).compareTo(v
.get(indexI
)) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
393 || (v
.get(indexI
).compareTo(v
.get(indexJ
)) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
397 String tempStr
= v
.get(indexI
);
398 v
.setElementAt(v
.get(indexJ
), indexI
);
399 v
.setElementAt(tempStr
, indexJ
);
404 int tempInt
= vSequence
.get(indexI
);
405 vSequence
.setElementAt(vSequence
.get(indexJ
), indexI
);
406 vSequence
.setElementAt(tempInt
, indexJ
);
415 Sort all elements of vector as input sequence
417 @param v The vector need to be sorted
418 @param vSequence The sort sequence should be followed
421 public static void sortVectorString(Vector
<String
> v
, Vector
<Integer
> vSequence
) {
422 if (v
!= null && vSequence
!= null && v
.size() == vSequence
.size()) {
423 Vector
<String
> tempV
= new Vector
<String
>();
424 for (int index
= 0; index
< v
.size(); index
++) {
425 tempV
.addElement(v
.get(index
));
427 for (int index
= 0; index
< v
.size(); index
++) {
428 v
.setElementAt(tempV
.get(vSequence
.get(index
)), index
);
440 public static void sortModules(Vector
<ModuleIdentification
> v
, int mode
) {
445 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
446 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
447 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
448 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
449 ModuleIdentification temp
= v
.get(indexI
);
450 v
.setElementAt(v
.get(indexJ
), indexI
);
451 v
.setElementAt(temp
, indexJ
);
465 public static void sortPackages(Vector
<PackageIdentification
> v
, int mode
) {
470 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
471 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
472 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
473 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
474 PackageIdentification temp
= v
.get(indexI
);
475 v
.setElementAt(v
.get(indexJ
), indexI
);
476 v
.setElementAt(temp
, indexJ
);
490 public static void sortPlatforms(Vector
<PlatformIdentification
> v
, int mode
) {
495 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
496 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
497 if ((v
.get(indexJ
).getName().compareTo(v
.get(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
498 || (v
.get(indexI
).getName().compareTo(v
.get(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
499 PlatformIdentification temp
= v
.get(indexI
);
500 v
.setElementAt(v
.get(indexJ
), indexI
);
501 v
.setElementAt(temp
, indexJ
);
515 public static void sortPcds(PcdVector v
, int mode
) {
520 for (int indexI
= 0; indexI
< v
.size(); indexI
++) {
521 for (int indexJ
= indexI
+ 1; indexJ
< v
.size(); indexJ
++) {
522 if ((v
.getPcd(indexJ
).getName().compareTo(v
.getPcd(indexI
).getName()) < 0 && mode
== DataType
.SORT_TYPE_ASCENDING
)
523 || (v
.getPcd(indexI
).getName().compareTo(v
.getPcd(indexJ
).getName()) < 0 && mode
== DataType
.SORT_TYPE_DESCENDING
)) {
524 PcdIdentification temp
= v
.getPcd(indexI
);
525 v
.setPcd(v
.getPcd(indexJ
), indexI
);
526 v
.setPcd(temp
, indexJ
);