001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 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.internal.core.site;
011:
012: import java.io.PrintWriter;
013: import java.util.Vector;
014:
015: import org.eclipse.core.resources.IFile;
016: import org.eclipse.core.resources.IProject;
017: import org.eclipse.core.resources.IResource;
018: import org.eclipse.core.runtime.CoreException;
019: import org.eclipse.core.runtime.Path;
020: import org.eclipse.pde.core.IModelChangedEvent;
021: import org.eclipse.pde.internal.core.isite.ISiteCategory;
022: import org.eclipse.pde.internal.core.isite.ISiteFeature;
023: import org.w3c.dom.Node;
024: import org.w3c.dom.NodeList;
025:
026: public class SiteFeature extends VersionableObject implements
027: ISiteFeature {
028: private static final long serialVersionUID = 1L;
029: private Vector fCategories = new Vector();
030: private String fType;
031: private String fUrl;
032: private String fOS;
033: private String fWS;
034: private String fArch;
035: private String fNL;
036: private boolean fIsPatch;
037:
038: public boolean isValid() {
039: if (fUrl == null)
040: return false;
041: for (int i = 0; i < fCategories.size(); i++) {
042: ISiteCategory category = (ISiteCategory) fCategories.get(i);
043: if (!category.isValid())
044: return false;
045: }
046: return true;
047: }
048:
049: /**
050: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#addCategories(org.eclipse.pde.internal.core.isite.ISiteCategory)
051: */
052: public void addCategories(ISiteCategory[] newCategories)
053: throws CoreException {
054: ensureModelEditable();
055: for (int i = 0; i < newCategories.length; i++) {
056: ISiteCategory category = newCategories[i];
057: ((SiteCategory) category).setInTheModel(true);
058: fCategories.add(newCategories[i]);
059: }
060: fireStructureChanged(newCategories, IModelChangedEvent.INSERT);
061: }
062:
063: /**
064: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#removeCategories(org.eclipse.pde.internal.core.isite.ISiteCategory)
065: */
066: public void removeCategories(ISiteCategory[] newCategories)
067: throws CoreException {
068: ensureModelEditable();
069: for (int i = 0; i < newCategories.length; i++) {
070: ISiteCategory category = newCategories[i];
071: ((SiteCategory) category).setInTheModel(false);
072: fCategories.remove(newCategories[i]);
073: }
074: fireStructureChanged(newCategories, IModelChangedEvent.REMOVE);
075: }
076:
077: /**
078: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getCategories()
079: */
080: public ISiteCategory[] getCategories() {
081: return (ISiteCategory[]) fCategories
082: .toArray(new ISiteCategory[fCategories.size()]);
083: }
084:
085: /**
086: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getType()
087: */
088: public String getType() {
089: return fType;
090: }
091:
092: /**
093: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getURL()
094: */
095: public String getURL() {
096: return fUrl;
097: }
098:
099: /**
100: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setType(java.lang.String)
101: */
102: public void setType(String type) throws CoreException {
103: ensureModelEditable();
104: Object 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 url) throws CoreException {
113: ensureModelEditable();
114: Object oldValue = this .fUrl;
115: this .fUrl = url;
116: firePropertyChanged(P_TYPE, oldValue, url);
117: }
118:
119: protected void parse(Node 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 value = getNodeAttribute(node, "patch"); //$NON-NLS-1$
128: fIsPatch = value != null && value.equals("true"); //$NON-NLS-1$
129: NodeList children = node.getChildNodes();
130: for (int i = 0; i < children.getLength(); i++) {
131: Node child = children.item(i);
132: if (child.getNodeType() == Node.ELEMENT_NODE
133: && child.getNodeName().equalsIgnoreCase("category")) { //$NON-NLS-1$
134: SiteCategory category = (SiteCategory) getModel()
135: .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 name, Object oldValue,
156: Object newValue) 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) 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 indent, PrintWriter 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 indent2 = indent + " "; //$NON-NLS-1$
205: for (int i = 0; i < fCategories.size(); i++) {
206: ISiteCategory category = (ISiteCategory) fCategories
207: .get(i);
208: category.write(indent2, writer);
209: }
210: writer.println(indent + "</feature>"); //$NON-NLS-1$
211: } else
212: writer.println("/>"); //$NON-NLS-1$
213: }
214:
215: public IFile getArchiveFile() {
216: if (fUrl == null)
217: return null;
218: IResource resource = getModel().getUnderlyingResource();
219: if (resource == null)
220: return null;
221: IProject project = resource.getProject();
222: IFile file = project.getFile(new Path(fUrl));
223: if (file.exists())
224: return file;
225: return null;
226: }
227:
228: /* (non-Javadoc)
229: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getOS()
230: */
231: public String getOS() {
232: return fOS;
233: }
234:
235: /* (non-Javadoc)
236: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getNL()
237: */
238: public String getNL() {
239: return fNL;
240: }
241:
242: /* (non-Javadoc)
243: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getArch()
244: */
245: public String getArch() {
246: return fArch;
247: }
248:
249: /* (non-Javadoc)
250: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#getWS()
251: */
252: public String getWS() {
253: return fWS;
254: }
255:
256: /* (non-Javadoc)
257: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setOS(java.lang.String)
258: */
259: public void setOS(String os) throws CoreException {
260: ensureModelEditable();
261: Object oldValue = fOS;
262: fOS = os;
263: firePropertyChanged(P_OS, oldValue, fOS);
264: }
265:
266: /* (non-Javadoc)
267: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setWS(java.lang.String)
268: */
269: public void setWS(String ws) throws CoreException {
270: ensureModelEditable();
271: Object oldValue = fWS;
272: fWS = ws;
273: firePropertyChanged(P_WS, oldValue, fWS);
274: }
275:
276: /* (non-Javadoc)
277: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setArch(java.lang.String)
278: */
279: public void setArch(String arch) throws CoreException {
280: ensureModelEditable();
281: Object oldValue = fArch;
282: fArch = arch;
283: firePropertyChanged(P_ARCH, oldValue, fArch);
284: }
285:
286: /* (non-Javadoc)
287: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setNL(java.lang.String)
288: */
289: public void setNL(String nl) throws CoreException {
290: ensureModelEditable();
291: Object oldValue = fNL;
292: fNL = nl;
293: firePropertyChanged(P_NL, oldValue, fNL);
294: }
295:
296: /* (non-Javadoc)
297: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#isPatch()
298: */
299: public boolean isPatch() {
300: return fIsPatch;
301: }
302:
303: /* (non-Javadoc)
304: * @see org.eclipse.pde.internal.core.isite.ISiteFeature#setIsPatch(boolean)
305: */
306: public void setIsPatch(boolean patch) throws CoreException {
307: ensureModelEditable();
308: Object oldValue = new Boolean(fIsPatch);
309: fIsPatch = patch;
310: firePropertyChanged(P_PATCH, oldValue, new Boolean(fIsPatch));
311: }
312: }
|