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