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