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