]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
e5cce71045b1e08c7eedd8d1b47edde075c5a25b
[mirror_edk2.git] / Tools / Java / 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.length() > 0 && 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.length() > 0 && 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 if (arg0 == null) {
336 return "";
337 }
338 if (arg0.length() <= 0) {
339 return "";
340 }
341
342 //
343 // Convert string to array by " "
344 //
345 String s[] = arg0.split(" ");
346 if (arg0.indexOf(" ") == -1) {
347 s[0] = arg0;
348 }
349
350 //
351 // Add each string of array one by one
352 //
353 for (int index = 0; index < s.length; index++) {
354 String ss = s[index];
355 isCopied = false;
356 //
357 // The word length > defined line length
358 //
359 if (ss.length() > intMaxLength) {
360 //
361 // Finish previous line
362 //
363 if (!isCopied) {
364 strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;
365 strTemp = "";
366 }
367 //
368 // Separater to short lines
369 //
370 while (ss.length() > 0) {
371 if (ss.length() > intMaxLength) {
372 strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;
373 ss = ss.substring(intMaxLength);
374 isCopied = true;
375 } else {
376 strTemp = ss;
377 ss = "";
378 isCopied = false;
379 }
380 }
381 } else {
382 if ((strTemp + " " + ss).length() <= intMaxLength) {
383 strTemp = strTemp + " " + ss;
384 continue;
385 } else {
386 strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;
387 if ((index == s.length - 1) && (!ss.equals(""))) {
388 strReturn = strReturn + ss;
389 } else {
390 strTemp = ss + " ";
391 }
392 isCopied = true;
393 }
394 }
395 }
396
397 if (!isCopied) {
398 strReturn = strReturn + strTemp;
399 }
400
401 return strReturn;
402 }
403
404 public static String convertUnicodeHexStringToString(String str) {
405 //
406 // Handle if str is null or empty
407 //
408 if (str == null) {
409 return "";
410 }
411 if (str.equals("")) {
412 return "";
413 }
414
415 String returnString = "";
416 String[] strArray = str.split(" ");
417 for (int index = 0; index < strArray.length; index++) {
418 String s = strArray[index];
419 if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {
420 s = s.substring(DataType.HEX_STRING_HEADER.length());
421 } else {
422 Log.err("convertUnicodeHexStringToString", "Incorrect input string: " + str);
423 continue;
424 }
425 //
426 // Change hex to dec
427 //
428 int dec = Integer.parseInt(s, 16);
429
430 returnString = returnString + (char) (dec);
431 }
432 return returnString;
433 }
434
435 /**
436 Convert input string to unicode hex string
437
438 @param str input string
439 @return unicode hex string
440
441 **/
442 public static String convertStringToUnicodeHexString(String str) {
443 //
444 // Handle if str is null or empty
445 //
446 if (str == null) {
447 return "";
448 }
449 if (str.equals("")) {
450 return "";
451 }
452
453 //
454 // convert string to hex string
455 //
456 String hexString = "";
457 for (int index = 0; index < str.length(); index++) {
458 int codePoint = str.codePointAt(index);
459 String s = Integer.toHexString(codePoint);
460 //
461 // Make the string to four length
462 //
463 if (s.length() == 3) {
464 s = "0" + s;
465 } else if (s.length() == 2) {
466 s = "00" + s;
467 } else if (s.length() == 1) {
468 s = "000" + s;
469 }
470
471 //
472 // Add the string to return hex string
473 //
474 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";
475 }
476
477 //
478 // return hex string
479 //
480 return hexString.trim();
481 }
482
483 public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {
484 MsaHeader head = msa.getMsaHeader();
485 String name = head.getModuleName();
486 String guid = head.getGuidValue();
487 String version = head.getVersion();
488 ModuleIdentification id = new ModuleIdentification(name, guid, version, path);
489 return id;
490 }
491
492 public static PackageIdentification getId(String path, PackageSurfaceArea spd) {
493 SpdHeader head = spd.getSpdHeader();
494 String name = head.getPackageName();
495 String guid = head.getGuidValue();
496 String version = head.getVersion();
497 PackageIdentification id = new PackageIdentification(name, guid, version, path);
498 return id;
499 }
500
501 public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {
502 PlatformHeader head = fpd.getPlatformHeader();
503 String name = head.getPlatformName();
504 String guid = head.getGuidValue();
505 String version = head.getVersion();
506 PlatformIdentification id = new PlatformIdentification(name, guid, version, path);
507 return id;
508 }
509
510 /**
511 * To reset the width of input component via container width
512 *
513 * @param c
514 * @param containerWidth
515 *
516 */
517 public static void resizeComponentWidth(Component c, int containerWidth, int preferredWidth) {
518 int newWidth = c.getPreferredSize().width + (containerWidth - preferredWidth);
519 if (newWidth < c.getPreferredSize().width) {
520 newWidth = c.getPreferredSize().width;
521 }
522 c.setSize(new java.awt.Dimension(newWidth, c.getHeight()));
523 c.validate();
524 }
525
526 /**
527 * To reset the height of input component via container height
528 *
529 * @param c
530 * @param containerHeight
531 *
532 */
533 public static void resizeComponentHeight(Component c, int containerHeight, int preferredHeight) {
534 int newHeight = c.getPreferredSize().height + (containerHeight - preferredHeight);
535 if (newHeight < c.getPreferredSize().height) {
536 newHeight = c.getPreferredSize().height;
537 }
538 c.setSize(new java.awt.Dimension(c.getWidth(), newHeight));
539 c.validate();
540 }
541
542 /**
543 * To reset the size of input component via container size
544 *
545 * @param c
546 * @param containerWidth
547 * @param containerHeight
548 *
549 */
550 public static void resizeComponent(Component c, int containerWidth, int containerHeight, int preferredWidth,
551 int preferredHeight) {
552 resizeComponentWidth(c, containerWidth, preferredWidth);
553 resizeComponentHeight(c, containerHeight, preferredHeight);
554 }
555
556 /**
557 * To relocate the input component
558 *
559 * @param c
560 * @param containerWidth
561 * @param spaceToRight
562 *
563 */
564 public static void relocateComponentX(Component c, int containerWidth, int preferredWidth, int spaceToRight) {
565 int intGapToRight = spaceToRight + c.getPreferredSize().width;
566 int newLocationX = containerWidth - intGapToRight;
567 if (newLocationX < preferredWidth - intGapToRight) {
568 newLocationX = preferredWidth - intGapToRight;
569 }
570 c.setLocation(newLocationX, c.getLocation().y);
571 c.validate();
572 }
573
574 /**
575 * To relocate the input component
576 *
577 * @param c
578 * @param containerHeight
579 * @param spaceToBottom
580 *
581 */
582 public static void relocateComponentY(Component c, int containerHeight, int preferredHeight, int spaceToBottom) {
583 int intGapToBottom = spaceToBottom + c.getPreferredSize().height;
584 int newLocationY = containerHeight - intGapToBottom;
585 if (newLocationY < preferredHeight - spaceToBottom) {
586 newLocationY = preferredHeight - spaceToBottom;
587 }
588 c.setLocation(c.getLocation().x, newLocationY);
589 c.validate();
590 }
591
592 /**
593 * To relocate the input component
594 *
595 * @param c
596 * @param containerWidth
597 * @param containerHeight
598 * @param spaceToBottom
599 * @param spaceToRight
600 *
601 */
602 public static void relocateComponent(Component c, int containerWidth, int containerHeight, int preferredWidht,
603 int preferredHeight, int spaceToRight, int spaceToBottom) {
604 relocateComponentX(c, containerWidth, preferredWidht, spaceToRight);
605 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);
606 }
607
608 /**
609 Move the component to the center of screen
610
611 @param c
612 @param width
613
614 **/
615 public static void centerComponent(Component c, int width) {
616 c.setLocation(width / 2 - c.getWidth() / 2, c.getLocation().y);
617 c.validate();
618 }
619
620 /**
621 Move the component to the center of screen and adjust the y location
622
623 @param c
624 @param width
625
626 **/
627 public static void centerComponent(Component c, int width, int containerHeight, int preferredHeight,
628 int spaceToBottom) {
629 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);
630 centerComponent(c, width);
631 }
632
633 /**
634 Find the count of searchString in wholeString
635
636 @param wholeString
637 @param searchString
638 @return
639
640 **/
641 public static int getSpecificStringCount(String wholeString, String searchString) {
642 int count = 0;
643 count = wholeString.split(searchString).length;
644 return count;
645 }
646
647 /**
648 * Check the input data is empty or not
649 *
650 * @param strValue
651 * The input data which need be checked
652 *
653 * @retval true - The input data is empty
654 * @retval fals - The input data is not empty
655 *
656 */
657 public static boolean isEmpty(String strValue) {
658 if (strValue.length() > 0) {
659 return false;
660 }
661 return true;
662 }
663 }