]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
021c2373a04cd8c1d52a91da232238331d239839
[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.packaging.PackageIdentification;
32 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
33
34 /**
35 The class is used to provides some useful interfaces
36
37 **/
38 public class Tools {
39
40 //
41 // The dir user selected to create new package in
42 //
43 public static String dirForNewSpd = null;
44
45 /**
46 Used for test
47
48 @param args
49
50 **/
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>();
55 //
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));
69 //
70 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
71 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
72 }
73
74 /**
75 Get current date and time and format it as "yyyy-MM-dd HH:mm"
76
77 @return formatted current date and time
78
79 **/
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);
84 }
85
86 /**
87 Generate a UUID
88
89 @return the created UUID
90
91 **/
92 public static String generateUuidString() {
93 return UUID.randomUUID().toString();
94 }
95
96 /**
97 Use current file separator in the path
98
99 @param strPath
100 @return
101
102 **/
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);
106 return strPath;
107 }
108
109 /**
110 Use Unix file separator in the path
111
112 @param strPath
113 @return
114
115 **/
116 public static String convertPathToUnixType(String strPath) {
117 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
118 return strPath;
119 }
120
121 /**
122 Use Dos file separator in the path
123
124 @param strPath
125 @return
126
127 **/
128 public static String convertPathToDosType(String strPath) {
129 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
130 return strPath;
131 }
132
133 /**
134 Get all system properties and output to the console
135
136 **/
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"));
151
152 System.out.println(System.getenv("PROCESSOR_REVISION"));
153 }
154
155 /**
156 Generate selection items for JComboBox by input vector
157
158 **/
159 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
160 if (jcb != null) {
161 jcb.removeAllItems();
162 }
163 if (vector != null) {
164 for (int index = 0; index < vector.size(); index++) {
165 jcb.addItem(vector.elementAt(index));
166 }
167 }
168 }
169
170 /**
171 Generate selection items for JList by input vector
172
173 **/
174 public static void generateListByVector(JList jl, Vector<String> vector) {
175 if (jl != null) {
176 DefaultListModel listModel = (DefaultListModel) jl.getModel();
177 listModel.removeAllElements();
178
179 if (vector != null) {
180 for (int index = 0; index < vector.size(); index++) {
181 listModel.addElement(vector.get(index));
182 }
183 }
184
185 if (listModel.size() > 0) {
186 jl.setSelectedIndex(0);
187 }
188 }
189 }
190
191 /**
192 Get path only from a path
193
194 @param filePath
195 @return
196
197 **/
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());
202 }
203
204 return path;
205 }
206
207 /**
208 Get file name from a path
209
210 @param filePath
211 @return
212
213 **/
214 public static String getFileNameOnly(String filePath) {
215 File f = new File(filePath);
216 return f.getAbsoluteFile().getName();
217 }
218
219 public static String getFileNameWithoutExt(String filePath) {
220 filePath = getFileNameOnly(filePath);
221 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
222 return filePath;
223 }
224
225 /**
226 Get relative path
227
228 @param wholePath
229 @param commonPath
230 @return wholePath - commonPath
231
232 **/
233 public static String getRelativePath(String wholePath, String commonPath) {
234 String path = "";
235 int i = 0;
236 i = wholePath.indexOf(commonPath);
237 if (i > -1) {
238 i = i + commonPath.length();
239 } else {
240 return "";
241 }
242 path = wholePath.substring(i);
243 //
244 // remove file separator of head
245 //
246 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
247 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
248 }
249 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
250 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
251 }
252 //
253 // remove file separator of rear
254 //
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());
257 }
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());
260 }
261 //
262 // convert to UNIX format
263 //
264 path = Tools.convertPathToUnixType(path);
265 return path;
266 }
267
268 /**
269 Convert List ot Vector
270
271 @param list
272 @return
273
274 **/
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());
280 }
281 }
282 return v;
283 }
284
285 /**
286 If the input path missing ext, append the ext to the path
287
288 @param path
289 @param type
290 @return
291
292 **/
293 public static String addPathExt(String path, int type) {
294 String match = "";
295 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
296 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
297 }
298 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
299 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
300 }
301 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
302 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
303 }
304 if (type == DataType.RETURN_TYPE_TEXT) {
305 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
306 }
307 if (path.length() <= match.length()) {
308 path = path + match;
309 return path;
310 }
311 if (!(path.substring(path.length() - match.length())).equals(match)) {
312 path = path + match;
313 }
314 return path;
315 }
316
317 /**
318 Show a message box
319
320 @param arg0
321
322 **/
323 public static void showInformationMessage(String arg0) {
324 JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
325 }
326
327 /**
328 if the string doesn't end with a file separator, append it to the string
329
330 @param arg0
331 @return
332
333 **/
334 public static String addFileSeparator(String arg0) {
335 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
336 arg0 = arg0 + DataType.FILE_SEPARATOR;
337 }
338 return arg0;
339 }
340
341 /**
342 Sort all elements in the vector as the specific sort type
343
344 @param v The vector need to be sorted
345 @param mode Sort type DataType.Sort_Type_Ascendin and DataType.Sort_Type_Descending
346
347 **/
348 public static void sortVectorString(Vector<String> v, int mode) {
349 if (v != null) {
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);
357 }
358 }
359 }
360 }
361 }
362
363 /**
364 Sort all elements of vector and return sorted sequence
365
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
369
370 **/
371 public static Vector<Integer> getVectorSortSequence(Vector<String> v, int mode) {
372 Vector<Integer> vSequence = new Vector<Integer>();
373 //
374 // Init sequence
375 //
376 if (v != null) {
377 for (int index = 0; index < v.size(); index++) {
378 vSequence.addElement(index);
379 }
380 }
381
382 //
383 // sort and get new sequence
384 //
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)) {
389 //
390 // Swap strings
391 //
392 String tempStr = v.get(indexI);
393 v.setElementAt(v.get(indexJ), indexI);
394 v.setElementAt(tempStr, indexJ);
395
396 //
397 // Swap sequences
398 //
399 int tempInt = vSequence.get(indexI);
400 vSequence.setElementAt(vSequence.get(indexJ), indexI);
401 vSequence.setElementAt(tempInt, indexJ);
402 }
403 }
404 }
405
406 return vSequence;
407 }
408
409 /**
410 Sort all elements of vector as input sequence
411
412 @param v The vector need to be sorted
413 @param vSequence The sort sequence should be followed
414
415 **/
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));
421 }
422 for (int index = 0; index < v.size(); index++) {
423 v.setElementAt(tempV.get(vSequence.get(index)), index);
424 }
425 }
426 }
427
428 /**
429 Sort all modules
430
431 @param v
432 @param mode
433
434 **/
435 public static void sortModules(Vector<ModuleIdentification> v, int mode) {
436 if (v != null) {
437 //
438 // sort by name
439 //
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);
447 }
448 }
449 }
450 }
451 }
452
453 /**
454 Sort all packages
455
456 @param v
457 @param mode
458
459 **/
460 public static void sortPackages(Vector<PackageIdentification> v, int mode) {
461 if (v != null) {
462 //
463 // sort by name
464 //
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);
472 }
473 }
474 }
475 }
476 }
477
478 /**
479 Sort all platforms
480
481 @param v
482 @param mode
483
484 **/
485 public static void sortPlatforms(Vector<PlatformIdentification> v, int mode) {
486 if (v != null) {
487 //
488 // sort by name
489 //
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);
497 }
498 }
499 }
500 }
501 }
502 }