/** store the given RenderingHints to a Properties object */ public static void formatRenderingHints(RenderingHints rh, Properties preferences) { if (rh.get(RenderingHints.KEY_ANTIALIASING).equals(RenderingHints.VALUE_ANTIALIAS_ON)) preferences.put("rendering.antialiasing", "on"); else if (rh.get(RenderingHints.KEY_ANTIALIASING).equals(RenderingHints.VALUE_ANTIALIAS_OFF)) preferences.put("rendering.antialiasing", "off"); if (rh.get(RenderingHints.KEY_TEXT_ANTIALIASING).equals(RenderingHints.VALUE_TEXT_ANTIALIAS_ON)) preferences.put("rendering.text-antialiasing", "on"); else if (rh.get(RenderingHints.KEY_TEXT_ANTIALIASING) .equals(RenderingHints.VALUE_TEXT_ANTIALIAS_OFF)) preferences.put("rendering.text-antialiasing", "off"); if (rh.get(RenderingHints.KEY_RENDERING).equals(RenderingHints.VALUE_RENDER_SPEED)) preferences.put("rendering.render", "speed"); else if (rh.get(RenderingHints.KEY_RENDERING).equals(RenderingHints.VALUE_RENDER_QUALITY)) preferences.put("rendering.render", "quality"); if (rh.get(RenderingHints.KEY_DITHERING).equals(RenderingHints.VALUE_DITHER_ENABLE)) preferences.put("rendering.dither", "on"); else if (rh.get(RenderingHints.KEY_DITHERING).equals(RenderingHints.VALUE_DITHER_DISABLE)) preferences.put("rendering.dither", "off"); if (rh.get(RenderingHints.KEY_FRACTIONALMETRICS) .equals(RenderingHints.VALUE_FRACTIONALMETRICS_ON)) preferences.put("rendering.fractional-metrics", "on"); else if (rh.get(RenderingHints.KEY_FRACTIONALMETRICS) .equals(RenderingHints.VALUE_FRACTIONALMETRICS_OFF)) preferences.put("rendering.fractional-metrics", "off"); }
/** * Return a integer parsed from the value associated with the given key, or "def" in key wasn't * found. */ public static int parseProperty(Properties preferences, String key, int def) { String val = preferences.getProperty(key); if (val == null) return def; try { return Integer.parseInt(val); } catch (NumberFormatException nfe) { nfe.printStackTrace(); return def; } }
/** Returns a RenderingHints parsed from the given Properties */ public static RenderingHints parseRenderingHints(Properties preferences) { RenderingHints rh = new RenderingHints(null); String str; str = preferences.getProperty("rendering.antialiasing"); if (str != null) { if (str.equals("on")) rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); else if (str.equals("off")) rh.put(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF); } str = preferences.getProperty("rendering.text-antialiasing"); if (str != null) { if (str.equals("on")) rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); else if (str.equals("off")) rh.put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); } str = preferences.getProperty("rendering.render"); if (str != null) { if (str.equals("speed")) rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_SPEED); else if (str.equals("quality")) rh.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY); } str = preferences.getProperty("rendering.dither"); if (str != null) { if (str.equals("on")) rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_ENABLE); else if (str.equals("off")) rh.put(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE); } str = preferences.getProperty("rendering.fractional-metrics"); if (str != null) { if (str.equals("on")) rh.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_ON); else if (str.equals("off")) rh.put(RenderingHints.KEY_FRACTIONALMETRICS, RenderingHints.VALUE_FRACTIONALMETRICS_OFF); } return rh; }
/** * @return an ArrayList containing recent file names, fetched from the given key (e.g. * PEMenuBar.KEY_RECENT_FILE) */ public static ArrayList<String> parseRecentFiles(Properties preferences) { int i = 1; String fileName; ArrayList<String> list = new ArrayList<String>(); // build key by appending ".X" where "X" runs from 1 to ... // i.e. fetch as many names as possible, until no key is found : while ((fileName = preferences.getProperty(KEY_RECENT_FILE + "." + Integer.toString(i++))) != null) { // System.out.println("filename=" + fileName + ";"); if (!fileName.equals("") && !fileName.equals(" ")) { list.add(fileName); } } return list; }
public static Properties getEnvironmentVariables() { synchronized (cygstartPath) { if (envVars != null) return envVars; envVars = new Properties(); try (BufferedReader br = new BufferedReader( new InputStreamReader(RUNTIME.exec(comSpec + "env").getInputStream()))) { for (String line = null; (line = br.readLine()) != null; ) { // if (debug) System.out.println("getEnvironmentVariables(): line=" + line); int idx = line.indexOf('='); if (idx > 0) envVars.put(line.substring(0, idx), line.substring(idx + 1)); } } catch (IOException e) { e.printStackTrace(); } return envVars; } }
/** * Add the given file name to the list of recent files in the given Properties object, trimming * the length of the list to 10. If the given name is already in the list, move it to the top. */ public static void addRecentFile(Properties preferences, String newName) { // - if newName isn't already in the list, we add it to the top of the list. // - if it's already in the list, we move it to the top. ArrayList<String> list = MiscUtilities.parseRecentFiles(preferences); if (list.contains(newName)) { // move it to the top : list.remove(newName); list.add(0, newName); } else { list.add(0, newName); } // store to preferences : for (int i = 0; i < MAX_RECENT_FILES && i < list.size(); i++) { String key = KEY_RECENT_FILE + "." + Integer.toString(i + 1); // starts from 1 String val = list.get(i); preferences.setProperty(key, val); } }
/** * @return a double parsed from the value associated with the given key in the given Properties. * returns "def" in key wasn't found, or if a parsing error occured. If "value" contains a "%" * sign, we use a <code>NumberFormat.getPercentInstance</code> to convert it to a double. */ public static double parseProperty(Properties preferences, String key, double def) { NumberFormat formatPercent = NumberFormat.getPercentInstance(Locale.US); // for zoom factor String val = preferences.getProperty(key); if (val == null) return def; if (val.indexOf("%") == -1) { // not in percent format ! try { return Double.parseDouble(val); } catch (NumberFormatException nfe) { nfe.printStackTrace(); return def; } } // else it's a percent format -> parse it try { Number n = formatPercent.parse(val); return n.doubleValue(); } catch (ParseException ex) { ex.printStackTrace(); return def; } }
/** * Create a new reader. * * @param in the input * @param out the output * @param bindings the key bindings to use * @param term the terminal to use */ public ConsoleReader(InputStream in, Writer out, InputStream bindings, Terminal term) throws IOException { this.terminal = term; setInput(in); this.out = out; if (bindings == null) { try { String bindingFile = System.getProperty( "jline.keybindings", new File(System.getProperty("user.home", ".jlinebindings.properties")) .getAbsolutePath()); if (new File(bindingFile).isFile()) { bindings = new FileInputStream(new File(bindingFile)); } } catch (Exception e) { // swallow exceptions with option debugging if (debugger != null) { e.printStackTrace(debugger); } } } if (bindings == null) { bindings = terminal.getDefaultBindings(); } this.keybindings = new short[Character.MAX_VALUE * 2]; Arrays.fill(this.keybindings, UNKNOWN); /** * Loads the key bindings. Bindings file is in the format: * * <p>keycode: operation name */ if (bindings != null) { Properties p = new Properties(); p.load(bindings); bindings.close(); for (Iterator i = p.keySet().iterator(); i.hasNext(); ) { String val = (String) i.next(); try { Short code = new Short(val); String op = (String) p.getProperty(val); Short opval = (Short) KEYMAP_NAMES.get(op); if (opval != null) { keybindings[code.shortValue()] = opval.shortValue(); } } catch (NumberFormatException nfe) { consumeException(nfe); } } // hardwired arrow key bindings // keybindings[VK_UP] = PREV_HISTORY; // keybindings[VK_DOWN] = NEXT_HISTORY; // keybindings[VK_LEFT] = PREV_CHAR; // keybindings[VK_RIGHT] = NEXT_CHAR; } }
/** Returns the setting of an environment variable set inside the JVM. */ public static String getEnvironmentVariable(String envVarName) { if (envVars == null) envVars = getEnvironmentVariables(); return (String) envVars.get(envVarName); }
/** Print all system property keys and values. */ public static void printSystemProperties() { Properties p = System.getProperties(); for (Object key : p.keySet()) System.out.println(key + ": " + p.getProperty(key.toString())); }
/** * @return a Color built from the value fetched from the given key in the given properties, or the * "def" value if the key wasn't found */ public static Color parseProperty(Properties preferences, String key, Color def) { String val = preferences.getProperty(key); if (val == null) return def; return new Color(Integer.parseInt(val)); // [pending] should catch NFE here }
/** * @return a boolean built from the value fetched from the given key in the given properties, or * the "def" value if the key wasn't found */ public static boolean parseProperty(Properties preferences, String key, boolean def) { String val = preferences.getProperty(key); if (val == null) return def; if (val.equals("true") || val.equals("on") || val.equals("yes")) return true; return false; }