KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > impl > GeneralChangeExecutor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.xml.refactoring.impl;
21
22 import java.io.IOException JavaDoc;
23 import javax.swing.event.UndoableEditEvent JavaDoc;
24 import javax.swing.event.UndoableEditListener JavaDoc;
25 import javax.swing.undo.AbstractUndoableEdit JavaDoc;
26 import javax.swing.undo.CannotRedoException JavaDoc;
27 import javax.swing.undo.CannotUndoException JavaDoc;
28 import javax.swing.undo.UndoableEdit JavaDoc;
29 import javax.swing.undo.UndoableEditSupport JavaDoc;
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 /**
39  * Change executor for refactoring actions that are not specific to any particular
40  * XML document models.
41  *
42  * @author Nam Nguyen
43  */

44 public class GeneralChangeExecutor extends ChangeExecutor {
45     private UndoableEditSupport JavaDoc ues;
46     
47     /** Creates a new instance of GenericChangeExecutor */
48     public GeneralChangeExecutor() {
49         ues = new UndoableEditSupport JavaDoc(this);
50     }
51     
52     public <T extends RefactorRequest> boolean canChange(Class JavaDoc<T> changeType, Referenceable target) {
53         if (changeType == FileRenameRequest.class && target instanceof Model) {
54             return true;
55         }
56         return false;
57     }
58     
59     /**
60      * Perform the change specified by the refactor request. Any errors that would
61      * fail the overall refactoring should be reported throught #RefactoringRequest.addError
62      * Implementation should quietly ignore unsupported refactoring type.
63      */

64     public void doChange(RefactorRequest request) throws IOException JavaDoc {
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     /**
74      * Returns UI helper in displaying the usages. Implementation could override
75      * the default UI to help display usages in a more intuitive way than the
76      * generic helper.
77      */

78     public UIHelper getUIHelper() {
79         return new UIHelper();
80     }
81     
82     public synchronized void addUndoableEditListener(UndoableEditListener JavaDoc l) {
83         ues.addUndoableEditListener(l);
84     }
85     
86     public synchronized void removeUndoableEditListener(UndoableEditListener JavaDoc l) {
87         ues.removeUndoableEditListener(l);
88     }
89     
90     protected void fireUndoEvent(UndoableEdit JavaDoc edit) {
91         UndoableEditEvent JavaDoc ue = new UndoableEditEvent JavaDoc(this, edit);
92         for (UndoableEditListener JavaDoc l:ues.getUndoableEditListeners()) {
93             l.undoableEditHappened(ue);
94         }
95     }
96     
97     public static class FileRenameUndoable extends AbstractUndoableEdit JavaDoc {
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 JavaDoc {
106             try {
107                 SharedUtils.undoRenameFile(request);
108                 super.undo();
109             } catch(IOException JavaDoc ioe) {
110                 CannotUndoException JavaDoc cue = new CannotUndoException JavaDoc();
111                 cue.initCause(ioe);
112                 throw cue;
113             }
114         }
115         
116         public void redo() throws CannotRedoException JavaDoc {
117             try {
118                 SharedUtils.renameFile(request);
119                 super.redo();
120             } catch(IOException JavaDoc ioe) {
121                 CannotUndoException JavaDoc cue = new CannotUndoException JavaDoc();
122                 cue.initCause(ioe);
123                 throw cue;
124             }
125         }
126     }
127 }
128
Popular Tags