1 19 20 package org.netbeans.editor; 21 22 import java.util.ArrayList ; 23 import java.util.Arrays ; 24 import javax.swing.text.JTextComponent ; 25 import javax.swing.KeyStroke ; 26 import javax.swing.Action ; 27 28 35 36 public class MultiKeyBinding extends JTextComponent.KeyBinding 37 implements java.io.Externalizable { 38 39 42 public KeyStroke [] keys; 43 44 static final long serialVersionUID =-8602816556604003688L; 45 46 47 public MultiKeyBinding() { 48 super(null, null); 49 } 50 51 57 public MultiKeyBinding(KeyStroke [] keys, String actionName) { 58 super(null, actionName); 59 this.keys = keys; 60 } 61 62 63 public MultiKeyBinding(KeyStroke key, String actionName) { 64 super(key, actionName); 65 } 66 67 68 public MultiKeyBinding(JTextComponent.KeyBinding kb) { 69 this(kb.key, kb.actionName); 70 } 71 72 public boolean equals(Object o) { 73 if (o instanceof MultiKeyBinding) { 74 MultiKeyBinding kb = (MultiKeyBinding)o; 75 76 if (actionName == null) { 78 if (kb.actionName != null) { 79 return false; 80 } 81 } else { 82 if (!actionName.equals(kb.actionName)) { 83 return false; 84 } 85 } 86 87 if (keys == null) { 89 if (kb.keys == null) { 90 return (key == null && kb.key == null) 91 || (key != null && key.equals(kb.key)); 92 } else { 93 return (kb.keys.length == 1 94 && ((key == null && kb.keys[0] == null) 95 || (key != null && key.equals(kb.keys[0])))); 96 } 97 } else { if (kb.keys != null) { 99 return Arrays.equals(keys, kb.keys); 100 } else { return (keys.length == 1 102 && ((kb.key == null && keys[0] == null) 103 || (kb.key != null && kb.key.equals(keys[0])))); 104 } 105 } 106 } 107 return false; 108 } 109 110 122 public static void updateKeyBindings(JTextComponent.KeyBinding [] target, 123 JTextComponent.KeyBinding [] changes) { 124 ArrayList tgt = new ArrayList (Arrays.asList(target)); 125 MultiKeyBinding tmp = new MultiKeyBinding(new KeyStroke [1], null); 126 MultiKeyBinding cur; 127 for (int i = 0; i < changes.length; i++) { 128 if (changes[i] instanceof MultiKeyBinding) { 129 cur = (MultiKeyBinding)changes[i]; 130 if (cur.keys == null) { tmp.keys[0] = cur.key; 132 tmp.actionName = cur.actionName; 133 cur = tmp; 134 } 135 } else { tmp.keys[0] = changes[i].key; 137 tmp.actionName = changes[i].actionName; 138 cur = tmp; 139 } 140 boolean matched = false; 142 for (int j = 0; j < tgt.size(); j++) { 143 JTextComponent.KeyBinding kb = (JTextComponent.KeyBinding )tgt.get(j); 144 if (kb instanceof MultiKeyBinding) { 145 MultiKeyBinding mkb = (MultiKeyBinding)kb; 146 if (mkb.keys == null) { if (cur.keys.length == 1 && cur.keys[0].equals(mkb.key)) { if (mkb.actionName == null) { tgt.remove(i); 150 } else { tgt.set(i, mkb); 152 } 153 matched = true; 154 break; 155 } 156 } else { if (cur.keys.length == mkb.keys.length) { 158 matched = true; 159 for (int k = 0; k < cur.keys.length; k++) { 160 if (!cur.keys[k].equals(mkb.keys[k])) { 161 matched = false; 162 break; 163 } 164 } 165 if (matched) { 166 if (mkb.actionName == null) { tgt.remove(i); 168 } else { tgt.set(i, mkb); 170 } 171 break; 172 } 173 } 174 } 175 } else { if (cur.keys.length == 1 && cur.keys[0].equals(kb.key)) { if (kb.actionName == null) { tgt.remove(i); 179 } else { tgt.set(i, kb); 181 } 182 matched = true; 183 break; 184 } 185 } 186 } 187 if (!matched) { 188 tgt.add(changes[tgt.size()]); 189 } 190 } 191 } 192 193 public void readExternal(java.io.ObjectInput in) 194 throws java.io.IOException , ClassNotFoundException { 195 Object obj = in.readObject (); 196 197 if( obj instanceof Integer ) { int len = ((Integer )obj).intValue(); 199 if( len >= 0 ) { 200 keys = new KeyStroke [ len ]; 201 for( int i=0; i<len; i++ ) { 202 keys[i] = KeyStroke.getKeyStroke( in.readInt(), in.readInt(), in.readBoolean() ); 203 } 204 } else { 205 keys = null; 206 } 207 208 if( in.readBoolean() ) { 209 key = KeyStroke.getKeyStroke( in.readInt(), in.readInt(), in.readBoolean() ); 210 } else { 211 key = null; 212 } 213 214 actionName = (String )in.readObject(); 215 216 } else { keys = (KeyStroke [])obj; 218 key = (KeyStroke )in.readObject(); 219 actionName = (String )in.readObject(); 220 } 221 } 222 223 public void writeExternal(java.io.ObjectOutput out) 224 throws java.io.IOException { 225 226 if( keys != null ) { 227 out.writeObject( new Integer ( keys.length ) ); 228 for( int i=0; i<keys.length; i++ ) { 229 out.writeInt( keys[i].getKeyCode() ); 230 out.writeInt( keys[i].getModifiers() ); 231 out.writeBoolean( keys[i].isOnKeyRelease() ); 232 } 233 } else { 234 out.writeObject( new Integer ( -1 ) ); 235 } 236 237 if( key != null ) { 238 out.writeBoolean( true ); 239 out.writeInt( key.getKeyCode() ); 240 out.writeInt( key.getModifiers() ); 241 out.writeBoolean( key.isOnKeyRelease() ); 242 } else { 243 out.writeBoolean( false ); 244 } 245 out.writeObject( actionName ); 246 } 247 248 public String toString() { 249 if (keys == null) { 250 return "key=" + key + ", actionName=" + actionName; } else { 252 StringBuffer sb = new StringBuffer (); 253 for (int i = 0; i < keys.length; i++) { 254 sb.append("key"); sb.append(i); 256 sb.append('='); 257 sb.append(keys[i]); 258 sb.append(", "); } 260 sb.append("actionName="); sb.append(actionName); 262 return sb.toString(); 263 } 264 } 265 266 } 267 | Popular Tags |