001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.core.plugin;
011:
012: import java.util.ArrayList;
013:
014: import org.eclipse.core.runtime.PlatformObject;
015: import org.eclipse.osgi.service.resolver.BundleDescription;
016:
017: /**
018: * A ModelEntry object has an ID and keeps track of all workspace plug-ins and target
019: * plug-ins that have that ID.
020: * <p>
021: * This class is not meant to be extended or instantiated by clients.
022: * </p>
023: *
024: * @since 3.3
025: */
026: public class ModelEntry extends PlatformObject {
027:
028: private String fId;
029: protected ArrayList fWorkspaceEntries = new ArrayList(1);
030: protected ArrayList fExternalEntries = new ArrayList(1);
031:
032: /**
033: * Constructor
034: *
035: * @param id the entry ID
036: */
037: public ModelEntry(String id) {
038: fId = id;
039: }
040:
041: /**
042: * Returns all the workspace plug-ins that have the model entry ID
043: *
044: * @return an array of workspace plug-ins that have the model entry ID
045: */
046: public IPluginModelBase[] getWorkspaceModels() {
047: return (IPluginModelBase[]) fWorkspaceEntries
048: .toArray(new IPluginModelBase[fWorkspaceEntries.size()]);
049: }
050:
051: /**
052: * Returns all plug-ins in the target platform that have the model entry ID.
053: * The returned result contains both plug-ins that are enabled (ie. checked on the
054: * <b>Plug-in Development > Target Platform</b> preference page) and disabled.
055: *
056: * @return an array of plug-ins in the target platform that have the model entry ID
057: */
058: public IPluginModelBase[] getExternalModels() {
059: return (IPluginModelBase[]) fExternalEntries
060: .toArray(new IPluginModelBase[fExternalEntries.size()]);
061: }
062:
063: /**
064: * Returns the plug-in model for the best match plug-in with the given ID.
065: * A null value is returned if no such bundle is found in the workspace or target platform.
066: * <p>
067: * A workspace plug-in is always preferably returned over a target plug-in.
068: * A plug-in that is checked/enabled on the Target Platform preference page is always
069: * preferably returned over a target plug-in that is unchecked/disabled.
070: * </p>
071: * <p>
072: * In the case of a tie among workspace plug-ins or among target plug-ins,
073: * the plug-in with the highest version is returned.
074: * </p>
075: * <p>
076: * In the case of a tie among more than one suitable plug-in that have the same version,
077: * one of those plug-ins is randomly returned.
078: * </p>
079: *
080: * @return the plug-in model for the best match plug-in with the given ID
081: */
082: public IPluginModelBase getModel() {
083: IPluginModelBase model = getBestCandidate(getWorkspaceModels());
084: if (model == null)
085: model = getBestCandidate(getExternalModels());
086: return model;
087: }
088:
089: private IPluginModelBase getBestCandidate(IPluginModelBase[] models) {
090: IPluginModelBase model = null;
091: for (int i = 0; i < models.length; i++) {
092: if (models[i].getBundleDescription() == null)
093: continue;
094:
095: if (model == null) {
096: model = models[i];
097: continue;
098: }
099:
100: if (!model.isEnabled() && models[i].isEnabled()) {
101: model = models[i];
102: continue;
103: }
104:
105: BundleDescription current = model.getBundleDescription();
106: BundleDescription candidate = models[i]
107: .getBundleDescription();
108: if (!current.isResolved() && candidate.isResolved()) {
109: model = models[i];
110: continue;
111: }
112:
113: if (current.getVersion().compareTo(candidate.getVersion()) < 0) {
114: model = models[i];
115: }
116: }
117: return model;
118: }
119:
120: /**
121: * Returns all the plug-ins, with the model entry ID, that are currently active.
122: * <p>
123: * Workspace plug-ins are always active.
124: * Target plug-ins are only active if:
125: * <ul>
126: * <li>they are checked on the <b>Plug-in Development > Target Platform</b> preference page</li>
127: * <li>there does not exist a workspace plug-in that has the same ID</li>
128: * </ul>
129: * </p>
130: *
131: * @return an array of the currently active plug-ins with the model entry ID
132: */
133: public IPluginModelBase[] getActiveModels() {
134: if (fWorkspaceEntries.size() > 0)
135: return getWorkspaceModels();
136:
137: if (fExternalEntries.size() > 0) {
138: ArrayList list = new ArrayList(fExternalEntries.size());
139: for (int i = 0; i < fExternalEntries.size(); i++) {
140: IPluginModelBase model = (IPluginModelBase) fExternalEntries
141: .get(i);
142: if (model.isEnabled())
143: list.add(model);
144: }
145: return (IPluginModelBase[]) list
146: .toArray(new IPluginModelBase[list.size()]);
147: }
148: return new IPluginModelBase[0];
149: }
150:
151: /**
152: * Returns the model entry ID
153: *
154: * @return the model entry ID
155: */
156: public String getId() {
157: return fId;
158: }
159:
160: /**
161: * Return the plug-in model associated with the given bundle description or
162: * <code>null</code> if none is found.
163: *
164: * @param desc the given bundle description
165: *
166: * @return the plug-in model associated with the given bundle description if such a
167: * model exists.
168: */
169: public IPluginModelBase getModel(BundleDescription desc) {
170: if (desc == null)
171: return null;
172:
173: for (int i = 0; i < fWorkspaceEntries.size(); i++) {
174: IPluginModelBase model = (IPluginModelBase) fWorkspaceEntries
175: .get(i);
176: if (desc.equals(model.getBundleDescription()))
177: return model;
178: }
179: for (int i = 0; i < fExternalEntries.size(); i++) {
180: IPluginModelBase model = (IPluginModelBase) fExternalEntries
181: .get(i);
182: if (desc.equals(model.getBundleDescription()))
183: return model;
184: }
185: return null;
186: }
187:
188: /**
189: * Returns <code>true</code> if there are workspace plug-ins associated with the ID
190: * of this model entry; <code>false</code>otherwise.
191: *
192: * @return <code>true</code> if there are workspace plug-ins associated with the ID
193: * of this model entry; <code>false</code>otherwise.
194: */
195: public boolean hasWorkspaceModels() {
196: return !fWorkspaceEntries.isEmpty();
197: }
198:
199: /**
200: * Returns <code>true</code> if there are target plug-ins associated with the ID
201: * of this model entry; <code>false</code>otherwise.
202: *
203: * @return <code>true</code> if there are target plug-ins associated with the ID
204: * of this model entry; <code>false</code>otherwise.
205: */
206: public boolean hasExternalModels() {
207: return !fExternalEntries.isEmpty();
208: }
209:
210: }
|