KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > site > SiteUndoManager


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.site;
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.internal.core.isite.ISite;
17 import org.eclipse.pde.internal.core.isite.ISiteArchive;
18 import org.eclipse.pde.internal.core.isite.ISiteCategory;
19 import org.eclipse.pde.internal.core.isite.ISiteCategoryDefinition;
20 import org.eclipse.pde.internal.core.isite.ISiteDescription;
21 import org.eclipse.pde.internal.core.isite.ISiteFeature;
22 import org.eclipse.pde.internal.core.isite.ISiteModel;
23 import org.eclipse.pde.internal.core.isite.ISiteObject;
24 import org.eclipse.pde.internal.core.site.SiteObject;
25 import org.eclipse.pde.internal.ui.PDEPlugin;
26 import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
27
28 public class SiteUndoManager extends ModelUndoManager {
29     public SiteUndoManager(SiteEditor editor) {
30         super(editor);
31         setUndoLevelLimit(30);
32     }
33
34     protected String JavaDoc getPageId(Object JavaDoc obj) {
35         if (obj instanceof ISiteDescription) {
36             return ArchivePage.PAGE_ID;
37         }
38         if (obj instanceof ISiteFeature || obj instanceof ISiteCategory
39                 || obj instanceof ISiteCategoryDefinition) {
40             return FeaturesPage.PAGE_ID;
41         }
42         // site elements and attributes are on different pages, stay on the
43
// current page
44
return null;
45     }
46     /*
47      * @see IModelUndoManager#execute(ModelUndoOperation)
48      */

49     protected void execute(IModelChangedEvent event, boolean undo) {
50         IModelChangeProvider model = event.getChangeProvider();
51         Object JavaDoc[] elements = event.getChangedObjects();
52         int type = event.getChangeType();
53         String JavaDoc propertyName = event.getChangedProperty();
54
55         switch (type) {
56             case IModelChangedEvent.INSERT :
57                 if (undo)
58                     executeRemove(model, elements);
59                 else
60                     executeAdd(model, elements);
61                 break;
62             case IModelChangedEvent.REMOVE :
63                 if (undo)
64                     executeAdd(model, elements);
65                 else
66                     executeRemove(model, elements);
67                 break;
68             case IModelChangedEvent.CHANGE :
69                 if (undo)
70                     executeChange(
71                         elements[0],
72                         propertyName,
73                         event.getNewValue(),
74                         event.getOldValue());
75                 else
76                     executeChange(
77                         elements[0],
78                         propertyName,
79                         event.getOldValue(),
80                         event.getNewValue());
81         }
82     }
83
84     private void executeAdd(IModelChangeProvider model, Object JavaDoc[] elements) {
85         ISiteModel siteModel = (model instanceof ISiteModel)?(ISiteModel)model:null;
86         ISite site = siteModel!=null?siteModel.getSite():null;
87
88         try {
89             for (int i = 0; i < elements.length; i++) {
90                 Object JavaDoc element = elements[i];
91
92                 if (element instanceof ISiteFeature) {
93                     site.addFeatures(new ISiteFeature [] {(ISiteFeature) element});
94                 } else if (element instanceof ISiteArchive) {
95                     site.addArchives(new ISiteArchive[] {(ISiteArchive) element});
96                 } else if (element instanceof ISiteCategoryDefinition) {
97                     site.addCategoryDefinitions(new ISiteCategoryDefinition[] {(ISiteCategoryDefinition) element});
98                 } else if (element instanceof ISiteCategory) {
99                     ISiteCategory category = (ISiteCategory)element;
100                     ISiteFeature feature = (ISiteFeature)category.getParent();
101                     feature.addCategories(new ISiteCategory[] {category});
102                 }
103             }
104         } catch (CoreException e) {
105             PDEPlugin.logException(e);
106         }
107     }
108
109     private void executeRemove(IModelChangeProvider model, Object JavaDoc[] elements) {
110         ISiteModel siteModel = (model instanceof ISiteModel)?(ISiteModel)model:null;
111         ISite site = siteModel!=null?siteModel.getSite():null;
112         
113         try {
114             for (int i = 0; i < elements.length; i++) {
115                 Object JavaDoc element = elements[i];
116
117                 if (element instanceof ISiteFeature) {
118                     site.removeFeatures(new ISiteFeature [] {(ISiteFeature) element});
119                 } else if (element instanceof ISiteArchive) {
120                     site.removeArchives(new ISiteArchive[] {(ISiteArchive) element});
121                 } else if (element instanceof ISiteCategoryDefinition) {
122                     site.removeCategoryDefinitions(new ISiteCategoryDefinition[] {(ISiteCategoryDefinition) element});
123                 } else if (element instanceof ISiteCategory) {
124                     ISiteCategory category = (ISiteCategory)element;
125                     ISiteFeature feature = (ISiteFeature)category.getParent();
126                     feature.removeCategories(new ISiteCategory[] {category});
127                 }
128             }
129         } catch (CoreException e) {
130             PDEPlugin.logException(e);
131         }
132     }
133
134     private void executeChange(
135         Object JavaDoc element,
136         String JavaDoc propertyName,
137         Object JavaDoc oldValue,
138         Object JavaDoc newValue) {
139
140         if (element instanceof SiteObject) {
141             SiteObject sobj = (SiteObject) element;
142             try {
143                 sobj.restoreProperty(propertyName, oldValue, newValue);
144             } catch (CoreException e) {
145                 PDEPlugin.logException(e);
146             }
147         }
148     }
149
150     public void modelChanged(IModelChangedEvent event) {
151         if (event.getChangeType() == IModelChangedEvent.CHANGE) {
152             Object JavaDoc object = event.getChangedObjects()[0];
153             if (object instanceof ISiteObject) {
154                 ISiteObject obj = (ISiteObject) object;
155                 //Ignore events from objects that are not yet in the model.
156
if (!(obj instanceof ISite) && !obj.isInTheModel())
157                     return;
158             }
159         }
160         super.modelChanged(event);
161     }
162 }
163
Popular Tags