Mercurial > projects > dwt-addons
annotate dwtx/jface/viewers/CheckboxTableViewer.d @ 104:04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
These new wrappers now use the tango.util.containers instead of the tango.util.collections.
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Thu, 07 Aug 2008 15:01:33 +0200 |
parents | 7ffeace6c47f |
children |
rev | line source |
---|---|
10 | 1 /******************************************************************************* |
90 | 2 * Copyright (c) 2000, 2008 IBM Corporation and others. |
10 | 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 dwtx.jface.viewers.CheckboxTableViewer; | |
14 | |
15 import dwtx.jface.viewers.ICheckable; | |
16 import dwtx.jface.viewers.TableViewer; | |
17 import dwtx.jface.viewers.ICheckStateListener; | |
18 import dwtx.jface.viewers.CheckStateChangedEvent; | |
19 import dwtx.jface.viewers.TableLayout; | |
20 import dwtx.jface.viewers.ColumnWeightData; | |
21 import dwtx.jface.viewers.CustomHashtable; | |
22 | |
23 | |
24 import dwt.DWT; | |
25 import dwt.events.SelectionEvent; | |
26 import dwt.widgets.Composite; | |
27 import dwt.widgets.Table; | |
28 import dwt.widgets.TableColumn; | |
29 import dwt.widgets.TableItem; | |
30 import dwt.widgets.Widget; | |
31 import dwtx.core.runtime.Assert; | |
32 import dwtx.core.runtime.ListenerList; | |
33 import dwtx.jface.util.SafeRunnable; | |
34 | |
35 import dwt.dwthelper.utils; | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
36 import dwtx.dwtxhelper.Collection; |
10 | 37 import dwt.dwthelper.Runnable; |
38 | |
39 /** | |
40 * A concrete viewer based on an DWT <code>Table</code> | |
41 * control with checkboxes on each node. | |
42 * <p> | |
43 * This class is not intended to be subclassed outside the viewer framework. | |
44 * It is designed to be instantiated with a pre-existing DWT table control and configured | |
45 * with a domain-specific content provider, label provider, element filter (optional), | |
46 * and element sorter (optional). | |
47 * </p> | |
90 | 48 * @noextend This class is not intended to be subclassed by clients. |
10 | 49 */ |
50 public class CheckboxTableViewer : TableViewer, ICheckable { | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
51 alias TableViewer.preservingSelection preservingSelection; |
10 | 52 |
53 /** | |
54 * List of check state listeners (element type: <code>ICheckStateListener</code>). | |
55 */ | |
56 private ListenerList checkStateListeners; | |
57 | |
58 /** | |
59 * Creates a table viewer on a newly-created table control under the given parent. | |
60 * The table control is created using the DWT style bits: | |
61 * <code>DWT.CHECK</code> and <code>DWT.BORDER</code>. | |
62 * The table has one column. | |
63 * The viewer has no input, no content provider, a default label provider, | |
64 * no sorter, and no filters. | |
65 * <p> | |
66 * This is equivalent to calling <code>new CheckboxTableViewer(parent, DWT.BORDER)</code>. | |
67 * See that constructor for more details. | |
68 * </p> | |
69 * | |
70 * @param parent the parent control | |
71 * | |
72 * @deprecated use newCheckList(Composite, int) or new CheckboxTableViewer(Table) | |
73 * instead (see below for details) | |
74 */ | |
75 public this(Composite parent) { | |
76 this(parent, DWT.BORDER); | |
77 } | |
78 | |
79 /** | |
80 * Creates a table viewer on a newly-created table control under the given parent. | |
81 * The table control is created using the given DWT style bits, plus the | |
82 * <code>DWT.CHECK</code> style bit. | |
83 * The table has one column. | |
84 * The viewer has no input, no content provider, a default label provider, | |
85 * no sorter, and no filters. | |
86 * <p> | |
87 * This also adds a <code>TableColumn</code> for the single column, | |
88 * and sets a <code>TableLayout</code> on the table which sizes the column to fill | |
89 * the table for its initial sizing, but does nothing on subsequent resizes. | |
90 * </p> | |
91 * <p> | |
92 * If the caller just needs to show a single column with no header, | |
93 * it is preferable to use the <code>newCheckList</code> factory method instead, | |
94 * since DWT properly handles the initial sizing and subsequent resizes in this case. | |
95 * </p> | |
96 * <p> | |
97 * If the caller adds its own columns, uses <code>Table.setHeadersVisible(true)</code>, | |
98 * or needs to handle dynamic resizing of the table, it is recommended to | |
99 * create the <code>Table</code> itself, specifying the <code>DWT.CHECK</code> style bit | |
100 * (along with any other style bits needed), and use <code>new CheckboxTableViewer(Table)</code> | |
101 * rather than this constructor. | |
102 * </p> | |
103 * | |
104 * @param parent the parent control | |
105 * @param style DWT style bits | |
106 * | |
107 * @deprecated use newCheckList(Composite, int) or new CheckboxTableViewer(Table) | |
108 * instead (see above for details) | |
109 */ | |
110 public this(Composite parent, int style) { | |
111 this(createTable(parent, style)); | |
112 } | |
113 | |
114 /** | |
115 * Creates a table viewer on a newly-created table control under the given parent. | |
116 * The table control is created using the given DWT style bits, plus the | |
117 * <code>DWT.CHECK</code> style bit. | |
118 * The table shows its contents in a single column, with no header. | |
119 * The viewer has no input, no content provider, a default label provider, | |
120 * no sorter, and no filters. | |
121 * <p> | |
122 * No <code>TableColumn</code> is added. DWT does not require a | |
123 * <code>TableColumn</code> if showing only a single column with no header. | |
124 * DWT correctly handles the initial sizing and subsequent resizes in this case. | |
125 * | |
126 * @param parent the parent control | |
127 * @param style DWT style bits | |
128 * | |
129 * @since 2.0 | |
130 * @return CheckboxTableViewer | |
131 */ | |
132 public static CheckboxTableViewer newCheckList(Composite parent, int style) { | |
133 Table table = new Table(parent, DWT.CHECK | style); | |
134 return new CheckboxTableViewer(table); | |
135 } | |
136 | |
137 /** | |
138 * Creates a table viewer on the given table control. | |
139 * The <code>DWT.CHECK</code> style bit must be set on the given table control. | |
140 * The viewer has no input, no content provider, a default label provider, | |
141 * no sorter, and no filters. | |
142 * | |
143 * @param table the table control | |
144 */ | |
145 public this(Table table) { | |
146 super(table); | |
147 checkStateListeners = new ListenerList(); | |
148 } | |
149 | |
150 /* (non-Javadoc) | |
151 * Method declared on ICheckable. | |
152 */ | |
153 public void addCheckStateListener(ICheckStateListener listener) { | |
154 checkStateListeners.add(cast(Object)listener); | |
155 } | |
156 | |
157 /** | |
158 * Creates a new table control with one column. | |
159 * | |
160 * @param parent the parent control | |
161 * @param style style bits | |
162 * @return a new table control | |
163 */ | |
164 protected static Table createTable(Composite parent, int style) { | |
165 Table table = new Table(parent, DWT.CHECK | style); | |
166 | |
167 // Although this table column is not needed, and can cause resize problems, | |
168 // it can't be removed since this would be a breaking change against R1.0. | |
169 // See bug 6643 for more details. | |
170 new TableColumn(table, DWT.NONE); | |
171 TableLayout layout = new TableLayout(); | |
172 layout.addColumnData(new ColumnWeightData(100)); | |
173 table.setLayout(layout); | |
174 | |
175 return table; | |
176 } | |
177 | |
178 /** | |
179 * Notifies any check state listeners that a check state changed has been received. | |
180 * Only listeners registered at the time this method is called are notified. | |
181 * | |
182 * @param event a check state changed event | |
183 * | |
184 * @see ICheckStateListener#checkStateChanged | |
185 */ | |
186 private void fireCheckStateChanged(CheckStateChangedEvent event) { | |
187 Object[] array = checkStateListeners.getListeners(); | |
188 for (int i = 0; i < array.length; i++) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
189 SafeRunnable.run( dgSafeRunnable( (ICheckStateListener l, CheckStateChangedEvent event_) { |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
190 l.checkStateChanged(event_); |
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
191 }, cast(ICheckStateListener) array[i], event )); |
10 | 192 } |
193 } | |
194 | |
195 /* (non-Javadoc) | |
196 * Method declared on ICheckable. | |
197 */ | |
198 public bool getChecked(Object element) { | |
199 Widget widget = findItem(element); | |
200 if ( auto ti = cast(TableItem) widget ) { | |
201 return ti.getChecked(); | |
202 } | |
203 return false; | |
204 } | |
205 | |
206 /** | |
207 * Returns a list of elements corresponding to checked table items in this | |
208 * viewer. | |
209 * <p> | |
210 * This method is typically used when preserving the interesting | |
211 * state of a viewer; <code>setCheckedElements</code> is used during the restore. | |
212 * </p> | |
213 * | |
214 * @return the array of checked elements | |
215 * @see #setCheckedElements | |
216 */ | |
217 public Object[] getCheckedElements() { | |
218 TableItem[] children = getTable().getItems(); | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
219 ArrayList v = new ArrayList(children.length); |
10 | 220 for (int i = 0; i < children.length; i++) { |
221 TableItem item = children[i]; | |
222 if (item.getChecked()) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
223 v.add(item.getData()); |
10 | 224 } |
225 } | |
226 return v.toArray(); | |
227 } | |
228 | |
229 /** | |
230 * Returns the grayed state of the given element. | |
231 * | |
232 * @param element the element | |
233 * @return <code>true</code> if the element is grayed, | |
234 * and <code>false</code> if not grayed | |
235 */ | |
236 public bool getGrayed(Object element) { | |
237 Widget widget = findItem(element); | |
238 if ( auto ti = cast(TableItem) widget ) { | |
239 return ti.getGrayed(); | |
240 } | |
241 return false; | |
242 } | |
243 | |
244 /** | |
245 * Returns a list of elements corresponding to grayed nodes in this | |
246 * viewer. | |
247 * <p> | |
248 * This method is typically used when preserving the interesting | |
249 * state of a viewer; <code>setGrayedElements</code> is used during the restore. | |
250 * </p> | |
251 * | |
252 * @return the array of grayed elements | |
253 * @see #setGrayedElements | |
254 */ | |
255 public Object[] getGrayedElements() { | |
256 TableItem[] children = getTable().getItems(); | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
257 ArrayList v = new ArrayList(children.length); |
10 | 258 for (int i = 0; i < children.length; i++) { |
259 TableItem item = children[i]; | |
260 if (item.getGrayed()) { | |
104
04b47443bb01
Reworked the collection uses to make use of a wrapper collection that is compatible to the Java Collections.
Frank Benoit <benoit@tionex.de>
parents:
90
diff
changeset
|
261 v.add(item.getData()); |
10 | 262 } |
263 } | |
264 return v.toArray(); | |
265 } | |
266 | |
267 /* (non-Javadoc) | |
268 * Method declared on StructuredViewer. | |
269 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
270 public override void handleSelect(SelectionEvent event) { |
10 | 271 if (event.detail is DWT.CHECK) { |
272 super.handleSelect(event); // this will change the current selection | |
273 | |
274 TableItem item = cast(TableItem) event.item; | |
275 Object data = item.getData(); | |
276 if (data !is null) { | |
277 fireCheckStateChanged(new CheckStateChangedEvent(this, data, | |
278 item.getChecked())); | |
279 } | |
280 } else { | |
281 super.handleSelect(event); | |
282 } | |
283 } | |
284 | |
285 /* (non-Javadoc) | |
286 * Method declared on Viewer. | |
287 */ | |
43
ea8ff534f622
Fix override and super aliases
Frank Benoit <benoit@tionex.de>
parents:
39
diff
changeset
|
288 protected override void preservingSelection(Runnable updateCode) { |
10 | 289 |
290 TableItem[] children = getTable().getItems(); | |
291 CustomHashtable checked = newHashtable(children.length * 2 + 1); | |
292 CustomHashtable grayed = newHashtable(children.length * 2 + 1); | |
293 | |
294 for (int i = 0; i < children.length; i++) { | |
295 TableItem item = children[i]; | |
296 Object data = item.getData(); | |
297 if (data !is null) { | |
298 if (item.getChecked()) { | |
299 checked.put(data, data); | |
300 } | |
301 if (item.getGrayed()) { | |
302 grayed.put(data, data); | |
303 } | |
304 } | |
305 } | |
306 | |
307 super.preservingSelection(updateCode); | |
308 | |
309 children = getTable().getItems(); | |
310 for (int i = 0; i < children.length; i++) { | |
311 TableItem item = children[i]; | |
312 Object data = item.getData(); | |
313 if (data !is null) { | |
314 item.setChecked(checked.containsKey(data)); | |
315 item.setGrayed(grayed.containsKey(data)); | |
316 } | |
317 } | |
318 } | |
319 | |
320 /* (non-Javadoc) | |
321 * Method declared on ICheckable. | |
322 */ | |
323 public void removeCheckStateListener(ICheckStateListener listener) { | |
324 checkStateListeners.remove(cast(Object)listener); | |
325 } | |
326 | |
327 /** | |
328 * Sets to the given value the checked state for all elements in this viewer. | |
329 * Does not fire events to check state listeners. | |
330 * | |
331 * @param state <code>true</code> if the element should be checked, | |
332 * and <code>false</code> if it should be unchecked | |
333 */ | |
334 public void setAllChecked(bool state) { | |
335 TableItem[] children = getTable().getItems(); | |
336 for (int i = 0; i < children.length; i++) { | |
337 TableItem item = children[i]; | |
338 item.setChecked(state); | |
339 } | |
340 } | |
341 | |
342 /** | |
343 * Sets to the given value the grayed state for all elements in this viewer. | |
344 * | |
345 * @param state <code>true</code> if the element should be grayed, | |
346 * and <code>false</code> if it should be ungrayed | |
347 */ | |
348 public void setAllGrayed(bool state) { | |
349 TableItem[] children = getTable().getItems(); | |
350 for (int i = 0; i < children.length; i++) { | |
351 TableItem item = children[i]; | |
352 item.setGrayed(state); | |
353 } | |
354 } | |
355 | |
356 /* (non-Javadoc) | |
357 * Method declared on ICheckable. | |
358 */ | |
359 public bool setChecked(Object element, bool state) { | |
360 Assert.isNotNull(element); | |
361 Widget widget = findItem(element); | |
362 if ( auto ti = cast(TableItem) widget ) { | |
363 ti.setChecked(state); | |
364 return true; | |
365 } | |
366 return false; | |
367 } | |
368 | |
369 /** | |
370 * Sets which nodes are checked in this viewer. | |
371 * The given list contains the elements that are to be checked; | |
372 * all other nodes are to be unchecked. | |
373 * Does not fire events to check state listeners. | |
374 * <p> | |
375 * This method is typically used when restoring the interesting | |
376 * state of a viewer captured by an earlier call to <code>getCheckedElements</code>. | |
377 * </p> | |
378 * | |
379 * @param elements the list of checked elements (element type: <code>Object</code>) | |
380 * @see #getCheckedElements | |
381 */ | |
382 public void setCheckedElements(Object[] elements) { | |
383 assertElementsNotNull(elements); | |
384 CustomHashtable set = newHashtable(elements.length * 2 + 1); | |
385 for (int i = 0; i < elements.length; ++i) { | |
386 set.put(elements[i], elements[i]); | |
387 } | |
388 TableItem[] items = getTable().getItems(); | |
389 for (int i = 0; i < items.length; ++i) { | |
390 TableItem item = items[i]; | |
391 Object element = item.getData(); | |
392 if (element !is null) { | |
393 bool check = set.containsKey(element); | |
394 // only set if different, to avoid flicker | |
395 if (item.getChecked() !is check) { | |
396 item.setChecked(check); | |
397 } | |
398 } | |
399 } | |
400 } | |
401 | |
402 /** | |
403 * Sets the grayed state for the given element in this viewer. | |
404 * | |
405 * @param element the element | |
406 * @param state <code>true</code> if the item should be grayed, | |
407 * and <code>false</code> if it should be ungrayed | |
408 * @return <code>true</code> if the element is visible and the gray | |
409 * state could be set, and <code>false</code> otherwise | |
410 */ | |
411 public bool setGrayed(Object element, bool state) { | |
412 Assert.isNotNull(element); | |
413 Widget widget = findItem(element); | |
414 if ( auto ti = cast(TableItem) widget ) { | |
415 ti.setGrayed(state); | |
416 return true; | |
417 } | |
418 return false; | |
419 } | |
420 | |
421 /** | |
422 * Sets which nodes are grayed in this viewer. | |
423 * The given list contains the elements that are to be grayed; | |
424 * all other nodes are to be ungrayed. | |
425 * <p> | |
426 * This method is typically used when restoring the interesting | |
427 * state of a viewer captured by an earlier call to <code>getGrayedElements</code>. | |
428 * </p> | |
429 * | |
430 * @param elements the array of grayed elements | |
431 * | |
432 * @see #getGrayedElements | |
433 */ | |
434 public void setGrayedElements(Object[] elements) { | |
435 assertElementsNotNull(elements); | |
436 CustomHashtable set = newHashtable(elements.length * 2 + 1); | |
437 for (int i = 0; i < elements.length; ++i) { | |
438 set.put(elements[i], elements[i]); | |
439 } | |
440 TableItem[] items = getTable().getItems(); | |
441 for (int i = 0; i < items.length; ++i) { | |
442 TableItem item = items[i]; | |
443 Object element = item.getData(); | |
444 if (element !is null) { | |
445 bool gray = set.containsKey(element); | |
446 // only set if different, to avoid flicker | |
447 if (item.getGrayed() !is gray) { | |
448 item.setGrayed(gray); | |
449 } | |
450 } | |
451 } | |
452 } | |
453 } |