Mercurial > projects > dwt2
comparison org.eclipse.jface/src/org/eclipse/jface/viewers/CheckboxTableViewer.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, 2008 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.CheckboxTableViewer; | |
14 | |
15 import org.eclipse.jface.viewers.ICheckable; | |
16 import org.eclipse.jface.viewers.TableViewer; | |
17 import org.eclipse.jface.viewers.ICheckStateListener; | |
18 import org.eclipse.jface.viewers.CheckStateChangedEvent; | |
19 import org.eclipse.jface.viewers.TableLayout; | |
20 import org.eclipse.jface.viewers.ColumnWeightData; | |
21 import org.eclipse.jface.viewers.CustomHashtable; | |
22 | |
23 | |
24 import org.eclipse.swt.SWT; | |
25 import org.eclipse.swt.events.SelectionEvent; | |
26 import org.eclipse.swt.widgets.Composite; | |
27 import org.eclipse.swt.widgets.Table; | |
28 import org.eclipse.swt.widgets.TableColumn; | |
29 import org.eclipse.swt.widgets.TableItem; | |
30 import org.eclipse.swt.widgets.Widget; | |
31 import org.eclipse.core.runtime.Assert; | |
32 import org.eclipse.core.runtime.ListenerList; | |
33 import org.eclipse.jface.util.SafeRunnable; | |
34 | |
35 import java.lang.all; | |
36 import java.util.List; | |
37 import java.util.ArrayList; | |
38 import java.util.Set; | |
39 | |
40 /** | |
41 * A concrete viewer based on an SWT <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 SWT table control and configured | |
46 * with a domain-specific content provider, label provider, element filter (optional), | |
47 * and element sorter (optional). | |
48 * </p> | |
49 * @noextend This class is not intended to be subclassed by clients. | |
50 */ | |
51 public class CheckboxTableViewer : TableViewer, ICheckable { | |
52 alias TableViewer.preservingSelection preservingSelection; | |
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 SWT style bits: | |
62 * <code>SWT.CHECK</code> and <code>SWT.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, SWT.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, SWT.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 SWT style bits, plus the | |
83 * <code>SWT.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 SWT 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>SWT.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 SWT 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 SWT style bits, plus the | |
118 * <code>SWT.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. SWT does not require a | |
124 * <code>TableColumn</code> if showing only a single column with no header. | |
125 * SWT correctly handles the initial sizing and subsequent resizes in this case. | |
126 * | |
127 * @param parent the parent control | |
128 * @param style SWT 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, SWT.CHECK | style); | |
135 return new CheckboxTableViewer(table); | |
136 } | |
137 | |
138 /** | |
139 * Creates a table viewer on the given table control. | |
140 * The <code>SWT.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, SWT.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, SWT.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++) { | |
190 SafeRunnable.run( dgSafeRunnable( (ICheckStateListener l, CheckStateChangedEvent event_) { | |
191 l.checkStateChanged(event_); | |
192 }, cast(ICheckStateListener) array[i], event )); | |
193 } | |
194 } | |
195 | |
196 /* (non-Javadoc) | |
197 * Method declared on ICheckable. | |
198 */ | |
199 public bool getChecked(Object element) { | |
200 Widget widget = findItem(element); | |
201 if ( auto ti = cast(TableItem) widget ) { | |
202 return ti.getChecked(); | |
203 } | |
204 return false; | |
205 } | |
206 | |
207 /** | |
208 * Returns a list of elements corresponding to checked table items in this | |
209 * viewer. | |
210 * <p> | |
211 * This method is typically used when preserving the interesting | |
212 * state of a viewer; <code>setCheckedElements</code> is used during the restore. | |
213 * </p> | |
214 * | |
215 * @return the array of checked elements | |
216 * @see #setCheckedElements | |
217 */ | |
218 public Object[] getCheckedElements() { | |
219 TableItem[] children = getTable().getItems(); | |
220 ArrayList v = new ArrayList(children.length); | |
221 for (int i = 0; i < children.length; i++) { | |
222 TableItem item = children[i]; | |
223 if (item.getChecked()) { | |
224 v.add(item.getData()); | |
225 } | |
226 } | |
227 return v.toArray(); | |
228 } | |
229 | |
230 /** | |
231 * Returns the grayed state of the given element. | |
232 * | |
233 * @param element the element | |
234 * @return <code>true</code> if the element is grayed, | |
235 * and <code>false</code> if not grayed | |
236 */ | |
237 public bool getGrayed(Object element) { | |
238 Widget widget = findItem(element); | |
239 if ( auto ti = cast(TableItem) widget ) { | |
240 return ti.getGrayed(); | |
241 } | |
242 return false; | |
243 } | |
244 | |
245 /** | |
246 * Returns a list of elements corresponding to grayed nodes in this | |
247 * viewer. | |
248 * <p> | |
249 * This method is typically used when preserving the interesting | |
250 * state of a viewer; <code>setGrayedElements</code> is used during the restore. | |
251 * </p> | |
252 * | |
253 * @return the array of grayed elements | |
254 * @see #setGrayedElements | |
255 */ | |
256 public Object[] getGrayedElements() { | |
257 TableItem[] children = getTable().getItems(); | |
258 ArrayList v = new ArrayList(children.length); | |
259 for (int i = 0; i < children.length; i++) { | |
260 TableItem item = children[i]; | |
261 if (item.getGrayed()) { | |
262 v.add(item.getData()); | |
263 } | |
264 } | |
265 return v.toArray(); | |
266 } | |
267 | |
268 /* (non-Javadoc) | |
269 * Method declared on StructuredViewer. | |
270 */ | |
271 public override void handleSelect(SelectionEvent event) { | |
272 if (event.detail is SWT.CHECK) { | |
273 super.handleSelect(event); // this will change the current selection | |
274 | |
275 TableItem item = cast(TableItem) event.item; | |
276 Object data = item.getData(); | |
277 if (data !is null) { | |
278 fireCheckStateChanged(new CheckStateChangedEvent(this, data, | |
279 item.getChecked())); | |
280 } | |
281 } else { | |
282 super.handleSelect(event); | |
283 } | |
284 } | |
285 | |
286 /* (non-Javadoc) | |
287 * Method declared on Viewer. | |
288 */ | |
289 protected override void preservingSelection(Runnable updateCode) { | |
290 | |
291 TableItem[] children = getTable().getItems(); | |
292 CustomHashtable checked = newHashtable(children.length * 2 + 1); | |
293 CustomHashtable grayed = newHashtable(children.length * 2 + 1); | |
294 | |
295 for (int i = 0; i < children.length; i++) { | |
296 TableItem item = children[i]; | |
297 Object data = item.getData(); | |
298 if (data !is null) { | |
299 if (item.getChecked()) { | |
300 checked.put(data, data); | |
301 } | |
302 if (item.getGrayed()) { | |
303 grayed.put(data, data); | |
304 } | |
305 } | |
306 } | |
307 | |
308 super.preservingSelection(updateCode); | |
309 | |
310 children = getTable().getItems(); | |
311 for (int i = 0; i < children.length; i++) { | |
312 TableItem item = children[i]; | |
313 Object data = item.getData(); | |
314 if (data !is null) { | |
315 item.setChecked(checked.containsKey(data)); | |
316 item.setGrayed(grayed.containsKey(data)); | |
317 } | |
318 } | |
319 } | |
320 | |
321 /* (non-Javadoc) | |
322 * Method declared on ICheckable. | |
323 */ | |
324 public void removeCheckStateListener(ICheckStateListener listener) { | |
325 checkStateListeners.remove(cast(Object)listener); | |
326 } | |
327 | |
328 /** | |
329 * Sets to the given value the checked state for all elements in this viewer. | |
330 * Does not fire events to check state listeners. | |
331 * | |
332 * @param state <code>true</code> if the element should be checked, | |
333 * and <code>false</code> if it should be unchecked | |
334 */ | |
335 public void setAllChecked(bool state) { | |
336 TableItem[] children = getTable().getItems(); | |
337 for (int i = 0; i < children.length; i++) { | |
338 TableItem item = children[i]; | |
339 item.setChecked(state); | |
340 } | |
341 } | |
342 | |
343 /** | |
344 * Sets to the given value the grayed state for all elements in this viewer. | |
345 * | |
346 * @param state <code>true</code> if the element should be grayed, | |
347 * and <code>false</code> if it should be ungrayed | |
348 */ | |
349 public void setAllGrayed(bool state) { | |
350 TableItem[] children = getTable().getItems(); | |
351 for (int i = 0; i < children.length; i++) { | |
352 TableItem item = children[i]; | |
353 item.setGrayed(state); | |
354 } | |
355 } | |
356 | |
357 /* (non-Javadoc) | |
358 * Method declared on ICheckable. | |
359 */ | |
360 public bool setChecked(Object element, bool state) { | |
361 Assert.isNotNull(element); | |
362 Widget widget = findItem(element); | |
363 if ( auto ti = cast(TableItem) widget ) { | |
364 ti.setChecked(state); | |
365 return true; | |
366 } | |
367 return false; | |
368 } | |
369 | |
370 /** | |
371 * Sets which nodes are checked in this viewer. | |
372 * The given list contains the elements that are to be checked; | |
373 * all other nodes are to be unchecked. | |
374 * Does not fire events to check state listeners. | |
375 * <p> | |
376 * This method is typically used when restoring the interesting | |
377 * state of a viewer captured by an earlier call to <code>getCheckedElements</code>. | |
378 * </p> | |
379 * | |
380 * @param elements the list of checked elements (element type: <code>Object</code>) | |
381 * @see #getCheckedElements | |
382 */ | |
383 public void setCheckedElements(Object[] elements) { | |
384 assertElementsNotNull(elements); | |
385 CustomHashtable set = newHashtable(elements.length * 2 + 1); | |
386 for (int i = 0; i < elements.length; ++i) { | |
387 set.put(elements[i], elements[i]); | |
388 } | |
389 TableItem[] items = getTable().getItems(); | |
390 for (int i = 0; i < items.length; ++i) { | |
391 TableItem item = items[i]; | |
392 Object element = item.getData(); | |
393 if (element !is null) { | |
394 bool check = set.containsKey(element); | |
395 // only set if different, to avoid flicker | |
396 if (item.getChecked() !is check) { | |
397 item.setChecked(check); | |
398 } | |
399 } | |
400 } | |
401 } | |
402 | |
403 /** | |
404 * Sets the grayed state for the given element in this viewer. | |
405 * | |
406 * @param element the element | |
407 * @param state <code>true</code> if the item should be grayed, | |
408 * and <code>false</code> if it should be ungrayed | |
409 * @return <code>true</code> if the element is visible and the gray | |
410 * state could be set, and <code>false</code> otherwise | |
411 */ | |
412 public bool setGrayed(Object element, bool state) { | |
413 Assert.isNotNull(element); | |
414 Widget widget = findItem(element); | |
415 if ( auto ti = cast(TableItem) widget ) { | |
416 ti.setGrayed(state); | |
417 return true; | |
418 } | |
419 return false; | |
420 } | |
421 | |
422 /** | |
423 * Sets which nodes are grayed in this viewer. | |
424 * The given list contains the elements that are to be grayed; | |
425 * all other nodes are to be ungrayed. | |
426 * <p> | |
427 * This method is typically used when restoring the interesting | |
428 * state of a viewer captured by an earlier call to <code>getGrayedElements</code>. | |
429 * </p> | |
430 * | |
431 * @param elements the array of grayed elements | |
432 * | |
433 * @see #getGrayedElements | |
434 */ | |
435 public void setGrayedElements(Object[] elements) { | |
436 assertElementsNotNull(elements); | |
437 CustomHashtable set = newHashtable(elements.length * 2 + 1); | |
438 for (int i = 0; i < elements.length; ++i) { | |
439 set.put(elements[i], elements[i]); | |
440 } | |
441 TableItem[] items = getTable().getItems(); | |
442 for (int i = 0; i < items.length; ++i) { | |
443 TableItem item = items[i]; | |
444 Object element = item.getData(); | |
445 if (element !is null) { | |
446 bool gray = set.containsKey(element); | |
447 // only set if different, to avoid flicker | |
448 if (item.getGrayed() !is gray) { | |
449 item.setGrayed(gray); | |
450 } | |
451 } | |
452 } | |
453 } | |
454 } |