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