📄 utils.java
字号:
package org.enhydra.jawe;import java.awt.Color;import java.awt.Dimension;import java.awt.GraphicsEnvironment;import java.awt.Point;import java.awt.Window;import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.FileReader;import java.io.FileWriter;import java.io.FilenameFilter;import java.io.InputStream;import java.io.InputStreamReader;import java.io.Writer;import java.lang.reflect.Constructor;import java.lang.reflect.Field;import java.net.URL;import java.net.URLConnection;import java.net.URLDecoder;import java.nio.channels.FileChannel;import java.util.ArrayList;import java.util.Arrays;import java.util.Calendar;import java.util.Collections;import java.util.Enumeration;import java.util.GregorianCalendar;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Properties;import java.util.Set;import java.util.StringTokenizer;import java.util.Vector;import java.util.jar.JarFile;import java.util.zip.ZipEntry;import javax.swing.ImageIcon;import org.enhydra.jawe.base.controller.JaWEController;import org.enhydra.jawe.base.xpdlvalidator.ValidationError;import org.enhydra.jawe.misc.PFLocale;import org.enhydra.shark.utilities.SequencedHashMap;import org.enhydra.shark.xpdl.XMLCollection;import org.enhydra.shark.xpdl.XMLComplexElement;import org.enhydra.shark.xpdl.XMLElement;import org.enhydra.shark.xpdl.XMLUtil;import org.enhydra.shark.xpdl.XMLValidationError;import org.enhydra.shark.xpdl.XPDLConstants;import org.enhydra.shark.xpdl.elements.Activity;import org.enhydra.shark.xpdl.elements.ActivitySet;import org.enhydra.shark.xpdl.elements.Application;import org.enhydra.shark.xpdl.elements.DataField;import org.enhydra.shark.xpdl.elements.ExtendedAttribute;import org.enhydra.shark.xpdl.elements.ExtendedAttributes;import org.enhydra.shark.xpdl.elements.FormalParameter;import org.enhydra.shark.xpdl.elements.Package;import org.enhydra.shark.xpdl.elements.Participant;import org.enhydra.shark.xpdl.elements.Transition;import org.enhydra.shark.xpdl.elements.TypeDeclaration;import org.enhydra.shark.xpdl.elements.WorkflowProcess;/** * Various utilities. * * @author Sasa Bojanic */public class Utils { public static final String LANG_PROP_PREFIX = "JaWE"; public static Properties getProperties(String path, Properties defaultProperties) throws Exception { Properties props = defaultProperties; if (path == null || path.equals("")) return props; File configFile = new File(path); if (configFile != null) { if (!configFile.isAbsolute()) { configFile = configFile.getAbsoluteFile(); } if (!configFile.exists()) configFile = null; } if (configFile == null) { path = System.getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME) + "/" + path; configFile = new File(path); if (configFile == null) { return props; } if (!configFile.isAbsolute()) { configFile = configFile.getAbsoluteFile(); } } if (configFile.exists()) { FileInputStream fis = null; try { fis = new FileInputStream(configFile); props.load(fis); fis.close(); } catch (Exception ex) { throw new Exception("Something went wrong while reading configuration from file " + configFile + "!!!", ex); } } else { throw new Exception("Component needs to be configured properly - configuration file " + configFile + " does not exist!!!"); } return props; } public static boolean checkFileExistence(String path) { if (path == null || path.equals("")) return false; File configFile = new File(path); if (configFile != null) { if (!configFile.isAbsolute()) { configFile = configFile.getAbsoluteFile(); } if (!configFile.exists()) configFile = null; } if (configFile == null) { path = System.getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME) + "/" + path; configFile = new File(path); if (configFile == null) { return false; } if (!configFile.isAbsolute()) { configFile = configFile.getAbsoluteFile(); } } if (!configFile.exists()) { return false; } return true; } public static boolean checkResourceExistence(String path, String name) { return Utils.class.getClassLoader().getResource(path + name) != null; } public static Map getProperties(Properties properties, String startsWith) { Map toRet = new SequencedHashMap(); Iterator it = properties.entrySet().iterator(); while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); if (((String) me.getKey()).startsWith(startsWith)) { toRet.put(me.getKey(), me.getValue()); } } return toRet; } public static boolean copyPropertyFile(String path, String name, boolean overwrite) throws Exception { String confhome = System.getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME); if (confhome == null) return true; String filename = confhome + "/" + name; if (!overwrite) { boolean doesExist = Utils.checkFileExistence(filename); if (doesExist) return false; } System.out.println("Copying property file " + name + " to " + filename); InputStream is = null; URL u = Utils.class.getClassLoader().getResource(path + name); BufferedReader input = null; Writer output = null; try { URLConnection urlConnection = u.openConnection(); is = urlConnection.getInputStream(); input = new BufferedReader(new InputStreamReader(is)); StringBuffer contents = new StringBuffer(); String line = null; // not declared within while loop while ((line = input.readLine()) != null) { contents.append(line); contents.append(System.getProperty("line.separator")); } output = new BufferedWriter(new FileWriter(filename)); output.write(contents.toString()); } finally { if (input != null) input.close(); if (output != null) output.close(); } return true; // return false; } public static void manageProperties(Properties properties, String path, String name) throws Exception { try { // if (true) return; if (!Utils.copyPropertyFile(path, name, false)) { Properties external = new Properties(); FileInputStream fis = null; try { fis = new FileInputStream(System.getProperty(JaWEConstants.JAWE_CURRENT_CONFIG_HOME) + "/" + name); external.load(fis); fis.close(); Utils.adjustProperties(properties, external); } catch (Exception ex) { throw new Error("Something went wrong while reading external component properties !!!", ex); } } else { URL u = JaWEManager.class.getClassLoader().getResource(path + name); if (u == null) return; URLConnection urlConnection = u.openConnection(); InputStream is = urlConnection.getInputStream(); properties.load(is); } } catch (Exception ex) { throw new Exception("Something went wrong while reading component's properties !!!", ex); } } public static void adjustProperties(Properties original, Properties external) { Iterator it = external.entrySet().iterator(); while (it.hasNext()) { Map.Entry me = (Map.Entry) it.next(); String key = (String) me.getKey(); String val = (String) me.getValue(); original.setProperty(key, val); } } /** * Take the given string and chop it up into a series of strings on given boundries. * This is useful for trying to get an array of strings out of the resource file. */ public static String[] tokenize(String input, String boundary) { if (input == null) input = ""; Vector v = new Vector(); StringTokenizer t = new StringTokenizer(input, boundary); String cmd[]; while (t.hasMoreTokens()) v.addElement(t.nextToken()); cmd = new String[v.size()]; for (int i = 0; i < cmd.length; i++) cmd[i] = (String) v.elementAt(i); return cmd; } /** Returns the class name without package. */ public static String getUnqualifiedClassName(Class cls) { String name = cls.getName(); int lastDot = name.lastIndexOf("."); if (lastDot >= 0) { name = name.substring(lastDot + 1, name.length()); } return name; } /** * Returns the color parsed from the given string. The color can be given in three * different string form: * <ul> * <li> using prefix Color, dot and wanted color, e.g. <b>Color.red</b> * <li> using prefix SystemColor, dot and wanted color, e.g. <b>SystemColor.desktop</b> * <li> using RGB like string, e.g. <b>R=124,G=213,B=12</b> * </ul> * * @param col The string representation of wanted color. * @return The color based on given string or null if incorrect */ public static Color getColor(String col) { Color c = null; int dotInd = col.indexOf("."); int r, g, b; if (col.indexOf("Color") != -1 && dotInd != -1) { try { ClassLoader cl = JaWEManager.class.getClassLoader(); Class cls = cl.loadClass("java.awt." + col.substring(0, dotInd)); Field f = cls.getField(col.substring(dotInd + 1)); c = (Color) f.get(null); c = new Color(c.getRed(), c.getGreen(), c.getBlue()); } catch (Exception ex) { } } else { try { int i1 = col.indexOf("R="); if (i1 == -1) i1 = col.indexOf("r="); int i1c = col.indexOf(",", i1 + 2); int i2 = col.indexOf("G="); if (i2 == -1) i2 = col.indexOf("g="); int i2c = col.indexOf(",", i2 + 2); int i3 = col.indexOf("B="); if (i3 == -1) i3 = col.indexOf("b="); if (i1 != -1 && i1c != -1 && i2 != -1 && i2c != -1 && i3 != -1 && (i1c < i2) && (i2c < i3)) { r = Integer.valueOf(col.substring(i1 + 2, i1c).trim()).intValue(); if (r < 0) r = 0; if (r > 255) r = 255; g = Integer.valueOf(col.substring(i2 + 2, i2c).trim()).intValue(); if (g < 0) g = 0; if (g > 255) g = 255; b = Integer.valueOf(col.substring(i3 + 2).trim()).intValue(); if (b < 0) b = 0; if (b > 255) b = 255; c = new Color(r, g, b); } } catch (Exception ex) { } } return c; } public static void flipCoordinates(Point p) { int x = p.x; p.x = p.y; p.y = x; } // ******************************** DEBUGGING STUFF **************************** /** Used for debug only */ public static void printStrings(String[] s) { if (s != null) { for (int i = 0; i < s.length; i++) { System.out.println("String no " + i + " = " + s[i]); } } else { System.out.println("Passed string array is null !!!"); } } // **************************** END OF DEBUGGING STUFF ************************* public static void center(Window w, int minXDiffFromMax, int minYDiffFromMax) { w.pack(); // make dialog smaller if needed, and center it // Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension screenSize = GraphicsEnvironment.getLocalGraphicsEnvironment() .getDefaultScreenDevice() .getDefaultConfiguration() .getBounds() .getSize(); Dimension windowSize = w.getPreferredSize(); if (windowSize.width > screenSize.width - minXDiffFromMax) { windowSize.width = screenSize.width - minXDiffFromMax; } if (windowSize.height > screenSize.height - minYDiffFromMax) { windowSize.height = screenSize.height - minYDiffFromMax; } w.setSize(windowSize); w.setLocation(screenSize.width / 2 - (windowSize.width / 2), screenSize.height / 2 - (windowSize.height / 2)); } public static java.util.List findPropertyFiles() { java.util.List pfs = new ArrayList(); URL u = Utils.class.getClassLoader().getResource("org/enhydra/jawe/language"); File path = null; if (u != null) { path = new File(u.getPath()); } // if folder exists and realy is a folder but not file if (path != null && path.exists() && path.isDirectory()) { // getting all .property files within folder pfs.addAll(Arrays.asList(path.list(new PFFilter()))); } else { JarFile jfile = null; try { String jp = "org/enhydra/jawe/language/JaWE.properties"; u = Utils.class.getClassLoader().getResource(jp); String jarPath = u.getPath() .substring(0, u.getPath().length() - jp.length() - 2); jarPath = URLDecoder.decode(jarPath, "UTF-8"); jarPath = jarPath.substring(5); jfile = new JarFile(jarPath, false); // get all entries Enumeration e = jfile.entries(); // loop through entries and find appropriate ones while (e.hasMoreElements()) { try {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -