KickJava   Java API By Example, From Geeks To Geeks.

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


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.IFragment;
17 import org.eclipse.pde.core.plugin.IFragmentModel;
18 import org.eclipse.pde.core.plugin.IPluginBase;
19 import org.eclipse.pde.core.plugin.IPluginModel;
20 import org.eclipse.pde.core.plugin.IPluginModelBase;
21 import org.eclipse.pde.core.plugin.ModelEntry;
22 import org.eclipse.pde.core.plugin.PluginRegistry;
23 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
24 import org.w3c.dom.Node JavaDoc;
25
26 public class FeaturePlugin extends FeatureData implements IFeaturePlugin {
27     private static final long serialVersionUID = 1L;
28     private boolean fFragment;
29     private String JavaDoc fVersion;
30     private boolean fUnpack = true;
31
32     public FeaturePlugin() {
33     }
34
35     protected void reset() {
36         super.reset();
37         fVersion = null;
38         fFragment = false;
39     }
40
41     public boolean isFragment() {
42         return fFragment;
43     }
44
45     public IPluginBase getPluginBase() {
46         if (id == null) {
47             return null;
48         }
49         String JavaDoc version = getVersion();
50         IPluginModelBase model = null;
51         if (version == null || version.equals("0.0.0")) //$NON-NLS-1$
52
model = PluginRegistry.findModel(id);
53         else {
54             ModelEntry entry = PluginRegistry.findEntry(id);
55             // if no plug-ins match the id, entry == null
56
if (entry != null) {
57                 IPluginModelBase bases[] = entry.getActiveModels();
58                 for (int i = 0; i < bases.length; i++) {
59                     if (bases[i].getPluginBase().getVersion().equals(version)) {
60                         model = bases[i];
61                         break;
62                     }
63                 }
64             }
65         }
66         if (fFragment && model instanceof IFragmentModel)
67             return model.getPluginBase();
68         if (!fFragment && model instanceof IPluginModel)
69                 return model.getPluginBase();
70         return null;
71     }
72     
73     public String JavaDoc getVersion() {
74         return fVersion;
75     }
76     public boolean isUnpack() {
77         return fUnpack;
78     }
79     public void setVersion(String JavaDoc version) throws CoreException {
80         ensureModelEditable();
81         Object JavaDoc oldValue = this.fVersion;
82         this.fVersion = version;
83         firePropertyChanged(this, P_VERSION, oldValue, version);
84     }
85
86     public void setUnpack(boolean unpack) throws CoreException {
87         ensureModelEditable();
88         boolean oldValue = fUnpack;
89         this.fUnpack = unpack;
90         firePropertyChanged(this, P_UNPACK, new Boolean JavaDoc(oldValue), new Boolean JavaDoc(unpack));
91     }
92
93     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
94         throws CoreException {
95         if (name.equals(P_VERSION)) {
96             setVersion(newValue != null ? newValue.toString() : null);
97         } else
98             super.restoreProperty(name, oldValue, newValue);
99     }
100
101     public void setFragment(boolean fragment) throws CoreException {
102         ensureModelEditable();
103         this.fFragment = fragment;
104     }
105
106     protected void parse(Node JavaDoc node) {
107         super.parse(node);
108         fVersion = getNodeAttribute(node, "version"); //$NON-NLS-1$
109
String JavaDoc f = getNodeAttribute(node, "fragment"); //$NON-NLS-1$
110
if (f != null && f.equalsIgnoreCase("true")) //$NON-NLS-1$
111
fFragment = true;
112         String JavaDoc unpack = getNodeAttribute(node, "unpack"); //$NON-NLS-1$
113
if (unpack != null && unpack.equalsIgnoreCase("false")) //$NON-NLS-1$
114
fUnpack = false;
115     }
116     
117     public void loadFrom(IPluginBase plugin) {
118         id = plugin.getId();
119         label = plugin.getTranslatedName();
120         fVersion = plugin.getVersion();
121         fFragment = plugin instanceof IFragment;
122     }
123
124     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
125         writer.print(indent + "<plugin"); //$NON-NLS-1$
126
String JavaDoc indent2 = indent + Feature.INDENT + Feature.INDENT;
127         writeAttributes(indent2, writer);
128         if (getVersion() != null) {
129             writer.println();
130             writer.print(indent2 + "version=\"" + getVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
131
}
132         if (isFragment()) {
133             writer.println();
134             writer.print(indent2 + "fragment=\"true\""); //$NON-NLS-1$
135
}
136         if (!isUnpack()) {
137             writer.println();
138             writer.print(indent2 + "unpack=\"false\""); //$NON-NLS-1$
139
}
140         writer.println("/>"); //$NON-NLS-1$
141
//writer.println(indent + "</plugin>");
142
}
143
144     public String JavaDoc getLabel() {
145         IPluginBase pluginBase = getPluginBase();
146         if (pluginBase != null) {
147             return pluginBase.getTranslatedName();
148         }
149         String JavaDoc name = super.getLabel();
150         if (name == null)
151             name = getId();
152         return name;
153     }
154
155     public String JavaDoc toString() {
156         return getLabel();
157     }
158     
159 }
160
Popular Tags