Skip to main content
summaryrefslogtreecommitdiffstats
blob: a5838624ed4ca94f4c3ae20d0461eb3ffbc3866a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
/*****************************************************************************
 * Copyright (c) 2012 CEA LIST.
 *
 *    
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Duplicated and adapted code from org.eclipse.pde.internal.ui.launcher.FilteredCheckboxTree
 *
 *****************************************************************************/

package org.eclipse.papyrus.infra.table.efacet.menu.handler;

import java.util.*;
import java.util.List;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer;
import org.eclipse.ui.dialogs.FilteredTree;
import org.eclipse.ui.dialogs.PatternFilter;
import org.eclipse.ui.progress.WorkbenchJob;

/**
 * A FilteredChecboxTree. This tree stores all the tree elements internally, and keeps the
 * check state in sync. This way, even if an element is filtered, the caller can get and set the
 * checked state.
 * 
 * The internal representation is additive. That is, elements are never removed from the internal
 * representation. This is OK since the PDE launch Dialog never changes the elements once
 * the view is opened. If any other tree is based on this code, they may want to address this issue.
 * 
 * This is not public because it was customized for the Launch Dialog.
 * 
 */
public class FilteredCheckboxTree extends FilteredTree {

	private WorkbenchJob refreshJob;

	/**
	 * The FilteredCheckboxTree Constructor.
	 * 
	 * @param parent
	 *        The parent composite where this Tree will be placed.
	 * @param treeStyle
	 *        Tree styles
	 * @param filter
	 *        The pattern filter that will be used to filter elements
	 */
	public FilteredCheckboxTree(Composite parent, int treeStyle, PatternFilter filter) {
		super(parent, treeStyle, filter, true);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.dialogs.FilteredTree#doCreateTreeViewer(org.eclipse.swt.widgets.Composite, int)
	 */
	protected TreeViewer doCreateTreeViewer(Composite parent, int style) {
		return new FilterableCheckboxTreeViewer(parent, style);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.dialogs.FilteredTree#doCreateRefreshJob()
	 */
	protected WorkbenchJob doCreateRefreshJob() {
		// Since refresh job is private, we have to get a handle to it
		// when it is created, and store it locally.  
		// 
		// See: 218903: [Viewers] support extensibility of the refresh job in FilteredTree
		// https://bugs.eclipse.org/bugs/show_bug.cgi?id=218903
		WorkbenchJob job = super.doCreateRefreshJob();
		refreshJob = job;
		return job;
	}

	/**
	 * Resets the filter and returns when the refresh is complete
	 */
	public void resetFilter() {
		// Set the next to the initial Text, stop any outstanding jobs
		// and call the refresh job to run synchronously.
		getFilterControl().setText(this.initialText);
		refreshJob.cancel();
		refreshJob.runInUIThread(new NullProgressMonitor());
	}

	/**
	 * Get the number of pixels the tree viewer is from the top of the filtered
	 * checkbox tree viewer. This is useful if you wish to align buttons with the
	 * tree.
	 * 
	 * @return the offset of the Tree from the top of the container
	 */
	int getTreeLocationOffset() {
		GridLayout layout = (GridLayout)getLayout();
		return layout.horizontalSpacing + layout.marginTop + ((GridData)getLayoutData()).verticalIndent + getFilterControl().getSize().y + 1;
	}

	/**
	 * Classes which implement this interface deal with notifications from the
	 * filtered checkbox tree viewer. The notifications are fired before a refresh
	 * happens.
	 */
	interface PreRefreshNotifier {

		public void preRefresh(FilterableCheckboxTreeViewer viewer, boolean filtered);
	}

	/**
	 * A CheckboxTreeViewer that maintains an internal representation of all the nodes.
	 * 
	 */
	public class FilterableCheckboxTreeViewer extends ContainerCheckedTreeViewer {

		static final String NONE = "none"; //$NON-NLS-1$

		static final String CHECKED = "checked"; //$NON-NLS-1$

		static final String GREYED = "greyed"; //$NON-NLS-1$

		static final String CHECKED_GREYED = "checked_greyed"; //$NON-NLS-1$

		/**
		 * The internal node for the FilterableCheckboxTreeViewer
		 */
		class FilteredCheckboxTreeItem {

			Object data; // Data element

			String state; // Checked State

			List children = new ArrayList();

			public FilteredCheckboxTreeItem(Object data, String state, Map itemCache, FilteredCheckboxTreeItem parent) {
				this.data = data;
				this.state = state;
				itemCache.put(data, this);
				if(parent != null) {
					parent.children.add(this);
				}
			}
		}

		/* A cache of all the nodes */
		Map itemCache = new HashMap();

		/* The preRefresh Listeners */
		List refreshingListeners = new ArrayList();

		protected void unmapAllElements() {
			itemCache = new HashMap();
			super.unmapAllElements();
		}

		/**
		 * FilterableCheckboxTreeViewer constructor. This creates the tree part of the filtered tree.
		 */
		public FilterableCheckboxTreeViewer(Composite parent, int style) {
			super(parent, style);
			addCheckStateListener(new ICheckStateListener() {

				public void checkStateChanged(CheckStateChangedEvent event) {
					FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)itemCache.get(event.getElement());
					if(item != null) {
						item.state = event.getChecked() ? CHECKED : NONE;
					}
				}
			});
		}

		/**
		 * Allows clients to listen to the tree refresh.
		 */
		public void addPreRefreshNotifier(PreRefreshNotifier notifier) {
			if(refreshingListeners.contains(notifier))
				return;
			refreshingListeners.add(notifier);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.CheckboxTreeViewer#getChecked(java.lang.Object)
		 */
		public boolean getChecked(Object element) {
			Widget testFindItem = getViewer().testFindItem(element);
			testFindItem = null;
			if(testFindItem == null) {
				if(itemCache.containsKey(element)) {
					FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)itemCache.get(element);
					if(item.state.equals(CHECKED))
						return true;
					if(item.state.equals(CHECKED_GREYED))
						return true;
					if(item.state.equals(GREYED))
						return true;
					else if(item.state.equals(NONE))
						return false;
				}
			}
			return super.getChecked(element);
		}

		public Object[] getCheckedChildren(Object element) {
			FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)itemCache.get(element);
			List checkedChildren = new ArrayList();
			if(item != null) {
				List children = item.children;
				Iterator iterator = children.iterator();
				while(iterator.hasNext()) {
					FilteredCheckboxTreeItem child = (FilteredCheckboxTreeItem)iterator.next();
					if(child.state == CHECKED) {
						checkedChildren.add(child.data);
					}
				}
			}
			return checkedChildren.toArray();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.CheckboxTreeViewer#getCheckedElements()
		 */
		public Object[] getCheckedElements() {
			Iterator iterator = itemCache.values().iterator();
			List checkedElements = new LinkedList();
			while(iterator.hasNext()) {
				FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)iterator.next();
				Widget testFindItem = getViewer().testFindItem(item.data);
				testFindItem = null;
				if(testFindItem == null) {
					if(item.state.equals(CHECKED) || item.state.equals(CHECKED_GREYED) || item.state.equals(GREYED)) {
						checkedElements.add(item.data);
					}
				} else {
					if(((TreeItem)testFindItem).getChecked()) {
						checkedElements.add(testFindItem.getData());
					}
				}
			}
			return checkedElements.toArray();
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.CheckboxTreeViewer#setChecked(java.lang.Object, boolean)
		 */
		public boolean setChecked(Object element, boolean state) {
			if(itemCache.containsKey(element)) {
				FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)itemCache.get(element);
				item.state = state ? CHECKED : NONE;
			}
			return super.setChecked(element, state);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.CheckboxTreeViewer#setCheckedElements(java.lang.Object[])
		 */
		public void setCheckedElements(Object[] elements) {
			Set s = new HashSet(itemCache.keySet());
			s.removeAll(new HashSet(Arrays.asList(elements)));
			for(int i = 0; i < elements.length; i++) {
				FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)itemCache.get(elements[i]);
				if(item != null) {
					item.state = CHECKED;
				}
			}
			for(Iterator iterator = s.iterator(); iterator.hasNext();) {
				Object object = iterator.next();
				FilteredCheckboxTreeItem item = (FilteredCheckboxTreeItem)itemCache.get(object);
				if(item != null) {
					item.state = NONE;
				}
			}
			super.setCheckedElements(elements);
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.CheckboxTreeViewer#setSubtreeChecked(java.lang.Object, boolean)
		 */
		public boolean setSubtreeChecked(Object element, boolean state) {
			String newState = state ? CHECKED : NONE;
			TreeItem item = (TreeItem)testFindItem(element);
			FilteredCheckboxTreeItem filteredCheckboxTreeItem = (FilteredCheckboxTreeItem)itemCache.get(element);
			if(item != null && filteredCheckboxTreeItem != null) {
				filteredCheckboxTreeItem.state = newState;
				TreeItem[] items = item.getItems();
				for(int i = 0; i < items.length; i++) {
					item = items[i];
					if(item != null) {
						filteredCheckboxTreeItem = (FilteredCheckboxTreeItem)itemCache.get(item.getData());
						if(filteredCheckboxTreeItem != null) {
							filteredCheckboxTreeItem.state = newState;
						}
					}
				}
			}
			return super.setSubtreeChecked(element, state);
		}

		/*
		 * public boolean setSubtreeChecked(Object element, boolean state) {
		 * String newState = state ? CHECKED : NONE;
		 * FilteredCheckboxTreeItem filteredCheckboxTreeItem = (FilteredCheckboxTreeItem) itemCache.get(element);
		 * if (filteredCheckboxTreeItem != null) {
		 * filteredCheckboxTreeItem.state = newState;
		 * List children = filteredCheckboxTreeItem.children;
		 * for (Iterator iterator = children.iterator(); iterator.hasNext();) {
		 * FilteredCheckboxTreeItem child = (FilteredCheckboxTreeItem) iterator.next();
		 * child.state = newState;
		 * }
		 * }
		 * return super.setSubtreeChecked(element, state);
		 * }
		 */

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.CheckboxTreeViewer#preservingSelection(java.lang.Runnable)
		 */
		protected void preservingSelection(Runnable updateCode) {
			super.preservingSelection(updateCode);

			// Re-apply the checked state
			ArrayList allTreeItems = getAllTreeItems(treeViewer.getTree().getItems());
			for(Iterator iterator = allTreeItems.iterator(); iterator.hasNext();) {
				TreeItem item = (TreeItem)iterator.next();
				doApplyCheckedState(item, item.getData());
			}
		}

		/*
		 * (non-Javadoc)
		 * 
		 * @see org.eclipse.jface.viewers.AbstractTreeViewer#internalRefresh(java.lang.Object, boolean)
		 */
		protected void internalRefresh(Object element, boolean updateLabels) {
			String text = FilteredCheckboxTree.this.getFilterString();
			boolean initial = initialText != null && initialText.equals(text);
			boolean filtered = (text.length() > 0 && !initial);

			// Notify anybody who is listening for the refresh
			for(Iterator iterator = refreshingListeners.iterator(); iterator.hasNext();) {
				PreRefreshNotifier notifier = (PreRefreshNotifier)iterator.next();
				notifier.preRefresh(FilterableCheckboxTreeViewer.this, filtered);
			}
			saveCheckedState();
			super.internalRefresh(element, updateLabels);
			treeViewer.expandAll();
		}

		/*
		 * Set the checked state
		 */
		private void doApplyCheckedState(Item item, Object element) {
			// update the item first
			super.doUpdateItem(item, element);

			// Update the checked state
			TreeItem treeItem = (TreeItem)item;
			if(itemCache.containsKey(element)) {
				String state = ((FilteredCheckboxTreeItem)itemCache.get(element)).state;
				if(state.equals(CHECKED_GREYED)) {
					treeItem.setGrayed(true);
					treeItem.setChecked(true);
				} else if(state.equals(CHECKED)) {
					treeItem.setChecked(true);
					treeItem.setGrayed(false);
				} else if(state.equals(GREYED)) {
					treeItem.setGrayed(true);
					treeItem.setChecked(false);
				} else {
					treeItem.setGrayed(false);
					treeItem.setChecked(false);
				}
			}
		}

		/*
		 * A helper method to get all the items in the tree
		 */
		private ArrayList getAllTreeItems(TreeItem[] roots) {
			ArrayList list = new ArrayList();
			for(int i = 0; i < roots.length; i++) {
				TreeItem item = roots[i];
				list.add(item);
				list.addAll(getAllTreeItems(item.getItems()));
			}
			return list;
		}

		/**
		 * Saves the checked state of all the elements in the tree
		 */
		private void saveCheckedState() {
			TreeItem[] items = treeViewer.getTree().getItems();
			for(int i = 0; i < items.length; i++) {
				TreeItem item = items[i];
				if(!itemCache.containsKey(item.getData())) {
					new FilteredCheckboxTreeItem(item.getData(), getItemState(item), itemCache, null);
				}
				FilteredCheckboxTreeItem filteredCheckboxTreeItem = (FilteredCheckboxTreeItem)itemCache.get(item.getData());
				filteredCheckboxTreeItem.state = getItemState(item);
				saveCheckedState(filteredCheckboxTreeItem, item);
			}
		}

		/**
		 * Saves the checked state of an item and all its children
		 */
		private void saveCheckedState(FilteredCheckboxTreeItem parent, TreeItem parentItem) {
			TreeItem[] items = parentItem.getItems();
			for(int i = 0; i < items.length; i++) {
				TreeItem item = items[i];
				if(!itemCache.containsKey(item.getData())) {
					new FilteredCheckboxTreeItem(item.getData(), getItemState(item), itemCache, parent);
				}
				FilteredCheckboxTreeItem filteredCheckboxTreeItem = (FilteredCheckboxTreeItem)itemCache.get(item.getData());
				filteredCheckboxTreeItem.state = getItemState(item);
				saveCheckedState(filteredCheckboxTreeItem, item);
			}
		}

		/**
		 * Computes the checked state from a tree item
		 */
		private String getItemState(TreeItem item) {
			if(item.getChecked() && item.getGrayed()) {
				return CHECKED_GREYED;
			} else if(item.getChecked()) {
				return CHECKED;
			} else if(item.getGrayed()) {
				return GREYED;
			} else {
				return NONE;
			}
		}

	} // end of FilterableCheckboxTreeViewer

}

Back to the top