+++ /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.awt.Component;\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.DefaultListModel;\r
-import javax.swing.JComboBox;\r
-import javax.swing.JList;\r
-import javax.swing.JOptionPane;\r
-import javax.swing.JTable;\r
-\r
-import org.tianocore.ModuleSurfaceAreaDocument.ModuleSurfaceArea;\r
-import org.tianocore.MsaHeaderDocument.MsaHeader;\r
-import org.tianocore.PackageSurfaceAreaDocument.PackageSurfaceArea;\r
-import org.tianocore.PlatformHeaderDocument.PlatformHeader;\r
-import org.tianocore.PlatformSurfaceAreaDocument.PlatformSurfaceArea;\r
-import org.tianocore.SpdHeaderDocument.SpdHeader;\r
-import org.tianocore.frameworkwizard.FrameworkWizardUI;\r
-import org.tianocore.frameworkwizard.module.Identifications.ModuleIdentification;\r
-import org.tianocore.frameworkwizard.packaging.PackageIdentification;\r
-import org.tianocore.frameworkwizard.platform.PlatformIdentification;\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
- 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
- Generate selection items for JList by input vector\r
- \r
- **/\r
- public static void generateListByVector(JList jl, Vector<String> vector) {\r
- if (jl != null) {\r
- DefaultListModel listModel = (DefaultListModel) jl.getModel();\r
- listModel.removeAllElements();\r
-\r
- if (vector != null) {\r
- for (int index = 0; index < vector.size(); index++) {\r
- listModel.addElement(vector.get(index));\r
- }\r
- }\r
-\r
- if (listModel.size() > 0) {\r
- jl.setSelectedIndex(0);\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
- 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.length() > 0\r
- && 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.length() > 0\r
- && 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
- Convert a Vector to a String, separator with ", "\r
- \r
- @param v\r
- @return\r
- \r
- **/\r
- public static String convertVectorToString(Vector<String> v) {\r
- String s = "";\r
- for (int index = 0; index < v.size(); index++) {\r
- s = s + v.elementAt(index).toString() + ", ";\r
- }\r
- if (s.length() > 0) {\r
- s = s.substring(0, s.length() - ", ".length());\r
- }\r
- return s;\r
- }\r
-\r
- /**\r
- Convert a List to a String\r
- \r
- @param list\r
- @return\r
- \r
- **/\r
- public static String convertListToString(List list) {\r
- return Tools.convertVectorToString(Tools.convertListToVector(list));\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 (type == DataType.RETURN_TYPE_TEXT) {\r
- match = DataType.FILE_EXT_SEPARATOR + DataType.TEXT_FILE_EXT;\r
- }\r
- if (type == DataType.RETURN_TYPE_FAR_SURFACE_AREA) {\r
- match = DataType.FILE_EXT_SEPARATOR + DataType.FAR_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(FrameworkWizardUI.getInstance(), arg0, "Info", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE);\r
- }\r
-\r
- /**\r
- if the string doesn't end with a file separator, append it to the string\r
- \r
- @param arg0\r
- @return\r
- \r
- **/\r
- public static String addFileSeparator(String arg0) {\r
- if (!arg0.endsWith(DataType.FILE_SEPARATOR)) {\r
- arg0 = arg0 + DataType.FILE_SEPARATOR;\r
- }\r
- return arg0;\r
- }\r
-\r
- /**\r
- Wrap single line long input string to multiple short line string by word\r
- \r
- @param arg0 input string\r
- @return wraped string\r
- \r
- **/\r
- public static String wrapStringByWord(String arg0) {\r
- int intMaxLength = 40;\r
- String strReturn = "";\r
- String strTemp = "";\r
- boolean isCopied = true;\r
-\r
- if (arg0 == null) {\r
- return "";\r
- }\r
- if (arg0.length() <= 0) {\r
- return "";\r
- }\r
-\r
- //\r
- // Convert string to array by " "\r
- //\r
- String s[] = arg0.split(" ");\r
- if (arg0.indexOf(" ") == -1) {\r
- s[0] = arg0;\r
- }\r
-\r
- //\r
- // Add each string of array one by one\r
- //\r
- for (int index = 0; index < s.length; index++) {\r
- String ss = s[index];\r
- isCopied = false;\r
- //\r
- // The word length > defined line length\r
- //\r
- if (ss.length() > intMaxLength) {\r
- //\r
- // Finish previous line\r
- //\r
- if (!isCopied) {\r
- strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;\r
- strTemp = "";\r
- }\r
- //\r
- // Separater to short lines\r
- //\r
- while (ss.length() > 0) {\r
- if (ss.length() > intMaxLength) {\r
- strReturn = strReturn + s[index].substring(0, intMaxLength - 1) + DataType.UNIX_LINE_SEPARATOR;\r
- ss = ss.substring(intMaxLength);\r
- isCopied = true;\r
- } else {\r
- strTemp = ss;\r
- ss = "";\r
- isCopied = false;\r
- }\r
- }\r
- } else {\r
- if ((strTemp + " " + ss).length() <= intMaxLength) {\r
- strTemp = strTemp + " " + ss;\r
- continue;\r
- } else {\r
- strReturn = strReturn + strTemp + DataType.LINE_SEPARATOR;\r
- if ((index == s.length - 1) && (!ss.equals(""))) {\r
- strReturn = strReturn + ss;\r
- } else {\r
- strTemp = ss + " ";\r
- }\r
- isCopied = true;\r
- }\r
- }\r
- }\r
-\r
- if (!isCopied) {\r
- strReturn = strReturn + strTemp;\r
- }\r
-\r
- return strReturn;\r
- }\r
-\r
- public static String convertUnicodeHexStringToString(String str) {\r
- //\r
- // Handle if str is null or empty\r
- //\r
- if (str == null) {\r
- return "";\r
- }\r
- if (str.equals("")) {\r
- return "";\r
- }\r
-\r
- String returnString = "";\r
- String[] strArray = str.split(" ");\r
- for (int index = 0; index < strArray.length; index++) {\r
- String s = strArray[index];\r
- if (s.length() == 6 && s.indexOf(DataType.HEX_STRING_HEADER) == 0) {\r
- s = s.substring(DataType.HEX_STRING_HEADER.length());\r
- } else {\r
- Log.err("convertUnicodeHexStringToString", "Incorrect input string: " + str);\r
- continue;\r
- }\r
- //\r
- // Change hex to dec\r
- //\r
- int dec = Integer.parseInt(s, 16);\r
-\r
- returnString = returnString + (char) (dec);\r
- }\r
- return returnString;\r
- }\r
-\r
- /**\r
- Convert input string to unicode hex string\r
- \r
- @param str input string\r
- @return unicode hex string\r
- \r
- **/\r
- public static String convertStringToUnicodeHexString(String str) {\r
- //\r
- // Handle if str is null or empty\r
- //\r
- if (str == null) {\r
- return "";\r
- }\r
- if (str.equals("")) {\r
- return "";\r
- }\r
-\r
- //\r
- // convert string to hex string\r
- //\r
- String hexString = "";\r
- for (int index = 0; index < str.length(); index++) {\r
- int codePoint = str.codePointAt(index);\r
- String s = Integer.toHexString(codePoint);\r
- //\r
- // Make the string to four length\r
- //\r
- if (s.length() == 3) {\r
- s = "0" + s;\r
- } else if (s.length() == 2) {\r
- s = "00" + s;\r
- } else if (s.length() == 1) {\r
- s = "000" + s;\r
- }\r
-\r
- //\r
- // Add the string to return hex string\r
- //\r
- hexString = hexString + DataType.HEX_STRING_HEADER + s + " ";\r
- }\r
-\r
- //\r
- // return hex string\r
- //\r
- return hexString.trim();\r
- }\r
-\r
- public static ModuleIdentification getId(String path, ModuleSurfaceArea msa) {\r
- MsaHeader head = msa.getMsaHeader();\r
- String name = head.getModuleName();\r
- String guid = head.getGuidValue();\r
- String version = head.getVersion();\r
- ModuleIdentification id = new ModuleIdentification(name, guid, version, path);\r
- return id;\r
- }\r
-\r
- public static PackageIdentification getId(String path, PackageSurfaceArea spd) {\r
- SpdHeader head = spd.getSpdHeader();\r
- String name = head.getPackageName();\r
- String guid = head.getGuidValue();\r
- String version = head.getVersion();\r
- PackageIdentification id = new PackageIdentification(name, guid, version, path);\r
- return id;\r
- }\r
-\r
- public static PlatformIdentification getId(String path, PlatformSurfaceArea fpd) {\r
- PlatformHeader head = fpd.getPlatformHeader();\r
- String name = head.getPlatformName();\r
- String guid = head.getGuidValue();\r
- String version = head.getVersion();\r
- PlatformIdentification id = new PlatformIdentification(name, guid, version, path);\r
- return id;\r
- }\r
-\r
- /**\r
- * To reset the width of input component via container width\r
- * \r
- * @param c\r
- * @param containerWidth\r
- * \r
- */\r
- public static void resizeComponentWidth(Component c, int containerWidth, int preferredWidth) {\r
- int newWidth = c.getPreferredSize().width + (containerWidth - preferredWidth);\r
- if (newWidth < c.getPreferredSize().width) {\r
- newWidth = c.getPreferredSize().width;\r
- }\r
- c.setSize(new java.awt.Dimension(newWidth, c.getHeight()));\r
- c.validate();\r
- }\r
-\r
- /**\r
- * To reset the height of input component via container height\r
- * \r
- * @param c\r
- * @param containerHeight\r
- * \r
- */\r
- public static void resizeComponentHeight(Component c, int containerHeight, int preferredHeight) {\r
- int newHeight = c.getPreferredSize().height + (containerHeight - preferredHeight);\r
- if (newHeight < c.getPreferredSize().height) {\r
- newHeight = c.getPreferredSize().height;\r
- }\r
- c.setSize(new java.awt.Dimension(c.getWidth(), newHeight));\r
- c.validate();\r
- }\r
-\r
- /**\r
- * To reset the size of input component via container size\r
- * \r
- * @param c\r
- * @param containerWidth\r
- * @param containerHeight\r
- * \r
- */\r
- public static void resizeComponent(Component c, int containerWidth, int containerHeight, int preferredWidth,\r
- int preferredHeight) {\r
- resizeComponentWidth(c, containerWidth, preferredWidth);\r
- resizeComponentHeight(c, containerHeight, preferredHeight);\r
- }\r
-\r
- /**\r
- To adjust each column's width to meet the table's size\r
- \r
- @param t the table need to be adjusted\r
- @param width the new width of the table\r
- \r
- **/\r
- public static void resizeTableColumn(JTable t, int width) {\r
- if (t != null) {\r
- int columnCount = t.getColumnCount();\r
- for (int index = 0; index < columnCount; index++) {\r
- t.getColumn(t.getColumnName(index)).setPreferredWidth(width / columnCount);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * To relocate the input component\r
- * \r
- * @param c\r
- * @param containerWidth\r
- * @param spaceToRight\r
- * \r
- */\r
- public static void relocateComponentX(Component c, int containerWidth, int preferredWidth, int spaceToRight) {\r
- int intGapToRight = spaceToRight + c.getPreferredSize().width;\r
- int newLocationX = containerWidth - intGapToRight;\r
- if (newLocationX < preferredWidth - intGapToRight) {\r
- newLocationX = preferredWidth - intGapToRight;\r
- }\r
- c.setLocation(newLocationX, c.getLocation().y);\r
- c.validate();\r
- }\r
-\r
- /**\r
- * To relocate the input component\r
- * \r
- * @param c\r
- * @param containerHeight\r
- * @param spaceToBottom\r
- * \r
- */\r
- public static void relocateComponentY(Component c, int containerHeight, int preferredHeight, int spaceToBottom) {\r
- int intGapToBottom = spaceToBottom + c.getPreferredSize().height;\r
- int newLocationY = containerHeight - intGapToBottom;\r
- if (newLocationY < preferredHeight - spaceToBottom) {\r
- newLocationY = preferredHeight - spaceToBottom;\r
- }\r
- c.setLocation(c.getLocation().x, newLocationY);\r
- c.validate();\r
- }\r
-\r
- /**\r
- * To relocate the input component\r
- * \r
- * @param c\r
- * @param containerWidth\r
- * @param containerHeight\r
- * @param spaceToBottom\r
- * @param spaceToRight\r
- * \r
- */\r
- public static void relocateComponent(Component c, int containerWidth, int containerHeight, int preferredWidht,\r
- int preferredHeight, int spaceToRight, int spaceToBottom) {\r
- relocateComponentX(c, containerWidth, preferredWidht, spaceToRight);\r
- relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);\r
- }\r
-\r
- /**\r
- Move the component to the center of screen \r
- \r
- @param c\r
- @param width\r
- \r
- **/\r
- public static void centerComponent(Component c, int width) {\r
- c.setLocation(width / 2 - c.getWidth() / 2, c.getLocation().y);\r
- c.validate();\r
- }\r
-\r
- /**\r
- Move the component to the center of screen and adjust the y location \r
- \r
- @param c\r
- @param width\r
- \r
- **/\r
- public static void centerComponent(Component c, int width, int containerHeight, int preferredHeight,\r
- int spaceToBottom) {\r
- relocateComponentY(c, containerHeight, preferredHeight, spaceToBottom);\r
- centerComponent(c, width);\r
- }\r
-\r
- /**\r
- Find the count of searchString in wholeString\r
- \r
- @param wholeString\r
- @param searchString\r
- @return\r
-\r
- **/\r
- public static int getSpecificStringCount(String wholeString, String searchString) {\r
- int count = 0;\r
- count = wholeString.split(searchString).length;\r
- return count;\r
- }\r
-\r
- /**\r
- * Check the input data is empty or not\r
- * \r
- * @param strValue\r
- * The input data which need be checked\r
- * \r
- * @retval true - The input data is empty\r
- * @retval fals - The input data is not empty\r
- * \r
- */\r
- public static boolean isEmpty(String strValue) {\r
- if (strValue == null) {\r
- return true;\r
- }\r
- if (strValue.length() > 0) {\r
- return false;\r
- }\r
- return true;\r
- }\r
-}\r