Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/bindings/keys/KeySequence.d @ 12:bc29606a740c
Added dwt-addons in original directory structure of eclipse.org
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Sat, 14 Mar 2009 18:23:29 +0100 |
parents | |
children | 634e4380db78 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2004, 2006 IBM Corporation and others. | |
3 * All rights reserved. This program and the accompanying materials | |
4 * are made available under the terms of the Eclipse Public License v1.0 | |
5 * which accompanies this distribution, and is available at | |
6 * http://www.eclipse.org/legal/epl-v10.html | |
7 * | |
8 * Contributors: | |
9 * IBM Corporation - initial API and implementation | |
10 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 | |
14 module org.eclipse.jface.bindings.keys.KeySequence; | |
15 | |
16 import org.eclipse.jface.bindings.keys.KeyStroke; | |
17 | |
18 | |
19 import org.eclipse.jface.bindings.TriggerSequence; | |
20 import org.eclipse.jface.bindings.keys.formatting.KeyFormatterFactory; | |
21 | |
22 import org.eclipse.jface.util.Util; | |
23 | |
24 import java.lang.all; | |
25 import java.util.List; | |
26 import java.util.Set; | |
27 import java.text.ParseException; | |
28 static import tango.text.Util; | |
29 /** | |
30 * <p> | |
31 * A <code>KeySequence</code> is defined as a list of zero or more | |
32 * <code>KeyStrokes</code>, with the stipulation that all | |
33 * <code>KeyStroke</code> objects must be complete, save for the last one, | |
34 * whose completeness is optional. A <code>KeySequence</code> is said to be | |
35 * complete if all of its <code>KeyStroke</code> objects are complete. | |
36 * </p> | |
37 * <p> | |
38 * All <code>KeySequence</code> objects have a formal string representation | |
39 * available via the <code>toString()</code> method. There are a number of | |
40 * methods to get instances of <code>KeySequence</code> objects, including one | |
41 * which can parse this formal string representation. | |
42 * </p> | |
43 * <p> | |
44 * All <code>KeySequence</code> objects, via the <code>format()</code> | |
45 * method, provide a version of their formal string representation translated by | |
46 * platform and locale, suitable for display to a user. | |
47 * </p> | |
48 * <p> | |
49 * <code>KeySequence</code> objects are immutable. Clients are not permitted | |
50 * to extend this class. | |
51 * </p> | |
52 * | |
53 * @since 3.1 | |
54 */ | |
55 public final class KeySequence : TriggerSequence, Comparable { | |
56 | |
57 /** | |
58 * An empty key sequence instance for use by everyone. | |
59 */ | |
60 private const static KeySequence EMPTY_KEY_SEQUENCE; | |
61 | |
62 static this(){ | |
63 EMPTY_KEY_SEQUENCE = new KeySequence(null); | |
64 } | |
65 | |
66 /** | |
67 * The delimiter between multiple key strokes in a single key sequence -- | |
68 * expressed in the formal key stroke grammar. This is not to be displayed | |
69 * to the user. It is only intended as an internal representation. | |
70 */ | |
71 public const static String KEY_STROKE_DELIMITER = "\u0020"; //$NON-NLS-1$ | |
72 | |
73 /** | |
74 * The set of delimiters for <code>KeyStroke</code> objects allowed during | |
75 * parsing of the formal string representation. | |
76 */ | |
77 public const static String KEY_STROKE_DELIMITERS = KEY_STROKE_DELIMITER | |
78 ~ "\b\r\u007F\u001B\f\n\0\t\u000B"; //$NON-NLS-1$ | |
79 | |
80 /** | |
81 * Gets an instance of <code>KeySequence</code>. | |
82 * | |
83 * @return a key sequence. This key sequence will have no key strokes. | |
84 * Guaranteed not to be <code>null</code>. | |
85 */ | |
86 public static final KeySequence getInstance() { | |
87 return EMPTY_KEY_SEQUENCE; | |
88 } | |
89 | |
90 /** | |
91 * Creates an instance of <code>KeySequence</code> given a key sequence | |
92 * and a key stroke. | |
93 * | |
94 * @param keySequence | |
95 * a key sequence. Must not be <code>null</code>. | |
96 * @param keyStroke | |
97 * a key stroke. Must not be <code>null</code>. | |
98 * @return a key sequence that is equal to the given key sequence with the | |
99 * given key stroke appended to the end. Guaranteed not to be | |
100 * <code>null</code>. | |
101 */ | |
102 public static final KeySequence getInstance(KeySequence keySequence, | |
103 KeyStroke keyStroke) { | |
104 if (keySequence is null || keyStroke is null) { | |
105 throw new NullPointerException(); | |
106 } | |
107 | |
108 KeyStroke[] oldKeyStrokes = keySequence.getKeyStrokes(); | |
109 int oldKeyStrokeLength = oldKeyStrokes.length; | |
110 KeyStroke[] newKeyStrokes = new KeyStroke[oldKeyStrokeLength + 1]; | |
111 System.arraycopy(oldKeyStrokes, 0, newKeyStrokes, 0, | |
112 oldKeyStrokeLength); | |
113 newKeyStrokes[oldKeyStrokeLength] = keyStroke; | |
114 return new KeySequence(newKeyStrokes); | |
115 } | |
116 | |
117 /** | |
118 * Creates an instance of <code>KeySequence</code> given a single key | |
119 * stroke. | |
120 * | |
121 * @param keyStroke | |
122 * a single key stroke. Must not be <code>null</code>. | |
123 * @return a key sequence. Guaranteed not to be <code>null</code>. | |
124 */ | |
125 public static final KeySequence getInstance(KeyStroke keyStroke) { | |
126 return new KeySequence([ keyStroke ]); | |
127 } | |
128 | |
129 /** | |
130 * Creates an instance of <code>KeySequence</code> given an array of key | |
131 * strokes. | |
132 * | |
133 * @param keyStrokes | |
134 * the array of key strokes. This array may be empty, but it must | |
135 * not be <code>null</code>. This array must not contain | |
136 * <code>null</code> elements. | |
137 * @return a key sequence. Guaranteed not to be <code>null</code>. | |
138 */ | |
139 public static final KeySequence getInstance(KeyStroke[] keyStrokes) { | |
140 return new KeySequence(keyStrokes); | |
141 } | |
142 | |
143 /** | |
144 * Creates an instance of <code>KeySequence</code> given a list of key | |
145 * strokes. | |
146 * | |
147 * @param keyStrokes | |
148 * the list of key strokes. This list may be empty, but it must | |
149 * not be <code>null</code>. If this list is not empty, it | |
150 * must only contain instances of <code>KeyStroke</code>. | |
151 * @return a key sequence. Guaranteed not to be <code>null</code>. | |
152 */ | |
153 public static final KeySequence getInstance(List keyStrokes) { | |
154 return new KeySequence(arraycast!(KeyStroke)(keyStrokes.toArray())); | |
155 } | |
156 | |
157 /** | |
158 * Creates an instance of <code>KeySequence</code> by parsing a given | |
159 * formal string representation. | |
160 * | |
161 * @param string | |
162 * the formal string representation to parse. | |
163 * @return a key sequence. Guaranteed not to be <code>null</code>. | |
164 * @throws ParseException | |
165 * if the given formal string representation could not be parsed | |
166 * to a valid key sequence. | |
167 */ | |
168 public static final KeySequence getInstance(String string) { | |
169 if (string is null) { | |
170 throw new NullPointerException(); | |
171 } | |
172 | |
173 try { | |
174 auto tokens = tango.text.Util.delimit( string, KEY_STROKE_DELIMITERS ); | |
175 KeyStroke[] keyStrokeArray = new KeyStroke[ tokens.length ]; | |
176 foreach( idx, tok; tokens ){ | |
177 keyStrokeArray[idx] = KeyStroke.getInstance(tok); | |
178 } | |
179 return new KeySequence(keyStrokeArray); | |
180 } catch (IllegalArgumentException e) { | |
181 throw new ParseException( | |
182 "Could not construct key sequence with these key strokes: " //$NON-NLS-1$ | |
183 /+~ keyStrokes+/); | |
184 } catch (NullPointerException e) { | |
185 throw new ParseException( | |
186 "Could not construct key sequence with these key strokes: " //$NON-NLS-1$ | |
187 /+~ keyStrokes+/); | |
188 } | |
189 } | |
190 | |
191 /** | |
192 * Constructs an instance of <code>KeySequence</code> given a list of key | |
193 * strokes. | |
194 * | |
195 * @param keyStrokes | |
196 * the list of key strokes. This list may be empty, but it must | |
197 * not be <code>null</code>. If this list is not empty, it | |
198 * must only contain instances of <code>KeyStroke</code>. | |
199 */ | |
200 protected this(KeyStroke[] keyStrokes) { | |
201 super(keyStrokes); | |
202 | |
203 for (int i = 0; i < cast(int)triggers.length - 1; i++) { | |
204 KeyStroke keyStroke = cast(KeyStroke) triggers[i]; | |
205 | |
206 if (!keyStroke.isComplete()) { | |
207 throw new IllegalArgumentException(null); | |
208 } | |
209 } | |
210 } | |
211 | |
212 /* | |
213 * (non-Javadoc) | |
214 * | |
215 * @see java.lang.Object#compareTo(java.lang.Object) | |
216 */ | |
217 public final int compareTo(Object object) { | |
218 KeySequence castedObject = cast(KeySequence) object; | |
219 return Util.compare( arraycast!(Comparable)(triggers), arraycast!(Comparable)(castedObject.triggers)); | |
220 } | |
221 | |
222 /** | |
223 * Formats this key sequence into the current default look. | |
224 * | |
225 * @return A string representation for this key sequence using the default | |
226 * look; never <code>null</code>. | |
227 */ | |
228 public override final String format() { | |
229 return KeyFormatterFactory.getDefault().format(this); | |
230 } | |
231 | |
232 /** | |
233 * Returns the list of key strokes for this key sequence. | |
234 * | |
235 * @return the list of key strokes keys. This list may be empty, but is | |
236 * guaranteed not to be <code>null</code>. If this list is not | |
237 * empty, it is guaranteed to only contain instances of | |
238 * <code>KeyStroke</code>. | |
239 */ | |
240 public final KeyStroke[] getKeyStrokes() { | |
241 int triggerLength = triggers.length; | |
242 KeyStroke[] keyStrokes = new KeyStroke[triggerLength]; | |
243 System.arraycopy(triggers, 0, keyStrokes, 0, triggerLength); | |
244 return keyStrokes; | |
245 } | |
246 | |
247 /* | |
248 * (non-Javadoc) | |
249 * | |
250 * @see org.eclipse.jface.bindings.TriggerSequence#getPrefixes() | |
251 */ | |
252 public override final TriggerSequence[] getPrefixes() { | |
253 int numberOfPrefixes = triggers.length; | |
254 TriggerSequence[] prefixes = new TriggerSequence[numberOfPrefixes]; | |
255 prefixes[0] = KeySequence.getInstance(); | |
256 for (int i = 0; i < numberOfPrefixes - 1; i++) { | |
257 final KeyStroke[] prefixKeyStrokes = new KeyStroke[i + 1]; | |
258 System.arraycopy(triggers, 0, prefixKeyStrokes, 0, i + 1); | |
259 prefixes[i + 1] = KeySequence.getInstance(prefixKeyStrokes); | |
260 } | |
261 | |
262 return prefixes; | |
263 } | |
264 | |
265 /** | |
266 * Returns whether or not this key sequence is complete. Key sequences are | |
267 * complete iff all of their key strokes are complete. | |
268 * | |
269 * @return <code>true</code>, iff the key sequence is complete. | |
270 */ | |
271 public final bool isComplete() { | |
272 int triggersLength = triggers.length; | |
273 for (int i = 0; i < triggersLength; i++) { | |
274 if (!(cast(KeyStroke) triggers[i]).isComplete()) { | |
275 return false; | |
276 } | |
277 } | |
278 | |
279 return true; | |
280 } | |
281 | |
282 /** | |
283 * Returns the formal string representation for this key sequence. | |
284 * | |
285 * @return The formal string representation for this key sequence. | |
286 * Guaranteed not to be <code>null</code>. | |
287 * @see java.lang.Object#toString() | |
288 */ | |
289 public override final String toString() { | |
290 return KeyFormatterFactory.getFormalKeyFormatter().format(this); | |
291 } | |
292 } |