]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
Fixed grammar in messages.
[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.awt.Component;
19 import java.io.File;
20 import java.text.SimpleDateFormat;
21 import java.util.Date;
22 import java.util.List;
23 import java.util.UUID;
24 import java.util.Vector;
25
26 import javax.swing.DefaultListModel;
27 import javax.swing.JComboBox;
28 import javax.swing.JList;
29 import javax.swing.JOptionPane;
30
31 import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;
32 import org.tianocore.MsaHeaderDocument.MsaHeader;
33 import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;
34 import org.tianocore.PlatformHeaderDocument.PlatformHeader;
35 import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;
36 import org.tianocore.SpdHeaderDocument.SpdHeader;
37 import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;
38 import org.tianocore.frameworkwizard.packaging.PackageIdentification;
39 import org.tianocore.frameworkwizard.platform.PlatformIdentification;
40
41 /**
42 The class is used to provides some useful interfaces
43
44 **/
45 public class Tools {
46
47 //
48 // The dir user selected to create new package in
49 //
50 public static String dirForNewSpd = null;
51
52 /**
53 Get current date and time and format it as "yyyy-MM-dd HH:mm"
54
55 @return formatted current date and time
56
57 **/
58 public static String getCurrentDateTime() {
59 Date now = new Date(System.currentTimeMillis());
60 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
61 return sdf.format(now);
62 }
63
64 /**
65 Generate a UUID
66
67 @return the created UUID
68
69 **/
70 public static String generateUuidString() {
71 return UUID.randomUUID().toString();
72 }
73
74 /**
75 Use current file separator in the path
76
77 @param strPath
78 @return
79
80 **/
81 public static String convertPathToCurrentOsType(String strPath) {
82 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
83 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);
84 return strPath;
85 }
86
87 /**
88 Use Unix file separator in the path
89
90 @param strPath
91 @return
92
93 **/
94 public static String convertPathToUnixType(String strPath) {
95 strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);
96 return strPath;
97 }
98
99 /**
100 Use Dos file separator in the path
101
102 @param strPath
103 @return
104
105 **/
106 public static String convertPathToDosType(String strPath) {
107 strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);
108 return strPath;
109 }
110
111 /**
112 Get all system properties and output to the console
113
114 **/
115 public static void getSystemProperties() {
116 System.out.println(System.getProperty("java.class.version"));
117 System.out.println(System.getProperty("java.class.path"));
118 System.out.println(System.getProperty("java.ext.dirs"));
119 System.out.println(System.getProperty("os.name"));
120 System.out.println(System.getProperty("os.arch"));
121 System.out.println(System.getProperty("os.version"));
122 System.out.println(System.getProperty("file.separator"));
123 System.out.println(System.getProperty("path.separator"));
124 System.out.println(System.getProperty("line.separator"));
125 System.out.println(System.getProperty("user.name"));
126 System.out.println(System.getProperty("user.home"));
127 System.out.println(System.getProperty("user.dir"));
128 System.out.println(System.getProperty("PATH"));
129
130 System.out.println(System.getenv("PROCESSOR_REVISION"));
131 }
132
133 /**
134 Generate selection items for JComboBox by input vector
135
136 **/
137 public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {
138 if (jcb != null) {
139 jcb.removeAllItems();
140 }
141 if (vector != null) {
142 for (int index = 0; index < vector.size(); index++) {
143 jcb.addItem(vector.elementAt(index));
144 }
145 }
146 }
147
148 /**
149 Generate selection items for JList by input vector
150
151 **/
152 public static void generateListByVector(JList jl, Vector<String> vector) {
153 if (jl != null) {
154 DefaultListModel listModel = (DefaultListModel) jl.getModel();
155 listModel.removeAllElements();
156
157 if (vector != null) {
158 for (int index = 0; index < vector.size(); index++) {
159 listModel.addElement(vector.get(index));
160 }
161 }
162
163 if (listModel.size() > 0) {
164 jl.setSelectedIndex(0);
165 }
166 }
167 }
168
169 /**
170 Get path only from a path
171
172 @param filePath
173 @return
174
175 **/
176 public static String getFilePathOnly(String filePath) {
177 String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());
178 if (path.endsWith(DataType.FILE_SEPARATOR)) {
179 path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());
180 }
181
182 return path;
183 }
184
185 /**
186 Get file name from a path
187
188 @param filePath
189 @return
190
191 **/
192 public static String getFileNameOnly(String filePath) {
193 File f = new File(filePath);
194 return f.getAbsoluteFile().getName();
195 }
196
197 public static String getFileNameWithoutExt(String filePath) {
198 filePath = getFileNameOnly(filePath);
199 filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));
200 return filePath;
201 }
202
203 /**
204 Get relative path
205
206 @param wholePath
207 @param commonPath
208 @return wholePath - commonPath
209
210 **/
211 public static String getRelativePath(String wholePath, String commonPath) {
212 String path = "";
213 int i = 0;
214 i = wholePath.indexOf(commonPath);
215 if (i > -1) {
216 i = i + commonPath.length();
217 } else {
218 return "";
219 }
220 path = wholePath.substring(i);
221 //
222 // remove file separator of head
223 //
224 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {
225 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
226 }
227 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {
228 path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());
229 }
230 //
231 // remove file separator of rear
232 //
233 if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {
234 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
235 }
236 if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {
237 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
238 }
239 //
240 // convert to UNIX format
241 //
242 path = Tools.convertPathToUnixType(path);
243 return path;
244 }
245
246 /**
247 Convert List ot Vector
248
249 @param list
250 @return
251
252 **/
253 public static Vector<String> convertListToVector(List list) {
254 Vector<String> v = new Vector<String>();
255 if (list != null && list.size() > 0) {
256 for (int index = 0; index < list.size(); index++) {
257 v.addElement(list.get(index).toString());
258 }
259 }
260 return v;
261 }
262
263 /**
264 If the input path missing ext, append the ext to the path
265
266 @param path
267 @param type
268 @return
269
270 **/
271 public static String addPathExt(String path, int type) {
272 String match = "";
273 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
274 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
275 }
276 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
277 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
278 }
279 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
280 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
281 }
282 if (type == DataType.RETURN_TYPE_TEXT) {
283 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
284 }
285 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
286 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
287 }
288 if (path.length() <= match.length()) {
289 path = path + match;
290 return path;
291 }
292 if (!(path.substring(path.length() - match.length())).equals(match)) {
293 path = path + match;
294 }
295 return path;
296 }
297
298 /**
299 Show a message box
300
301 @param arg0
302
303 **/
304 public static void showInformationMessage(String arg0) {
305 JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
306 }
307
308 /**
309 if the string doesn't end with a file separator, append it to the string
310
311 @param arg0
312 @return
313
314 **/
315 public static String addFileSeparator(String arg0) {
316 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
317 arg0 = arg0 + DataType.FILE_SEPARATOR;
318 }
319 return arg0;
320 }
321
322 /**
323 Wrap single line long input string to multiple short line string by word
324
325 @param arg0 input string
326 @return wraped string
327
328 **/
329 public static String wrapStringByWord(String arg0) {
330 int intMaxLength = 40;
331 String strReturn = "";
332 String strTemp = "";
333 boolean isCopied = true;
334
335 //
336 // Convert string to array by " "
337 //
338 String s[] = arg0.split(" ");
339 if (arg0.indexOf(" ") == -1) {
340 s[0] = arg0;
341 }
342
343 //
344 // Add each string of array one by one
345 //
346 for (int index = 0; index < s.length; index++) {
347 String ss = s[index];
348 isCopied = false;
349 //
350 // The word length > defined line length
351 //
352 if (ss.length() > intMaxLength) {
353 //
354 // Finish previous line
355 //
356 if (!isCopied) {
357 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
358 strTemp = "";
359 }
360 //
361 // Separater to short lines
362 //
363 while (ss.length() > 0) {
364 if (ss.length() > intMaxLength) {
365 strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;
366 ss = ss.substring(intMaxLength);
367 isCopied = true;
368 } else {
369 strTemp = ss;
370 ss = "";
371 isCopied = false;
372 }
373 }
374 } else {
375 if ((strTemp + " " + ss).length() <= intMaxLength) {
376 strTemp = strTemp + " " + ss;
377 continue;
378 } else {
379 strReturn = strReturn + strTemp + DataType.UNIX_LINE_SEPARATOR;
380 strTemp = ss + " ";
381 isCopied = true;
382 }
383 }
384 }
385
386 if (!isCopied) {
387 strReturn = strReturn + strTemp;
388 }
389
390 return strReturn;
391 }
392
393 public static String convertUnicodeHexStringToString(String str) {
394 //
395 // Handle if str is null or empty
396 //
397 if (str == null) {
398 return "";
399 }
400 if (str.equals("")) {
401 return "";
402 }
403
404 String returnString = "";
405 String[] strArray = str.split(" ");
406 for (int index = 0; index < strArray.length; index++) {
407 String s = strArray[index];
408 if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {
409 s = s.substring(DataType.HEX_STRING_HEADER.length());
410 } else {
411 Log.err("convertUnicodeHexStringToString", "Incorrect input string: " + str);
412 continue;
413 }
414 //
415 // Change hex to dec
416 //
417 int dec = Integer.parseInt(s, 16);
418
419 returnString = returnString + (char)(dec);
420 }
421 return returnString;
422 }
423
424 /**
425 Convert input string to unicode hex string
426
427 @param str input string
428 @return unicode hex string
429
430 **/
431 public static String convertStringToUnicodeHexString(String str) {
432 //
433 // Handle if str is null or empty
434 //
435 if (str == null) {
436 return "";
437 }
438 if (str.equals("")) {
439 return "";
440 }
441
442 //
443 // convert string to hex string
444 //
445 String hexString = "";
446 for (int index = 0; index < str.length(); index++) {
447 int codePoint = str.codePointAt(index);
448 String s = Integer.toHexString(codePoint);
449 //
450 // Make the string to four length
451 //
452 if (s.length() == 3) {
453 s = "0" + s;
454 } else if (s.length() == 2) {
455 s = "00" + s;
456 } else if (s.length() == 1) {
457 s = "000" + s;
458 }
459
460 //
461 // Add the string to return hex string
462 //
463 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";
464 }
465
466 //
467 // return hex string
468 //
469 return hexString.trim();
470 }
471
472 public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {
473 MsaHeader head = msa.getMsaHeader();
474 String name = head.getModuleName();
475 String guid = head.getGuidValue();
476 String version = head.getVersion();
477 ModuleIdentification id = new ModuleIdentification(name, guid, version, path);
478 return id;
479 }
480
481 public static PackageIdentification getId(String path, PackageSurfaceArea spd) {
482 SpdHeader head = spd.getSpdHeader();
483 String name = head.getPackageName();
484 String guid = head.getGuidValue();
485 String version = head.getVersion();
486 PackageIdentification id = new PackageIdentification(name, guid, version, path);
487 return id;
488 }
489
490 public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {
491 PlatformHeader head = fpd.getPlatformHeader();
492 String name = head.getPlatformName();
493 String guid = head.getGuidValue();
494 String version = head.getVersion();
495 PlatformIdentification id = new PlatformIdentification(name, guid, version, path);
496 return id;
497 }
498
499 /**
500 * To reset the width of input component via container width
501 *
502 * @param c
503 * @param containerWidth
504 *
505 */
506 public static void resizeComponentWidth(Component c, int containerWidth, int preferredWidth) {
507 int newWidth = c.getPreferredSize().width + (containerWidth - preferredWidth);
508 if (newWidth < c.getPreferredSize().width) {
509 newWidth = c.getPreferredSize().width;
510 }
511 c.setSize(new java.awt.Dimension(newWidth, c.getHeight()));
512 c.validate();
513 }
514
515 /**
516 * To reset the height of input component via container height
517 *
518 * @param c
519 * @param containerHeight
520 *
521 */
522 public static void resizeComponentHeight(Component c, int containerHeight, int preferredHeight) {
523 int newHeight = c.getPreferredSize().height + (containerHeight - preferredHeight);
524 if (newHeight < c.getPreferredSize().height) {
525 newHeight = c.getPreferredSize().height;
526 }
527 c.setSize(new java.awt.Dimension(c.getWidth(), newHeight));
528 c.validate();
529 }
530
531 /**
532 * To reset the size of input component via container size
533 *
534 * @param c
535 * @param containerWidth
536 * @param containerHeight
537 *
538 */
539 public static void resizeComponent(Component c, int containerWidth, int containerHeight, int preferredWidth,
540 int preferredHeight) {
541 resizeComponentWidth(c, containerWidth, preferredWidth);
542 resizeComponentHeight(c, containerHeight, preferredHeight);
543 }
544
545 /**
546 * To relocate the input component
547 *
548 * @param c
549 * @param containerWidth
550 * @param spaceToRight
551 *
552 */
553 public static void relocateComponentX(Component c, int containerWidth, int preferredWidth, int spaceToRight) {
554 int intGapToRight = spaceToRight + c.getPreferredSize().width;
555 int newLocationX = containerWidth - intGapToRight;
556 if (newLocationX < preferredWidth - intGapToRight) {
557 newLocationX = preferredWidth - intGapToRight;
558 }
559 c.setLocation(newLocationX, c.getLocation().y);
560 c.validate();
561 }
562
563 /**
564 * To relocate the input component
565 *
566 * @param c
567 * @param containerHeight
568 * @param spaceToBottom
569 *
570 */
571 public static void relocateComponentY(Component c, int containerHeight, int preferredHeight, int spaceToBottom) {
572 int intGapToBottom = spaceToBottom + c.getPreferredSize().height;
573 int newLocationY = containerHeight - intGapToBottom;
574 if (newLocationY < preferredHeight - spaceToBottom) {
575 newLocationY = preferredHeight - spaceToBottom;
576 }
577 c.setLocation(c.getLocation().x, newLocationY);
578 c.validate();
579 }
580
581 /**
582 * To relocate the input component
583 *
584 * @param c
585 * @param containerWidth
586 * @param containerHeight
587 * @param spaceToBottom
588 * @param spaceToRight
589 *
590 */
591 public static void relocateComponent(Component c, int containerWidth, int containerHeight, int preferredWidht,
592 int preferredHeight, int spaceToRight, int spaceToBottom) {
593 relocateComponentX(c, containerWidth, preferredWidht, spaceToRight);
594 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);
595 }
596
597 /**
598 Move the component to the center of screen
599
600 @param c
601 @param width
602
603 **/
604 public static void centerComponent(Component c, int width) {
605 c.setLocation(width / 2 - c.getWidth() / 2, c.getLocation().y);
606 c.validate();
607 }
608
609 /**
610 Move the component to the center of screen and adjust the y location
611
612 @param c
613 @param width
614
615 **/
616 public static void centerComponent(Component c, int width, int containerHeight, int preferredHeight, int spaceToBottom) {
617 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);
618 centerComponent(c, width);
619 }
620 }