KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > editor > settings > MultiKeyBinding


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.api.editor.settings;
21
22 import java.util.List JavaDoc;
23 import javax.swing.KeyStroke JavaDoc;
24
25 /**
26  * Variant of <code>JTextComponent.KeyBinding</code> to hold several successive keystrokes.
27  *
28  * @author Miloslav Metelka, Martin Roskanin
29  */

30
31 public final class MultiKeyBinding {
32
33     /**
34      * List of the keystrokes that invoke the action.
35      */

36     private List JavaDoc<KeyStroke JavaDoc> keyStrokeList;
37
38     /**
39      * The name of the action to be invoked by the keyStrokes.
40      */

41     private String JavaDoc actionName;
42
43
44     /**
45      * Constructor for assigning keystroke sequence to action
46      *
47      * @param keyStrokes non-null successive keystrokes that must be pressed in order
48      * to invoke action. The passed array must not be modified by the caller.
49      * @param actionName non-null name of the action that will be invoked.
50      */

51     public MultiKeyBinding(KeyStroke JavaDoc[] keyStrokes, String JavaDoc actionName) {
52         if (keyStrokes == null) {
53             throw new NullPointerException JavaDoc("keyStrokes cannot be null"); // NOI18N
54
}
55         if (actionName == null) {
56             throw new NullPointerException JavaDoc("actionName cannot be null"); // NOI18N
57
}
58         this.keyStrokeList = new UnmodifiableArrayList<KeyStroke JavaDoc>(keyStrokes);
59         this.actionName = actionName;
60     }
61
62     /**
63      * Constructor for array single keystroke to action assignment.
64      */

65     public MultiKeyBinding(KeyStroke JavaDoc keyStroke, String JavaDoc actionName) {
66         this(new KeyStroke JavaDoc[] { keyStroke }, actionName);
67         if (keyStroke == null) {
68             throw new NullPointerException JavaDoc("keyStroke cannot be null"); // NOI18N
69
}
70     }
71     
72     /**
73      * Get the key of this keybinding at the given index of the keystroke sequence.
74      *
75      * @param index &gt;=0 and &lt;{@link #getKeyStrokeCount()} index.
76      * @return keystroke at the given index.
77      * @see #getKeyStrokeCount()
78      */

79     public KeyStroke JavaDoc getKeyStroke(int index) {
80         return (KeyStroke JavaDoc)keyStrokeList.get(index);
81     }
82     
83     /**
84      * Get total number of keystrokes contained in this keybinding sequence.
85      *
86      * @return &gt;=0 total count of keystrokes.
87      */

88     public int getKeyStrokeCount() {
89         return keyStrokeList.size();
90     }
91     
92     /**
93      * Get list of keystrokes represented by this keybinding.
94      *
95      * @return non-null unmodifiable list of the keystrokes.
96      */

97     public List JavaDoc<KeyStroke JavaDoc> getKeyStrokeList() {
98         return keyStrokeList;
99     }
100     
101     /**
102      * Get the name of the action which this keybinding triggers.
103      *
104      * @return action non-null name of the action triggered by the sequence
105      * of the keystrokes contained in this keybinding.
106      */

107     public String JavaDoc getActionName() {
108         return actionName;
109     }
110
111     /**
112      * Two keybindings are equal if and only if they represent
113      * the same keystrokes sequence and the same action name.
114      */

115     public boolean equals(Object JavaDoc o) {
116         if (o instanceof MultiKeyBinding) {
117             MultiKeyBinding kb = (MultiKeyBinding)o;
118
119             // Compare action names
120
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             // Action names match, now compare action keys
131
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 JavaDoc toString() {
148         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
149         sb.append("keys("); // NOI18N
150
for (KeyStroke JavaDoc ks : keyStrokeList) {
151             sb.append(ks);
152         }
153         sb.append("), actionName="); // NOI18N
154
sb.append(actionName);
155         return sb.toString();
156     }
157
158     private static final class UnmodifiableArrayList<E> extends java.util.AbstractList JavaDoc<E>
159     implements java.util.RandomAccess JavaDoc, java.io.Serializable JavaDoc {
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 JavaDoc();
168             }
169         this.array = array;
170     }
171
172     public int size() {
173         return array.length;
174     }
175
176     public Object JavaDoc[] toArray() {
177         return (Object JavaDoc[]) array.clone();
178     }
179
180     public E get(int index) {
181         return array[index];
182     }
183
184         public int indexOf(Object JavaDoc 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 JavaDoc o) {
198             return indexOf(o) != -1;
199         }
200         
201         public boolean equals(Object JavaDoc 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