--- /dev/null
+/** @file\r
+ \r
+ The file is used to provides some useful interfaces \r
+ \r
+ Copyright (c) 2006, Intel Corporation\r
+ All rights reserved. This program and the accompanying materials\r
+ are licensed and made available under the terms and conditions of the BSD License\r
+ which accompanies this distribution. The full text of the license may be found at\r
+ http://opensource.org/licenses/bsd-license.php\r
+ \r
+ THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
+ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
+ \r
+ **/\r
+\r
+package org.tianocore.frameworkwizard.common;\r
+\r
+import java.io.File;\r
+import java.text.SimpleDateFormat;\r
+import java.util.Date;\r
+import java.util.List;\r
+import java.util.UUID;\r
+import java.util.Vector;\r
+\r
+import javax.swing.JComboBox;\r
+import javax.swing.JOptionPane;\r
+\r
+/**\r
+ The class is used to provides some useful interfaces \r
+ \r
+ **/\r
+public class Tools {\r
+\r
+ //\r
+ // The dir user selected to create new package in\r
+ //\r
+ public static String dirForNewSpd = null;\r
+\r
+ /**\r
+ Used for test\r
+ \r
+ @param args\r
+ \r
+ **/\r
+ public static void main(String[] args) {\r
+ System.out.println(getCurrentDateTime());\r
+ }\r
+\r
+ /**\r
+ Get current date and time and format it as "yyyy-MM-dd HH:mm"\r
+ \r
+ @return formatted current date and time\r
+ \r
+ **/\r
+ public static String getCurrentDateTime() {\r
+ Date now = new Date(System.currentTimeMillis());\r
+ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");\r
+ return sdf.format(now);\r
+ }\r
+\r
+ /**\r
+ Generate a UUID\r
+ \r
+ @return the created UUID\r
+ \r
+ **/\r
+ public static String generateUuidString() {\r
+ return UUID.randomUUID().toString();\r
+ }\r
+\r
+ /**\r
+ Use current file separator in the path\r
+ \r
+ @param strPath\r
+ @return\r
+ \r
+ **/\r
+ public static String convertPathToCurrentOsType(String strPath) {\r
+ strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r
+ strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.FILE_SEPARATOR);\r
+ return strPath;\r
+ }\r
+\r
+ /**\r
+ Use Unix file separator in the path\r
+ \r
+ @param strPath\r
+ @return\r
+ \r
+ **/\r
+ public static String convertPathToUnixType(String strPath) {\r
+ strPath = strPath.replace(DataType.DOS_FILE_SEPARATOR, DataType.UNIX_FILE_SEPARATOR);\r
+ return strPath;\r
+ }\r
+\r
+ /**\r
+ Use Dos file separator in the path\r
+ \r
+ @param strPath\r
+ @return\r
+ \r
+ **/\r
+ public static String convertPathToDosType(String strPath) {\r
+ strPath = strPath.replace(DataType.UNIX_FILE_SEPARATOR, DataType.DOS_FILE_SEPARATOR);\r
+ return strPath;\r
+ }\r
+\r
+ /**\r
+ Get all system properties and output to the console\r
+ \r
+ **/\r
+ public static void getSystemProperties() {\r
+ System.out.println(System.getProperty("java.class.version"));\r
+ System.out.println(System.getProperty("java.class.path"));\r
+ System.out.println(System.getProperty("java.ext.dirs"));\r
+ System.out.println(System.getProperty("os.name"));\r
+ System.out.println(System.getProperty("os.arch"));\r
+ System.out.println(System.getProperty("os.version"));\r
+ System.out.println(System.getProperty("file.separator"));\r
+ System.out.println(System.getProperty("path.separator"));\r
+ System.out.println(System.getProperty("line.separator"));\r
+ System.out.println(System.getProperty("user.name"));\r
+ System.out.println(System.getProperty("user.home"));\r
+ System.out.println(System.getProperty("user.dir"));\r
+ System.out.println(System.getProperty("PATH"));\r
+\r
+ System.out.println(System.getenv("PROCESSOR_REVISION"));\r
+ }\r
+\r
+ /**\r
+ Generate selection items for JComboBox by input vector\r
+ \r
+ **/\r
+ public static void generateComboBoxByVector(JComboBox jcb, Vector<String> vector) {\r
+ if (jcb != null) {\r
+ jcb.removeAllItems();\r
+ }\r
+ if (vector != null) {\r
+ for (int index = 0; index < vector.size(); index++) {\r
+ jcb.addItem(vector.elementAt(index));\r
+ }\r
+ }\r
+ }\r
+ \r
+ /**\r
+ Get path only from a path\r
+ \r
+ @param filePath\r
+ @return\r
+ \r
+ **/\r
+ public static String getFilePathOnly(String filePath) {\r
+ String path = filePath.substring(0, filePath.length() - getFileNameOnly(filePath).length());\r
+ if (path.endsWith(DataType.FILE_SEPARATOR)) {\r
+ path = path.substring(0, path.length() - DataType.FILE_SEPARATOR.length());\r
+ }\r
+ \r
+ return path;\r
+ }\r
+ \r
+ \r
+ /**\r
+ Get file name from a path\r
+ \r
+ @param filePath\r
+ @return\r
+ \r
+ **/\r
+ public static String getFileNameOnly(String filePath) {\r
+ File f = new File(filePath);\r
+ return f.getAbsoluteFile().getName();\r
+ }\r
+ \r
+ public static String getFileNameWithoutExt(String filePath) {\r
+ filePath = getFileNameOnly(filePath);\r
+ filePath = filePath.substring(0, filePath.lastIndexOf(DataType.FILE_EXT_SEPARATOR));\r
+ return filePath;\r
+ }\r
+\r
+ /**\r
+ Get relative path\r
+ \r
+ @param wholePath\r
+ @param commonPath\r
+ @return wholePath - commonPath \r
+ \r
+ **/\r
+ public static String getRelativePath(String wholePath, String commonPath) {\r
+ String path = "";\r
+ int i = 0;\r
+ i = wholePath.indexOf(commonPath);\r
+ if (i > -1) {\r
+ i = i + commonPath.length();\r
+ } else {\r
+ return "";\r
+ }\r
+ path = wholePath.substring(i);\r
+ //\r
+ // remove file separator of head\r
+ //\r
+ if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == 0) {\r
+ path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r
+ }\r
+ if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == 0) {\r
+ path = path.substring(0 + DataType.DOS_FILE_SEPARATOR.length());\r
+ }\r
+ //\r
+ // remove file separator of rear\r
+ //\r
+ if (path.indexOf(DataType.DOS_FILE_SEPARATOR) == path.length() - DataType.DOS_FILE_SEPARATOR.length()) {\r
+ path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
+ }\r
+ if (path.indexOf(DataType.UNIX_FILE_SEPARATOR) == path.length() - DataType.UNIX_FILE_SEPARATOR.length()) {\r
+ path = path.substring(0, path.length() - DataType.DOS_FILE_SEPARATOR.length());\r
+ }\r
+ //\r
+ // convert to UNIX format\r
+ //\r
+ path = Tools.convertPathToUnixType(path);\r
+ return path;\r
+ }\r
+\r
+ /**\r
+ Convert List ot Vector\r
+ \r
+ @param list\r
+ @return\r
+ \r
+ **/\r
+ public static Vector<String> convertListToVector(List list) {\r
+ Vector<String> v = new Vector<String>();\r
+ if (list != null && list.size() > 0) {\r
+ for (int index = 0; index < list.size(); index++) {\r
+ v.addElement(list.get(index).toString());\r
+ }\r
+ }\r
+ return v;\r
+ }\r
+\r
+ /**\r
+ If the input path missing ext, append the ext to the path\r
+ \r
+ @param path\r
+ @param type\r
+ @return\r
+ \r
+ **/\r
+ public static String addPathExt(String path, int type) {\r
+ String match = "";\r
+ if (type == DataType.RETURN_TYPE_MODULE_SURFACE_AREA) {\r
+ match = DataType.FILE_EXT_SEPARATOR + DataType.MODULE_SURFACE_AREA_EXT;\r
+ }\r
+ if (type == DataType.RETURN_TYPE_PACKAGE_SURFACE_AREA) {\r
+ match = DataType.FILE_EXT_SEPARATOR + DataType.PACKAGE_SURFACE_AREA_EXT;\r
+ }\r
+ if (type == DataType.RETURN_TYPE_PLATFORM_SURFACE_AREA) {\r
+ match = DataType.FILE_EXT_SEPARATOR + DataType.PLATFORM_SURFACE_AREA_EXT;\r
+ }\r
+ if (path.length() <= match.length()) {\r
+ path = path + match;\r
+ return path;\r
+ }\r
+ if (!(path.substring(path.length() - match.length())).equals(match)) {\r
+ path = path + match;\r
+ }\r
+ return path;\r
+ }\r
+ \r
+ /**\r
+ Show a message box\r
+ \r
+ @param arg0\r
+ \r
+ **/\r
+ public static void showInformationMessage(String arg0) {\r
+ JOptionPane.showConfirmDialog(null, arg0, "Error", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r
+ }\r
+}\r