Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f683e4d8b93245f13e8d32850c2a7a2bcadfbf3a (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
/*****************************************************************************
 * Copyright (c) 2013 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:
 *  CEA LIST - Initial API and implementation
 *  Christian W. Damus (CEA LIST) - Replace workspace IResource dependency with URI for CDO compatibility
 *
 *****************************************************************************/
package org.eclipse.papyrus.views.search.scope;

import static org.eclipse.papyrus.views.search.scope.WorkspaceScopeProvider.findPapyrusModels;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedSet;

import org.eclipse.core.internal.resources.Project;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IAdaptable;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.plugin.RegistryReader;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.papyrus.infra.emf.utils.BusinessModelResolver;
import org.eclipse.papyrus.infra.onefile.model.IPapyrusFile;
//import org.eclipse.papyrus.uml.stereotypecollector.Messages;
//import org.eclipse.papyrus.uml.stereotypecollector.UMLResourceVisitor;
import org.eclipse.papyrus.views.search.Activator;
import org.eclipse.search.ui.ISearchPageContainer;
import org.eclipse.ui.IWorkingSet;

public class ScopeCollector implements IScopeCollector {

	private static ScopeCollector instance = new ScopeCollector();

	private final Iterable<? extends IScopeProvider> scopeProviders;

	private ScopeCollector() {
		super();

		scopeProviders = loadScopeProviders();
	}

	public final static ScopeCollector getInstance() {



		synchronized (ScopeCollector.class) {
			if (ScopeCollector.instance == null) {
				ScopeCollector.instance = new ScopeCollector();

			}
		}
		return ScopeCollector.instance;
	}

	/**
	 * @see org.eclipse.papyrus.views.search.scope.IScopeCollector#computeSearchScope(org.eclipse.search.ui.ISearchPageContainer)
	 *
	 * @param container
	 * @return
	 */
	public Collection<URI> computeSearchScope(ISearchPageContainer container) {

		Set<URI> results = new HashSet<URI>();

		if (container == null) {
			results.addAll(createWorkspaceScope());

		} else {
			switch (container.getSelectedScope()) {
			case ISearchPageContainer.WORKSPACE_SCOPE: {
				results.addAll(createWorkspaceScope());
				break;
			}
			case ISearchPageContainer.SELECTION_SCOPE: {
				ISelection selection = container.getSelection();

				if (!selection.isEmpty()) {
					if (selection instanceof IStructuredSelection) {
						results.addAll(createSelectionScope((IStructuredSelection) selection));
					} else {
						// Do a workspace search instead
						results.addAll(createWorkspaceScope());
					}
				} else {
					// Do a workspace search instead
					results.addAll(createWorkspaceScope());
				}
				break;
			}
			case ISearchPageContainer.SELECTED_PROJECTS_SCOPE: {
				String[] projects = container.getSelectedProjectNames();
				results.addAll(createProjectsScope(projects));
				break;
			}
			case ISearchPageContainer.WORKING_SET_SCOPE: {
				IWorkingSet[] workingSets = container.getSelectedWorkingSets();
				results.addAll(createWorkingSetsScope(workingSets));
				break;
			}
			default: {
				break;
			}
			}
		}

		return results;

	}

	/**
	 * Create a scope when the container is ISearchPageContainer.SELECTION_SCOPE
	 *
	 * @param selection
	 *            the selection of the container
	 * @return
	 *         the scope
	 */
	// Old version, to keep?
	protected List<URI> createSelectionScope(IStructuredSelection selection) {
		List<URI> results = new ArrayList<URI>();

		Iterator<?> it = selection.iterator();
		while (it.hasNext()) {
			Object next = it.next();

			if (!(next instanceof IPapyrusFile) && !(next instanceof IPapyrusFile)) {
				if (next instanceof Project) {
					Project project = (Project) next;
					ArrayList<URI> diFiles = new ArrayList<URI>();
					IPath path = project.getLocation();
					IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
					
					recursiveFindDiFiles(diFiles, path, workspaceRoot);
					results.addAll(diFiles);
				} else {
					Object element = BusinessModelResolver.getInstance().getBusinessModel(next);
					if (element instanceof EObject) {
						// CDO resource *are* EObjects
						Resource eResource = (element instanceof Resource) ? (Resource) element : ((EObject) element).eResource();
						if (eResource != null) {
							// TODO This is a quick fix. Find a more robust solution.
							URI theURI = null;
							if (eResource.getURI().fileExtension().equalsIgnoreCase("uml")) {
								theURI = eResource.getURI().trimFileExtension();
								theURI = theURI.appendFileExtension("di");
							} else {
								theURI = eResource.getURI();
							}
							
							results.add(theURI);
						} else {
							// Do a workspace search instead
							results.addAll(createWorkspaceScope());
						}

					} else {
						// Do a workspace search instead
						results.addAll(createWorkspaceScope());
					}
				}
			} else {
				for (IScopeProvider provider : getScopeProviders()) {
					Collection<URI> scope = provider.getScope(next);
					if (!scope.isEmpty()) {
						results.addAll(scope);

						// don't consult the next provider
						break;
					}
				}
			}
		}

		if (results.isEmpty()) {
			// search the workspace instead, then
			results.addAll(createWorkspaceScope());
		}

		return results;
	}
	
	protected void recursiveFindDiFiles(ArrayList<URI> diFiles, IPath path, IWorkspaceRoot workspaceRoot) {
		IContainer  container =  workspaceRoot.getContainerForLocation(path);

		try {
			IResource[] iResources;
			iResources = container.members();
			for (IResource iResource : iResources){
				// for c files
				if ("di".equalsIgnoreCase(iResource.getFileExtension())) {
					//diFiles.add(iResource.getLocationURI());
					URI theURI = URI.createPlatformResourceURI(iResource.getFullPath().toString(), true);
					diFiles.add(theURI);
				} else if (iResource.getType() == IResource.FOLDER) {
					IPath tmpPath = iResource.getLocation();
					recursiveFindDiFiles(diFiles, tmpPath, workspaceRoot);
				}
			}
		} catch (CoreException e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create a scope when the container is ISearchPageContainer.SELECTED_PROJECTS_SCOPE
	 *
	 * @param projects
	 *            the selected scope
	 * @return
	 *         the scope
	 */
	protected List<URI> createProjectsScope(String[] projects) {
		List<URI> results = new ArrayList<URI>();

		for (String projectName : projects) {
			IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName);
			if (project.isOpen()) {
				results.addAll(findPapyrusModels(project));
			}
		}
		return results;
	}

	/**
	 * Create a scope when the container is ISearchPageContainer.WORKING_SET_SCOPE
	 *
	 * @param workingSets
	 *            the selected workingSets
	 * @return
	 *         the scope
	 */
	protected List<URI> createWorkingSetsScope(IWorkingSet[] workingSets) {
		List<URI> results = new ArrayList<URI>();

		if (workingSets != null && workingSets.length > 0) {
			for (IWorkingSet iWorkingSet : workingSets) {
				for (IAdaptable element : iWorkingSet.getElements()) {
					Object resource = element.getAdapter(IResource.class);
					if (resource instanceof IResource) {
						results.addAll(findPapyrusModels((IResource) resource));
					}
				}
			}
		}

		return results;
	}

	/**
	 * Create a scope when the container is ISearchPageContainer.WORKSPACE_SCOPE
	 *
	 * @return
	 *         the scope
	 */
	protected Collection<URI> createWorkspaceScope() {
		Collection<URI> result = new ArrayList<URI>();

		for (IScopeProvider next : getScopeProviders()) {
			result.addAll(next.getScope());
		}

		return result;
	}

	private Iterable<? extends IScopeProvider> loadScopeProviders() {
		return new ProvidersReader().load();
	}

	final Iterable<? extends IScopeProvider> getScopeProviders() {
		List<IScopeProvider> result = new ArrayList<IScopeProvider>();

		synchronized (scopeProviders) {
			for (IScopeProvider next : scopeProviders) {
				result.add(next);
			}
		}

		return result;
	}

	//
	// Nested types
	//

	private static class PriorityScopeProvider implements IScopeProvider, Comparable<PriorityScopeProvider> {
		private final IScopeProvider delegate;

		private final int priority;

		public PriorityScopeProvider(IScopeProvider delegate, int priority) {
			this.delegate = delegate;
			this.priority = priority;
		}

		public int compareTo(PriorityScopeProvider o) {
			// sort by descending priority
			return o.priority - this.priority;
		}

		@Override
		public int hashCode() {
			return delegate.hashCode();
		}

		@Override
		public boolean equals(Object obj) {
			return (obj instanceof PriorityScopeProvider) && ((PriorityScopeProvider) obj).delegate.equals(delegate);
		}

		//
		// API delegation
		//

		public Collection<URI> getScope() {
			return delegate.getScope();
		}

		public Collection<URI> getScope(Object selected) {
			return delegate.getScope(selected);
		}
	}

	private class ProvidersReader extends RegistryReader {
		private static final String EXT_PT = "scopeProviders"; //$NON-NLS-1$

		private static final String TAG_PROVIDER = "scopeProvider"; //$NON-NLS-1$

		private static final String ATTR_CLASS = "class"; //$NON-NLS-1$

		private static final String ATTR_PRIORITY = "priority"; //$NON-NLS-1$

		private final SortedSet<PriorityScopeProvider> providers = new java.util.TreeSet<PriorityScopeProvider>();

		ProvidersReader() {
			super(Platform.getExtensionRegistry(), Activator.PLUGIN_ID, EXT_PT);
		}

		Iterable<? extends IScopeProvider> load() {
			synchronized (providers) {
				providers.clear();
				readRegistry();
			}

			return providers;
		}

		@Override
		protected boolean readElement(IConfigurationElement element, boolean add) {
			boolean result = false;

			if (TAG_PROVIDER.equals(element.getName())) {
				result = true;

				String className = element.getAttribute(ATTR_CLASS);
				if ((className == null) || (className.length() == 0)) {
					logMissingAttribute(element, ATTR_CLASS);
				} else if (add) {
					addProvider(element, className);
				} else {
					removeProvider(element, className);
				}
			}

			return result;
		}

		private void addProvider(IConfigurationElement element, String className) {
			try {
				Object provider = element.createExecutableExtension(ATTR_CLASS);

				if (!(provider instanceof IScopeProvider)) {
					Activator.log.error("Scope provider extension does not implement IScopeProvider interface: " + className, null); //$NON-NLS-1$
				} else {
					String priorityString = element.getAttribute(ATTR_PRIORITY);
					int priority = 0;

					try {
						if ((priorityString) != null && (priorityString.length() > 0)) {
							priority = Integer.parseInt(priorityString);
							if (priority < 0) {
								Activator.log.warn("Negative priority in scope provider " + className); //$NON-NLS-1$
								priority = 0;
							}
						}
					} catch (NumberFormatException e) {
						Activator.log.warn("Not an integer priority in scope provider " + className); //$NON-NLS-1$
					}

					synchronized (providers) {
						providers.add(new PriorityScopeProvider((IScopeProvider) provider, priority));
					}
				}
			} catch (CoreException e) {
				Activator.getDefault().getLog().log(e.getStatus());
			}
		}

		private void removeProvider(IConfigurationElement element, String className) {
			synchronized (providers) {
				for (Iterator<PriorityScopeProvider> iter = providers.iterator(); iter.hasNext();) {
					if (iter.next().delegate.getClass().getName().equals(className)) {
						iter.remove();
					}
				}
			}
		}
	}
}

Back to the top