KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > product > Product


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.product;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.TreeMap JavaDoc;
18
19 import org.eclipse.pde.core.IModelChangedEvent;
20 import org.eclipse.pde.internal.core.iproduct.IAboutInfo;
21 import org.eclipse.pde.internal.core.iproduct.IArgumentsInfo;
22 import org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo;
23 import org.eclipse.pde.internal.core.iproduct.IIntroInfo;
24 import org.eclipse.pde.internal.core.iproduct.IJREInfo;
25 import org.eclipse.pde.internal.core.iproduct.ILauncherInfo;
26 import org.eclipse.pde.internal.core.iproduct.IProduct;
27 import org.eclipse.pde.internal.core.iproduct.IProductFeature;
28 import org.eclipse.pde.internal.core.iproduct.IProductModel;
29 import org.eclipse.pde.internal.core.iproduct.IProductModelFactory;
30 import org.eclipse.pde.internal.core.iproduct.IProductPlugin;
31 import org.eclipse.pde.internal.core.iproduct.ISplashInfo;
32 import org.eclipse.pde.internal.core.iproduct.IWindowImages;
33 import org.w3c.dom.Element JavaDoc;
34 import org.w3c.dom.Node JavaDoc;
35 import org.w3c.dom.NodeList JavaDoc;
36
37
38 public class Product extends ProductObject implements IProduct {
39
40     private static final long serialVersionUID = 1L;
41     private String JavaDoc fId;
42     private String JavaDoc fName;
43     private String JavaDoc fApplication;
44     private IAboutInfo fAboutInfo;
45
46     private TreeMap JavaDoc fPlugins = new TreeMap JavaDoc();
47     private List JavaDoc fFeatures = new ArrayList JavaDoc();
48     private IConfigurationFileInfo fConfigIniInfo;
49     private IJREInfo fJVMInfo;
50     private boolean fUseFeatures;
51     private IWindowImages fWindowImages;
52     private ISplashInfo fSplashInfo;
53     private ILauncherInfo fLauncherInfo;
54     private IArgumentsInfo fLauncherArgs;
55     private IIntroInfo fIntroInfo;
56
57     public Product(IProductModel model) {
58         super(model);
59     }
60
61     /* (non-Javadoc)
62      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getId()
63      */

64     public String JavaDoc getId() {
65         return fId;
66     }
67
68     /* (non-Javadoc)
69      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getName()
70      */

71     public String JavaDoc getName() {
72         return fName;
73     }
74
75     /* (non-Javadoc)
76      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getApplication()
77      */

78     public String JavaDoc getApplication() {
79         return fApplication;
80     }
81
82     /* (non-Javadoc)
83      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getDefiningPluginId()
84      */

85     public String JavaDoc getDefiningPluginId() {
86         if (fId == null)
87             return null;
88         int dot = fId.lastIndexOf('.');
89         return (dot != -1) ? fId.substring(0, dot) : null;
90     }
91
92     /* (non-Javadoc)
93      * @see org.eclipse.pde.internal.core.iproduct.IProduct#setId(java.lang.String)
94      */

95     public void setId(String JavaDoc id) {
96         String JavaDoc old = fId;
97         fId = id;
98         if (isEditable())
99             firePropertyChanged(P_ID, old, fId);
100     }
101
102     /* (non-Javadoc)
103      * @see org.eclipse.pde.internal.core.iproduct.IProduct#setName(java.lang.String)
104      */

105     public void setName(String JavaDoc name) {
106         String JavaDoc old = fName;
107         fName = name;
108         if (isEditable())
109             firePropertyChanged(P_NAME, old, fName);
110     }
111
112     /* (non-Javadoc)
113      * @see org.eclipse.pde.internal.core.iproduct.IProduct#setAboutInfo(org.eclipse.pde.internal.core.iproduct.IAboutInfo)
114      */

115     public void setAboutInfo(IAboutInfo info) {
116         fAboutInfo = info;
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.pde.internal.core.iproduct.IProduct#setApplication(java.lang.String)
121      */

122     public void setApplication(String JavaDoc application) {
123         String JavaDoc old = fApplication;
124         fApplication = application;
125         if (isEditable())
126             firePropertyChanged(P_APPLICATION, old, fApplication);
127     }
128
129     /* (non-Javadoc)
130      * @see org.eclipse.pde.internal.core.product.ProductObject#write(java.lang.String, java.io.PrintWriter)
131      */

132     public void write(String JavaDoc indent, PrintWriter JavaDoc writer) {
133         writer.print(indent + "<product"); //$NON-NLS-1$
134
if (fName != null && fName.length() > 0)
135             writer.print(" " + P_NAME + "=\"" + getWritableString(fName) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
136
if (fId != null && fId.length() > 0)
137             writer.print(" " + P_ID + "=\"" + fId + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
138
if (fApplication != null && fApplication.length() > 0)
139             writer.print(" " + P_APPLICATION + "=\"" + fApplication + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
140
writer.print(" " + P_USEFEATURES + "=\"" + Boolean.toString(fUseFeatures) + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
141
writer.println(">"); //$NON-NLS-1$
142

143         if (fAboutInfo != null) {
144             writer.println();
145             fAboutInfo.write(indent + " ", writer); //$NON-NLS-1$
146
}
147
148         if (fConfigIniInfo != null) {
149             writer.println();
150             fConfigIniInfo.write(indent + " ", writer); //$NON-NLS-1$
151
}
152
153         if (fLauncherArgs != null) {
154             writer.println();
155             fLauncherArgs.write(indent + " ", writer); //$NON-NLS-1$
156
}
157
158         if (fWindowImages != null) {
159             writer.println();
160             fWindowImages.write(indent + " ", writer); //$NON-NLS-1$
161
}
162
163         if (fSplashInfo != null) {
164             writer.println();
165             fSplashInfo.write(indent + " ", writer); //$NON-NLS-1$
166
}
167
168         if (fLauncherInfo != null) {
169             writer.println();
170             fLauncherInfo.write(indent + " ", writer); //$NON-NLS-1$
171
}
172
173         if (fIntroInfo != null) {
174             writer.println();
175             fIntroInfo.write(indent + " ", writer); //$NON-NLS-1$
176
}
177
178         if (fJVMInfo != null) {
179             writer.println();
180             fJVMInfo.write(indent + " ", writer); //$NON-NLS-1$
181
}
182
183         writer.println();
184         writer.println(indent + " <plugins>"); //$NON-NLS-1$
185
Iterator JavaDoc iter = fPlugins.values().iterator();
186         while (iter.hasNext()) {
187             IProductPlugin plugin = (IProductPlugin)iter.next();
188             plugin.write(indent + " ", writer); //$NON-NLS-1$
189
}
190         writer.println(indent + " </plugins>"); //$NON-NLS-1$
191

192         if (fFeatures.size() > 0) {
193             writer.println();
194             writer.println(indent + " <features>"); //$NON-NLS-1$
195
iter = fFeatures.iterator();
196             while (iter.hasNext()) {
197                 IProductFeature feature = (IProductFeature)iter.next();
198                 feature.write(indent + " ", writer); //$NON-NLS-1$
199
}
200             writer.println(indent + " </features>"); //$NON-NLS-1$
201
}
202
203         writer.println();
204         writer.println("</product>"); //$NON-NLS-1$
205
}
206
207     /* (non-Javadoc)
208      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getAboutInfo()
209      */

210     public IAboutInfo getAboutInfo() {
211         return fAboutInfo;
212     }
213
214     /* (non-Javadoc)
215      * @see org.eclipse.pde.internal.core.iproduct.IProduct#reset()
216      */

217     public void reset() {
218         fApplication = null;
219         fId = null;
220         fName = null;
221         fUseFeatures = false;
222         fAboutInfo = null;
223         fPlugins.clear();
224         fFeatures.clear();
225         fConfigIniInfo = null;
226         fWindowImages = null;
227         fSplashInfo = null;
228         fLauncherInfo = null;
229         fLauncherArgs = null;
230         fIntroInfo = null;
231         fJVMInfo = null;
232     }
233
234     /* (non-Javadoc)
235      * @see org.eclipse.pde.internal.core.iproduct.IProductObject#parse(org.w3c.dom.Node)
236      */

237     public void parse(Node JavaDoc node) {
238         if (node.getNodeType() == Node.ELEMENT_NODE
239                 && node.getNodeName().equals("product")) { //$NON-NLS-1$
240
Element JavaDoc element = (Element JavaDoc)node;
241             fApplication = element.getAttribute(P_APPLICATION);
242             fId = element.getAttribute(P_ID);
243             fName = element.getAttribute(P_NAME);
244             fUseFeatures = "true".equals(element.getAttribute(P_USEFEATURES)); //$NON-NLS-1$
245
NodeList JavaDoc children = node.getChildNodes();
246             IProductModelFactory factory = getModel().getFactory();
247             for (int i = 0; i < children.getLength(); i++) {
248                 Node JavaDoc child = children.item(i);
249                 if (child.getNodeType() == Node.ELEMENT_NODE) {
250                     String JavaDoc name = child.getNodeName();
251                     if (name.equals("aboutInfo")) { //$NON-NLS-1$
252
fAboutInfo = factory.createAboutInfo();
253                         fAboutInfo.parse(child);
254                     } else if (name.equals("plugins")) { //$NON-NLS-1$
255
parsePlugins(child.getChildNodes());
256                     } else if (name.equals("features")) { //$NON-NLS-1$
257
parseFeatures(child.getChildNodes());
258                     } else if (name.equals("configIni")) { //$NON-NLS-1$
259
fConfigIniInfo = factory.createConfigFileInfo();
260                         fConfigIniInfo.parse(child);
261                     } else if (name.equals("windowImages")) { //$NON-NLS-1$
262
fWindowImages = factory.createWindowImages();
263                         fWindowImages.parse(child);
264                     } else if (name.equals("splash")) { //$NON-NLS-1$
265
fSplashInfo = factory.createSplashInfo();
266                         fSplashInfo.parse(child);
267                     } else if (name.equals("launcher")) { //$NON-NLS-1$
268
fLauncherInfo = factory.createLauncherInfo();
269                         fLauncherInfo.parse(child);
270                     } else if (name.equals("launcherArgs")) { //$NON-NLS-1$
271
fLauncherArgs = factory.createLauncherArguments();
272                         fLauncherArgs.parse(child);
273                     } else if (name.equals("intro")) { //$NON-NLS-1$
274
fIntroInfo = factory.createIntroInfo();
275                         fIntroInfo.parse(child);
276                     } else if (name.equals("vm")) { //$NON-NLS-1$
277
fJVMInfo = factory.createJVMInfo();
278                         fJVMInfo.parse(child);
279                     }
280                 }
281             }
282         }
283     }
284
285     private void parsePlugins(NodeList JavaDoc children) {
286         for (int i = 0; i < children.getLength(); i++) {
287             Node JavaDoc child = children.item(i);
288             if (child.getNodeType() == Node.ELEMENT_NODE) {
289                 if (child.getNodeName().equals("plugin")) { //$NON-NLS-1$
290
IProductPlugin plugin = getModel().getFactory().createPlugin();
291                     plugin.parse(child);
292                     fPlugins.put(plugin.getId(), plugin);
293                 }
294             }
295         }
296     }
297
298     private void parseFeatures(NodeList JavaDoc children) {
299         for (int i = 0; i < children.getLength(); i++) {
300             Node JavaDoc child = children.item(i);
301             if (child.getNodeType() == Node.ELEMENT_NODE) {
302                 if (child.getNodeName().equals("feature")) { //$NON-NLS-1$
303
IProductFeature feature = getModel().getFactory().createFeature();
304                     feature.parse(child);
305                     fFeatures.add(feature);
306                 }
307             }
308         }
309     }
310
311     /* (non-Javadoc)
312      * @see org.eclipse.pde.internal.core.iproduct.IProduct#addPlugin(org.eclipse.pde.internal.core.iproduct.IProductPlugin)
313      */

314     public void addPlugins(IProductPlugin[] plugins) {
315         boolean modified = false;
316         for (int i = 0; i < plugins.length; i++) {
317             if (plugins[i] == null)
318                 continue;
319             String JavaDoc id = plugins[i].getId();
320             if (id == null || fPlugins.containsKey(id)) {
321                 plugins[i] = null;
322                 continue;
323             }
324
325             plugins[i].setModel(getModel());
326             fPlugins.put(id, plugins[i]);
327             modified = true;
328         }
329         if (modified && isEditable())
330             fireStructureChanged(plugins, IModelChangedEvent.INSERT);
331     }
332
333     /* (non-Javadoc)
334      * @see org.eclipse.pde.internal.core.iproduct.IProduct#removePlugins(org.eclipse.pde.internal.core.iproduct.IProductPlugin[])
335      */

336     public void removePlugins(IProductPlugin[] plugins) {
337         boolean modified = false;
338         for (int i = 0; i < plugins.length; i++) {
339             if (fPlugins.remove(plugins[i].getId()) != null)
340                 modified = true;
341         }
342         if (modified && isEditable())
343             fireStructureChanged(plugins, IModelChangedEvent.REMOVE);
344     }
345
346     /* (non-Javadoc)
347      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getPlugins()
348      */

349     public IProductPlugin[] getPlugins() {
350         return (IProductPlugin[])fPlugins.values().toArray(new IProductPlugin[fPlugins.size()]);
351     }
352
353     /* (non-Javadoc)
354      * @see org.eclipse.pde.internal.core.iproduct.IProduct#getConfigurationFileInfo()
355      */

356     public IConfigurationFileInfo getConfigurationFileInfo() {
357         return fConfigIniInfo;
358     }
359
360     /* (non-Javadoc)
361      * @see org.eclipse.pde.internal.core.iproduct.IProduct#setConfigurationFileInfo(org.eclipse.pde.internal.core.iproduct.IConfigurationFileInfo)
362      */

363     public void setConfigurationFileInfo(IConfigurationFileInfo info) {
364         fConfigIniInfo = info;
365     }
366
367     public boolean useFeatures() {
368         return fUseFeatures;
369     }
370
371     public void setUseFeatures(boolean use) {
372         boolean old = fUseFeatures;
373         fUseFeatures = use;
374         if (isEditable())
375             firePropertyChanged(P_USEFEATURES, Boolean.toString(old), Boolean.toString(fUseFeatures));
376     }
377
378     public boolean containsPlugin(String JavaDoc id) {
379         return fPlugins.containsKey(id);
380     }
381
382     public boolean containsFeature(String JavaDoc id) {
383         IProductFeature[] features = getFeatures();
384         for(int i = 0; i < features.length; i++) {
385             if(features[i].getId().equals(id))
386                 return true;
387         }
388         return false;
389     }
390
391     public IWindowImages getWindowImages() {
392         return fWindowImages;
393     }
394
395     public void setWindowImages(IWindowImages images) {
396         fWindowImages = images;
397     }
398
399     public ISplashInfo getSplashInfo() {
400         return fSplashInfo;
401     }
402
403     public void setSplashInfo(ISplashInfo info) {
404         fSplashInfo = info;
405     }
406
407     public ILauncherInfo getLauncherInfo() {
408         return fLauncherInfo;
409     }
410
411     public void setLauncherInfo(ILauncherInfo info) {
412         fLauncherInfo = info;
413     }
414
415     public void addFeatures(IProductFeature[] features) {
416         boolean modified = false;
417         for (int i = 0; i < features.length; i++) {
418             if (features[i] == null)
419                 continue;
420             String JavaDoc id = features[i].getId();
421             if (fFeatures.contains((id))) {
422                 features[i] = null;
423                 continue;
424             }
425
426             features[i].setModel(getModel());
427             fFeatures.add(features[i]);
428             modified = true;
429         }
430
431         if (modified && isEditable())
432             fireStructureChanged(features, IModelChangedEvent.INSERT);
433     }
434
435     public void removeFeatures(IProductFeature[] features) {
436         boolean modified = false;
437         for (int i = 0; i < features.length; i++) {
438             if(features[i].getId() != null) {
439                 fFeatures.remove(features[i]);
440                 modified = true;
441             }
442         }
443         if (modified && isEditable())
444             fireStructureChanged(features, IModelChangedEvent.REMOVE);
445     }
446
447     public IProductFeature[] getFeatures() {
448         return (IProductFeature[])fFeatures.toArray(new IProductFeature[fFeatures.size()]);
449     }
450
451     public IArgumentsInfo getLauncherArguments() {
452         return fLauncherArgs;
453     }
454
455     public void setLauncherArguments(IArgumentsInfo info) {
456         fLauncherArgs = info;
457     }
458
459     public IIntroInfo getIntroInfo() {
460         return fIntroInfo;
461     }
462
463     public void setIntroInfo(IIntroInfo introInfo) {
464         fIntroInfo = introInfo;
465     }
466
467     public IJREInfo getJREInfo() {
468         return fJVMInfo;
469     }
470
471     public void setJREInfo(IJREInfo info) {
472         fJVMInfo = info;
473     }
474
475     public void swap(IProductFeature feature1, IProductFeature feature2) {
476         int index1 = fFeatures.indexOf(feature1);
477         int index2 = fFeatures.indexOf(feature2);
478         if (index1 == -1 || index2 == -1)
479             return;
480
481         fFeatures.set(index2, feature1);
482         fFeatures.set(index1, feature2);
483
484         fireStructureChanged(feature1, IModelChangedEvent.CHANGE);
485     }
486     
487 }
488
Popular Tags