1 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 getPageId(Object 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 [] elements = event.getChangedObjects(); 39 int type = event.getChangeType(); 40 String 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 [] elements) { 73 IBuild build = model.getBuild(); 74 75 try { 76 for (int i = 0; i < elements.length; i++) { 77 Object 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 [] elements) { 89 IBuild build = model.getBuild(); 90 91 try { 92 for (int i = 0; i < elements.length; i++) { 93 Object 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 element, 106 String propertyName, 107 Object oldValue, 108 Object 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 obj = event.getChangedObjects()[0]; 122 if (obj instanceof IBuildObject) { 123 IBuildObject bobj = (IBuildObject) event.getChangedObjects()[0]; 124 if (!(bobj instanceof IBuild) && bobj.isInTheModel() == false) 126 return; 127 } 128 } 129 super.modelChanged(event); 130 } 131 } 132 | Popular Tags |