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.site;
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.internal.core.isite.ISite;
016: import org.eclipse.pde.internal.core.isite.ISiteArchive;
017: import org.eclipse.pde.internal.core.isite.ISiteCategory;
018: import org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition;
019: import org.eclipse.pde.internal.core.isite.ISiteDescription;
020: import org.eclipse.pde.internal.core.isite.ISiteFeature;
021: import org.eclipse.pde.internal.core.isite.ISiteModel;
022: import org.eclipse.pde.internal.core.isite.ISiteObject;
023: import org.eclipse.pde.internal.core.site.SiteObject;
024: import org.eclipse.pde.internal.ui.PDEPlugin;
025: import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
026:
027: public class SiteUndoManager extends ModelUndoManager {
028: public SiteUndoManager(SiteEditor editor) {
029: super (editor);
030: setUndoLevelLimit(30);
031: }
032:
033: protected String getPageId(Object obj) {
034: if (obj instanceof ISiteDescription) {
035: return ArchivePage.PAGE_ID;
036: }
037: if (obj instanceof ISiteFeature || obj instanceof ISiteCategory
038: || obj instanceof ISiteCategoryDefinition) {
039: return FeaturesPage.PAGE_ID;
040: }
041: // site elements and attributes are on different pages, stay on the
042: // current page
043: return null;
044: }
045:
046: /*
047: * @see IModelUndoManager#execute(ModelUndoOperation)
048: */
049: protected void execute(IModelChangedEvent event, boolean undo) {
050: IModelChangeProvider model = event.getChangeProvider();
051: Object[] elements = event.getChangedObjects();
052: int type = event.getChangeType();
053: String propertyName = event.getChangedProperty();
054:
055: switch (type) {
056: case IModelChangedEvent.INSERT:
057: if (undo)
058: executeRemove(model, elements);
059: else
060: executeAdd(model, elements);
061: break;
062: case IModelChangedEvent.REMOVE:
063: if (undo)
064: executeAdd(model, elements);
065: else
066: executeRemove(model, elements);
067: break;
068: case IModelChangedEvent.CHANGE:
069: if (undo)
070: executeChange(elements[0], propertyName, event
071: .getNewValue(), event.getOldValue());
072: else
073: executeChange(elements[0], propertyName, event
074: .getOldValue(), event.getNewValue());
075: }
076: }
077:
078: private void executeAdd(IModelChangeProvider model,
079: Object[] elements) {
080: ISiteModel siteModel = (model instanceof ISiteModel) ? (ISiteModel) model
081: : null;
082: ISite site = siteModel != null ? siteModel.getSite() : null;
083:
084: try {
085: for (int i = 0; i < elements.length; i++) {
086: Object element = elements[i];
087:
088: if (element instanceof ISiteFeature) {
089: site
090: .addFeatures(new ISiteFeature[] { (ISiteFeature) element });
091: } else if (element instanceof ISiteArchive) {
092: site
093: .addArchives(new ISiteArchive[] { (ISiteArchive) element });
094: } else if (element instanceof ISiteCategoryDefinition) {
095: site
096: .addCategoryDefinitions(new ISiteCategoryDefinition[] { (ISiteCategoryDefinition) element });
097: } else if (element instanceof ISiteCategory) {
098: ISiteCategory category = (ISiteCategory) element;
099: ISiteFeature feature = (ISiteFeature) category
100: .getParent();
101: feature
102: .addCategories(new ISiteCategory[] { category });
103: }
104: }
105: } catch (CoreException e) {
106: PDEPlugin.logException(e);
107: }
108: }
109:
110: private void executeRemove(IModelChangeProvider model,
111: Object[] elements) {
112: ISiteModel siteModel = (model instanceof ISiteModel) ? (ISiteModel) model
113: : null;
114: ISite site = siteModel != null ? siteModel.getSite() : null;
115:
116: try {
117: for (int i = 0; i < elements.length; i++) {
118: Object element = elements[i];
119:
120: if (element instanceof ISiteFeature) {
121: site
122: .removeFeatures(new ISiteFeature[] { (ISiteFeature) element });
123: } else if (element instanceof ISiteArchive) {
124: site
125: .removeArchives(new ISiteArchive[] { (ISiteArchive) element });
126: } else if (element instanceof ISiteCategoryDefinition) {
127: site
128: .removeCategoryDefinitions(new ISiteCategoryDefinition[] { (ISiteCategoryDefinition) element });
129: } else if (element instanceof ISiteCategory) {
130: ISiteCategory category = (ISiteCategory) element;
131: ISiteFeature feature = (ISiteFeature) category
132: .getParent();
133: feature
134: .removeCategories(new ISiteCategory[] { category });
135: }
136: }
137: } catch (CoreException e) {
138: PDEPlugin.logException(e);
139: }
140: }
141:
142: private void executeChange(Object element, String propertyName,
143: Object oldValue, Object newValue) {
144:
145: if (element instanceof SiteObject) {
146: SiteObject sobj = (SiteObject) element;
147: try {
148: sobj.restoreProperty(propertyName, oldValue, newValue);
149: } catch (CoreException e) {
150: PDEPlugin.logException(e);
151: }
152: }
153: }
154:
155: public void modelChanged(IModelChangedEvent event) {
156: if (event.getChangeType() == IModelChangedEvent.CHANGE) {
157: Object object = event.getChangedObjects()[0];
158: if (object instanceof ISiteObject) {
159: ISiteObject obj = (ISiteObject) object;
160: //Ignore events from objects that are not yet in the model.
161: if (!(obj instanceof ISite) && !obj.isInTheModel())
162: return;
163: }
164: }
165: super.modelChanged(event);
166: }
167: }
|