1 19 20 package org.netbeans.modules.xml.refactoring.impl; 21 22 import java.io.IOException ; 23 import javax.swing.event.UndoableEditEvent ; 24 import javax.swing.event.UndoableEditListener ; 25 import javax.swing.undo.AbstractUndoableEdit ; 26 import javax.swing.undo.CannotRedoException ; 27 import javax.swing.undo.CannotUndoException ; 28 import javax.swing.undo.UndoableEdit ; 29 import javax.swing.undo.UndoableEditSupport ; 30 import org.netbeans.modules.xml.refactoring.FileRenameRequest; 31 import org.netbeans.modules.xml.refactoring.RefactorRequest; 32 import org.netbeans.modules.xml.refactoring.spi.ChangeExecutor; 33 import org.netbeans.modules.xml.refactoring.spi.SharedUtils; 34 import org.netbeans.modules.xml.refactoring.spi.UIHelper; 35 import org.netbeans.modules.xml.xam.Model; 36 import org.netbeans.modules.xml.xam.Referenceable; 37 38 44 public class GeneralChangeExecutor extends ChangeExecutor { 45 private UndoableEditSupport ues; 46 47 48 public GeneralChangeExecutor() { 49 ues = new UndoableEditSupport (this); 50 } 51 52 public <T extends RefactorRequest> boolean canChange(Class <T> changeType, Referenceable target) { 53 if (changeType == FileRenameRequest.class && target instanceof Model) { 54 return true; 55 } 56 return false; 57 } 58 59 64 public void doChange(RefactorRequest request) throws IOException { 65 if (request instanceof FileRenameRequest) { 66 FileRenameRequest frr = (FileRenameRequest)request; 67 SharedUtils.renameFile(frr); 68 FileRenameUndoable ue = new FileRenameUndoable(frr); 69 fireUndoEvent(ue); 70 } 71 } 72 73 78 public UIHelper getUIHelper() { 79 return new UIHelper(); 80 } 81 82 public synchronized void addUndoableEditListener(UndoableEditListener l) { 83 ues.addUndoableEditListener(l); 84 } 85 86 public synchronized void removeUndoableEditListener(UndoableEditListener l) { 87 ues.removeUndoableEditListener(l); 88 } 89 90 protected void fireUndoEvent(UndoableEdit edit) { 91 UndoableEditEvent ue = new UndoableEditEvent (this, edit); 92 for (UndoableEditListener l:ues.getUndoableEditListeners()) { 93 l.undoableEditHappened(ue); 94 } 95 } 96 97 public static class FileRenameUndoable extends AbstractUndoableEdit { 98 private static final long serialVersionUID = -1L; 99 private FileRenameRequest request; 100 101 public FileRenameUndoable(FileRenameRequest request) { 102 this.request = request; 103 } 104 105 public void undo() throws CannotUndoException { 106 try { 107 SharedUtils.undoRenameFile(request); 108 super.undo(); 109 } catch(IOException ioe) { 110 CannotUndoException cue = new CannotUndoException (); 111 cue.initCause(ioe); 112 throw cue; 113 } 114 } 115 116 public void redo() throws CannotRedoException { 117 try { 118 SharedUtils.renameFile(request); 119 super.redo(); 120 } catch(IOException ioe) { 121 CannotUndoException cue = new CannotUndoException (); 122 cue.initCause(ioe); 123 throw cue; 124 } 125 } 126 } 127 } 128 | Popular Tags |