001: /*******************************************************************************
002: * Copyright (c) 2000, 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.editor.plugin;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.pde.core.IModelChangeProvider;
014: import org.eclipse.pde.core.IModelChangedEvent;
015: import org.eclipse.pde.core.build.IBuild;
016: import org.eclipse.pde.core.build.IBuildEntry;
017: import org.eclipse.pde.core.build.IBuildModel;
018: import org.eclipse.pde.core.plugin.IPluginAttribute;
019: import org.eclipse.pde.core.plugin.IPluginBase;
020: import org.eclipse.pde.core.plugin.IPluginElement;
021: import org.eclipse.pde.core.plugin.IPluginExtension;
022: import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
023: import org.eclipse.pde.core.plugin.IPluginImport;
024: import org.eclipse.pde.core.plugin.IPluginLibrary;
025: import org.eclipse.pde.core.plugin.IPluginModelBase;
026: import org.eclipse.pde.core.plugin.IPluginObject;
027: import org.eclipse.pde.core.plugin.IPluginParent;
028: import org.eclipse.pde.internal.core.build.BuildObject;
029: import org.eclipse.pde.internal.core.build.IBuildObject;
030: import org.eclipse.pde.internal.core.plugin.AttributeChangedEvent;
031: import org.eclipse.pde.internal.core.plugin.PluginAttribute;
032: import org.eclipse.pde.internal.core.plugin.PluginElement;
033: import org.eclipse.pde.internal.core.plugin.PluginObject;
034: import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
035: import org.eclipse.pde.internal.core.text.plugin.PluginLibraryNode;
036: import org.eclipse.pde.internal.core.text.plugin.PluginObjectNode;
037: import org.eclipse.pde.internal.ui.PDEPlugin;
038: import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
039: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
040:
041: public class PluginUndoManager extends ModelUndoManager {
042:
043: public PluginUndoManager(PDEFormEditor editor) {
044: super (editor);
045: setUndoLevelLimit(30);
046: }
047:
048: protected String getPageId(Object obj) {
049: if (obj instanceof IPluginBase)
050: return OverviewPage.PAGE_ID;
051: if (obj instanceof IPluginImport)
052: return DependenciesPage.PAGE_ID;
053: if (obj instanceof IPluginLibrary
054: || (obj instanceof IPluginElement && ((IPluginElement) obj)
055: .getParent() instanceof IPluginLibrary))
056: return RuntimePage.PAGE_ID;
057: if (obj instanceof IPluginExtension
058: || (obj instanceof IPluginElement && ((IPluginElement) obj)
059: .getParent() instanceof IPluginParent)
060: || obj instanceof IPluginAttribute)
061: return ExtensionsPage.PAGE_ID;
062: if (obj instanceof IPluginExtensionPoint)
063: return ExtensionPointsPage.PAGE_ID;
064: return null;
065: }
066:
067: protected void execute(IModelChangedEvent event, boolean undo) {
068: Object[] elements = event.getChangedObjects();
069: int type = event.getChangeType();
070: String propertyName = event.getChangedProperty();
071: IModelChangeProvider model = event.getChangeProvider();
072:
073: switch (type) {
074: case IModelChangedEvent.INSERT:
075: if (undo)
076: executeRemove(model, elements);
077: else
078: executeAdd(model, elements);
079: break;
080: case IModelChangedEvent.REMOVE:
081: if (undo)
082: executeAdd(model, elements);
083: else
084: executeRemove(model, elements);
085: break;
086: case IModelChangedEvent.CHANGE:
087: if (event instanceof AttributeChangedEvent) {
088: executeAttributeChange((AttributeChangedEvent) event,
089: undo);
090: } else {
091: if (undo)
092: executeChange(elements[0], propertyName, event
093: .getNewValue(), event.getOldValue());
094: else
095: executeChange(elements[0], propertyName, event
096: .getOldValue(), event.getNewValue());
097: }
098: }
099: }
100:
101: private void executeAdd(IModelChangeProvider model,
102: Object[] elements) {
103: IPluginBase pluginBase = null;
104: IBuild build = null;
105: if (model instanceof IPluginModelBase)
106: pluginBase = ((IPluginModelBase) model).getPluginBase();
107: if (model instanceof IBuildModel)
108: build = ((IBuildModel) model).getBuild();
109:
110: try {
111: for (int i = 0; i < elements.length; i++) {
112: Object element = elements[i];
113:
114: if (element instanceof IPluginImport) {
115: pluginBase.add((IPluginImport) element);
116: } else if (element instanceof IPluginLibrary) {
117: pluginBase.add((IPluginLibrary) element);
118: } else if (element instanceof IPluginExtensionPoint) {
119: pluginBase.add((IPluginExtensionPoint) element);
120: } else if (element instanceof IPluginExtension) {
121: pluginBase.add((IPluginExtension) element);
122: } else if (element instanceof IPluginElement) {
123: IPluginElement e = (IPluginElement) element;
124: Object parent = e.getParent();
125: if (parent instanceof PluginLibraryNode
126: && e instanceof PluginElementNode) {
127: ((PluginLibraryNode) parent)
128: .addContentFilter((PluginElementNode) e);
129: } else if (parent instanceof IPluginParent) {
130: ((IPluginParent) parent).add(e);
131: }
132: } else if (element instanceof IBuildEntry) {
133: IBuildEntry e = (IBuildEntry) element;
134: build.add(e);
135: }
136: }
137: } catch (CoreException e) {
138: PDEPlugin.logException(e);
139: }
140: }
141:
142: private void executeRemove(IModelChangeProvider model,
143: Object[] elements) {
144: IPluginBase pluginBase = null;
145: IBuild build = null;
146: if (model instanceof IPluginModelBase)
147: pluginBase = ((IPluginModelBase) model).getPluginBase();
148: if (model instanceof IBuildModel)
149: build = ((IBuildModel) model).getBuild();
150:
151: try {
152: for (int i = 0; i < elements.length; i++) {
153: Object element = elements[i];
154:
155: if (element instanceof IPluginImport) {
156: pluginBase.remove((IPluginImport) element);
157: } else if (element instanceof IPluginLibrary) {
158: pluginBase.remove((IPluginLibrary) element);
159: } else if (element instanceof IPluginExtensionPoint) {
160: pluginBase.remove((IPluginExtensionPoint) element);
161: } else if (element instanceof IPluginExtension) {
162: pluginBase.remove((IPluginExtension) element);
163: } else if (element instanceof IPluginElement) {
164: IPluginElement e = (IPluginElement) element;
165: Object parent = e.getParent();
166: if (parent instanceof PluginLibraryNode
167: && e instanceof PluginElementNode) {
168: ((PluginLibraryNode) parent)
169: .removeContentFilter((PluginElementNode) e);
170: } else if (parent instanceof IPluginParent) {
171: ((IPluginParent) parent).remove(e);
172: }
173: } else if (element instanceof IBuildEntry) {
174: IBuildEntry e = (IBuildEntry) element;
175: build.remove(e);
176: }
177: }
178: } catch (CoreException e) {
179: PDEPlugin.logException(e);
180: }
181: }
182:
183: private void executeAttributeChange(AttributeChangedEvent e,
184: boolean undo) {
185: PluginElement element = (PluginElement) e.getChangedObjects()[0];
186: PluginAttribute att = (PluginAttribute) e.getChangedAttribute();
187: Object oldValue = e.getOldValue();
188: Object newValue = e.getNewValue();
189: try {
190: if (undo)
191: element
192: .setAttribute(att.getName(), oldValue
193: .toString());
194: else
195: element
196: .setAttribute(att.getName(), newValue
197: .toString());
198: } catch (CoreException ex) {
199: PDEPlugin.logException(ex);
200: }
201: }
202:
203: private void executeChange(Object element, String propertyName,
204: Object oldValue, Object newValue) {
205: if (element instanceof PluginObject) {
206: PluginObject pobj = (PluginObject) element;
207: try {
208: pobj.restoreProperty(propertyName, oldValue, newValue);
209: } catch (CoreException e) {
210: PDEPlugin.logException(e);
211: }
212: } else if (element instanceof BuildObject) {
213: BuildObject bobj = (BuildObject) element;
214: try {
215: bobj.restoreProperty(propertyName, oldValue, newValue);
216: } catch (CoreException e) {
217: PDEPlugin.logException(e);
218: }
219: } else if (element instanceof PluginObjectNode) {
220: PluginObjectNode node = (PluginObjectNode) element;
221: String newString = newValue != null ? newValue.toString()
222: : null;
223: node.setXMLAttribute(propertyName, newString);
224: }
225: }
226:
227: public void modelChanged(IModelChangedEvent event) {
228: if (event.getChangeType() == IModelChangedEvent.CHANGE) {
229: Object changedObject = event.getChangedObjects()[0];
230: if (changedObject instanceof IPluginObject) {
231: IPluginObject obj = (IPluginObject) event
232: .getChangedObjects()[0];
233: //Ignore events from objects that are not yet in the model.
234: if (!(obj instanceof IPluginBase)
235: && obj.isInTheModel() == false)
236: return;
237: }
238: if (changedObject instanceof IBuildObject) {
239: IBuildObject obj = (IBuildObject) event
240: .getChangedObjects()[0];
241: //Ignore events from objects that are not yet in the model.
242: if (obj.isInTheModel() == false)
243: return;
244: }
245: }
246: super.modelChanged(event);
247: }
248: }
|