1 33 34 package edu.rice.cs.drjava.config; 35 36 import edu.rice.cs.drjava.platform.PlatformFactory; 37 import edu.rice.cs.util.UnexpectedException; 38 import java.lang.reflect.Field ; 39 import javax.swing.KeyStroke ; 40 import java.awt.event.KeyEvent ; 41 import java.awt.Event ; 42 import java.util.Hashtable ; 43 44 46 public class KeyStrokeOption extends Option<KeyStroke > { 47 48 49 static Hashtable <Integer , String > keys = new Hashtable <Integer , String >(); 50 public static final KeyStroke NULL_KEYSTROKE = KeyStroke.getKeyStroke(0, 0); 51 54 public KeyStrokeOption(String key, KeyStroke def) { 55 super(key,def); } 56 57 static { 61 try { 62 Field [] fields = KeyEvent .class.getFields(); 63 for (int i = 0; i < fields.length; i++) { 64 Field currfield = fields[i]; 65 String name = currfield.getName(); 66 if (name.startsWith("VK_")) { 67 keys.put(new Integer (currfield.getInt(null)), name.substring(3)); 68 } 69 } 70 } 71 catch(IllegalAccessException iae) { 72 throw new UnexpectedException(iae); 73 } 74 } 75 76 77 83 public KeyStroke parse(String s) { 84 if (s.equals("<none>")) { return NULL_KEYSTROKE; } 85 86 int cIndex = s.indexOf("command"); 88 if (cIndex > -1) { 89 final StringBuilder sb = new StringBuilder (s.substring(0, cIndex)); 90 sb.append("meta"); 91 sb.append(s.substring(cIndex + "command".length(), s.length())); 92 s = sb.toString(); 93 } 94 95 int oIndex = s.indexOf("option"); 97 if (oIndex > -1) { 98 final StringBuilder sb = new StringBuilder (s.substring(0, oIndex)); 99 sb.append("alt"); 100 sb.append(s.substring(oIndex + "option".length(), s.length())); 101 s = sb.toString(); 102 } 103 104 KeyStroke ks = KeyStroke.getKeyStroke(s); 105 if (ks == null) { 106 throw new OptionParseException(name, s, 107 "Must be a valid string representation of a Keystroke."); 108 } 109 return ks; 110 } 111 112 115 public String format(KeyStroke k) { 116 if (k == NULL_KEYSTROKE) { 117 return "<none>"; 118 } 119 120 130 131 int modifiers = k.getModifiers(); 133 boolean isMac = PlatformFactory.ONLY.isMacPlatform(); 134 final StringBuilder buf = new StringBuilder (); 135 if ((modifiers & Event.META_MASK) > 0) { 136 String meta = (! isMac) ? "meta " : "command "; 137 buf.append(meta); 138 } 139 if ((modifiers & Event.CTRL_MASK) > 0) { 140 buf.append("ctrl "); 141 } 142 if ((modifiers & Event.ALT_MASK) > 0) { 143 String alt = (!isMac) ? "alt " : "option "; 144 buf.append(alt); 145 } 146 if ((modifiers & Event.SHIFT_MASK) > 0) { 147 buf.append("shift "); 148 } 149 150 if (k.getKeyCode() == KeyEvent.VK_UNDEFINED) { 152 buf.append("typed "); 153 buf.append(k.getKeyChar()); 154 } 155 else { 157 if (k.isOnKeyRelease()) { 159 buf.append("released "); 160 } 161 String key = keys.get(new Integer (k.getKeyCode())); 162 if (key == null) { 163 throw new IllegalArgumentException ("Invalid keystroke"); 164 } 165 if (key.equals("CONTROL") || key.equals("ALT") || key.equals("META") || 166 key.equals("SHIFT") || key.equals("ALT_GRAPH")) { 167 return buf.toString(); 168 } 169 else { 170 buf.append(key); 171 return buf.toString(); 172 } 173 } 174 return buf.toString(); 175 } 176 } 177 | Popular Tags |