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