|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| KeyStrokeOption.java | 82.4% | 80.4% | 100% | 81.9% |
|
||||||||||||||
| 1 | /*BEGIN_COPYRIGHT_BLOCK | |
| 2 | * | |
| 3 | * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu) | |
| 4 | * All rights reserved. | |
| 5 | * | |
| 6 | * Redistribution and use in source and binary forms, with or without | |
| 7 | * modification, are permitted provided that the following conditions are met: | |
| 8 | * * Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * * Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the | |
| 14 | * names of its contributors may be used to endorse or promote products | |
| 15 | * derived from this software without specific prior written permission. | |
| 16 | * | |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 18 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 19 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | |
| 20 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR | |
| 21 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
| 22 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
| 23 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
| 24 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF | |
| 25 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING | |
| 26 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
| 27 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
| 28 | * | |
| 29 | * This software is Open Source Initiative approved Open Source Software. | |
| 30 | * Open Source Initative Approved is a trademark of the Open Source Initiative. | |
| 31 | * | |
| 32 | * This file is part of DrJava. Download the current version of this project | |
| 33 | * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/ | |
| 34 | * | |
| 35 | * END_COPYRIGHT_BLOCK*/ | |
| 36 | ||
| 37 | package edu.rice.cs.drjava.config; | |
| 38 | ||
| 39 | import edu.rice.cs.drjava.platform.PlatformFactory; | |
| 40 | import edu.rice.cs.util.UnexpectedException; | |
| 41 | import java.lang.reflect.Field; | |
| 42 | import javax.swing.KeyStroke; | |
| 43 | import java.awt.event.KeyEvent; | |
| 44 | import java.awt.Event; | |
| 45 | import java.util.HashMap; | |
| 46 | ||
| 47 | /** Class representing all configuration options with values of type KeyStroke. Only runs in the event thread, so no | |
| 48 | * synchronization is necessary (or advisable).*/ | |
| 49 | public class KeyStrokeOption extends Option<KeyStroke> { | |
| 50 | ||
| 51 | /** Storage for keystrokes.*/ | |
| 52 | static HashMap<Integer, String> keys = new HashMap<Integer, String>(); | |
| 53 | public static final KeyStroke NULL_KEYSTROKE = KeyStroke.getKeyStroke(0, 0); | |
| 54 | /** Standard constructor | |
| 55 | * @param key The name of this option. | |
| 56 | */ | |
| 57 | 15566 | public KeyStrokeOption(String key, KeyStroke def) { super(key,def); } |
| 58 | ||
| 59 | // This sets up the hashtable that has key-value pairs consisting of | |
| 60 | // ascii codes and Strings that describe the ascii character and are | |
| 61 | // in the form that KeyStroke.getKeyStroke(String s) requires. | |
| 62 | static { | |
| 63 | 118 | try { |
| 64 | 118 | Field[] fields = KeyEvent.class.getFields(); |
| 65 | 118 | for (int i = 0; i < fields.length; i++) { |
| 66 | 28556 | Field currfield = fields[i]; |
| 67 | 28556 | String name = currfield.getName(); |
| 68 | 28556 | if (name.startsWith("VK_")) { |
| 69 | 22302 | keys.put(Integer.valueOf(currfield.getInt(null)), name.substring(3)); |
| 70 | } | |
| 71 | } | |
| 72 | } | |
| 73 | catch(IllegalAccessException iae) { | |
| 74 | 0 | throw new UnexpectedException(iae); |
| 75 | } | |
| 76 | } | |
| 77 | ||
| 78 | ||
| 79 | /** @param s The String to be parsed; must be the string representation of the KeyStroke to be created. Uses the | |
| 80 | * method KeyStroke.getKeyStroke(String s) which returns a KeyStroke if the string is correctly formatted or null | |
| 81 | * otherwise. | |
| 82 | * @return The KeyStroke object corresponding to the input string "s". | |
| 83 | */ | |
| 84 | 115165 | public KeyStroke parse(String s) { |
| 85 | 1 | if (s.equals("<none>")) { return NULL_KEYSTROKE; } |
| 86 | ||
| 87 | // Replace "command" with "meta" (OS X) | |
| 88 | 115164 | int cIndex = s.indexOf("command"); |
| 89 | 115164 | if (cIndex > -1) { |
| 90 | 0 | final StringBuilder sb = new StringBuilder(s.substring(0, cIndex)); |
| 91 | 0 | sb.append("meta"); |
| 92 | 0 | sb.append(s.substring(cIndex + "command".length(), s.length())); |
| 93 | 0 | s = sb.toString(); |
| 94 | } | |
| 95 | ||
| 96 | // Replace "option" with "alt" (OS X) | |
| 97 | 115164 | int oIndex = s.indexOf("option"); |
| 98 | 115164 | if (oIndex > -1) { |
| 99 | 0 | final StringBuilder sb = new StringBuilder(s.substring(0, oIndex)); |
| 100 | 0 | sb.append("alt"); |
| 101 | 0 | sb.append(s.substring(oIndex + "option".length(), s.length())); |
| 102 | 0 | s = sb.toString(); |
| 103 | } | |
| 104 | ||
| 105 | 115164 | KeyStroke ks = KeyStroke.getKeyStroke(s); |
| 106 | 115164 | if (ks == null) { |
| 107 | 4 | throw new OptionParseException(name, s, "Must be a valid string representation of a Keystroke."); |
| 108 | } | |
| 109 | 115160 | return ks; |
| 110 | } | |
| 111 | ||
| 112 | /** @param k The instance of class KeyStroke to be formatted. | |
| 113 | * @return A String representing the KeyStroke "k". | |
| 114 | */ | |
| 115 | 11486 | public String format(KeyStroke k) { return formatKeyStroke(k); } |
| 116 | ||
| 117 | /** @param k The instance of class KeyStroke to be formatted. | |
| 118 | * @return A String representing the KeyStroke "k". | |
| 119 | */ | |
| 120 | 11486 | public static String formatKeyStroke(KeyStroke k) { |
| 121 | 11486 | if (k == NULL_KEYSTROKE) { |
| 122 | 4 | return "<none>"; |
| 123 | } | |
| 124 | ||
| 125 | // This code prints out locale specific text, which is bad! | |
| 126 | // (KeyStroke.getKeystroke(s) can't parse it.) | |
| 127 | /* | |
| 128 | StringBuffer buf = new StringBuffer(); | |
| 129 | String s = KeyEvent.getKeyModifiersText(k.getModifiers()).toLowerCase(); | |
| 130 | s = s.replace('+', ' '); | |
| 131 | if (!s.equals("")) | |
| 132 | s += " "; | |
| 133 | buf.append(s); | |
| 134 | */ | |
| 135 | ||
| 136 | // Generate modifiers text on our own, since getKeyStroke can't parse locale-specific modifiers. | |
| 137 | 11482 | int modifiers = k.getModifiers(); |
| 138 | 11482 | boolean isMac = PlatformFactory.ONLY.isMacPlatform(); |
| 139 | 11482 | final StringBuilder buf = new StringBuilder(); |
| 140 | 11482 | if ((modifiers & Event.META_MASK) > 0) { |
| 141 | 2 | String meta = (! isMac) ? "meta " : "command "; |
| 142 | 2 | buf.append(meta); |
| 143 | } | |
| 144 | 11482 | if ((modifiers & Event.CTRL_MASK) > 0) { |
| 145 | 7264 | buf.append("ctrl "); |
| 146 | } | |
| 147 | 11482 | if ((modifiers & Event.ALT_MASK) > 0) { |
| 148 | 823 | String alt = (!isMac) ? "alt " : "option "; |
| 149 | 823 | buf.append(alt); |
| 150 | } | |
| 151 | 11482 | if ((modifiers & Event.SHIFT_MASK) > 0) { |
| 152 | 5158 | buf.append("shift "); |
| 153 | } | |
| 154 | ||
| 155 | // If the key code is undefined, this is a "typed" unicode character | |
| 156 | 11482 | if (k.getKeyCode() == KeyEvent.VK_UNDEFINED) { |
| 157 | 2 | buf.append("typed "); |
| 158 | 2 | buf.append(k.getKeyChar()); |
| 159 | } | |
| 160 | // else this corresponds to a static KeyEvent constant | |
| 161 | else { | |
| 162 | // defaults to pressed | |
| 163 | 11480 | if (k.isOnKeyRelease()) { |
| 164 | 1 | buf.append("released "); |
| 165 | } | |
| 166 | 11480 | String key = keys.get(Integer.valueOf(k.getKeyCode())); |
| 167 | 11480 | if (key == null) { |
| 168 | 0 | throw new IllegalArgumentException("Invalid keystroke"); |
| 169 | } | |
| 170 | 11480 | if (key.equals("CONTROL") || key.equals("ALT") || key.equals("META") || |
| 171 | key.equals("SHIFT") || key.equals("ALT_GRAPH")) { | |
| 172 | 0 | return buf.toString(); |
| 173 | } | |
| 174 | else { | |
| 175 | 11480 | buf.append(key); |
| 176 | 11480 | return buf.toString(); |
| 177 | } | |
| 178 | } | |
| 179 | 2 | return buf.toString(); |
| 180 | } | |
| 181 | } |
|
||||||||||