1 19 20 package org.netbeans.api.editor.settings; 21 22 import java.util.List ; 23 import javax.swing.KeyStroke ; 24 25 30 31 public final class MultiKeyBinding { 32 33 36 private List <KeyStroke > keyStrokeList; 37 38 41 private String actionName; 42 43 44 51 public MultiKeyBinding(KeyStroke [] keyStrokes, String actionName) { 52 if (keyStrokes == null) { 53 throw new NullPointerException ("keyStrokes cannot be null"); } 55 if (actionName == null) { 56 throw new NullPointerException ("actionName cannot be null"); } 58 this.keyStrokeList = new UnmodifiableArrayList<KeyStroke >(keyStrokes); 59 this.actionName = actionName; 60 } 61 62 65 public MultiKeyBinding(KeyStroke keyStroke, String actionName) { 66 this(new KeyStroke [] { keyStroke }, actionName); 67 if (keyStroke == null) { 68 throw new NullPointerException ("keyStroke cannot be null"); } 70 } 71 72 79 public KeyStroke getKeyStroke(int index) { 80 return (KeyStroke )keyStrokeList.get(index); 81 } 82 83 88 public int getKeyStrokeCount() { 89 return keyStrokeList.size(); 90 } 91 92 97 public List <KeyStroke > getKeyStrokeList() { 98 return keyStrokeList; 99 } 100 101 107 public String getActionName() { 108 return actionName; 109 } 110 111 115 public boolean equals(Object o) { 116 if (o instanceof MultiKeyBinding) { 117 MultiKeyBinding kb = (MultiKeyBinding)o; 118 119 if (actionName == null) { 121 if (kb.actionName != null) { 122 return false; 123 } 124 } else { 125 if (!actionName.equals(kb.actionName)) { 126 return false; 127 } 128 } 129 130 return keyStrokeList.equals(kb.keyStrokeList); 132 } 133 return false; 134 } 135 136 public int hashCode() { 137 int result = 17; 138 for (int i = 0; i < getKeyStrokeCount(); i++){ 139 result = 37*result + getKeyStroke(i).hashCode(); 140 } 141 if (actionName != null) { 142 result = 37*result + actionName.hashCode(); 143 } 144 return result; 145 } 146 147 public String toString() { 148 StringBuffer sb = new StringBuffer (); 149 sb.append("keys("); for (KeyStroke ks : keyStrokeList) { 151 sb.append(ks); 152 } 153 sb.append("), actionName="); sb.append(actionName); 155 return sb.toString(); 156 } 157 158 private static final class UnmodifiableArrayList<E> extends java.util.AbstractList <E> 159 implements java.util.RandomAccess , java.io.Serializable { 160 161 private static final long serialVersionUID = 0L; 162 163 private E[] array; 164 165 UnmodifiableArrayList(E[] array) { 166 if (array == null) { 167 throw new NullPointerException (); 168 } 169 this.array = array; 170 } 171 172 public int size() { 173 return array.length; 174 } 175 176 public Object [] toArray() { 177 return (Object []) array.clone(); 178 } 179 180 public E get(int index) { 181 return array[index]; 182 } 183 184 public int indexOf(Object o) { 185 if (o == null) { 186 for (int i = 0; i < array.length; i++) 187 if (array[i] == null) 188 return i; 189 } else { 190 for (int i = 0; i < array.length; i++) 191 if (o.equals(array[i])) 192 return i; 193 } 194 return -1; 195 } 196 197 public boolean contains(Object o) { 198 return indexOf(o) != -1; 199 } 200 201 public boolean equals(Object o) { 202 return (o instanceof UnmodifiableArrayList) 203 ? java.util.Arrays.equals(this.array, ((UnmodifiableArrayList)o).array) 204 : super.equals(o); 205 } 206 207 } 208 209 } 210 | Popular Tags |