]>
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 | |
18 | import java.io.File;\r | |
19 | import java.text.SimpleDateFormat;\r | |
20 | import java.util.Date;\r | |
21 | import java.util.List;\r | |
22 | import java.util.UUID;\r | |
23 | import java.util.Vector;\r | |
24 | \r | |
92e29378 | 25 | import javax.swing.DefaultListModel;\r |
a13899c5 | 26 | import javax.swing.JComboBox;\r |
92e29378 | 27 | import javax.swing.JList;\r |
a13899c5 | 28 | import javax.swing.JOptionPane;\r |
29 | \r | |
30 | /**\r | |
31 | The class is used to provides some useful interfaces \r | |
32 | \r | |
33 | **/\r | |
34 | public class Tools {\r | |
35 | \r | |
36 | //\r | |
37 | // The dir user selected to create new package in\r | |
38 | //\r | |
39 | public static String dirForNewSpd = null;\r | |
40 | \r | |
41 | /**\r | |
42 | Used for test\r | |
43 | \r | |
44 | @param args\r | |
45 | \r | |
46 | **/\r | |
47 | public static void main(String[] args) {\r | |
48 | System.out.println(getCurrentDateTime());\r | |
9a8d6d9f | 49 | // Vector<String> v = new Vector<String>();\r |
50 | // Vector<String> v1 = new Vector<String>();\r | |
51 | // \r | |
52 | // v.addElement("CAC");\r | |
53 | // v1.addElement("1111");\r | |
54 | // v.addElement("1AC");\r | |
55 | // v1.addElement("2222");\r | |
56 | // v.addElement("ABC");\r | |
57 | // v1.addElement("3333");\r | |
58 | // v.addElement("0C");\r | |
59 | // v1.addElement("4444");\r | |
60 | // v.addElement("AAC");\r | |
61 | // v1.addElement("5555");\r | |
62 | // Vector<Integer> vs = new Vector<Integer>();\r | |
63 | // vs = Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending);\r | |
64 | // Tools.sortVectorString(v1, Tools.getVectorSortSequence(v, DataType.Sort_Type_Ascending));\r | |
65 | // \r | |
66 | // Tools.sortVectorString(v, DataType.Sort_Type_Ascending);\r | |
67 | // Tools.sortVectorString(v, DataType.Sort_Type_Descending);\r | |
a13899c5 | 68 | }\r |
69 | \r | |
70 | /**\r | |
71 | Get current date and time and format it as "yyyy-MM-dd HH:mm"\r | |
72 | \r | |
73 | @return formatted current date and time\r | |
74 | \r | |
75 | **/\r | |
76 | public static String getCurrentDateTime() {\r | |
77 | Date now = new Date(System.currentTimeMillis());\r | |
78 | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r | |
79 | return sdf.format(now);\r | |
80 | }\r | |
81 | \r | |
82 | /**\r | |
83 | Generate a UUID\r | |
84 | \r | |
85 | @return the created UUID\r | |
86 | \r | |
87 | **/\r | |
88 | public static String generateUuidString() {\r | |
89 | return UUID.randomUUID().toString();\r | |
90 | }\r | |
91 | \r | |
92 | /**\r | |
93 | Use current file separator in the path\r | |
94 | \r | |
95 | @param strPath\r | |
96 | @return\r | |
97 | \r | |
98 | **/\r | |
99 | public static String convertPathToCurrentOsType(String strPath) {\r | |
100 | strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r | |
101 | strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r | |
102 | return strPath;\r | |
103 | }\r | |
104 | \r | |
105 | /**\r | |
106 | Use Unix file separator in the path\r | |
107 | \r | |
108 | @param strPath\r | |
109 | @return\r | |
110 | \r | |
111 | **/\r | |
112 | public static String convertPathToUnixType(String strPath) {\r | |
113 | strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);\r | |
114 | return strPath;\r | |
115 | }\r | |
116 | \r | |
117 | /**\r | |
118 | Use Dos file separator in the path\r | |
119 | \r | |
120 | @param strPath\r | |
121 | @return\r | |
122 | \r | |
123 | **/\r | |
124 | public static String convertPathToDosType(String strPath) {\r | |
125 | strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);\r | |
126 | return strPath;\r | |
127 | }\r | |
128 | \r | |
129 | /**\r | |
130 | Get all system properties and output to the console\r | |
131 | \r | |
132 | **/\r | |
133 | public static void getSystemProperties() {\r | |
134 | System.out.println(System.getProperty("java.class.version"));\r | |
135 | System.out.println(System.getProperty("java.class.path"));\r | |
136 | System.out.println(System.getProperty("java.ext.dirs"));\r | |
137 | System.out.println(System.getProperty("os.name"));\r | |
138 | System.out.println(System.getProperty("os.arch"));\r | |
139 | System.out.println(System.getProperty("os.version"));\r | |
140 | System.out.println(System.getProperty("file.separator"));\r | |
141 | System.out.println(System.getProperty("path.separator"));\r | |
142 | System.out.println(System.getProperty("line.separator"));\r | |
143 | System.out.println(System.getProperty("user.name"));\r | |
144 | System.out.println(System.getProperty("user.home"));\r | |
145 | System.out.println(System.getProperty("user.dir"));\r | |
146 | System.out.println(System.getProperty("PATH"));\r | |
147 | \r | |
148 | System.out.println(System.getenv("PROCESSOR_REVISION"));\r | |
149 | }\r | |
150 | \r | |
151 | /**\r | |
152 | Generate selection items for JComboBox by input vector\r | |
153 | \r | |
154 | **/\r | |
155 | public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {\r | |
156 | if (jcb != null) {\r | |
157 | jcb.removeAllItems();\r | |
158 | }\r | |
159 | if (vector != null) {\r | |
160 | for (int index = 0; index < vector.size(); index++) {\r | |
161 | jcb.addItem(vector.elementAt(index));\r | |
162 | }\r | |
163 | }\r | |
164 | }\r | |
9a8d6d9f | 165 | \r |
92e29378 | 166 | /**\r |
9a8d6d9f | 167 | Generate selection items for JList by input vector\r |
168 | \r | |
169 | **/\r | |
170 | public static void generateListByVector(JList jl, Vector<String> vector) {\r | |
171 | if (jl != null) {\r | |
172 | DefaultListModel listModel = (DefaultListModel) jl.getModel();\r | |
173 | listModel.removeAllElements();\r | |
174 | \r | |
175 | if (vector != null) {\r | |
176 | for (int index = 0; index < vector.size(); index++) {\r | |
177 | listModel.addElement(vector.get(index));\r | |
178 | }\r | |
179 | }\r | |
180 | \r | |
181 | if (listModel.size() > 0) {\r | |
182 | jl.setSelectedIndex(0);\r | |
183 | }\r | |
184 | }\r | |
185 | }\r | |
79cb6fdb | 186 | \r |
a13899c5 | 187 | /**\r |
188 | Get path only from a path\r | |
79cb6fdb | 189 | \r |
a13899c5 | 190 | @param filePath\r |
191 | @return\r | |
79cb6fdb | 192 | \r |
193 | **/\r | |
a13899c5 | 194 | public static String getFilePathOnly(String filePath) {\r |
195 | String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());\r | |
196 | if (path.endsWith(DataType.FILE_SEPARATOR)) {\r | |
197 | path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());\r | |
198 | }\r | |
79cb6fdb | 199 | \r |
a13899c5 | 200 | return path;\r |
201 | }\r | |
79cb6fdb | 202 | \r |
a13899c5 | 203 | /**\r |
204 | Get file name from a path\r | |
205 | \r | |
206 | @param filePath\r | |
207 | @return\r | |
79cb6fdb | 208 | \r |
209 | **/\r | |
a13899c5 | 210 | public static String getFileNameOnly(String filePath) {\r |
211 | File f = new File(filePath);\r | |
212 | return f.getAbsoluteFile().getName();\r | |
213 | }\r | |
79cb6fdb | 214 | \r |
a13899c5 | 215 | public static String getFileNameWithoutExt(String filePath) {\r |
216 | filePath = getFileNameOnly(filePath);\r | |
217 | filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));\r | |
218 | return filePath;\r | |
219 | }\r | |
220 | \r | |
221 | /**\r | |
222 | Get relative path\r | |
223 | \r | |
224 | @param wholePath\r | |
225 | @param commonPath\r | |
226 | @return wholePath - commonPath \r | |
227 | \r | |
228 | **/\r | |
229 | public static String getRelativePath(String wholePath, String commonPath) {\r | |
230 | String path = "";\r | |
231 | int i = 0;\r | |
232 | i = wholePath.indexOf(commonPath);\r | |
233 | if (i > -1) {\r | |
234 | i = i + commonPath.length();\r | |
235 | } else {\r | |
236 | return "";\r | |
237 | }\r | |
238 | path = wholePath.substring(i);\r | |
239 | //\r | |
240 | // remove file separator of head\r | |
241 | //\r | |
242 | if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {\r | |
243 | path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r | |
244 | }\r | |
245 | if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {\r | |
246 | path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r | |
247 | }\r | |
248 | //\r | |
249 | // remove file separator of rear\r | |
250 | //\r | |
251 | if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r | |
252 | path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r | |
253 | }\r | |
254 | if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r | |
255 | path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r | |
256 | }\r | |
257 | //\r | |
258 | // convert to UNIX format\r | |
259 | //\r | |
260 | path = Tools.convertPathToUnixType(path);\r | |
261 | return path;\r | |
262 | }\r | |
263 | \r | |
264 | /**\r | |
265 | Convert List ot Vector\r | |
266 | \r | |
267 | @param list\r | |
268 | @return\r | |
269 | \r | |
270 | **/\r | |
271 | public static Vector<String> convertListToVector(List list) {\r | |
272 | Vector<String> v = new Vector<String>();\r | |
273 | if (list != null && list.size() > 0) {\r | |
274 | for (int index = 0; index < list.size(); index++) {\r | |
275 | v.addElement(list.get(index).toString());\r | |
276 | }\r | |
277 | }\r | |
278 | return v;\r | |
279 | }\r | |
280 | \r | |
281 | /**\r | |
282 | If the input path missing ext, append the ext to the path\r | |
283 | \r | |
284 | @param path\r | |
285 | @param type\r | |
286 | @return\r | |
287 | \r | |
288 | **/\r | |
289 | public static String addPathExt(String path, int type) {\r | |
290 | String match = "";\r | |
291 | if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {\r | |
292 | match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;\r | |
293 | }\r | |
294 | if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {\r | |
295 | match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;\r | |
296 | }\r | |
297 | if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {\r | |
298 | match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;\r | |
299 | }\r | |
92e29378 | 300 | if (type == DataType.RETURN_TYPE_TEXT) {\r |
301 | match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;\r | |
302 | }\r | |
5a24e806 | 303 | if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {\r |
55a2762d | 304 | match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_SURFACE_AREA_EXT;\r |
305 | }\r | |
a13899c5 | 306 | if (path.length() <= match.length()) {\r |
307 | path = path + match;\r | |
308 | return path;\r | |
309 | }\r | |
310 | if (!(path.substring(path.length() - match.length())).equals(match)) {\r | |
311 | path = path + match;\r | |
312 | }\r | |
313 | return path;\r | |
314 | }\r | |
79cb6fdb | 315 | \r |
a13899c5 | 316 | /**\r |
317 | Show a message box\r | |
318 | \r | |
319 | @param arg0\r | |
79cb6fdb | 320 | \r |
321 | **/\r | |
a13899c5 | 322 | public static void showInformationMessage(String arg0) {\r |
323 | JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r | |
324 | }\r | |
92e29378 | 325 | \r |
326 | /**\r | |
327 | if the string doesn't end with a file separator, append it to the string\r | |
328 | \r | |
329 | @param arg0\r | |
330 | @return\r | |
331 | \r | |
332 | **/\r | |
333 | public static String addFileSeparator(String arg0) {\r | |
334 | if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {\r | |
335 | arg0 = arg0 + DataType.FILE_SEPARATOR;\r | |
336 | }\r | |
337 | return arg0;\r | |
338 | }\r | |
a13899c5 | 339 | }\r |