001: /*******************************************************************************
002: * Copyright (c) 2000, 2005 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.editor.feature;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.pde.core.IModelChangedEvent;
014: import org.eclipse.pde.internal.core.feature.FeatureObject;
015: import org.eclipse.pde.internal.core.ifeature.IFeature;
016: import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
017: import org.eclipse.pde.internal.core.ifeature.IFeatureData;
018: import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
019: import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
020: import org.eclipse.pde.internal.core.ifeature.IFeatureObject;
021: import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
022: import org.eclipse.pde.internal.core.ifeature.IFeatureURL;
023: import org.eclipse.pde.internal.ui.PDEPlugin;
024: import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
025: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
026:
027: public class FeatureUndoManager extends ModelUndoManager {
028:
029: public FeatureUndoManager(PDEFormEditor editor) {
030: super (editor);
031: setUndoLevelLimit(30);
032: }
033:
034: protected String getPageId(Object obj) {
035: if (obj instanceof IFeature || obj instanceof IFeatureURL)
036: return FeatureFormPage.PAGE_ID;
037: if (obj instanceof IFeaturePlugin)
038: return FeatureReferencePage.PAGE_ID;
039: if (obj instanceof IFeatureImport)
040: return FeatureDependenciesPage.PAGE_ID;
041: if (obj instanceof IFeatureData || obj instanceof IFeatureChild)
042: return FeatureAdvancedPage.PAGE_ID;
043: return null;
044: }
045:
046: protected void execute(IModelChangedEvent event, boolean undo) {
047: Object[] elements = event.getChangedObjects();
048: int type = event.getChangeType();
049: String propertyName = event.getChangedProperty();
050: IFeatureModel model = (IFeatureModel) event.getChangeProvider();
051:
052: switch (type) {
053: case IModelChangedEvent.INSERT:
054: if (undo)
055: executeRemove(model, elements);
056: else
057: executeAdd(model, elements);
058: break;
059: case IModelChangedEvent.REMOVE:
060: if (undo)
061: executeAdd(model, elements);
062: else
063: executeRemove(model, elements);
064: break;
065: case IModelChangedEvent.CHANGE:
066: if (undo)
067: executeChange(elements[0], propertyName, event
068: .getNewValue(), event.getOldValue());
069: else
070: executeChange(elements[0], propertyName, event
071: .getOldValue(), event.getNewValue());
072: }
073: }
074:
075: private void executeAdd(IFeatureModel model, Object[] elements) {
076: IFeature feature = model.getFeature();
077:
078: try {
079: for (int i = 0; i < elements.length; i++) {
080: Object element = elements[i];
081:
082: if (element instanceof IFeaturePlugin) {
083: feature
084: .addPlugins(new IFeaturePlugin[] { (IFeaturePlugin) element });
085: } else if (element instanceof IFeatureData) {
086: feature
087: .addData(new IFeatureData[] { (IFeatureData) element });
088: } else if (element instanceof IFeatureImport) {
089: feature
090: .addImports(new IFeatureImport[] { (IFeatureImport) element });
091: } else if (element instanceof IFeatureChild) {
092: feature
093: .addIncludedFeatures(new IFeatureChild[] { (IFeatureChild) element });
094: }
095: }
096: } catch (CoreException e) {
097: PDEPlugin.logException(e);
098: }
099: }
100:
101: private void executeRemove(IFeatureModel model, Object[] elements) {
102: IFeature feature = model.getFeature();
103:
104: try {
105: for (int i = 0; i < elements.length; i++) {
106: Object element = elements[i];
107:
108: if (element instanceof IFeaturePlugin) {
109: feature
110: .removePlugins(new IFeaturePlugin[] { (IFeaturePlugin) element });
111: } else if (element instanceof IFeatureData) {
112: feature
113: .removeData(new IFeatureData[] { (IFeatureData) element });
114: } else if (element instanceof IFeatureImport) {
115: feature
116: .removeImports(new IFeatureImport[] { (IFeatureImport) element });
117: } else if (element instanceof IFeatureChild) {
118: feature
119: .removeIncludedFeatures(new IFeatureChild[] { (IFeatureChild) element });
120: }
121: }
122: } catch (CoreException e) {
123: PDEPlugin.logException(e);
124: }
125: }
126:
127: private void executeChange(Object element, String propertyName,
128: Object oldValue, Object newValue) {
129: if (element instanceof FeatureObject) {
130: FeatureObject pobj = (FeatureObject) element;
131: try {
132: pobj.restoreProperty(propertyName, oldValue, newValue);
133: } catch (CoreException e) {
134: PDEPlugin.logException(e);
135: }
136: }
137: }
138:
139: public void modelChanged(IModelChangedEvent event) {
140: if (event.getChangeType() == IModelChangedEvent.CHANGE) {
141: Object obj = event.getChangedObjects()[0];
142: if (obj instanceof IFeatureObject) {
143: IFeatureObject fobj = (IFeatureObject) event
144: .getChangedObjects()[0];
145: // Ignore events from objects that are not yet in the model.
146: if (!(fobj instanceof IFeature)
147: && fobj.isInTheModel() == false)
148: return;
149: }
150: }
151: super.modelChanged(event);
152: }
153: }
|