Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: ed8c9c553b636ff40b0b5d9bd70f240139c36c76 (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
/*****************************************************************************
 * Copyright (c) 2011 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:
 *  Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.widgets.providers;

import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.papyrus.infra.widgets.editors.AbstractEditor;
import org.eclipse.papyrus.infra.widgets.editors.ICommitListener;
import org.eclipse.papyrus.infra.widgets.editors.StringEditor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;


/**
 * A generic implementation for a IGraphicalContentProvider.
 * This class doesn't provide any element, and should be extended.
 * 
 * It implements a filter for List or Tree elements, based on the label
 * provided by the viewer's label provider (Or Object#toString() if the viewer
 * doesn't have a label provider).
 * 
 * A Text widget is added before the display control to insert the filter
 * pattern. An element is matched if at least one of these conditions is
 * matched :
 * - The element's name matches the pattern
 * - One of the element's children matches the pattern
 * - One of the element's parent matches the pattern
 * 
 * The elements' hierarchy is obtained via the viewer's ContentProvider.
 * 
 * @author Camille Letavernier
 */
//TODO : Encapsulate a IStructuredContentProvider and make this class concrete
public abstract class AbstractFilteredContentProvider implements IGraphicalContentProvider {

	protected StructuredViewer viewer;

	private StringEditor filterPattern;

	private PatternViewerFilter filter;

	public static final String BASE_PATTERN = "*"; //$NON-NLS-1$

	protected boolean showIfHasVisibleParent = false;

	public void dispose() {
		// Nothing
	}

	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		if(viewer instanceof StructuredViewer) {
			this.viewer = (StructuredViewer)viewer;
			updateFilter();
		}
	}

	private void updateFilter() {
		if(this.viewer != null && filterPattern != null) {
			this.viewer.setFilters(new ViewerFilter[]{ filter });
		}
	}

	public void createBefore(Composite parent) {
		filterPattern = new StringEditor(parent, SWT.NONE, "Filter : "); //$NON-NLS-1$
		filterPattern.setValidateOnDelay(true);
		filterPattern.setValue(BASE_PATTERN);
		filter = getViewerFilter();
		filterPattern.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				filter.setPattern((String)filterPattern.getValue());
				viewer.refresh();
			}

		});
		updateFilter();
	}

	public void createAfter(Composite parent) {
		//Nothing
	}

	protected PatternViewerFilter getViewerFilter() {
		PatternViewerFilter filter = new PatternViewerFilter();
		filter.setStrict(false);
		filter.setPattern(BASE_PATTERN);
		filter.setShowIfHasVisibleParent(showIfHasVisibleParent);
		return filter;
	}

}

Back to the top