KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > core > plugin > ModelEntry


1 /*******************************************************************************
2  * Copyright (c) 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.core.plugin;
12
13 import java.util.ArrayList JavaDoc;
14
15 import org.eclipse.core.runtime.PlatformObject;
16 import org.eclipse.osgi.service.resolver.BundleDescription;
17
18 /**
19  * A ModelEntry object has an ID and keeps track of all workspace plug-ins and target
20  * plug-ins that have that ID.
21  * <p>
22  * This class is not meant to be extended or instantiated by clients.
23  * </p>
24  *
25  * @since 3.3
26  */

27 public class ModelEntry extends PlatformObject {
28     
29     private String JavaDoc fId;
30     protected ArrayList JavaDoc fWorkspaceEntries = new ArrayList JavaDoc(1);
31     protected ArrayList JavaDoc fExternalEntries = new ArrayList JavaDoc(1);
32
33     /**
34      * Constructor
35      *
36      * @param id the entry ID
37      */

38     public ModelEntry(String JavaDoc id) {
39         fId = id;
40     }
41     
42     /**
43      * Returns all the workspace plug-ins that have the model entry ID
44      *
45      * @return an array of workspace plug-ins that have the model entry ID
46      */

47     public IPluginModelBase[] getWorkspaceModels() {
48         return (IPluginModelBase[])fWorkspaceEntries.toArray(new IPluginModelBase[fWorkspaceEntries.size()]);
49     }
50     
51     /**
52      * Returns all plug-ins in the target platform that have the model entry ID.
53      * The returned result contains both plug-ins that are enabled (ie. checked on the
54      * <b>Plug-in Development > Target Platform</b> preference page) and disabled.
55      *
56      * @return an array of plug-ins in the target platform that have the model entry ID
57      */

58     public IPluginModelBase[] getExternalModels() {
59         return (IPluginModelBase[])fExternalEntries.toArray(new IPluginModelBase[fExternalEntries.size()]);
60     }
61     
62     /**
63      * Returns the plug-in model for the best match plug-in with the given ID.
64      * A null value is returned if no such bundle is found in the workspace or target platform.
65      * <p>
66      * A workspace plug-in is always preferably returned over a target plug-in.
67      * A plug-in that is checked/enabled on the Target Platform preference page is always
68      * preferably returned over a target plug-in that is unchecked/disabled.
69      * </p>
70      * <p>
71      * In the case of a tie among workspace plug-ins or among target plug-ins,
72      * the plug-in with the highest version is returned.
73      * </p>
74      * <p>
75      * In the case of a tie among more than one suitable plug-in that have the same version,
76      * one of those plug-ins is randomly returned.
77      * </p>
78      *
79      * @return the plug-in model for the best match plug-in with the given ID
80      */

81     public IPluginModelBase getModel() {
82         IPluginModelBase model = getBestCandidate(getWorkspaceModels());
83         if (model == null)
84             model = getBestCandidate(getExternalModels());
85         return model;
86     }
87     
88     private IPluginModelBase getBestCandidate(IPluginModelBase[] models) {
89         IPluginModelBase model = null;
90         for (int i = 0; i < models.length; i++) {
91             if (models[i].getBundleDescription() == null)
92                 continue;
93             
94             if (model == null) {
95                 model = models[i];
96                 continue;
97             }
98             
99             if (!model.isEnabled() && models[i].isEnabled()) {
100                 model = models[i];
101                 continue;
102             }
103             
104             BundleDescription current = model.getBundleDescription();
105             BundleDescription candidate = models[i].getBundleDescription();
106             if (!current.isResolved() && candidate.isResolved()) {
107                 model = models[i];
108                 continue;
109             }
110             
111             if (current.getVersion().compareTo(candidate.getVersion()) < 0) {
112                 model = models[i];
113             }
114         }
115         return model;
116     }
117     
118     /**
119      * Returns all the plug-ins, with the model entry ID, that are currently active.
120      * <p>
121      * Workspace plug-ins are always active.
122      * Target plug-ins are only active if:
123      * <ul>
124      * <li>they are checked on the <b>Plug-in Development > Target Platform</b> preference page</li>
125      * <li>there does not exist a workspace plug-in that has the same ID</li>
126      * </ul>
127      * </p>
128      *
129      * @return an array of the currently active plug-ins with the model entry ID
130      */

131     public IPluginModelBase[] getActiveModels() {
132         if (fWorkspaceEntries.size() > 0)
133             return getWorkspaceModels();
134         
135         if (fExternalEntries.size() > 0) {
136             ArrayList JavaDoc list = new ArrayList JavaDoc(fExternalEntries.size());
137             for (int i = 0; i < fExternalEntries.size(); i++) {
138                 IPluginModelBase model = (IPluginModelBase)fExternalEntries.get(i);
139                 if (model.isEnabled())
140                     list.add(model);
141             }
142             return (IPluginModelBase[])list.toArray(new IPluginModelBase[list.size()]);
143         }
144         return new IPluginModelBase[0];
145     }
146     
147     /**
148      * Returns the model entry ID
149      *
150      * @return the model entry ID
151      */

152     public String JavaDoc getId() {
153         return fId;
154     }
155     
156     /**
157      * Return the plug-in model associated with the given bundle description or
158      * <code>null</code> if none is found.
159      *
160      * @param desc the given bundle description
161      *
162      * @return the plug-in model associated with the given bundle description if such a
163      * model exists.
164      */

165     public IPluginModelBase getModel(BundleDescription desc) {
166         if (desc == null)
167             return null;
168         
169         for (int i = 0; i < fWorkspaceEntries.size(); i++) {
170             IPluginModelBase model = (IPluginModelBase)fWorkspaceEntries.get(i);
171             if (desc.equals(model.getBundleDescription()))
172                 return model;
173         }
174         for (int i = 0; i < fExternalEntries.size(); i++) {
175             IPluginModelBase model = (IPluginModelBase)fExternalEntries.get(i);
176             if (desc.equals(model.getBundleDescription()))
177                 return model;
178         }
179         return null;
180     }
181     
182     /**
183      * Returns <code>true</code> if there are workspace plug-ins associated with the ID
184      * of this model entry; <code>false</code>otherwise.
185      *
186      * @return <code>true</code> if there are workspace plug-ins associated with the ID
187      * of this model entry; <code>false</code>otherwise.
188      */

189     public boolean hasWorkspaceModels() {
190         return !fWorkspaceEntries.isEmpty();
191     }
192     
193     /**
194      * Returns <code>true</code> if there are target plug-ins associated with the ID
195      * of this model entry; <code>false</code>otherwise.
196      *
197      * @return <code>true</code> if there are target plug-ins associated with the ID
198      * of this model entry; <code>false</code>otherwise.
199      */

200     public boolean hasExternalModels() {
201         return !fExternalEntries.isEmpty();
202     }
203     
204 }
205
Popular Tags