001: /*******************************************************************************
002: * Copyright (c) 2005, 2007 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.product;
011:
012: import java.io.PrintWriter;
013: import java.util.ArrayList;
014: import java.util.Iterator;
015: import java.util.List;
016: import java.util.TreeMap;
017:
018: import org.eclipse.pde.core.IModelChangedEvent;
019: import org.eclipse.pde.internal.core.iproduct.IAboutInfo;
020: import org.eclipse.pde.internal.core.iproduct.IArgumentsInfo;
021: import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo;
022: import org.eclipse.pde.internal.core.iproduct.IIntroInfo;
023: import org.eclipse.pde.internal.core.iproduct.IJREInfo;
024: import org.eclipse.pde.internal.core.iproduct.ILauncherInfo;
025: import org.eclipse.pde.internal.core.iproduct.IProduct;
026: import org.eclipse.pde.internal.core.iproduct.IProductFeature;
027: import org.eclipse.pde.internal.core.iproduct.IProductModel;
028: import org.eclipse.pde.internal.core.iproduct.IProductModelFactory;
029: import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
030: import org.eclipse.pde.internal.core.iproduct.ISplashInfo;
031: import org.eclipse.pde.internal.core.iproduct.IWindowImages;
032: import org.w3c.dom.Element;
033: import org.w3c.dom.Node;
034: import org.w3c.dom.NodeList;
035:
036: public class Product extends ProductObject implements IProduct {
037:
038: private static final long serialVersionUID = 1L;
039: private String fId;
040: private String fName;
041: private String fApplication;
042: private IAboutInfo fAboutInfo;
043:
044: private TreeMap fPlugins = new TreeMap();
045: private List fFeatures = new ArrayList();
046: private IConfigurationFileInfo fConfigIniInfo;
047: private IJREInfo fJVMInfo;
048: private boolean fUseFeatures;
049: private IWindowImages fWindowImages;
050: private ISplashInfo fSplashInfo;
051: private ILauncherInfo fLauncherInfo;
052: private IArgumentsInfo fLauncherArgs;
053: private IIntroInfo fIntroInfo;
054:
055: public Product(IProductModel model) {
056: super (model);
057: }
058:
059: /* (non-Javadoc)
060: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getId()
061: */
062: public String getId() {
063: return fId;
064: }
065:
066: /* (non-Javadoc)
067: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getName()
068: */
069: public String getName() {
070: return fName;
071: }
072:
073: /* (non-Javadoc)
074: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getApplication()
075: */
076: public String getApplication() {
077: return fApplication;
078: }
079:
080: /* (non-Javadoc)
081: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getDefiningPluginId()
082: */
083: public String getDefiningPluginId() {
084: if (fId == null)
085: return null;
086: int dot = fId.lastIndexOf('.');
087: return (dot != -1) ? fId.substring(0, dot) : null;
088: }
089:
090: /* (non-Javadoc)
091: * @see org.eclipse.pde.internal.core.iproduct.IProduct#setId(java.lang.String)
092: */
093: public void setId(String id) {
094: String old = fId;
095: fId = id;
096: if (isEditable())
097: firePropertyChanged(P_ID, old, fId);
098: }
099:
100: /* (non-Javadoc)
101: * @see org.eclipse.pde.internal.core.iproduct.IProduct#setName(java.lang.String)
102: */
103: public void setName(String name) {
104: String old = fName;
105: fName = name;
106: if (isEditable())
107: firePropertyChanged(P_NAME, old, fName);
108: }
109:
110: /* (non-Javadoc)
111: * @see org.eclipse.pde.internal.core.iproduct.IProduct#setAboutInfo(org.eclipse.pde.internal.core.iproduct.IAboutInfo)
112: */
113: public void setAboutInfo(IAboutInfo info) {
114: fAboutInfo = info;
115: }
116:
117: /* (non-Javadoc)
118: * @see org.eclipse.pde.internal.core.iproduct.IProduct#setApplication(java.lang.String)
119: */
120: public void setApplication(String application) {
121: String old = fApplication;
122: fApplication = application;
123: if (isEditable())
124: firePropertyChanged(P_APPLICATION, old, fApplication);
125: }
126:
127: /* (non-Javadoc)
128: * @see org.eclipse.pde.internal.core.product.ProductObject#write(java.lang.String, java.io.PrintWriter)
129: */
130: public void write(String indent, PrintWriter writer) {
131: writer.print(indent + "<product"); //$NON-NLS-1$
132: if (fName != null && fName.length() > 0)
133: writer
134: .print(" " + P_NAME + "=\"" + getWritableString(fName) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
135: if (fId != null && fId.length() > 0)
136: writer.print(" " + P_ID + "=\"" + fId + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
137: if (fApplication != null && fApplication.length() > 0)
138: writer
139: .print(" " + P_APPLICATION + "=\"" + fApplication + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
140: writer
141: .print(" " + P_USEFEATURES + "=\"" + Boolean.toString(fUseFeatures) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
142: writer.println(">"); //$NON-NLS-1$
143:
144: if (fAboutInfo != null) {
145: writer.println();
146: fAboutInfo.write(indent + " ", writer); //$NON-NLS-1$
147: }
148:
149: if (fConfigIniInfo != null) {
150: writer.println();
151: fConfigIniInfo.write(indent + " ", writer); //$NON-NLS-1$
152: }
153:
154: if (fLauncherArgs != null) {
155: writer.println();
156: fLauncherArgs.write(indent + " ", writer); //$NON-NLS-1$
157: }
158:
159: if (fWindowImages != null) {
160: writer.println();
161: fWindowImages.write(indent + " ", writer); //$NON-NLS-1$
162: }
163:
164: if (fSplashInfo != null) {
165: writer.println();
166: fSplashInfo.write(indent + " ", writer); //$NON-NLS-1$
167: }
168:
169: if (fLauncherInfo != null) {
170: writer.println();
171: fLauncherInfo.write(indent + " ", writer); //$NON-NLS-1$
172: }
173:
174: if (fIntroInfo != null) {
175: writer.println();
176: fIntroInfo.write(indent + " ", writer); //$NON-NLS-1$
177: }
178:
179: if (fJVMInfo != null) {
180: writer.println();
181: fJVMInfo.write(indent + " ", writer); //$NON-NLS-1$
182: }
183:
184: writer.println();
185: writer.println(indent + " <plugins>"); //$NON-NLS-1$
186: Iterator iter = fPlugins.values().iterator();
187: while (iter.hasNext()) {
188: IProductPlugin plugin = (IProductPlugin) iter.next();
189: plugin.write(indent + " ", writer); //$NON-NLS-1$
190: }
191: writer.println(indent + " </plugins>"); //$NON-NLS-1$
192:
193: if (fFeatures.size() > 0) {
194: writer.println();
195: writer.println(indent + " <features>"); //$NON-NLS-1$
196: iter = fFeatures.iterator();
197: while (iter.hasNext()) {
198: IProductFeature feature = (IProductFeature) iter.next();
199: feature.write(indent + " ", writer); //$NON-NLS-1$
200: }
201: writer.println(indent + " </features>"); //$NON-NLS-1$
202: }
203:
204: writer.println();
205: writer.println("</product>"); //$NON-NLS-1$
206: }
207:
208: /* (non-Javadoc)
209: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getAboutInfo()
210: */
211: public IAboutInfo getAboutInfo() {
212: return fAboutInfo;
213: }
214:
215: /* (non-Javadoc)
216: * @see org.eclipse.pde.internal.core.iproduct.IProduct#reset()
217: */
218: public void reset() {
219: fApplication = null;
220: fId = null;
221: fName = null;
222: fUseFeatures = false;
223: fAboutInfo = null;
224: fPlugins.clear();
225: fFeatures.clear();
226: fConfigIniInfo = null;
227: fWindowImages = null;
228: fSplashInfo = null;
229: fLauncherInfo = null;
230: fLauncherArgs = null;
231: fIntroInfo = null;
232: fJVMInfo = null;
233: }
234:
235: /* (non-Javadoc)
236: * @see org.eclipse.pde.internal.core.iproduct.IProductObject#parse(org.w3c.dom.Node)
237: */
238: public void parse(Node node) {
239: if (node.getNodeType() == Node.ELEMENT_NODE
240: && node.getNodeName().equals("product")) { //$NON-NLS-1$
241: Element element = (Element) node;
242: fApplication = element.getAttribute(P_APPLICATION);
243: fId = element.getAttribute(P_ID);
244: fName = element.getAttribute(P_NAME);
245: fUseFeatures = "true".equals(element.getAttribute(P_USEFEATURES)); //$NON-NLS-1$
246: NodeList children = node.getChildNodes();
247: IProductModelFactory factory = getModel().getFactory();
248: for (int i = 0; i < children.getLength(); i++) {
249: Node child = children.item(i);
250: if (child.getNodeType() == Node.ELEMENT_NODE) {
251: String name = child.getNodeName();
252: if (name.equals("aboutInfo")) { //$NON-NLS-1$
253: fAboutInfo = factory.createAboutInfo();
254: fAboutInfo.parse(child);
255: } else if (name.equals("plugins")) { //$NON-NLS-1$
256: parsePlugins(child.getChildNodes());
257: } else if (name.equals("features")) { //$NON-NLS-1$
258: parseFeatures(child.getChildNodes());
259: } else if (name.equals("configIni")) { //$NON-NLS-1$
260: fConfigIniInfo = factory.createConfigFileInfo();
261: fConfigIniInfo.parse(child);
262: } else if (name.equals("windowImages")) { //$NON-NLS-1$
263: fWindowImages = factory.createWindowImages();
264: fWindowImages.parse(child);
265: } else if (name.equals("splash")) { //$NON-NLS-1$
266: fSplashInfo = factory.createSplashInfo();
267: fSplashInfo.parse(child);
268: } else if (name.equals("launcher")) { //$NON-NLS-1$
269: fLauncherInfo = factory.createLauncherInfo();
270: fLauncherInfo.parse(child);
271: } else if (name.equals("launcherArgs")) { //$NON-NLS-1$
272: fLauncherArgs = factory
273: .createLauncherArguments();
274: fLauncherArgs.parse(child);
275: } else if (name.equals("intro")) { //$NON-NLS-1$
276: fIntroInfo = factory.createIntroInfo();
277: fIntroInfo.parse(child);
278: } else if (name.equals("vm")) { //$NON-NLS-1$
279: fJVMInfo = factory.createJVMInfo();
280: fJVMInfo.parse(child);
281: }
282: }
283: }
284: }
285: }
286:
287: private void parsePlugins(NodeList children) {
288: for (int i = 0; i < children.getLength(); i++) {
289: Node child = children.item(i);
290: if (child.getNodeType() == Node.ELEMENT_NODE) {
291: if (child.getNodeName().equals("plugin")) { //$NON-NLS-1$
292: IProductPlugin plugin = getModel().getFactory()
293: .createPlugin();
294: plugin.parse(child);
295: fPlugins.put(plugin.getId(), plugin);
296: }
297: }
298: }
299: }
300:
301: private void parseFeatures(NodeList children) {
302: for (int i = 0; i < children.getLength(); i++) {
303: Node child = children.item(i);
304: if (child.getNodeType() == Node.ELEMENT_NODE) {
305: if (child.getNodeName().equals("feature")) { //$NON-NLS-1$
306: IProductFeature feature = getModel().getFactory()
307: .createFeature();
308: feature.parse(child);
309: fFeatures.add(feature);
310: }
311: }
312: }
313: }
314:
315: /* (non-Javadoc)
316: * @see org.eclipse.pde.internal.core.iproduct.IProduct#addPlugin(org.eclipse.pde.internal.core.iproduct.IProductPlugin)
317: */
318: public void addPlugins(IProductPlugin[] plugins) {
319: boolean modified = false;
320: for (int i = 0; i < plugins.length; i++) {
321: if (plugins[i] == null)
322: continue;
323: String id = plugins[i].getId();
324: if (id == null || fPlugins.containsKey(id)) {
325: plugins[i] = null;
326: continue;
327: }
328:
329: plugins[i].setModel(getModel());
330: fPlugins.put(id, plugins[i]);
331: modified = true;
332: }
333: if (modified && isEditable())
334: fireStructureChanged(plugins, IModelChangedEvent.INSERT);
335: }
336:
337: /* (non-Javadoc)
338: * @see org.eclipse.pde.internal.core.iproduct.IProduct#removePlugins(org.eclipse.pde.internal.core.iproduct.IProductPlugin[])
339: */
340: public void removePlugins(IProductPlugin[] plugins) {
341: boolean modified = false;
342: for (int i = 0; i < plugins.length; i++) {
343: if (fPlugins.remove(plugins[i].getId()) != null)
344: modified = true;
345: }
346: if (modified && isEditable())
347: fireStructureChanged(plugins, IModelChangedEvent.REMOVE);
348: }
349:
350: /* (non-Javadoc)
351: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getPlugins()
352: */
353: public IProductPlugin[] getPlugins() {
354: return (IProductPlugin[]) fPlugins.values().toArray(
355: new IProductPlugin[fPlugins.size()]);
356: }
357:
358: /* (non-Javadoc)
359: * @see org.eclipse.pde.internal.core.iproduct.IProduct#getConfigurationFileInfo()
360: */
361: public IConfigurationFileInfo getConfigurationFileInfo() {
362: return fConfigIniInfo;
363: }
364:
365: /* (non-Javadoc)
366: * @see org.eclipse.pde.internal.core.iproduct.IProduct#setConfigurationFileInfo(org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo)
367: */
368: public void setConfigurationFileInfo(IConfigurationFileInfo info) {
369: fConfigIniInfo = info;
370: }
371:
372: public boolean useFeatures() {
373: return fUseFeatures;
374: }
375:
376: public void setUseFeatures(boolean use) {
377: boolean old = fUseFeatures;
378: fUseFeatures = use;
379: if (isEditable())
380: firePropertyChanged(P_USEFEATURES, Boolean.toString(old),
381: Boolean.toString(fUseFeatures));
382: }
383:
384: public boolean containsPlugin(String id) {
385: return fPlugins.containsKey(id);
386: }
387:
388: public boolean containsFeature(String id) {
389: IProductFeature[] features = getFeatures();
390: for (int i = 0; i < features.length; i++) {
391: if (features[i].getId().equals(id))
392: return true;
393: }
394: return false;
395: }
396:
397: public IWindowImages getWindowImages() {
398: return fWindowImages;
399: }
400:
401: public void setWindowImages(IWindowImages images) {
402: fWindowImages = images;
403: }
404:
405: public ISplashInfo getSplashInfo() {
406: return fSplashInfo;
407: }
408:
409: public void setSplashInfo(ISplashInfo info) {
410: fSplashInfo = info;
411: }
412:
413: public ILauncherInfo getLauncherInfo() {
414: return fLauncherInfo;
415: }
416:
417: public void setLauncherInfo(ILauncherInfo info) {
418: fLauncherInfo = info;
419: }
420:
421: public void addFeatures(IProductFeature[] features) {
422: boolean modified = false;
423: for (int i = 0; i < features.length; i++) {
424: if (features[i] == null)
425: continue;
426: String id = features[i].getId();
427: if (fFeatures.contains((id))) {
428: features[i] = null;
429: continue;
430: }
431:
432: features[i].setModel(getModel());
433: fFeatures.add(features[i]);
434: modified = true;
435: }
436:
437: if (modified && isEditable())
438: fireStructureChanged(features, IModelChangedEvent.INSERT);
439: }
440:
441: public void removeFeatures(IProductFeature[] features) {
442: boolean modified = false;
443: for (int i = 0; i < features.length; i++) {
444: if (features[i].getId() != null) {
445: fFeatures.remove(features[i]);
446: modified = true;
447: }
448: }
449: if (modified && isEditable())
450: fireStructureChanged(features, IModelChangedEvent.REMOVE);
451: }
452:
453: public IProductFeature[] getFeatures() {
454: return (IProductFeature[]) fFeatures
455: .toArray(new IProductFeature[fFeatures.size()]);
456: }
457:
458: public IArgumentsInfo getLauncherArguments() {
459: return fLauncherArgs;
460: }
461:
462: public void setLauncherArguments(IArgumentsInfo info) {
463: fLauncherArgs = info;
464: }
465:
466: public IIntroInfo getIntroInfo() {
467: return fIntroInfo;
468: }
469:
470: public void setIntroInfo(IIntroInfo introInfo) {
471: fIntroInfo = introInfo;
472: }
473:
474: public IJREInfo getJREInfo() {
475: return fJVMInfo;
476: }
477:
478: public void setJREInfo(IJREInfo info) {
479: fJVMInfo = info;
480: }
481:
482: public void swap(IProductFeature feature1, IProductFeature feature2) {
483: int index1 = fFeatures.indexOf(feature1);
484: int index2 = fFeatures.indexOf(feature2);
485: if (index1 == -1 || index2 == -1)
486: return;
487:
488: fFeatures.set(index2, feature1);
489: fFeatures.set(index1, feature2);
490:
491: fireStructureChanged(feature1, IModelChangedEvent.CHANGE);
492: }
493:
494: }
|