Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: a6d6f0eda9d925d786fffce711852b0b3b870823 (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
package org.eclipse.papyrus.infra.table.properties.provider;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

import org.eclipse.gmf.runtime.emf.type.core.ElementTypeRegistry;
import org.eclipse.gmf.runtime.emf.type.core.IClientContext;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.StructuredViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.papyrus.infra.core.services.ServiceException;
import org.eclipse.papyrus.infra.services.edit.internal.context.TypeContext;
import org.eclipse.papyrus.infra.table.properties.Activator;
import org.eclipse.papyrus.infra.table.properties.messages.Messages;
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.papyrus.infra.widgets.providers.AbstractStaticContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.IGraphicalContentProvider;
import org.eclipse.papyrus.infra.widgets.providers.PatternViewerFilter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;


public class ElementTypeContentProvider extends AbstractStaticContentProvider implements ITreeContentProvider, IGraphicalContentProvider {

	private Set<String> values = null;


	public String[] getElements() {
		if(this.values == null) {
			this.values = getValues();
		}

		return this.values.toArray(new String[values.size()]);
	}


	private Set<String> getValues() {
		final Set<String> allValues = getAllPossiblesValues();
		return allValues;
	}

	private Set<String> getAllPossiblesValues() {

		IClientContext clientContext = null;
		try {
			clientContext = TypeContext.getContext();
		} catch (ServiceException e1) {
		Activator.log.error(e1);
		}
		IElementType[] types = ElementTypeRegistry.getInstance().getElementTypes(clientContext);
		final Set<String> ids = new TreeSet<String>();
		for(IElementType iElementType : types) {
			final String id = iElementType.getId();
			if(id.contains("sysml.stereotype.") || id.contains("uml.stereotype.")) {//FIXME should not be done here -> should be adapted in the Papyrus Table V3 //$NON-NLS-1$ //$NON-NLS-2$
				//nothing to do 
			} else {
				ids.add(id);
			}
		}
		return ids;
	}


	public Object[] getChildren(Object parentElement) {
		return new Object[0];
	}

	public Object getParent(Object element) {
		return null; //Flat provider
	}

	public boolean hasChildren(Object element) {
		return false;
	}

	/**
	 * {@inheritDoc}
	 */
	public void createBefore(Composite parent) {
		createPatternFilter(parent);
	}

	protected ViewerFilter patternFilter;

	private String currentFilterPattern = ""; //$NON-NLS-1$

	private StructuredViewer viewer;

	protected void createPatternFilter(Composite parent) {
		StringEditor editor = new StringEditor(parent, SWT.NONE);
		editor.setLabel("Filter:"); //$NON-NLS-1$
		editor.setToolTipText(Messages.ElementTypeContentProvider_EnterTheNameOfTheWantedElement);
		editor.setValidateOnDelay(true);
		patternFilter = new PatternViewerFilter();
		((PatternViewerFilter)patternFilter).setPattern(currentFilterPattern);

		editor.addCommitListener(new ICommitListener() {

			public void commit(AbstractEditor editor) {
				String filterPattern = (String)((StringEditor)editor).getValue();
				((PatternViewerFilter)patternFilter).setPattern(filterPattern);
				viewer.refresh();
				if(!("".equals(filterPattern) || currentFilterPattern.equals(filterPattern))) { //$NON-NLS-1$
					Object firstMatch = getFirstMatchingElement(null);
					if(firstMatch != null) {
						//						revealSemanticElement(Collections.singletonList(firstMatch));
					}
					currentFilterPattern = filterPattern;
				}
			}

		});

		List<ViewerFilter> filters = new LinkedList<ViewerFilter>(Arrays.asList(viewer.getFilters()));
		filters.add(patternFilter);
		viewer.setFilters(filters.toArray(new ViewerFilter[filters.size()]));
	}


	public void createAfter(Composite parent) {
	}


	@Override
	public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		super.inputChanged(viewer, oldInput, newInput);
		this.viewer = (StructuredViewer)viewer;
	}

	/**
	 * Returns the first (encapsulated) element matching the current filters
	 * 
	 * @return
	 */
	protected Object getFirstMatchingElement(Object parent) {
		//Browse from the root element
		if(parent == null) {
			for(Object parentElement : getElements(viewer.getInput())) {
				Object firstMatch = getFirstMatchingElement(parentElement);
				if(firstMatch != null) {
					return firstMatch;
				}
			}
			return null;
		}

		for(ViewerFilter filter : viewer.getFilters()) {
			if(!filter.select(viewer, getParent(parent), parent)) {
				return null;
			}
		}

		//Browse the child elements
		for(Object childElement : getChildren(parent)) {
			Object firstMatch = getFirstMatchingElement(childElement);
			if(firstMatch != null) {
				return firstMatch;
			}
		}

		//No match found
		return null;
	}


}

Back to the top