]> git.proxmox.com Git - mirror_edk2.git/blob - Tools/Java/Source/FrameworkWizard/src/org/tianocore/frameworkwizard/common/Tools.java
1. Fix EDKT411: Need right and left scroll bar to display find results
[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 && path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {
235 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
236 }
237 if (path.length() > 0 && path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {
238 path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());
239 }
240 //
241 // convert to UNIX format
242 //
243 path = Tools.convertPathToUnixType(path);
244 return path;
245 }
246
247 /**
248 Convert List ot Vector
249
250 @param list
251 @return
252
253 **/
254 public static Vector<String> convertListToVector(List list) {
255 Vector<String> v = new Vector<String>();
256 if (list != null && list.size() > 0) {
257 for (int index = 0; index < list.size(); index++) {
258 v.addElement(list.get(index).toString());
259 }
260 }
261 return v;
262 }
263
264 /**
265 If the input path missing ext, append the ext to the path
266
267 @param path
268 @param type
269 @return
270
271 **/
272 public static String addPathExt(String path, int type) {
273 String match = "";
274 if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {
275 match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;
276 }
277 if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {
278 match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;
279 }
280 if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {
281 match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;
282 }
283 if (type == DataType.RETURN_TYPE_TEXT) {
284 match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;
285 }
286 if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {
287 match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;
288 }
289 if (path.length() <= match.length()) {
290 path = path + match;
291 return path;
292 }
293 if (!(path.substring(path.length() - match.length())).equals(match)) {
294 path = path + match;
295 }
296 return path;
297 }
298
299 /**
300 Show a message box
301
302 @param arg0
303
304 **/
305 public static void showInformationMessage(String arg0) {
306 JOptionPane.showConfirmDialog(null, arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);
307 }
308
309 /**
310 if the string doesn't end with a file separator, append it to the string
311
312 @param arg0
313 @return
314
315 **/
316 public static String addFileSeparator(String arg0) {
317 if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {
318 arg0 = arg0 + DataType.FILE_SEPARATOR;
319 }
320 return arg0;
321 }
322
323 /**
324 Wrap single line long input string to multiple short line string by word
325
326 @param arg0 input string
327 @return wraped string
328
329 **/
330 public static String wrapStringByWord(String arg0) {
331 int intMaxLength = 40;
332 String strReturn = "";
333 String strTemp = "";
334 boolean isCopied = true;
335
336 if (arg0 == null) {
337 return "";
338 }
339 if (arg0.length() <= 0) {
340 return "";
341 }
342
343 //
344 // Convert string to array by " "
345 //
346 String s[] = arg0.split(" ");
347 if (arg0.indexOf(" ") == -1) {
348 s[0] = arg0;
349 }
350
351 //
352 // Add each string of array one by one
353 //
354 for (int index = 0; index < s.length; index++) {
355 String ss = s[index];
356 isCopied = false;
357 //
358 // The word length > defined line length
359 //
360 if (ss.length() > intMaxLength) {
361 //
362 // Finish previous line
363 //
364 if (!isCopied) {
365 strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;
366 strTemp = "";
367 }
368 //
369 // Separater to short lines
370 //
371 while (ss.length() > 0) {
372 if (ss.length() > intMaxLength) {
373 strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;
374 ss = ss.substring(intMaxLength);
375 isCopied = true;
376 } else {
377 strTemp = ss;
378 ss = "";
379 isCopied = false;
380 }
381 }
382 } else {
383 if ((strTemp + " " + ss).length() <= intMaxLength) {
384 strTemp = strTemp + " " + ss;
385 continue;
386 } else {
387 strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;
388 if ((index == s.length - 1) && (!ss.equals(""))) {
389 strReturn = strReturn + ss;
390 } else {
391 strTemp = ss + " ";
392 }
393 isCopied = true;
394 }
395 }
396 }
397
398 if (!isCopied) {
399 strReturn = strReturn + strTemp;
400 }
401
402 return strReturn;
403 }
404
405 public static String convertUnicodeHexStringToString(String str) {
406 //
407 // Handle if str is null or empty
408 //
409 if (str == null) {
410 return "";
411 }
412 if (str.equals("")) {
413 return "";
414 }
415
416 String returnString = "";
417 String[] strArray = str.split(" ");
418 for (int index = 0; index < strArray.length; index++) {
419 String s = strArray[index];
420 if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {
421 s = s.substring(DataType.HEX_STRING_HEADER.length());
422 } else {
423 Log.err("convertUnicodeHexStringToString", "Incorrect input string: " + str);
424 continue;
425 }
426 //
427 // Change hex to dec
428 //
429 int dec = Integer.parseInt(s, 16);
430
431 returnString = returnString + (char) (dec);
432 }
433 return returnString;
434 }
435
436 /**
437 Convert input string to unicode hex string
438
439 @param str input string
440 @return unicode hex string
441
442 **/
443 public static String convertStringToUnicodeHexString(String str) {
444 //
445 // Handle if str is null or empty
446 //
447 if (str == null) {
448 return "";
449 }
450 if (str.equals("")) {
451 return "";
452 }
453
454 //
455 // convert string to hex string
456 //
457 String hexString = "";
458 for (int index = 0; index < str.length(); index++) {
459 int codePoint = str.codePointAt(index);
460 String s = Integer.toHexString(codePoint);
461 //
462 // Make the string to four length
463 //
464 if (s.length() == 3) {
465 s = "0" + s;
466 } else if (s.length() == 2) {
467 s = "00" + s;
468 } else if (s.length() == 1) {
469 s = "000" + s;
470 }
471
472 //
473 // Add the string to return hex string
474 //
475 hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";
476 }
477
478 //
479 // return hex string
480 //
481 return hexString.trim();
482 }
483
484 public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {
485 MsaHeader head = msa.getMsaHeader();
486 String name = head.getModuleName();
487 String guid = head.getGuidValue();
488 String version = head.getVersion();
489 ModuleIdentification id = new ModuleIdentification(name, guid, version, path);
490 return id;
491 }
492
493 public static PackageIdentification getId(String path, PackageSurfaceArea spd) {
494 SpdHeader head = spd.getSpdHeader();
495 String name = head.getPackageName();
496 String guid = head.getGuidValue();
497 String version = head.getVersion();
498 PackageIdentification id = new PackageIdentification(name, guid, version, path);
499 return id;
500 }
501
502 public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {
503 PlatformHeader head = fpd.getPlatformHeader();
504 String name = head.getPlatformName();
505 String guid = head.getGuidValue();
506 String version = head.getVersion();
507 PlatformIdentification id = new PlatformIdentification(name, guid, version, path);
508 return id;
509 }
510
511 /**
512 * To reset the width of input component via container width
513 *
514 * @param c
515 * @param containerWidth
516 *
517 */
518 public static void resizeComponentWidth(Component c, int containerWidth, int preferredWidth) {
519 int newWidth = c.getPreferredSize().width + (containerWidth - preferredWidth);
520 if (newWidth < c.getPreferredSize().width) {
521 newWidth = c.getPreferredSize().width;
522 }
523 c.setSize(new java.awt.Dimension(newWidth, c.getHeight()));
524 c.validate();
525 }
526
527 /**
528 * To reset the height of input component via container height
529 *
530 * @param c
531 * @param containerHeight
532 *
533 */
534 public static void resizeComponentHeight(Component c, int containerHeight, int preferredHeight) {
535 int newHeight = c.getPreferredSize().height + (containerHeight - preferredHeight);
536 if (newHeight < c.getPreferredSize().height) {
537 newHeight = c.getPreferredSize().height;
538 }
539 c.setSize(new java.awt.Dimension(c.getWidth(), newHeight));
540 c.validate();
541 }
542
543 /**
544 * To reset the size of input component via container size
545 *
546 * @param c
547 * @param containerWidth
548 * @param containerHeight
549 *
550 */
551 public static void resizeComponent(Component c, int containerWidth, int containerHeight, int preferredWidth,
552 int preferredHeight) {
553 resizeComponentWidth(c, containerWidth, preferredWidth);
554 resizeComponentHeight(c, containerHeight, preferredHeight);
555 }
556
557 /**
558 To adjust each column's width to meet the table's size
559
560 @param t the table need to be adjusted
561 @param width the new width of the table
562
563 **/
564 public static void resizeTableColumn(JTable t, int width) {
565 if (t != null) {
566 int columnCount = t.getColumnCount();
567 for (int index = 0; index < columnCount; index++) {
568 t.getColumn(t.getColumnName(index)).setPreferredWidth(width / columnCount);
569 }
570 }
571 }
572
573 /**
574 * To relocate the input component
575 *
576 * @param c
577 * @param containerWidth
578 * @param spaceToRight
579 *
580 */
581 public static void relocateComponentX(Component c, int containerWidth, int preferredWidth, int spaceToRight) {
582 int intGapToRight = spaceToRight + c.getPreferredSize().width;
583 int newLocationX = containerWidth - intGapToRight;
584 if (newLocationX < preferredWidth - intGapToRight) {
585 newLocationX = preferredWidth - intGapToRight;
586 }
587 c.setLocation(newLocationX, c.getLocation().y);
588 c.validate();
589 }
590
591 /**
592 * To relocate the input component
593 *
594 * @param c
595 * @param containerHeight
596 * @param spaceToBottom
597 *
598 */
599 public static void relocateComponentY(Component c, int containerHeight, int preferredHeight, int spaceToBottom) {
600 int intGapToBottom = spaceToBottom + c.getPreferredSize().height;
601 int newLocationY = containerHeight - intGapToBottom;
602 if (newLocationY < preferredHeight - spaceToBottom) {
603 newLocationY = preferredHeight - spaceToBottom;
604 }
605 c.setLocation(c.getLocation().x, newLocationY);
606 c.validate();
607 }
608
609 /**
610 * To relocate the input component
611 *
612 * @param c
613 * @param containerWidth
614 * @param containerHeight
615 * @param spaceToBottom
616 * @param spaceToRight
617 *
618 */
619 public static void relocateComponent(Component c, int containerWidth, int containerHeight, int preferredWidht,
620 int preferredHeight, int spaceToRight, int spaceToBottom) {
621 relocateComponentX(c, containerWidth, preferredWidht, spaceToRight);
622 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);
623 }
624
625 /**
626 Move the component to the center of screen
627
628 @param c
629 @param width
630
631 **/
632 public static void centerComponent(Component c, int width) {
633 c.setLocation(width / 2 - c.getWidth() / 2, c.getLocation().y);
634 c.validate();
635 }
636
637 /**
638 Move the component to the center of screen and adjust the y location
639
640 @param c
641 @param width
642
643 **/
644 public static void centerComponent(Component c, int width, int containerHeight, int preferredHeight,
645 int spaceToBottom) {
646 relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);
647 centerComponent(c, width);
648 }
649
650 /**
651 Find the count of searchString in wholeString
652
653 @param wholeString
654 @param searchString
655 @return
656
657 **/
658 public static int getSpecificStringCount(String wholeString, String searchString) {
659 int count = 0;
660 count = wholeString.split(searchString).length;
661 return count;
662 }
663
664 /**
665 * Check the input data is empty or not
666 *
667 * @param strValue
668 * The input data which need be checked
669 *
670 * @retval true - The input data is empty
671 * @retval fals - The input data is not empty
672 *
673 */
674 public static boolean isEmpty(String strValue) {
675 if (strValue.length() > 0) {
676 return false;
677 }
678 return true;
679 }
680 }