KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > bundle > BundlePluginModelBase


1 /*******************************************************************************
2  * Copyright (c) 2003, 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.bundle;
12 import java.io.InputStream JavaDoc;
13 import java.io.PrintWriter JavaDoc;
14 import java.net.MalformedURLException JavaDoc;
15 import java.net.URL JavaDoc;
16
17 import org.eclipse.core.resources.IResource;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.osgi.service.resolver.BundleDescription;
20 import org.eclipse.pde.core.IEditable;
21 import org.eclipse.pde.core.IEditableModel;
22 import org.eclipse.pde.core.IModelChangedEvent;
23 import org.eclipse.pde.core.build.IBuildModel;
24 import org.eclipse.pde.core.plugin.IExtensions;
25 import org.eclipse.pde.core.plugin.IExtensionsModelFactory;
26 import org.eclipse.pde.core.plugin.IPluginAttribute;
27 import org.eclipse.pde.core.plugin.IPluginBase;
28 import org.eclipse.pde.core.plugin.IPluginElement;
29 import org.eclipse.pde.core.plugin.IPluginExtension;
30 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
31 import org.eclipse.pde.core.plugin.IPluginImport;
32 import org.eclipse.pde.core.plugin.IPluginLibrary;
33 import org.eclipse.pde.core.plugin.IPluginModelFactory;
34 import org.eclipse.pde.core.plugin.IPluginObject;
35 import org.eclipse.pde.core.plugin.ISharedExtensionsModel;
36 import org.eclipse.pde.internal.core.AbstractNLModel;
37 import org.eclipse.pde.internal.core.NLResourceHelper;
38 import org.eclipse.pde.internal.core.PDEManager;
39 import org.eclipse.pde.internal.core.ibundle.IBundle;
40 import org.eclipse.pde.internal.core.ibundle.IBundleModel;
41 import org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase;
42 import org.eclipse.pde.internal.core.plugin.PluginImport;
43 import org.eclipse.pde.internal.core.plugin.PluginLibrary;
44 import org.eclipse.pde.internal.core.text.plugin.PluginModelBase;
45
46 public abstract class BundlePluginModelBase extends AbstractNLModel
47         implements IBundlePluginModelBase, IPluginModelFactory {
48     
49     private IBundleModel fBundleModel;
50     private ISharedExtensionsModel fExtensionsModel;
51     private BundlePluginBase fBundlePluginBase;
52     private IBuildModel fBuildModel;
53     private BundleDescription fBundleDescription;
54     
55     public BundlePluginModelBase() {
56         getPluginBase();
57     }
58     /*
59      * (non-Javadoc)
60      *
61      * @see org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase#getBundleModel()
62      */

63     public IBundleModel getBundleModel() {
64         return fBundleModel;
65     }
66     public IResource getUnderlyingResource() {
67         return fBundleModel.getUnderlyingResource();
68     }
69     /*
70      * (non-Javadoc)
71      *
72      * @see org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase#getExtensionsModel()
73      */

74     public ISharedExtensionsModel getExtensionsModel() {
75         return fExtensionsModel;
76     }
77     public void dispose() {
78         if (fBundleModel != null) {
79             if (fBundlePluginBase != null)
80                 fBundleModel.removeModelChangedListener(fBundlePluginBase);
81             fBundleModel.dispose();
82             fBundleModel = null;
83         }
84         if (fExtensionsModel != null) {
85             if (fBundlePluginBase != null)
86                 fExtensionsModel.removeModelChangedListener(fBundlePluginBase);
87             fExtensionsModel.dispose();
88             fExtensionsModel = null;
89         }
90         super.dispose();
91     }
92     public void save() {
93         if (fBundleModel != null && fBundleModel instanceof IEditableModel) {
94             IEditableModel emodel = (IEditableModel) fBundleModel;
95             if (emodel.isDirty())
96                 emodel.save();
97         }
98         if (fExtensionsModel != null
99                 && fExtensionsModel instanceof IEditableModel) {
100             IEditableModel emodel = (IEditableModel) fExtensionsModel;
101             if (emodel.isDirty())
102                 emodel.save();
103         }
104     }
105     /*
106      * (non-Javadoc)
107      *
108      * @see org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase#setBundleModel(org.eclipse.pde.internal.core.ibundle.IBundleModel)
109      */

110     public void setBundleModel(IBundleModel bundleModel) {
111         if (fBundleModel != null && fBundlePluginBase != null) {
112             fBundleModel.removeModelChangedListener(fBundlePluginBase);
113         }
114         fBundleModel = bundleModel;
115         if (fBundleModel != null && fBundlePluginBase != null)
116             bundleModel.addModelChangedListener(fBundlePluginBase);
117     }
118     /*
119      * (non-Javadoc)
120      *
121      * @see org.eclipse.pde.internal.core.ibundle.IBundlePluginModelBase#setExtensionsModel(org.eclipse.pde.core.plugin.IExtensionsModel)
122      */

