]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
7cb2e9d526537b3062889d6ccca7911f91db6a98
[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 /**
31 The class is used to provides some useful interfaces
32
33 **/
34 public class Tools {
35
36 //
37 // The dir user selected to create new package in
38 //
39 public static String dirForNewSpd = null;
40
41 /**
42 Used for test
43
44 @param args
45
46 **/
47 public static void main(String[] args) {
48 System.out.println(getCurrentDateTime());
49 // Vector<String> v = new Vector<String>();
50 // Vector<String> v1 = new Vector<String>();
51 //
52 // v.addElement("CAC");
53 // v1.addElement("1111");
54 // v.addElement("1AC");
55 // v1.addElement("2222");
56 // v.addElement("ABC");
57 // v1.addElement("3333");
58 // v.addElement("0C");
59 // v1.addElement("4444");
60 // v.addElement("AAC");
61 // v1.addElement("5555");
62 // Vector<Integer> vs = new Vector<Integer>();
63 // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);
64 // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));
65 //
66 // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);
67 // Tools.sortVectorString(v, DataType.Sort_Type_Descending);
68 }
69
70 /**
71 Get current date and time and format it as "yyyy-MM-dd HH:mm"
72
73 @return formatted current date and time
74
75 **/
76 public static String getCurrentDateTime() {
77 Date now = new Date(System.currentTimeMillis());
78 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
79 return sdf.format(now);
80 }
81
82 /**
83 Generate a UUID
84
85 @return the created UUID
86
87 **/
88 public static String generateUuidString() {
89 return UUID.randomUUID().toString();
90 }
91
92 /**
93 Use current file separator in the path
94
95 @param strPath
96 @return
97
98 **/
99 public static String convertPathToCurrentOsType(String strPath) {
100 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
101 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
102 return strPath;
103 }
104
105 /**
106 Use Unix file separator in the path
107
108 @param strPath
109 @return
110
111 **/
112 public static String convertPathToUnixType(String strPath) {
113 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
114 return strPath;
115 }
116
117 /**
118 Use Dos file separator in the path
119
120 @param strPath
121 @return
122
123 **/
124 public static String convertPathToDosType(String strPath) {
125 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
126 return strPath;
127 }
128
129 /**
130 Get all system properties and output to the console
131
132 **/
133 public static void getSystemProperties() {
134 System.out.println(System.getProperty("java.class.version"));
135 System.out.println(System.getProperty("java.class.path"));
136 System.out.println(System.getProperty("java.ext.dirs"));
137 System.out.println(System.getProperty("os.name"));
138 System.out.println(System.getProperty("os.arch"));
139 System.out.println(System.getProperty("os.version"));
140 System.out.println(System.getProperty("file.separator"));
141 System.out.println(System.getProperty("path.separator"));
142 System.out.println(System.getProperty("line.separator"));
143 System.out.println(System.getProperty("user.name"));
144 System.out.println(System.getProperty("user.home"));
145 System.out.println(System.getProperty("user.dir"));
146 System.out.println(System.getProperty("PATH"));
147
148 System.out.println(System.getenv("PROCESSOR_REVISION"));
149 }
150
151 /**
152 Generate selection items for JComboBox by input vector
153
154 **/
155 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
156 if (jcb != null) {
157 jcb.removeAllItems();
158 }
159 if (vector != null) {
160 for (int index = 0; index < vector.size(); index++) {
161 jcb.addItem(vector.elementAt(index));
162 }
163 }
164 }
165
166 /**
167 Generate selection items for JList by input vector
168
169 **/
170 public static void generateListByVector(JList jl, Vector<String> vector) {
171 if (jl != null) {
172 DefaultListModel listModel = (DefaultListModel) jl.getModel();
173 listModel.removeAllElements();
174
175 if (vector != null) {
176 for (int index = 0; index < vector.size(); index++) {
177 listModel.addElement(vector.get(index));
178 }
179 }
180
181 if (listModel.size() > 0) {
182 jl.setSelectedIndex(0);
183 }
184 }
185 }
186
187 /**
188 Get path only from a path
189
190 @param filePath
191 @return
192
193 **/
194 public static String getFilePathOnly(String filePath) {
195 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());
196 if (path.endsWith(DataType.FILE_SEPARATOR)) {
197 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());
198 }
199
200 return path;
201 }
202
203 /**
204 Get file name from a path
205
206 @param filePath
207 @return
208
209 **/
210 public static String getFileNameOnly(String filePath) {
211 File f = new File(filePath);
212 return f.getAbsoluteFile().getName();
213 }
214
215 public static String getFileNameWithoutExt(String filePath) {
216 filePath = getFileNameOnly(filePath);
217 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
218 return filePath;
219 }
220
221 /**
222 Get relative path
223
224 @param wholePath
225 @param commonPath
226 @return wholePath - commonPath
227
228 **/
229 public static String getRelativePath(String wholePath, String commonPath) {
230 String path = "";
231 int i = 0;
232 i = wholePath.indexOf(commonPath);
233 if (i > -1) {
234 i = i + commonPath.length();
235 } else {
236 return "";
237 }
238 path = wholePath.substring(i);
239 //
240 // remove file separator of head
241 //
242 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
243 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
244 }
245 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
246 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
247 }
248 //
249 // remove file separator of rear
250 //
251 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {
252 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
253 }
254 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {
255 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
256 }
257 //
258 // convert to UNIX format
259 //
260 path = Tools.convertPathToUnixType(path);
261 return path;
262 }
263
264 /**
265 Convert List ot Vector
266
267 @param list
268 @return
269
270 **/
271 public static Vector<String> convertListToVector(List list) {
272 Vector<String> v = new Vector<String>();
273 if (list != null && list.size() > 0) {
274 for (int index = 0; index < list.size(); index++) {
275 v.addElement(list.get(index).toString());
276 }
277 }
278 return v;
279 }
280
281 /**
282 If the input path missing ext, append the ext to the path
283
284 @param path
285 @param type
286 @return
287
288 **/
289 public static String addPathExt(String path, int type) {
290 String match = "";
291 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
292 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
293 }
294 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
295 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
296 }
297 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
298 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
299 }
300 if (type == DataType.RETURN_TYPE_TEXT) {
301 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
302 }
303 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
304 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
305 }
306 if (path.length() <= match.length()) {
307 path = path + match;
308 return path;
309 }
310 if (!(path.substring(path.length() - match.length())).equals(match)) {
311 path = path + match;
312 }
313 return path;
314 }
315
316 /**
317 Show a message box
318
319 @param arg0
320
321 **/
322 public static void showInformationMessage(String arg0) {
323 JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
324 }
325
326 /**
327 if the string doesn't end with a file separator, append it to the string
328
329 @param arg0
330 @return
331
332 **/
333 public static String addFileSeparator(String arg0) {
334 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
335 arg0 = arg0 + DataType.FILE_SEPARATOR;
336 }
337 return arg0;
338 }
339
340 /**
341 Wrap single line long input string to multiple short line string by word
342
343 @param arg0 input string
344 @return wraped string
345
346 **/
347 public static String wrapStringByWord(String arg0) {
348 int intMaxLength = 40;
349 String strReturn = "";
350 String strTemp = "";
351 boolean isCopied = true;
352
353 //
354 // Convert string to array by " "
355 //
356 String s[] = arg0.split(" ");
357 if (arg0.indexOf(" ") == -1) {
358 s[0] = arg0;
359 }
360
361 //
362 // Add each string of array one by one
363 //
364 for (int index = 0; index < s.length; index++) {
365 String ss = s[index];
366 isCopied = false;
367 //
368 // The word length > defined line length
369 //
370 if (ss.length() > intMaxLength) {
371 //
372 // Finish previous line
373 //
374 if (!isCopied) {
375 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
376 strTemp = "";
377 }
378 //
379 // Separater to short lines
380 //
381 while (ss.length() > 0) {
382 if (ss.length() > intMaxLength) {
383 strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;
384 ss = ss.substring(intMaxLength);
385 isCopied = true;
386 } else {
387 strTemp = ss;
388 ss = "";
389 isCopied = false;
390 }
391 }
392 } else {
393 if ((strTemp + " " + ss).length() <= intMaxLength) {
394 strTemp = strTemp + " " + ss;
395 continue;
396 } else {
397 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
398 strTemp = ss + " ";
399 isCopied = true;
400 }
401 }
402 }
403
404 if (!isCopied) {
405 strReturn = strReturn + strTemp;
406 }
407
408 return strReturn;
409 }
410 }