KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > site > SiteFeature


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.site;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.Vector JavaDoc;
15
16 import org.eclipse.core.resources.IFile;
17 import org.eclipse.core.resources.IProject;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.pde.core.IModelChangedEvent;
22 import org.eclipse.pde.internal.core.isite.ISiteCategory;
23 import org.eclipse.pde.internal.core.isite.ISiteFeature;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.NodeList JavaDoc;
26
27 public class SiteFeature extends VersionableObject implements ISiteFeature {
28     private static final long serialVersionUID = 1L;
29     private Vector JavaDoc fCategories = new Vector JavaDoc();
30     private String JavaDoc fType;
31     private String JavaDoc fUrl;
32     private String JavaDoc fOS;
33     private String JavaDoc fWS;
34     private String JavaDoc fArch;
35     private String JavaDoc fNL;
36     private boolean fIsPatch;
37     
38     public boolean isValid() {
39         if (fUrl == null)
40             return false;
41         for (int i = 0; i < fCategories.size(); i++) {
42             ISiteCategory category = (ISiteCategory) fCategories.get(i);
43             if (!category.isValid())
44                 return false;
45         }
46         return true;
47     }
48
49     /**
50      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#addCategories(org.eclipse.pde.internal.core.isite.ISiteCategory)
51      */

52     public void addCategories(ISiteCategory[] newCategories)
53         throws CoreException {
54         ensureModelEditable();
55         for (int i = 0; i < newCategories.length; i++) {
56             ISiteCategory category = newCategories[i];
57             ((SiteCategory) category).setInTheModel(true);
58             fCategories.add(newCategories[i]);
59         }
60         fireStructureChanged(newCategories, IModelChangedEvent.INSERT);
61     }
62
63     /**
64      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#removeCategories(org.eclipse.pde.internal.core.isite.ISiteCategory)
65      */

66     public void removeCategories(ISiteCategory[] newCategories)
67         throws CoreException {
68         ensureModelEditable();
69         for (int i = 0; i < newCategories.length; i++) {
70             ISiteCategory category = newCategories[i];
71             ((SiteCategory) category).setInTheModel(false);
72             fCategories.remove(newCategories[i]);
73         }
74         fireStructureChanged(newCategories, IModelChangedEvent.REMOVE);
75     }
76     
77     /**
78      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getCategories()
79      */

80     public ISiteCategory[] getCategories() {
81         return (ISiteCategory[]) fCategories.toArray(
82             new ISiteCategory[fCategories.size()]);
83     }
84
85     /**
86      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getType()
87      */

88     public String JavaDoc getType() {
89         return fType;
90     }
91
92     /**
93      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getURL()
94      */

95     public String JavaDoc getURL() {
96         return fUrl;
97     }
98
99     /**
100      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setType(java.lang.String)
101      */

102     public void setType(String JavaDoc type) throws CoreException {
103         ensureModelEditable();
104         Object JavaDoc oldValue = this.fType;
105         this.fType = type;
106         firePropertyChanged(P_TYPE, oldValue, fType);
107     }
108
109     /**
110      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setURL(java.net.URL)
111      */

112     public void setURL(String JavaDoc url) throws CoreException {
113         ensureModelEditable();
114         Object JavaDoc oldValue = this.fUrl;
115         this.fUrl = url;
116         firePropertyChanged(P_TYPE, oldValue, url);
117     }
118
119     protected void parse(Node JavaDoc node) {
120         super.parse(node);
121         fType = getNodeAttribute(node, "type"); //$NON-NLS-1$
122
fUrl = getNodeAttribute(node, "url"); //$NON-NLS-1$
123
fOS = getNodeAttribute(node, "os"); //$NON-NLS-1$
124
fNL = getNodeAttribute(node, "nl"); //$NON-NLS-1$
125
fWS = getNodeAttribute(node, "ws"); //$NON-NLS-1$
126
fArch = getNodeAttribute(node, "arch"); //$NON-NLS-1$
127
String JavaDoc value = getNodeAttribute(node, "patch"); //$NON-NLS-1$
128
fIsPatch = value != null && value.equals("true"); //$NON-NLS-1$
129
NodeList JavaDoc children = node.getChildNodes();
130         for (int i = 0; i < children.getLength(); i++) {
131             Node JavaDoc child = children.item(i);
132             if (child.getNodeType() == Node.ELEMENT_NODE
133                 && child.getNodeName().equalsIgnoreCase("category")) { //$NON-NLS-1$
134
SiteCategory category =
135                     (SiteCategory) getModel().getFactory().createCategory(this);
136                 category.parse(child);
137                 category.setInTheModel(true);
138                 fCategories.add(category);
139             }
140         }
141     }
142
143     protected void reset() {
144         super.reset();
145         fType = null;
146         fUrl = null;
147         fOS = null;
148         fWS = null;
149         fArch = null;
150         fNL = null;
151         fIsPatch = false;
152         fCategories.clear();
153     }
154
155     public void restoreProperty(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue)
156         throws CoreException {
157         if (name.equals(P_TYPE)) {
158             setType(newValue != null ? newValue.toString() : null);
159         } else if (name.equals(P_URL)) {
160             setURL(newValue != null ? newValue.toString() : null);
161         } else if (name.equals(P_ARCH)) {
162             setArch(newValue != null ? newValue.toString() : null);
163         } else if (name.equals(P_NL)) {
164             setNL(newValue != null ? newValue.toString() : null);
165         } else if (name.equals(P_OS)) {
166             setOS(newValue != null ? newValue.toString() : null);
167         } else if (name.equals(P_WS)) {
168             setWS(newValue != null ? newValue.toString() : null);
169         } else if (name.equals(P_PATCH)) {
170             setIsPatch(((Boolean JavaDoc)newValue).booleanValue());
171         } else {
172             super.restoreProperty(name, oldValue, newValue);
173         }
174     }
175
176     /**
177      * @see org.eclipse.pde.core.IWritable#write(java.lang.String, java.io.PrintWriter)
178      */

179     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
180         writer.print(indent);
181         writer.print("<feature"); //$NON-NLS-1$
182
if (fType != null)
183             writer.print(" type=\"" + fType + "\""); //$NON-NLS-1$ //$NON-NLS-2$
184
if (fUrl != null)
185             writer.print(" url=\"" + fUrl + "\""); //$NON-NLS-1$ //$NON-NLS-2$
186
if (id != null)
187             writer.print(" id=\"" + getId() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
188
if (version != null)
189             writer.print(" version=\"" + getVersion() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
190
if (label != null)
191             writer.print(" label=\"" + getLabel() + "\""); //$NON-NLS-1$ //$NON-NLS-2$
192
if (fOS != null)
193             writer.print(" os=\"" + fOS + "\""); //$NON-NLS-1$ //$NON-NLS-2$
194
if (fWS != null)
195             writer.print(" ws=\"" + fWS + "\""); //$NON-NLS-1$ //$NON-NLS-2$
196
if (fNL != null)
197             writer.print(" nl=\"" + fNL + "\""); //$NON-NLS-1$ //$NON-NLS-2$
198
if (fArch != null)
199             writer.print(" arch=\"" + fArch + "\""); //$NON-NLS-1$ //$NON-NLS-2$
200
if (fIsPatch)
201             writer.print(" patch=\"true\""); //$NON-NLS-1$
202
if (fCategories.size() > 0) {
203             writer.println(">"); //$NON-NLS-1$
204
String JavaDoc indent2 = indent + " "; //$NON-NLS-1$
205
for (int i = 0; i < fCategories.size(); i++) {
206                 ISiteCategory category = (ISiteCategory) fCategories.get(i);
207                 category.write(indent2, writer);
208             }
209             writer.println(indent + "</feature>"); //$NON-NLS-1$
210
} else
211             writer.println("/>"); //$NON-NLS-1$
212
}
213     
214     public IFile getArchiveFile() {
215         if (fUrl==null) return null;
216         IResource resource = getModel().getUnderlyingResource();
217         if (resource==null) return null;
218         IProject project = resource.getProject();
219         IFile file = project.getFile(new Path(fUrl));
220         if (file.exists()) return file;
221         return null;
222     }
223
224     /* (non-Javadoc)
225      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getOS()
226      */

227     public String JavaDoc getOS() {
228         return fOS;
229     }
230
231     /* (non-Javadoc)
232      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getNL()
233      */

234     public String JavaDoc getNL() {
235         return fNL;
236     }
237
238     /* (non-Javadoc)
239      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getArch()
240      */

241     public String JavaDoc getArch() {
242         return fArch;
243     }
244
245     /* (non-Javadoc)
246      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getWS()
247      */

248     public String JavaDoc getWS() {
249         return fWS;
250     }
251
252     /* (non-Javadoc)
253      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setOS(java.lang.String)
254      */

255     public void setOS(String JavaDoc os) throws CoreException{
256         ensureModelEditable();
257         Object JavaDoc oldValue = fOS;
258         fOS = os;
259         firePropertyChanged(P_OS, oldValue, fOS);
260     }
261
262     /* (non-Javadoc)
263      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setWS(java.lang.String)
264      */

265     public void setWS(String JavaDoc ws) throws CoreException {
266         ensureModelEditable();
267         Object JavaDoc oldValue = fWS;
268         fWS = ws;
269         firePropertyChanged(P_WS, oldValue, fWS);
270     }
271
272     /* (non-Javadoc)
273      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setArch(java.lang.String)
274      */

275     public void setArch(String JavaDoc arch) throws CoreException {
276         ensureModelEditable();
277         Object JavaDoc oldValue = fArch;
278         fArch = arch;
279         firePropertyChanged(P_ARCH, oldValue, fArch);
280     }
281
282     /* (non-Javadoc)
283      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setNL(java.lang.String)
284      */

285     public void setNL(String JavaDoc nl) throws CoreException {
286         ensureModelEditable();
287         Object JavaDoc oldValue = fNL;
288         fNL = nl;
289         firePropertyChanged(P_NL, oldValue, fNL);
290     }
291
292     /* (non-Javadoc)
293      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#isPatch()
294      */

295     public boolean isPatch() {
296         return fIsPatch;
297     }
298
299     /* (non-Javadoc)
300      * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setIsPatch(boolean)
301      */

302     public void setIsPatch(boolean patch) throws CoreException {
303         ensureModelEditable();
304         Object JavaDoc oldValue = new Boolean JavaDoc(fIsPatch);
305         fIsPatch = patch;
306         firePropertyChanged(P_PATCH, oldValue, new Boolean JavaDoc(fIsPatch));
307     }
308 }
309
Popular Tags