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.build;
011:
012: import org.eclipse.core.runtime.CoreException;
013: import org.eclipse.pde.core.IModelChangedEvent;
014: import org.eclipse.pde.core.build.IBuild;
015: import org.eclipse.pde.core.build.IBuildEntry;
016: import org.eclipse.pde.core.build.IBuildModel;
017: import org.eclipse.pde.internal.core.build.BuildObject;
018: import org.eclipse.pde.internal.core.build.IBuildObject;
019: import org.eclipse.pde.internal.ui.PDEPlugin;
020: import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
021: import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
022:
023: public class BuildUndoManager extends ModelUndoManager {
024:
025: public BuildUndoManager(PDEFormEditor editor) {
026: super (editor);
027: setUndoLevelLimit(30);
028: }
029:
030: protected String getPageId(Object obj) {
031: if (obj instanceof IBuildEntry)
032: return BuildPage.PAGE_ID;
033: return null;
034: }
035:
036: protected void execute(IModelChangedEvent event, boolean undo) {
037: Object[] elements = event.getChangedObjects();
038: int type = event.getChangeType();
039: String propertyName = event.getChangedProperty();
040: IBuildModel model = (IBuildModel) event.getChangeProvider();
041:
042: switch (type) {
043: case IModelChangedEvent.INSERT:
044: if (undo)
045: executeRemove(model, elements);
046: else
047: executeAdd(model, elements);
048: break;
049: case IModelChangedEvent.REMOVE:
050: if (undo)
051: executeAdd(model, elements);
052: else
053: executeRemove(model, elements);
054: break;
055: case IModelChangedEvent.CHANGE:
056: if (undo)
057: executeChange(elements[0], propertyName, event
058: .getNewValue(), event.getOldValue());
059: else
060: executeChange(elements[0], propertyName, event
061: .getOldValue(), event.getNewValue());
062: }
063: }
064:
065: private void executeAdd(IBuildModel model, Object[] elements) {
066: IBuild build = model.getBuild();
067:
068: try {
069: for (int i = 0; i < elements.length; i++) {
070: Object element = elements[i];
071:
072: if (element instanceof IBuildEntry) {
073: build.add((IBuildEntry) element);
074: }
075: }
076: } catch (CoreException e) {
077: PDEPlugin.logException(e);
078: }
079: }
080:
081: private void executeRemove(IBuildModel model, Object[] elements) {
082: IBuild build = model.getBuild();
083:
084: try {
085: for (int i = 0; i < elements.length; i++) {
086: Object element = elements[i];
087:
088: if (element instanceof IBuildEntry) {
089: build.remove((IBuildEntry) element);
090: }
091: }
092: } catch (CoreException e) {
093: PDEPlugin.logException(e);
094: }
095: }
096:
097: private void executeChange(Object element, String propertyName,
098: Object oldValue, Object newValue) {
099: if (element instanceof BuildObject) {
100: BuildObject bobj = (BuildObject) element;
101: try {
102: bobj.restoreProperty(propertyName, oldValue, newValue);
103: } catch (CoreException e) {
104: PDEPlugin.logException(e);
105: }
106: }
107: }
108:
109: public void modelChanged(IModelChangedEvent event) {
110: if (event.getChangeType() == IModelChangedEvent.CHANGE) {
111: Object obj = event.getChangedObjects()[0];
112: if (obj instanceof IBuildObject) {
113: IBuildObject bobj = (IBuildObject) event
114: .getChangedObjects()[0];
115: //Ignore events from objects that are not yet in the model.
116: if (!(bobj instanceof IBuild)
117: && bobj.isInTheModel() == false)
118: return;
119: }
120: }
121: super.modelChanged(event);
122: }
123: }
|