KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > feature > FeatureUndoManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.editor.feature;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.pde.core.IModelChangedEvent;
15 import org.eclipse.pde.internal.core.feature.FeatureObject;
16 import org.eclipse.pde.internal.core.ifeature.IFeature;
17 import org.eclipse.pde.internal.core.ifeature.IFeatureChild;
18 import org.eclipse.pde.internal.core.ifeature.IFeatureData;
19 import org.eclipse.pde.internal.core.ifeature.IFeatureImport;
20 import org.eclipse.pde.internal.core.ifeature.IFeatureModel;
21 import org.eclipse.pde.internal.core.ifeature.IFeatureObject;
22 import org.eclipse.pde.internal.core.ifeature.IFeaturePlugin;
23 import org.eclipse.pde.internal.core.ifeature.IFeatureURL;
24 import org.eclipse.pde.internal.ui.PDEPlugin;
25 import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
26 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
27
28 public class FeatureUndoManager extends ModelUndoManager {
29
30     public FeatureUndoManager(PDEFormEditor editor) {
31         super(editor);
32         setUndoLevelLimit(30);
33     }
34
35     protected String JavaDoc getPageId(Object JavaDoc obj) {
36         if (obj instanceof IFeature || obj instanceof IFeatureURL)
37             return FeatureFormPage.PAGE_ID;
38         if (obj instanceof IFeaturePlugin)
39             return FeatureReferencePage.PAGE_ID;
40         if (obj instanceof IFeatureImport)
41             return FeatureDependenciesPage.PAGE_ID;
42         if (obj instanceof IFeatureData || obj instanceof IFeatureChild)
43             return FeatureAdvancedPage.PAGE_ID;
44         return null;
45     }
46
47     protected void execute(IModelChangedEvent event, boolean undo) {
48         Object JavaDoc[] elements = event.getChangedObjects();
49         int type = event.getChangeType();
50         String JavaDoc propertyName = event.getChangedProperty();
51         IFeatureModel model = (IFeatureModel) event.getChangeProvider();
52
53         switch (type) {
54         case IModelChangedEvent.INSERT:
55             if (undo)
56                 executeRemove(model, elements);
57             else
58                 executeAdd(model, elements);
59             break;
60         case IModelChangedEvent.REMOVE:
61             if (undo)
62                 executeAdd(model, elements);
63             else
64                 executeRemove(model, elements);
65             break;
66         case IModelChangedEvent.CHANGE:
67             if (undo)
68                 executeChange(elements[0], propertyName, event.getNewValue(),
69                         event.getOldValue());
70             else
71                 executeChange(elements[0], propertyName, event.getOldValue(),
72                         event.getNewValue());
73         }
74     }
75
76     private void executeAdd(IFeatureModel model, Object JavaDoc[] elements) {
77         IFeature feature = model.getFeature();
78
79         try {
80             for (int i = 0; i < elements.length; i++) {
81                 Object JavaDoc element = elements[i];
82
83                 if (element instanceof IFeaturePlugin) {
84                     feature
85                             .addPlugins(new IFeaturePlugin[] { (IFeaturePlugin) element });
86                 } else if (element instanceof IFeatureData) {
87                     feature
88                             .addData(new IFeatureData[] { (IFeatureData) element });
89                 } else if (element instanceof IFeatureImport) {
90                     feature
91                             .addImports(new IFeatureImport[] { (IFeatureImport) element });
92                 } else if (element instanceof IFeatureChild) {
93                     feature
94                             .addIncludedFeatures(new IFeatureChild[] { (IFeatureChild) element });
95                 }
96             }
97         } catch (CoreException e) {
98             PDEPlugin.logException(e);
99         }
100     }
101
102     private void executeRemove(IFeatureModel model, Object JavaDoc[] elements) {
103         IFeature feature = model.getFeature();
104
105         try {
106             for (int i = 0; i < elements.length; i++) {
107                 Object JavaDoc element = elements[i];
108
109                 if (element instanceof IFeaturePlugin) {
110                     feature
111                             .removePlugins(new IFeaturePlugin[] { (IFeaturePlugin) element });
112                 } else if (element instanceof IFeatureData) {
113                     feature
114                             .removeData(new IFeatureData[] { (IFeatureData) element });
115                 } else if (element instanceof IFeatureImport) {
116                     feature
117                             .removeImports(new IFeatureImport[] { (IFeatureImport) element });
118                 } else if (element instanceof IFeatureChild) {
119                     feature
120                             .removeIncludedFeatures(new IFeatureChild[] { (IFeatureChild) element });
121                 }
122             }
123         } catch (CoreException e) {
124             PDEPlugin.logException(e);
125         }
126     }
127
128     private void executeChange(Object JavaDoc element, String JavaDoc propertyName,
129             Object JavaDoc oldValue, Object JavaDoc newValue) {
130         if (element instanceof FeatureObject) {
131             FeatureObject pobj = (FeatureObject) element;
132             try {
133                 pobj.restoreProperty(propertyName, oldValue, newValue);
134             } catch (CoreException e) {
135                 PDEPlugin.logException(e);
136             }
137         }
138     }
139
140     public void modelChanged(IModelChangedEvent event) {
141         if (event.getChangeType() == IModelChangedEvent.CHANGE) {
142             Object JavaDoc obj = event.getChangedObjects()[0];
143             if (obj instanceof IFeatureObject) {
144                 IFeatureObject fobj = (IFeatureObject) event
145                         .getChangedObjects()[0];
146                 // Ignore events from objects that are not yet in the model.
147
if (!(fobj instanceof IFeature) && fobj.isInTheModel() == false)
148                     return;
149             }
150         }
151         super.modelChanged(event);
152     }
153 }
154
Popular Tags