Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/Viewer.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 |
comparison
equal
deleted
inserted
replaced
11:43904fec5dca | 12:bc29606a740c |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2000, 2007 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 module org.eclipse.jface.viewers.Viewer; | |
14 | |
15 import org.eclipse.jface.viewers.IInputSelectionProvider; | |
16 import org.eclipse.jface.viewers.ISelectionChangedListener; | |
17 import org.eclipse.jface.viewers.SelectionChangedEvent; | |
18 import org.eclipse.jface.viewers.ISelection; | |
19 | |
20 import org.eclipse.swt.events.HelpEvent; | |
21 import org.eclipse.swt.events.HelpListener; | |
22 import org.eclipse.swt.widgets.Control; | |
23 import org.eclipse.swt.widgets.Item; | |
24 import org.eclipse.core.runtime.Assert; | |
25 import org.eclipse.core.runtime.ListenerList; | |
26 import org.eclipse.jface.util.SafeRunnable; | |
27 | |
28 import java.lang.all; | |
29 import java.util.List; | |
30 import java.util.Set; | |
31 | |
32 /** | |
33 * A viewer is a model-based adapter on a widget. | |
34 * <p> | |
35 * A viewer can be created as an adapter on a pre-existing control (e.g., | |
36 * creating a <code>ListViewer</code> on an existing <code>List</code> control). | |
37 * All viewers also provide a convenience constructor for creating the control. | |
38 * </p> | |
39 * <p> | |
40 * Implementing a concrete viewer typically involves the following steps: | |
41 * <ul> | |
42 * <li> | |
43 * create SWT controls for viewer (in constructor) (optional) | |
44 * </li> | |
45 * <li> | |
46 * initialize SWT controls from input (inputChanged) | |
47 * </li> | |
48 * <li> | |
49 * define viewer-specific update methods | |
50 * </li> | |
51 * <li> | |
52 * support selections (<code>setSelection</code>, <code>getSelection</code>) | |
53 * </li> | |
54 * </ul> | |
55 * </p> | |
56 */ | |
57 public abstract class Viewer : IInputSelectionProvider { | |
58 | |
59 /** | |
60 * List of selection change listeners (element type: <code>ISelectionChangedListener</code>). | |
61 * | |
62 * @see #fireSelectionChanged | |
63 */ | |
64 private ListenerList selectionChangedListeners; | |
65 | |
66 /** | |
67 * List of help request listeners (element type: <code>org.eclipse.swt.events.HelpListener</code>). | |
68 * Help request listeners. | |
69 * | |
70 * @see #handleHelpRequest | |
71 */ | |
72 private ListenerList helpListeners; | |
73 | |
74 /** | |
75 * The names of this viewer's properties. | |
76 * <code>null</code> if this viewer has no properties. | |
77 * | |
78 * @see #setData | |
79 */ | |
80 private String[] keys; | |
81 | |
82 /** | |
83 * The values of this viewer's properties. | |
84 * <code>null</code> if this viewer has no properties. | |
85 * This array parallels the value of the <code>keys</code> field. | |
86 * | |
87 * @see #setData | |
88 */ | |
89 private Object[] values; | |
90 | |
91 /** | |
92 * Remembers whether we've hooked the help listener on the control or not. | |
93 */ | |
94 private bool helpHooked = false; | |
95 | |
96 /** | |
97 * Help listener for the control, created lazily when client's first help listener is added. | |
98 */ | |
99 private HelpListener helpListener = null; | |
100 | |
101 /** | |
102 * Unique key for associating element data with widgets. | |
103 * @see org.eclipse.swt.widgets.Widget#setData(String, Object) | |
104 */ | |
105 protected static const String WIDGET_DATA_KEY = "org.eclipse.jface.viewers.WIDGET_DATA";//$NON-NLS-1$ | |
106 | |
107 /** | |
108 * Creates a new viewer. | |
109 */ | |
110 protected this() { | |
111 selectionChangedListeners = new ListenerList(); | |
112 helpListeners = new ListenerList(); | |
113 } | |
114 | |
115 /** | |
116 * Adds a listener for help requests in this viewer. | |
117 * Has no effect if an identical listener is already registered. | |
118 * | |
119 * @param listener a help listener | |
120 */ | |
121 public void addHelpListener(HelpListener listener) { | |
122 helpListeners.add(cast(Object)listener); | |
123 if (!helpHooked) { | |
124 Control control = getControl(); | |
125 if (control !is null && !control.isDisposed()) { | |
126 if (this.helpListener is null) { | |
127 this.helpListener = new class HelpListener { | |
128 public void helpRequested(HelpEvent event) { | |
129 handleHelpRequest(event); | |
130 } | |
131 }; | |
132 } | |
133 control.addHelpListener(this.helpListener); | |
134 helpHooked = true; | |
135 } | |
136 } | |
137 } | |
138 | |
139 /* (non-Javadoc) | |
140 * Method declared on ISelectionProvider. | |
141 */ | |
142 public void addSelectionChangedListener(ISelectionChangedListener listener) { | |
143 selectionChangedListeners.add(cast(Object)listener); | |
144 } | |
145 | |
146 /** | |
147 * Notifies any help listeners that help has been requested. | |
148 * Only listeners registered at the time this method is called are notified. | |
149 * | |
150 * @param event a help event | |
151 * | |
152 * @see HelpListener#helpRequested(org.eclipse.swt.events.HelpEvent) | |
153 */ | |
154 protected void fireHelpRequested(HelpEvent event) { | |
155 Object[] listeners = helpListeners.getListeners(); | |
156 for (int i = 0; i < listeners.length; ++i) { | |
157 (cast(HelpListener) listeners[i]).helpRequested(event); | |
158 } | |
159 } | |
160 | |
161 /** | |
162 * Notifies any selection changed listeners that the viewer's selection has changed. | |
163 * Only listeners registered at the time this method is called are notified. | |
164 * | |
165 * @param event a selection changed event | |
166 * | |
167 * @see ISelectionChangedListener#selectionChanged | |
168 */ | |
169 protected void fireSelectionChanged(SelectionChangedEvent event) { | |
170 Object[] listeners = selectionChangedListeners.getListeners(); | |
171 for (int i = 0; i < listeners.length; ++i) { | |
172 SafeRunnable.run(new class(event,cast(ISelectionChangedListener) listeners[i]) SafeRunnable { | |
173 ISelectionChangedListener l; | |
174 SelectionChangedEvent event_; | |
175 this(SelectionChangedEvent a,ISelectionChangedListener b){ | |
176 event_=a; | |
177 l = b; | |
178 } | |
179 public void run() { | |
180 l.selectionChanged(event_); | |
181 } | |
182 }); | |
183 } | |
184 } | |
185 | |
186 /** | |
187 * Returns the primary control associated with this viewer. | |
188 * | |
189 * @return the SWT control which displays this viewer's content | |
190 */ | |
191 public abstract Control getControl(); | |
192 | |
193 /** | |
194 * Returns the value of the property with the given name, | |
195 * or <code>null</code> if the property is not found. | |
196 * <p> | |
197 * The default implementation performs a (linear) search of | |
198 * an internal table. Overriding this method is generally not | |
199 * required if the number of different keys is small. If a more | |
200 * efficient representation of a viewer's properties is required, | |
201 * override both <code>getData</code> and <code>setData</code>. | |
202 * </p> | |
203 * | |
204 * @param key the property name | |
205 * @return the property value, or <code>null</code> if | |
206 * the property is not found | |
207 */ | |
208 public Object getData(String key) { | |
209 Assert.isNotNull(key); | |
210 if (keys is null) { | |
211 return null; | |
212 } | |
213 for (int i = 0; i < keys.length; i++) { | |
214 if (keys[i].equals(key)) { | |
215 return values[i]; | |
216 } | |
217 } | |
218 return null; | |
219 } | |
220 | |
221 /* (non-Javadoc) | |
222 * Copy-down of method declared on <code>IInputProvider</code>. | |
223 */ | |
224 public abstract Object getInput(); | |
225 | |
226 /* (non-Javadoc) | |
227 * Copy-down of method declared on <code>ISelectionProvider</code>. | |
228 */ | |
229 public abstract ISelection getSelection(); | |
230 | |
231 /** | |
232 * Handles a help request from the underlying SWT control. | |
233 * The default behavior is to fire a help request, | |
234 * with the event's data modified to hold this viewer. | |
235 * @param event the event | |
236 * | |
237 */ | |
238 protected void handleHelpRequest(HelpEvent event) { | |
239 Object oldData = event.data; | |
240 event.data = this; | |
241 fireHelpRequested(event); | |
242 event.data = oldData; | |
243 } | |
244 | |
245 /** | |
246 * Internal hook method called when the input to this viewer is | |
247 * initially set or subsequently changed. | |
248 * <p> | |
249 * The default implementation does nothing. Subclassers may override | |
250 * this method to do something when a viewer's input is set. | |
251 * A typical use is populate the viewer. | |
252 * </p> | |
253 * | |
254 * @param input the new input of this viewer, or <code>null</code> if none | |
255 * @param oldInput the old input element or <code>null</code> if there | |
256 * was previously no input | |
257 */ | |
258 protected void inputChanged(Object input, Object oldInput) { | |
259 } | |
260 | |
261 /** | |
262 * Refreshes this viewer completely with information freshly obtained from this | |
263 * viewer's model. | |
264 */ | |
265 public abstract void refresh(); | |
266 | |
267 /** | |
268 * Removes the given help listener from this viewer. | |
269 * Has no affect if an identical listener is not registered. | |
270 * | |
271 * @param listener a help listener | |
272 */ | |
273 public void removeHelpListener(HelpListener listener) { | |
274 helpListeners.remove(cast(Object)listener); | |
275 if (helpListeners.size() is 0) { | |
276 Control control = getControl(); | |
277 if (control !is null && !control.isDisposed()) { | |
278 control.removeHelpListener(this.helpListener); | |
279 helpHooked = false; | |
280 } | |
281 } | |
282 } | |
283 | |
284 /* (non-Javadoc) | |
285 * Method declared on ISelectionProvider. | |
286 */ | |
287 public void removeSelectionChangedListener( | |
288 ISelectionChangedListener listener) { | |
289 selectionChangedListeners.remove(cast(Object)listener); | |
290 } | |
291 | |
292 /** | |
293 * Scrolls the viewer's control down by one item from the given | |
294 * display-relative coordinates. Returns the newly revealed Item, | |
295 * or <code>null</code> if no scrolling occurred or if the viewer | |
296 * doesn't represent an item-based widget. | |
297 * | |
298 * @param x horizontal coordinate | |
299 * @param y vertical coordinate | |
300 * @return the item scrolled down to | |
301 */ | |
302 public Item scrollDown(int x, int y) { | |
303 return null; | |
304 } | |
305 | |
306 /** | |
307 * Scrolls the viewer's control up by one item from the given | |
308 * display-relative coordinates. Returns the newly revealed Item, | |
309 * or <code>null</code> if no scrolling occurred or if the viewer | |
310 * doesn't represent an item-based widget. | |
311 * | |
312 * @param x horizontal coordinate | |
313 * @param y vertical coordinate | |
314 * @return the item scrolled up to | |
315 */ | |
316 public Item scrollUp(int x, int y) { | |
317 return null; | |
318 } | |
319 | |
320 /** | |
321 * Sets the value of the property with the given name to the | |
322 * given value, or to <code>null</code> if the property is to be | |
323 * removed. If this viewer has such a property, its value is | |
324 * replaced; otherwise a new property is added. | |
325 * <p> | |
326 * The default implementation records properties in an internal | |
327 * table which is searched linearly. Overriding this method is generally not | |
328 * required if the number of different keys is small. If a more | |
329 * efficient representation of a viewer's properties is required, | |
330 * override both <code>getData</code> and <code>setData</code>. | |
331 * </p> | |
332 * | |
333 * @param key the property name | |
334 * @param value the property value, or <code>null</code> if | |
335 * the property is not found | |
336 */ | |
337 public void setData(String key, Object value) { | |
338 Assert.isNotNull(key); | |
339 /* Remove the key/value pair */ | |
340 if (value is null) { | |
341 if (keys is null) { | |
342 return; | |
343 } | |
344 int index = 0; | |
345 while (index < keys.length && !keys[index].equals(key)) { | |
346 index++; | |
347 } | |
348 if (index is keys.length) { | |
349 return; | |
350 } | |
351 if (keys.length is 1) { | |
352 keys = null; | |
353 values = null; | |
354 } else { | |
355 String[] newKeys = new String[keys.length - 1]; | |
356 Object[] newValues = new Object[values.length - 1]; | |
357 System.arraycopy(keys, 0, newKeys, 0, index); | |
358 System.arraycopy(keys, index + 1, newKeys, index, | |
359 newKeys.length - index); | |
360 System.arraycopy(values, 0, newValues, 0, index); | |
361 System.arraycopy(values, index + 1, newValues, index, | |
362 newValues.length - index); | |
363 keys = newKeys; | |
364 values = newValues; | |
365 } | |
366 return; | |
367 } | |
368 | |
369 /* Add the key/value pair */ | |
370 if (keys is null) { | |
371 keys = [ key ]; | |
372 values = [ value ]; | |
373 return; | |
374 } | |
375 for (int i = 0; i < keys.length; i++) { | |
376 if (keys[i].equals(key)) { | |
377 values[i] = value; | |
378 return; | |
379 } | |
380 } | |
381 String[] newKeys = new String[](keys.length + 1); | |
382 Object[] newValues = new Object[](values.length + 1); | |
383 System.arraycopy(keys, 0, newKeys, 0, keys.length); | |
384 System.arraycopy(values, 0, newValues, 0, values.length); | |
385 newKeys[keys.length] = key; | |
386 newValues[values.length] = value; | |
387 keys = newKeys; | |
388 values = newValues; | |
389 } | |
390 | |
391 /** | |
392 * Sets or clears the input for this viewer. | |
393 * | |
394 * @param input the input of this viewer, or <code>null</code> if none | |
395 */ | |
396 public abstract void setInput(Object input); | |
397 | |
398 /** | |
399 * The viewer implementation of this <code>ISelectionProvider</code> | |
400 * method make the new selection for this viewer without making it visible. | |
401 * <p> | |
402 * This method is equivalent to <code>setSelection(selection,false)</code>. | |
403 * </p> | |
404 * <p> | |
405 * Note that some implementations may not be able to set the selection | |
406 * without also revealing it, for example (as of 3.3) TreeViewer. | |
407 * </p> | |
408 */ | |
409 public void setSelection(ISelection selection) { | |
410 setSelection(selection, false); | |
411 } | |
412 | |
413 /** | |
414 * Sets a new selection for this viewer and optionally makes it visible. | |
415 * <p> | |
416 * Subclasses must implement this method. | |
417 * </p> | |
418 * | |
419 * @param selection the new selection | |
420 * @param reveal <code>true</code> if the selection is to be made | |
421 * visible, and <code>false</code> otherwise | |
422 */ | |
423 public abstract void setSelection(ISelection selection, bool reveal); | |
424 } |