001: /*******************************************************************************
002: * Copyright (c) 2005, 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.ui.util;
011:
012: import org.eclipse.core.resources.IFile;
013: import org.eclipse.core.resources.IProject;
014: import org.eclipse.core.runtime.Assert;
015: import org.eclipse.core.runtime.CoreException;
016: import org.eclipse.core.runtime.IProgressMonitor;
017: import org.eclipse.pde.core.IBaseModel;
018:
019: /**
020: * ModelModification class used my the PDEModelUtility
021: * Subclass me to create changes to your models.
022: *
023: */
024: public abstract class ModelModification {
025:
026: private IFile fModelFile;
027: private IFile fManifestFile;
028: private IFile fXMLFile;
029: private IFile fPropertiesFile;
030: private boolean fIsBundleModel;
031:
032: /**
033: * Create a single model modification - used for modifying single AbstractEditingModels
034: * @param modelFile the basic underlying file for the model you wish to modify.
035: */
036: public ModelModification(IFile modelFile) {
037: singleFileModification(modelFile);
038: }
039:
040: /**
041: * Create a full IBundlePluginModelBase modification
042: * @param bundleFile the MANIFEST.MF file
043: * @param xmlFile the plugin.xml/fragment.xml file for this modification (optional - can be null)
044: * @pre bundleFile must not be <code>null</code>
045: */
046: public ModelModification(IFile bundleFile, IFile xmlFile) {
047: createFullBundleModification(bundleFile, xmlFile);
048: }
049:
050: /**
051: * Create a ModelModification based on the contents of the project
052: * ie. if the project contains a MANIFEST.MF this will be tagged as a
053: * fullBundleModification, otherwise (this project is an old-style plugin)
054: * this will be a PluginModel/FragmentModel modification.
055: * @param project
056: */
057: public ModelModification(IProject project) {
058: IFile xml = project.getFile(PDEModelUtility.F_PLUGIN);
059: if (!xml.exists())
060: xml = project.getFile(PDEModelUtility.F_FRAGMENT);
061: if (!xml.exists())
062: xml = null;
063: IFile manifest = project.getFile(PDEModelUtility.F_MANIFEST_FP);
064: if (!manifest.exists() && xml != null)
065: singleFileModification(xml);
066: else if (manifest.exists())
067: createFullBundleModification(manifest, xml);
068: }
069:
070: private void singleFileModification(IFile file) {
071: assignFile(file);
072: if (fManifestFile != null)
073: fModelFile = fManifestFile;
074: else if (fXMLFile != null)
075: fModelFile = fXMLFile;
076: else if (fPropertiesFile != null)
077: fModelFile = fPropertiesFile;
078: fIsBundleModel = file.getName().equals(
079: PDEModelUtility.F_MANIFEST);
080: }
081:
082: private void createFullBundleModification(IFile bundleFile,
083: IFile xmlFile) {
084: assignFile(bundleFile);
085: assignFile(xmlFile);
086:
087: Assert.isNotNull(fManifestFile);
088: fModelFile = fManifestFile;
089: fIsBundleModel = true;
090: }
091:
092: private void assignFile(IFile file) {
093: if (file == null)
094: return;
095: String name = file.getName();
096: if (name.equals(PDEModelUtility.F_MANIFEST))
097: fManifestFile = file;
098: else if (name.equals(PDEModelUtility.F_PLUGIN)
099: || name.equals(PDEModelUtility.F_FRAGMENT))
100: fXMLFile = file;
101: else if (name.endsWith(PDEModelUtility.F_PROPERTIES))
102: fPropertiesFile = file;
103: }
104:
105: /**
106: * Invoke this using PDEModelUtility.modifyModel(ModelModification modification)
107: * Clients / subclasses should not invoke this method.
108: * @param model
109: * @param monitor
110: * @throws CoreException
111: */
112: protected abstract void modifyModel(IBaseModel model,
113: IProgressMonitor monitor) throws CoreException;
114:
115: protected final IFile getFile() {
116: return fModelFile;
117: }
118:
119: protected final IFile getManifestFile() {
120: return fManifestFile;
121: }
122:
123: protected final IFile getXMLFile() {
124: return fXMLFile;
125: }
126:
127: protected final IFile getPropertiesFile() {
128: return fPropertiesFile;
129: }
130:
131: protected final boolean isFullBundleModification() {
132: return fIsBundleModel;
133: }
134:
135: public boolean saveOpenEditor() {
136: return true;
137: }
138: }
|