KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > feature > FeatureImport


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.internal.core.feature;
12
13 import java.io.PrintWriter JavaDoc;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.pde.core.plugin.IPlugin;
17 import org.eclipse.pde.core.plugin.IPluginModel;
18 import org.eclipse.pde.core.plugin.IPluginModelBase;
19 import org.eclipse.pde.core.plugin.PluginRegistry;
20 import org.eclipse.pde.internal.core.PDECore;
21 import org.eclipse.pde.internal.core.ifeature.IFeature;
22 import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
23 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
24 import org.eclipse.pde.internal.core.util.VersionUtil;
25 import org.w3c.dom.Node JavaDoc;
26
27 public class FeatureImport
28     extends VersionableObject
29     implements IFeatureImport {
30     private static final long serialVersionUID = 1L;
31     private int fMatch = NONE;
32     private int fIdMatch = PERFECT;
33     private int fType = PLUGIN;
34     private boolean fPatch = false;
35
36     public FeatureImport() {
37     }
38
39     public IPlugin getPlugin() {
40         if (id != null && fType == PLUGIN) {
41             IPluginModelBase model = PluginRegistry.findModel(id);
42             return model instanceof IPluginModel ? ((IPluginModel)model).getPlugin() : null;
43         }
44         return null;
45     }
46
47     public IFeature getFeature() {
48         if (id != null && fType == FEATURE) {
49             return findFeature(id, getVersion(), fMatch);
50         }
51         return null;
52     }
53
54     private IFeature findFeature(
55             IFeatureModel[] models,
56             String JavaDoc id,
57             String JavaDoc version,
58             int match) {
59
60             for (int i = 0; i < models.length; i++) {
61                 IFeatureModel model = models[i];
62
63                 IFeature feature = model.getFeature();
64                 String JavaDoc pid = feature.getId();
65                 String JavaDoc pversion = feature.getVersion();
66                 if (VersionUtil.compare(id, version, pid, pversion, match))
67                     return feature;
68             }
69             return null;
70         }
71
72         /**
73          * Finds a feature with the given ID and satisfying constraints
74          * of the version and the match.
75          * @param id
76          * @param version
77          * @param match
78          * @return IFeature or null
79          */

80         public IFeature findFeature(String JavaDoc id, String JavaDoc version, int match) {
81             IFeatureModel[] models = PDECore.getDefault().getFeatureModelManager().findFeatureModels(id);
82             return findFeature(models, id, version, match);
83         }
84
85     public int getIdMatch() {
86         return fIdMatch;
87     }
88
89     protected void reset() {
90         super.reset();
91         fPatch = false;
92         fType = PLUGIN;
93         fMatch = NONE;
94         fIdMatch = PERFECT;
95     }
96
97     protected void parse(Node JavaDoc node) {
98         super.parse(node);
99         this.id = getNodeAttribute(node, "plugin"); //$NON-NLS-1$
100
if (id != null)
101             fType = PLUGIN;
102         else {
103             this.id = getNodeAttribute(node, "feature"); //$NON-NLS-1$
104
if (id != null)
105                 fType = FEATURE;
106         }
107         String JavaDoc mvalue = getNodeAttribute(node, "match"); //$NON-NLS-1$
108
if (mvalue != null && mvalue.length() > 0) {
109             String JavaDoc[] choices = RULE_NAME_TABLE;
110             for (int i = 0; i < choices.length; i++) {
111                 if (mvalue.equalsIgnoreCase(choices[i])) {
112                     fMatch = i;
113                     break;
114                 }
115             }
116         }
117         mvalue = getNodeAttribute(node, "id-match"); //$NON-NLS-1$
118

119         if (mvalue != null && mvalue.length() > 0) {
120             if (mvalue.equalsIgnoreCase(RULE_PREFIX))
121                 fIdMatch = PREFIX;
122         }
123         fPatch = getBooleanAttribute(node, "patch"); //$NON-NLS-1$
124
}
125
126     public void loadFrom(IFeature feature) {
127         reset();
128         fType = FEATURE;
129         id = feature.getId();
130         version = feature.getVersion();
131     }
132
133     public int getMatch() {
134         return fMatch;
135     }
136
137     public void setMatch(int match) throws CoreException {
138         ensureModelEditable();
139         Integer JavaDoc oldValue = new Integer JavaDoc(this.fMatch);
140         this.fMatch = match;
141         firePropertyChanged(P_MATCH, oldValue, new Integer JavaDoc(match));
142     }
143
144     public void setIdMatch(int idMatch) throws CoreException {
145         ensureModelEditable();
146         Integer JavaDoc oldValue = new Integer JavaDoc(this.fIdMatch);
147         this.fIdMatch = idMatch;
148         firePropertyChanged(P_ID_MATCH, oldValue, new Integer JavaDoc(idMatch));
149     }
150
151     public int getType() {
152         return fType;
153     }
154
155     public void setType(int type) throws CoreException {
156         ensureModelEditable();
157         Integer JavaDoc oldValue = new Integer JavaDoc(this.fType);
158         this.fType = type;
159         firePropertyChanged(P_TYPE, oldValue, new Integer JavaDoc(type));
160     }
161
162     public boolean isPatch() {
163         return fPatch;
164     }
165
166     public void setPatch(boolean patch) throws CoreException {
167         ensureModelEditable();
168         Boolean JavaDoc oldValue = new Boolean JavaDoc(this.fPatch);
169         this.fPatch = patch;
170         firePropertyChanged(P_PATCH, oldValue, new Boolean JavaDoc(patch));
171     }
172
173     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
174         throws CoreException {
175         if (name.equals(P_MATCH)) {
176             setMatch(newValue != null ? ((Integer JavaDoc) newValue).intValue() : 0);
177         } else if (name.equals(P_ID_MATCH)) {
178             setIdMatch(newValue != null ? ((Integer JavaDoc) newValue).intValue() : 0);
179         } else if (name.equals(P_TYPE)) {
180             setType(
181                 newValue != null ? ((Integer JavaDoc) newValue).intValue() : PLUGIN);
182         } else if (name.equals(P_PATCH)) {
183             setPatch(
184                 newValue != null ? ((Boolean JavaDoc) newValue).booleanValue() : false);
185         } else
186             super.restoreProperty(name, oldValue, newValue);
187     }
188
189     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
190         String JavaDoc typeAtt = fType == FEATURE ? "feature" : "plugin"; //$NON-NLS-1$ //$NON-NLS-2$
191
writer.print(indent + "<import " + typeAtt + "=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
192
String JavaDoc version = getVersion();
193         if (version != null && version.length() > 0) {
194             writer.print(" version=\"" + version + "\""); //$NON-NLS-1$ //$NON-NLS-2$
195
}
196         if (!fPatch && fMatch != NONE) {
197             writer.print(" match=\"" + RULE_NAME_TABLE[fMatch] + "\""); //$NON-NLS-1$ //$NON-NLS-2$
198
}
199         if (fIdMatch == PREFIX) {
200             writer.print(" id-match=\"prefix\""); //$NON-NLS-1$
201
}
202         if (fPatch) {
203             writer.print(" patch=\"true\""); //$NON-NLS-1$
204
}
205         writer.println("/>"); //$NON-NLS-1$
206
}
207
208     public String JavaDoc toString() {
209         IPlugin plugin = getPlugin();
210         if (plugin != null)
211             return plugin.getTranslatedName();
212         IFeature feature = getFeature();
213         if (feature != null)
214             return feature.getLabel();
215         return getId();
216     }
217 }
218
Popular Tags