123     public void setExtensionsModel(ISharedExtensionsModel extensionsModel) {
124         if (fExtensionsModel != null && fBundlePluginBase != null) {
125             fExtensionsModel.removeModelChangedListener(fBundlePluginBase);
126         }
127         fExtensionsModel = extensionsModel;
128         if (fExtensionsModel instanceof PluginModelBase) {
129             ((PluginModelBase)fExtensionsModel).setLocalization(getBundleLocalization());
130         }
131         if (extensionsModel != null && fBundlePluginBase != null)
132             extensionsModel.addModelChangedListener(fBundlePluginBase);
133     }
134
135     public IBuildModel getBuildModel() {
136         return fBuildModel;
137     }
138     
139     public void setBuildModel(IBuildModel buildModel) {
140         fBuildModel = buildModel;
141     }
142     
143     public IPluginBase getPluginBase() {
144         return getPluginBase(true);
145     }
146     
147     public IExtensions getExtensions() {
148         return getPluginBase();
149     }
150     
151     /* (non-Javadoc)
152      * @see org.eclipse.pde.core.IModelChangeProvider#fireModelChanged(org.eclipse.pde.core.IModelChangedEvent)
153      */

154     public void fireModelChanged(IModelChangedEvent event) {
155         super.fireModelChanged(event);
156         Object JavaDoc[] objects = event.getChangedObjects();
157         if (objects!= null && objects.length > 0) {
158             if (objects[0] instanceof IPluginImport) {
159                 fBundlePluginBase.updateImport((IPluginImport)objects[0]);
160             }
161         }
162     }
163     
164     /*
165      * (non-Javadoc)
166      *
167      * @see org.eclipse.pde.core.plugin.IPluginModelBase#getPluginBase(boolean)
168      */

169     public IPluginBase getPluginBase(boolean createIfMissing) {
170         if (fBundlePluginBase == null && createIfMissing) {
171             fBundlePluginBase = (BundlePluginBase) createPluginBase();
172             if (fBundleModel != null)
173                 fBundleModel.addModelChangedListener(fBundlePluginBase);
174             setLoaded(true);
175         }
176         return fBundlePluginBase;
177     }
178     public IExtensions getExtensions(boolean createIfMissing) {
179         return getPluginBase(createIfMissing);
180     }
181     /*
182      * (non-Javadoc)
183      *
184      * @see org.eclipse.pde.core.plugin.IPluginModelBase#getPluginFactory()
185      */

186     public IPluginModelFactory getPluginFactory() {
187         return this;
188     }
189     /*
190      * (non-Javadoc)
191      *
192      * @see org.eclipse.pde.core.plugin.ISharedPluginModel#getFactory()
193      */

194     public IExtensionsModelFactory getFactory() {
195         if (fExtensionsModel != null)
196             return fExtensionsModel.getFactory();
197         return null;
198     }
199     /*
200      * (non-Javadoc)
201      *
202      * @see org.eclipse.pde.core.plugin.ISharedPluginModel#getInstallLocation()
203      */

204     public String JavaDoc getInstallLocation() {
205         if (fBundleModel != null)
206             return fBundleModel.getInstallLocation();
207         return null;
208     }
209     
210     public String JavaDoc getBundleLocalization() {
211         IBundle bundle = fBundleModel != null ? fBundleModel.getBundle() : null;
212         return bundle != null ? bundle.getLocalization() : null;
213         
214     }
215     
216     protected NLResourceHelper createNLResourceHelper() {
217         String JavaDoc localization = getBundleLocalization();
218         return localization == null
219                 ? null
220                 : new NLResourceHelper(localization, PDEManager.getNLLookupLocations(this));
221     }
222
223     public URL JavaDoc getNLLookupLocation() {
224         try {
225             return new URL JavaDoc("file:" + getInstallLocation()); //$NON-NLS-1$ //$NON-NLS-2$
226
} catch (MalformedURLException JavaDoc e) {
227             return null;
228         }
229     }
230     /*
231      * (non-Javadoc)
232      *
233      * @see org.eclipse.pde.core.IModel#isEditable()
234      */

235     public boolean isEditable() {
236         if (fBundleModel != null && fBundleModel.isEditable() == false)
237             return false;
238         if (fExtensionsModel != null && fExtensionsModel.isEditable() == false)
239             return false;
240         return true;
241     }
242     /*
243      * (non-Javadoc)
244      *
245      * @see org.eclipse.pde.core.IModel#isInSync()
246      */

