KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > plugin > PluginUndoManager


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.plugin;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.pde.core.IModelChangeProvider;
15 import org.eclipse.pde.core.IModelChangedEvent;
16 import org.eclipse.pde.core.build.IBuild;
17 import org.eclipse.pde.core.build.IBuildEntry;
18 import org.eclipse.pde.core.build.IBuildModel;
19 import org.eclipse.pde.core.plugin.IPluginAttribute;
20 import org.eclipse.pde.core.plugin.IPluginBase;
21 import org.eclipse.pde.core.plugin.IPluginElement;
22 import org.eclipse.pde.core.plugin.IPluginExtension;
23 import org.eclipse.pde.core.plugin.IPluginExtensionPoint;
24 import org.eclipse.pde.core.plugin.IPluginImport;
25 import org.eclipse.pde.core.plugin.IPluginLibrary;
26 import org.eclipse.pde.core.plugin.IPluginModelBase;
27 import org.eclipse.pde.core.plugin.IPluginObject;
28 import org.eclipse.pde.core.plugin.IPluginParent;
29 import org.eclipse.pde.internal.core.build.BuildObject;
30 import org.eclipse.pde.internal.core.build.IBuildObject;
31 import org.eclipse.pde.internal.core.plugin.AttributeChangedEvent;
32 import org.eclipse.pde.internal.core.plugin.PluginAttribute;
33 import org.eclipse.pde.internal.core.plugin.PluginElement;
34 import org.eclipse.pde.internal.core.plugin.PluginObject;
35 import org.eclipse.pde.internal.core.text.plugin.PluginElementNode;
36 import org.eclipse.pde.internal.core.text.plugin.PluginLibraryNode;
37 import org.eclipse.pde.internal.core.text.plugin.PluginObjectNode;
38 import org.eclipse.pde.internal.ui.PDEPlugin;
39 import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
40 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
41
42 public class PluginUndoManager extends ModelUndoManager {
43     
44     public PluginUndoManager(PDEFormEditor editor) {
45         super(editor);
46         setUndoLevelLimit(30);
47     }
48
49     protected String JavaDoc getPageId(Object JavaDoc obj) {
50         if (obj instanceof IPluginBase)
51             return OverviewPage.PAGE_ID;
52         if (obj instanceof IPluginImport)
53             return DependenciesPage.PAGE_ID;
54         if (obj instanceof IPluginLibrary
55             || (obj instanceof IPluginElement && ((IPluginElement)obj).getParent() instanceof IPluginLibrary))
56             return RuntimePage.PAGE_ID;
57         if (obj instanceof IPluginExtension
58             || (obj instanceof IPluginElement && ((IPluginElement)obj).getParent() instanceof IPluginParent)
59             || obj instanceof IPluginAttribute)
60             return ExtensionsPage.PAGE_ID;
61         if (obj instanceof IPluginExtensionPoint)
62             return ExtensionPointsPage.PAGE_ID;
63         return null;
64     }
65     
66     protected void execute(IModelChangedEvent event, boolean undo) {
67         Object JavaDoc[] elements = event.getChangedObjects();
68         int type = event.getChangeType();
69         String JavaDoc propertyName = event.getChangedProperty();
70         IModelChangeProvider model = event.getChangeProvider();
71
72         switch (type) {
73             case IModelChangedEvent.INSERT :
74                 if (undo)
75                     executeRemove(model, elements);
76                 else
77                     executeAdd(model, elements);
78                 break;
79             case IModelChangedEvent.REMOVE :
80                 if (undo)
81                     executeAdd(model, elements);
82                 else
83                     executeRemove(model, elements);
84                 break;
85             case IModelChangedEvent.CHANGE :
86                 if (event instanceof AttributeChangedEvent) {
87                     executeAttributeChange((AttributeChangedEvent) event, undo);
88                 } else {
89                     if (undo)
90                         executeChange(
91                             elements[0],
92                             propertyName,
93                             event.getNewValue(),
94                             event.getOldValue());
95                     else
96                         executeChange(
97                             elements[0],
98                             propertyName,
99                             event.getOldValue(),
100                             event.getNewValue());
101                 }
102         }
103     }
104
105     private void executeAdd(IModelChangeProvider model, Object JavaDoc[] elements) {
106         IPluginBase pluginBase=null;
107         IBuild build=null;
108         if (model instanceof IPluginModelBase)
109             pluginBase = ((IPluginModelBase)model).getPluginBase();
110         if (model instanceof IBuildModel)
111             build = ((IBuildModel)model).getBuild();
112         
113         try {
114             for (int i = 0; i < elements.length; i++) {
115                 Object JavaDoc element = elements[i];
116
117                 if (element instanceof IPluginImport) {
118                     pluginBase.add((IPluginImport) element);
119                 } else if (element instanceof IPluginLibrary) {
120                     pluginBase.add((IPluginLibrary) element);
121                 } else if (element instanceof IPluginExtensionPoint) {
122                     pluginBase.add((IPluginExtensionPoint) element);
123                 } else if (element instanceof IPluginExtension) {
124                     pluginBase.add((IPluginExtension) element);
125                 } else if (element instanceof IPluginElement) {
126                     IPluginElement e = (IPluginElement) element;
127                     Object JavaDoc parent = e.getParent();
128                     if (parent instanceof PluginLibraryNode && e instanceof PluginElementNode) {
129                         ((PluginLibraryNode)parent).addContentFilter((PluginElementNode)e);
130                     } else if (parent instanceof IPluginParent) {
131                         ((IPluginParent)parent).add(e);
132                     }
133                 } else if (element instanceof IBuildEntry) {
134                     IBuildEntry e = (IBuildEntry)element;
135                     build.add(e);
136                 }
137             }
138         } catch (CoreException e) {
139             PDEPlugin.logException(e);
140         }
141     }
142     
143     private void executeRemove(IModelChangeProvider model, Object JavaDoc[] 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 JavaDoc 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 JavaDoc parent = e.getParent();
166                     if (parent instanceof PluginLibraryNode && e instanceof PluginElementNode) {
167                         ((PluginLibraryNode)parent).removeContentFilter((PluginElementNode)e);
168                     } else if (parent instanceof IPluginParent) {
169                         ((IPluginParent)parent).remove(e);
170                     }
171                 } else if (element instanceof IBuildEntry) {
172                     IBuildEntry e = (IBuildEntry)element;
173                     build.remove(e);
174                 }
175             }
176         } catch (CoreException e) {
177             PDEPlugin.logException(e);
178         }
179     }
180
181     private void executeAttributeChange(AttributeChangedEvent e, boolean undo) {
182         PluginElement element = (PluginElement) e.getChangedObjects()[0];
183         PluginAttribute att = (PluginAttribute) e.getChangedAttribute();
184         Object JavaDoc oldValue = e.getOldValue();
185         Object JavaDoc newValue = e.getNewValue();
186         try {
187             if (undo)
188                 element.setAttribute(att.getName(), oldValue.toString());
189             else
190                 element.setAttribute(att.getName(), newValue.toString());
191         } catch (CoreException ex) {
192             PDEPlugin.logException(ex);
193         }
194     }
195
196     private void executeChange(
197         Object JavaDoc element,
198         String JavaDoc propertyName,
199         Object JavaDoc oldValue,
200         Object JavaDoc newValue) {
201         if (element instanceof PluginObject) {
202             PluginObject pobj = (PluginObject) element;
203             try {
204                 pobj.restoreProperty(propertyName, oldValue, newValue);
205             } catch (CoreException e) {
206                 PDEPlugin.logException(e);
207             }
208         }
209         else if (element instanceof BuildObject) {
210             BuildObject bobj = (BuildObject) element;
211             try {
212                 bobj.restoreProperty(propertyName, oldValue, newValue);
213             } catch (CoreException e) {
214                 PDEPlugin.logException(e);
215             }
216         }
217         else if (element instanceof PluginObjectNode) {
218             PluginObjectNode node = (PluginObjectNode)element;
219             String JavaDoc newString = newValue!=null?newValue.toString():null;
220             node.setXMLAttribute(propertyName, newString);
221         }
222     }
223
224     public void modelChanged(IModelChangedEvent event) {
225         if (event.getChangeType() == IModelChangedEvent.CHANGE) {
226             Object JavaDoc changedObject = event.getChangedObjects()[0];
227             if (changedObject instanceof IPluginObject) {
228                 IPluginObject obj = (IPluginObject) event.getChangedObjects()[0];
229                 //Ignore events from objects that are not yet in the model.
230
if (!(obj instanceof IPluginBase) && obj.isInTheModel() == false)
231                     return;
232             }
233             if (changedObject instanceof IBuildObject) {
234                 IBuildObject obj = (IBuildObject) event.getChangedObjects()[0];
235                 //Ignore events from objects that are not yet in the model.
236
if (obj.isInTheModel() == false)
237                     return;
238             }
239         }
240         super.modelChanged(event);
241     }
242 }
243
Popular Tags