01: /*******************************************************************************
02: * Copyright (c) 2000, 2007 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.pde.internal.core.search;
11:
12: import java.util.ArrayList;
13: import java.util.HashSet;
14:
15: import org.eclipse.pde.core.plugin.IPluginModelBase;
16: import org.eclipse.pde.core.plugin.PluginRegistry;
17:
18: public class PluginSearchScope {
19:
20: public static final int SCOPE_WORKSPACE = 0;
21: public static final int SCOPE_SELECTION = 1;
22: public static final int SCOPE_WORKING_SETS = 2;
23:
24: public static final int EXTERNAL_SCOPE_NONE = 0;
25: public static final int EXTERNAL_SCOPE_ENABLED = 1;
26: public static final int EXTERNAL_SCOPE_ALL = 2;
27:
28: private int workspaceScope;
29: private int externalScope;
30: private HashSet selectedResources;
31:
32: /**
33: * Create a scope object with the provided arguments.
34: * @param workspaceScope one of SCOPE_WORKSPACE, SCOPE_SELECTION,
35: * SCOPE_WORKING_SETS
36: * @param externalScope one of EXTERNAL_SCOPE_NONE, EXTERNAL_SCOPE_ENABLED,
37: * EXTERNAL_SCOPE_ALL
38: * @param workingSets goes with SCOPE_WORKING_SETS, otherwise null
39: */
40: public PluginSearchScope(int workspaceScope, int externalScope,
41: HashSet selectedResources) {
42: this .workspaceScope = workspaceScope;
43: this .externalScope = externalScope;
44: this .selectedResources = selectedResources;
45: }
46:
47: /**
48: * Creates a default scope object that will return all the entries in the
49: * PluginSearchScope. It is equivalent to workspace scope being set to
50: * 'Workspace' and external scope being set to 'Only Enabled'
51: */
52: public PluginSearchScope() {
53: this (SCOPE_WORKSPACE, EXTERNAL_SCOPE_ENABLED, null);
54: }
55:
56: protected final void addExternalModel(IPluginModelBase candidate,
57: ArrayList result) {
58: if (externalScope == EXTERNAL_SCOPE_ALL)
59: result.add(candidate);
60: else if (externalScope == EXTERNAL_SCOPE_ENABLED
61: && candidate.isEnabled())
62: result.add(candidate);
63: }
64:
65: protected final void addWorkspaceModel(IPluginModelBase candidate,
66: ArrayList result) {
67: if (workspaceScope == SCOPE_WORKSPACE) {
68: result.add(candidate);
69: } else if (selectedResources.contains(candidate
70: .getUnderlyingResource().getProject())) {
71: result.add(candidate);
72: }
73: }
74:
75: public IPluginModelBase[] getMatchingModels() {
76: return addRelevantModels(PluginRegistry.getAllModels());
77: }
78:
79: protected final IPluginModelBase[] addRelevantModels(
80: IPluginModelBase[] models) {
81: ArrayList result = new ArrayList();
82: for (int i = 0; i < models.length; i++) {
83: if (models[i].getUnderlyingResource() != null) {
84: addWorkspaceModel(models[i], result);
85: } else {
86: addExternalModel(models[i], result);
87: }
88: }
89: return (IPluginModelBase[]) result
90: .toArray(new IPluginModelBase[result.size()]);
91: }
92:
93: }
|