]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
0bfca453cdf9eca2f76e17b9b71059b264c3f248
[mirror_edk2.git] / Tools / Source / FrameworkWizard / src / org / tianocore / frameworkwizard / common / Tools.java
1 /** @file
2
3 The file is used to provides some useful interfaces
4
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
10
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.
13
14 **/
15
16 package org.tianocore.frameworkwizard.common;
17
18 import java.io.File;
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;
24
25 import javax.swing.DefaultListModel;
26 import javax.swing.JComboBox;
27 import javax.swing.JList;
28 import javax.swing.JOptionPane;
29
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;
35
36 /**
37 The class is used to provides some useful interfaces
38
39 **/
40 public class Tools {
41
42 //
43 // The dir user selected to create new package in
44 //
45 public static String dirForNewSpd = null;
46
47 /**
48 Used for test
49
50 @param args
51
52 **/
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>();
57 //
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));
71 //
72 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
73 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
74 }
75
76 /**
77 Get current date and time and format it as "yyyy-MM-dd HH:mm"
78
79 @return formatted current date and time
80
81 **/
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);
86 }
87
88 /**
89 Generate a UUID
90
91 @return the created UUID
92
93 **/
94 public static String generateUuidString() {
95 return UUID.randomUUID().toString();
96 }
97
98 /**
99 Use current file separator in the path
100
101 @param strPath
102 @return
103
104 **/
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);
108 return strPath;
109 }
110
111 /**
112 Use Unix file separator in the path
113
114 @param strPath
115 @return
116
117 **/
118 public static String convertPathToUnixType(String strPath) {
119 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
120 return strPath;
121 }
122
123 /**
124 Use Dos file separator in the path
125
126 @param strPath
127 @return
128
129 **/
130 public static String convertPathToDosType(String strPath) {
131 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
132 return strPath;
133 }
134
135 /**
136 Get all system properties and output to the console
137
138 **/
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"));
153
154 System.out.println(System.getenv("PROCESSOR_REVISION"));
155 }
156
157 /**
158 Generate selection items for JComboBox by input vector
159
160 **/
161 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
162 if (jcb != null) {
163 jcb.removeAllItems();
164 }
165 if (vector != null) {
166 for (int index = 0; index < vector.size(); index++) {
167 jcb.addItem(vector.elementAt(index));
168 }
169 }
170 }
171
172 /**
173 Generate selection items for JList by input vector
174
175 **/
176 public static void generateListByVector(JList jl, Vector<String> vector) {
177 if (jl != null) {
178 DefaultListModel listModel = (DefaultListModel) jl.getModel();
179 listModel.removeAllElements();
180
181 if (vector != null) {
182 for (int index = 0; index < vector.size(); index++) {
183 listModel.addElement(vector.get(index));
184 }
185 }
186
187 if (listModel.size() > 0) {
188 jl.setSelectedIndex(0);
189 }
190 }
191 }
192
193 /**
194 Get path only from a path
195
196 @param filePath
197 @return
198
199 **/
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());
204 }
205
206 return path;
207 }
208
209 /**
210 Get file name from a path
211
212 @param filePath
213 @return
214
215 **/
216 public static String getFileNameOnly(String filePath) {
217 File f = new File(filePath);
218 return f.getAbsoluteFile().getName();
219 }
220
221 public static String getFileNameWithoutExt(String filePath) {
222 filePath = getFileNameOnly(filePath);
223 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
224 return filePath;
225 }
226
227 /**
228 Get relative path
229
230 @param wholePath
231 @param commonPath
232 @return wholePath - commonPath
233
234 **/
235 public static String getRelativePath(String wholePath, String commonPath) {
236 String path = "";
237 int i = 0;
238 i = wholePath.indexOf(commonPath);
239 if (i > -1) {
240 i = i + commonPath.length();
241 } else {
242 return "";
243 }
244 path = wholePath.substring(i);
245 //
246 // remove file separator of head
247 //
248 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
249 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
250 }
251 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
252 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
253 }
254 //
255 // remove file separator of rear
256 //
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());
259 }
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());
262 }
263 //
264 // convert to UNIX format
265 //
266 path = Tools.convertPathToUnixType(path);
267 return path;
268 }
269
270 /**
271 Convert List ot Vector
272
273 @param list
274 @return
275
276 **/
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());
282 }
283 }
284 return v;
285 }
286
287 /**
288 If the input path missing ext, append the ext to the path
289
290 @param path
291 @param type
292 @return
293
294 **/
295 public static String addPathExt(String path, int type) {
296 String match = "";
297 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
298 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
299 }
300 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
301 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
302 }
303 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
304 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
305 }
306 if (type == DataType.RETURN_TYPE_TEXT) {
307 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
308 }
309 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
310 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
311 }
312 if (path.length() <= match.length()) {
313 path = path + match;
314 return path;
315 }
316 if (!(path.substring(path.length() - match.length())).equals(match)) {
317 path = path + match;
318 }
319 return path;
320 }
321
322 /**
323 Show a message box
324
325 @param arg0
326
327 **/
328 public static void showInformationMessage(String arg0) {
329 JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
330 }
331
332 /**
333 if the string doesn't end with a file separator, append it to the string
334
335 @param arg0
336 @return
337
338 **/
339 public static String addFileSeparator(String arg0) {
340 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
341 arg0 = arg0 + DataType.FILE_SEPARATOR;
342 }
343 return arg0;
344 }
345
346 /**
347 Sort all elements in the vector as the specific sort type
348
349 @param v The vector need to be sorted
350 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
351
352 **/
353 public static void sortVectorString(Vector<String> v, int mode) {
354 if (v != null) {
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);
362 }
363 }
364 }
365 }
366 }
367
368 /**
369 Sort all elements of vector and return sorted sequence
370
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
374
375 **/
376 public static Vector<Integer> getVectorSortSequence(Vector<String> v, int mode) {
377 Vector<Integer> vSequence = new Vector<Integer>();
378 //
379 // Init sequence
380 //
381 if (v != null) {
382 for (int index = 0; index < v.size(); index++) {
383 vSequence.addElement(index);
384 }
385 }
386
387 //
388 // sort and get new sequence
389 //
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)) {
394 //
395 // Swap strings
396 //
397 String tempStr = v.get(indexI);
398 v.setElementAt(v.get(indexJ), indexI);
399 v.setElementAt(tempStr, indexJ);
400
401 //
402 // Swap sequences
403 //
404 int tempInt = vSequence.get(indexI);
405 vSequence.setElementAt(vSequence.get(indexJ), indexI);
406 vSequence.setElementAt(tempInt, indexJ);
407 }
408 }
409 }
410
411 return vSequence;
412 }
413
414 /**
415 Sort all elements of vector as input sequence
416
417 @param v The vector need to be sorted
418 @param vSequence The sort sequence should be followed
419
420 **/
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));
426 }
427 for (int index = 0; index < v.size(); index++) {
428 v.setElementAt(tempV.get(vSequence.get(index)), index);
429 }
430 }
431 }
432
433 /**
434 Sort all modules
435
436 @param v
437 @param mode
438
439 **/
440 public static void sortModules(Vector<ModuleIdentification> v, int mode) {
441 if (v != null) {
442 //
443 // sort by name
444 //
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);
452 }
453 }
454 }
455 }
456 }
457
458 /**
459 Sort all packages
460
461 @param v
462 @param mode
463
464 **/
465 public static void sortPackages(Vector<PackageIdentification> v, int mode) {
466 if (v != null) {
467 //
468 // sort by name
469 //
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);
477 }
478 }
479 }
480 }
481 }
482
483 /**
484 Sort all platforms
485
486 @param v
487 @param mode
488
489 **/
490 public static void sortPlatforms(Vector<PlatformIdentification> v, int mode) {
491 if (v != null) {
492 //
493 // sort by name
494 //
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);
502 }
503 }
504 }
505 }
506 }
507
508 /**
509 Sort all pcd entries
510
511 @param v
512 @param mode
513
514 **/
515 public static void sortPcds(PcdVector v, int mode) {
516 if (v != null) {
517 //
518 // sort by name
519 //
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);
527 }
528 }
529 }
530 }
531 }
532 }