247     public boolean isInSync() {
248         return ((fBundleModel == null || fBundleModel.isInSync()) && (fExtensionsModel == null || fExtensionsModel
249                 .isInSync()));
250     }
251     /*
252      * (non-Javadoc)
253      *
254      * @see org.eclipse.pde.core.IModel#isValid()
255      */

256     public boolean isValid() {
257         return ((fBundleModel == null || fBundleModel.isValid()) && (fExtensionsModel == null || fExtensionsModel
258                 .isValid()));
259     }
260     /*
261      * (non-Javadoc)
262      *
263      * @see org.eclipse.pde.core.IModel#load()
264      */

265     public void load() throws CoreException {
266     }
267     /*
268      * (non-Javadoc)
269      *
270      * @see org.eclipse.pde.core.IModel#load(java.io.InputStream, boolean)
271      */

272     public void load(InputStream JavaDoc source, boolean outOfSync)
273             throws CoreException {
274     }
275     /*
276      * (non-Javadoc)
277      *
278      * @see org.eclipse.pde.core.IModel#reload(java.io.InputStream, boolean)
279      */

280     public void reload(InputStream JavaDoc source, boolean outOfSync)
281             throws CoreException {
282     }
283     /**
284      * @return Returns the enabled.
285      */

286     public boolean isEnabled() {
287         return true;
288     }
289
290     public void setEnabled(boolean enabled) {
291     }
292     
293     /*
294      * (non-Javadoc)
295      *
296      * @see org.eclipse.pde.internal.core.AbstractModel#updateTimeStamp()
297      */

298     protected void updateTimeStamp() {
299     }
300     
301     public IPluginImport createImport() {
302         PluginImport iimport = new PluginImport();
303         iimport.setModel(this);
304         iimport.setParent(getPluginBase());
305         return iimport;
306     }
307     
308     public IPluginImport createImport(String JavaDoc pluginId) {
309         PluginImport iimport = new PluginImport(this, pluginId);
310         iimport.setParent(getPluginBase());
311         return iimport;
312     }
313
314     public IPluginLibrary createLibrary() {
315         PluginLibrary library = new PluginLibrary();
316         library.setModel(this);
317         library.setParent(getPluginBase());
318         return library;
319     }
320     public IPluginAttribute createAttribute(IPluginElement element) {
321         if (fExtensionsModel != null)
322             return fExtensionsModel.getFactory().createAttribute(element);
323         return null;
324     }
325     public IPluginElement createElement(IPluginObject parent) {
326         if (fExtensionsModel != null)
327             return fExtensionsModel.getFactory().createElement(parent);
328         return null;
329     }
330     public IPluginExtension createExtension() {
331         if (fExtensionsModel != null)
332             return fExtensionsModel.getFactory().createExtension();
333         return null;
334     }
335     public IPluginExtensionPoint createExtensionPoint() {
336         if (fExtensionsModel != null)
337             return fExtensionsModel.getFactory().createExtensionPoint();
338         return null;
339     }
340
341     public boolean isBundleModel() {
342         return true;
343     }
344     /*
345      * (non-Javadoc)
346      *
347      * @see org.eclipse.pde.core.plugin.IPluginModelBase#getBundleDescription()
348      */

349     public BundleDescription getBundleDescription() {
350         return fBundleDescription;
351     }
352     /*
353      * (non-Javadoc)
354      *
355      * @see org.eclipse.pde.core.plugin.IPluginModelBase#setBundleDescription(org.eclipse.osgi.service.resolver.BundleDescription)
356      */

357     public void setBundleDescription(BundleDescription description) {
358         fBundleDescription = description;
359     }
360     /*
361      * (non-Javadoc)
362      *
363      * @see org.eclipse.pde.core.IEditable#isDirty()
364      */

365     public boolean isDirty() {
366         if (fBundleModel != null && (fBundleModel instanceof IEditable)
367                 && ((IEditable) fBundleModel).isDirty())
368             return true;
369         if (fExtensionsModel != null && (fExtensionsModel instanceof IEditable)
370                 && ((IEditable) fExtensionsModel).isDirty())
371             return true;
372         return false;
373     }
374     /*
375      * (non-Javadoc)
376      *
377      * @see org.eclipse.pde.core.IEditable#save(java.io.PrintWriter)
378      */

379     public void save(PrintWriter JavaDoc writer) {
380         // Does nothing - individual models are saved instead
381
}
382     /*
383      * (non-Javadoc)
384      *
385      * @see org.eclipse.pde.core.IEditable#setDirty(boolean)
386      */

387     public void setDirty(boolean dirty) {
388         //does nothing
389
}
390     
391     public String JavaDoc toString() {
392         IPluginBase base = getPluginBase();
393         if (base != null)
394             return base.getId();
395         return super.toString();
396     }
397 }
398
Popular Tags