KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > util > ModelModification


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.ui.util;
12
13 import org.eclipse.core.resources.IFile;
14 import org.eclipse.core.resources.IProject;
15 import org.eclipse.core.runtime.Assert;
16 import org.eclipse.core.runtime.CoreException;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.pde.core.IBaseModel;
19
20 /**
21  * ModelModification class used my the PDEModelUtility
22  * Subclass me to create changes to your models.
23  *
24  */

25 public abstract class ModelModification {
26
27     private IFile fModelFile;
28     private IFile fManifestFile;
29     private IFile fXMLFile;
30     private IFile fPropertiesFile;
31     private boolean fIsBundleModel;
32     
33     /**
34      * Create a single model modification - used for modifying single AbstractEditingModels
35      * @param modelFile the basic underlying file for the model you wish to modify.
36      */

37     public ModelModification(IFile modelFile) {
38         singleFileModification(modelFile);
39     }
40     
41     
42     /**
43      * Create a full IBundlePluginModelBase modification
44      * @param bundleFile the MANIFEST.MF file
45      * @param xmlFile the plugin.xml/fragment.xml file for this modification (optional - can be null)
46      * @pre bundleFile must not be <code>null</code>
47      */

48     public ModelModification(IFile bundleFile, IFile xmlFile) {
49         createFullBundleModification(bundleFile, xmlFile);
50     }
51     
52     /**
53      * Create a ModelModification based on the contents of the project
54      * ie. if the project contains a MANIFEST.MF this will be tagged as a
55      * fullBundleModification, otherwise (this project is an old-style plugin)
56      * this will be a PluginModel/FragmentModel modification.
57      * @param project
58      */

59     public ModelModification(IProject project) {
60         IFile xml = project.getFile(PDEModelUtility.F_PLUGIN);
61         if (!xml.exists())
62             xml = project.getFile(PDEModelUtility.F_FRAGMENT);
63         if (!xml.exists())
64             xml = null;
65         IFile manifest = project.getFile(PDEModelUtility.F_MANIFEST_FP);
66         if (!manifest.exists() && xml != null)
67             singleFileModification(xml);
68         else if (manifest.exists())
69             createFullBundleModification(manifest, xml);
70     }
71     
72     private void singleFileModification(IFile file) {
73         assignFile(file);
74         if (fManifestFile != null)
75             fModelFile = fManifestFile;
76         else if (fXMLFile != null)
77             fModelFile = fXMLFile;
78         else if (fPropertiesFile != null)
79             fModelFile = fPropertiesFile;
80         fIsBundleModel = file.getName().equals(PDEModelUtility.F_MANIFEST);
81     }
82     
83     private void createFullBundleModification(IFile bundleFile, IFile xmlFile) {
84         assignFile(bundleFile);
85         assignFile(xmlFile);
86         
87         Assert.isNotNull(fManifestFile);
88         fModelFile = fManifestFile;
89         fIsBundleModel = true;
90     }
91     
92     private void assignFile(IFile file) {
93         if (file == null)
94             return;
95         String JavaDoc name = file.getName();
96         if (name.equals(PDEModelUtility.F_MANIFEST))
97             fManifestFile = file;
98         else if (name.equals(PDEModelUtility.F_PLUGIN) || name.equals(PDEModelUtility.F_FRAGMENT))
99             fXMLFile = file;
100         else if (name.endsWith(PDEModelUtility.F_PROPERTIES))
101             fPropertiesFile = file;
102     }
103     
104     /**
105      * Invoke this using PDEModelUtility.modifyModel(ModelModification modification)
106      * Clients / subclasses should not invoke this method.
107      * @param model
108      * @param monitor
109      * @throws CoreException
110      */

111     protected abstract void modifyModel(IBaseModel model, IProgressMonitor monitor) throws CoreException;
112     
113     protected final IFile getFile() {
114         return fModelFile;
115     }
116     
117     protected final IFile getManifestFile() {
118         return fManifestFile;
119     }
120     
121     protected final IFile getXMLFile() {
122         return fXMLFile;
123     }
124     
125     protected final IFile getPropertiesFile() {
126         return fPropertiesFile;
127     }
128     
129     protected final boolean isFullBundleModification() {
130         return fIsBundleModel;
131     }
132     
133     public boolean saveOpenEditor() {
134         return true;
135     }
136 }
137
Popular Tags