KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > editor > build > BuildUndoManager


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.build;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.pde.core.IModelChangedEvent;
15 import org.eclipse.pde.core.build.IBuild;
16 import org.eclipse.pde.core.build.IBuildEntry;
17 import org.eclipse.pde.core.build.IBuildModel;
18 import org.eclipse.pde.internal.core.build.BuildObject;
19 import org.eclipse.pde.internal.core.build.IBuildObject;
20 import org.eclipse.pde.internal.ui.PDEPlugin;
21 import org.eclipse.pde.internal.ui.editor.ModelUndoManager;
22 import org.eclipse.pde.internal.ui.editor.PDEFormEditor;
23
24 public class BuildUndoManager extends ModelUndoManager {
25
26     public BuildUndoManager(PDEFormEditor editor) {
27         super(editor);
28         setUndoLevelLimit(30);
29     }
30
31     protected String JavaDoc getPageId(Object JavaDoc obj) {
32         if (obj instanceof IBuildEntry)
33             return BuildPage.PAGE_ID;
34         return null;
35     }
36
37     protected void execute(IModelChangedEvent event, boolean undo) {
38         Object JavaDoc[] elements = event.getChangedObjects();
39         int type = event.getChangeType();
40         String JavaDoc propertyName = event.getChangedProperty();
41         IBuildModel model = (IBuildModel)event.getChangeProvider();
42
43         switch (type) {
44             case IModelChangedEvent.INSERT :
45                 if (undo)
46                     executeRemove(model, elements);
47                 else
48                     executeAdd(model, elements);
49                 break;
50             case IModelChangedEvent.REMOVE :
51                 if (undo)
52                     executeAdd(model, elements);
53                 else
54                     executeRemove(model, elements);
55                 break;
56             case IModelChangedEvent.CHANGE :
57                 if (undo)
58                     executeChange(
59                         elements[0],
60                         propertyName,
61                         event.getNewValue(),
62                         event.getOldValue());
63                 else
64                     executeChange(
65                         elements[0],
66                         propertyName,
67                         event.getOldValue(),
68                         event.getNewValue());
69         }
70     }
71
72     private void executeAdd(IBuildModel model, Object JavaDoc[] elements) {
73         IBuild build = model.getBuild();
74
75         try {
76             for (int i = 0; i < elements.length; i++) {
77                 Object JavaDoc element = elements[i];
78
79                 if (element instanceof IBuildEntry) {
80                     build.add((IBuildEntry)element);
81                 }
82             }
83         } catch (CoreException e) {
84             PDEPlugin.logException(e);
85         }
86     }
87
88     private void executeRemove(IBuildModel model, Object JavaDoc[] elements) {
89         IBuild build = model.getBuild();
90
91         try {
92             for (int i = 0; i < elements.length; i++) {
93                 Object JavaDoc element = elements[i];
94
95                 if (element instanceof IBuildEntry) {
96                     build.remove((IBuildEntry)element);
97                 }
98             }
99         } catch (CoreException e) {
100             PDEPlugin.logException(e);
101         }
102     }
103
104     private void executeChange(
105         Object JavaDoc element,
106         String JavaDoc propertyName,
107         Object JavaDoc oldValue,
108         Object JavaDoc newValue) {
109         if (element instanceof BuildObject) {
110             BuildObject bobj = (BuildObject) element;
111             try {
112                 bobj.restoreProperty(propertyName, oldValue, newValue);
113             } catch (CoreException e) {
114                 PDEPlugin.logException(e);
115             }
116         }
117     }
118
119     public void modelChanged(IModelChangedEvent event) {
120         if (event.getChangeType() == IModelChangedEvent.CHANGE) {
121             Object JavaDoc obj = event.getChangedObjects()[0];
122             if (obj instanceof IBuildObject) {
123                 IBuildObject bobj = (IBuildObject) event.getChangedObjects()[0];
124                 //Ignore events from objects that are not yet in the model.
125
if (!(bobj instanceof IBuild) && bobj.isInTheModel() == false)
126                     return;
127             }
128         }
129         super.modelChanged(event);
130     }
131 }
132
Popular Tags