KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > refactoring > ui > j > spi > ui > AbstractRefactoringAction


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 package org.netbeans.modules.xml.refactoring.ui.j.spi.ui;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import javax.swing.Action JavaDoc;
23 import javax.swing.Icon JavaDoc;
24 import javax.swing.JEditorPane JavaDoc;
25 import javax.swing.text.JTextComponent JavaDoc;
26 import javax.swing.text.TextAction JavaDoc;
27 //import org.netbeans.jmi.javamodel.Element;
28
//import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
29
//import org.netbeans.modules.refactoring.api.ui.RefactoringActionsFactory;
30
//import org.netbeans.modules.refactoring.ui.RefactoringPanel;
31
//import org.netbeans.api.mdr.MDRepository;
32
//import org.netbeans.modules.javacore.api.JavaModel;
33
import org.openide.cookies.EditorCookie;
34 import org.openide.loaders.DataObject;
35 import org.openide.nodes.Node;
36 import org.openide.text.CloneableEditorSupport;
37 import org.openide.util.HelpCtx;
38 import org.openide.util.actions.NodeAction;
39 import org.openide.windows.TopComponent;
40
41 /** Generic superclass for refactoring actions.
42  * Subclasses should implement createRefactoringUI and enabled methods.
43  *
44  * @author Martin Matula
45  */

46 public abstract class AbstractRefactoringAction extends NodeAction implements RefactoringAction, Runnable JavaDoc {
47     private Node[] activatedNodes;
48     private int caretPosition;
49     private int startSelectionPosition;
50     private int endSelectionPosition;
51     private static DelegateTextAction delegate = new DelegateTextAction();
52
53     /** Creates a new instance of AbstractRefactoringAction */
54     public AbstractRefactoringAction(String JavaDoc name, Icon JavaDoc icon) {
55         setName(name);
56         setIcon(icon);
57     }
58     
59     public final String JavaDoc getName() {
60         return (String JavaDoc) getValue(Action.NAME);
61     }
62     
63     protected void setName(String JavaDoc name) {
64         putValue(Action.NAME, name);
65     }
66     
67     protected void setMnemonic(char m) {
68         putValue(Action.MNEMONIC_KEY, new Integer JavaDoc(m));
69     }
70     
71     public final void performAction(final Node[] n) {
72         new Runnable JavaDoc() {
73             public void run() {
74                 processContext(n, delegate.getTextComponent());
75                 AbstractRefactoringAction.this.run();
76             }
77         };
78 // JavaMetamodel.getManager().invokeAfterScanFinished(r, trim(getName()));
79
}
80     
81     private boolean invokedExternally = false;
82 // public void actionPerformed(final ActionEvent ev) {
83
//// if (ev == RefactoringActionsFactory.DEFAULT_EVENT) {
84
//// invokedExternally = true;
85
//// }
86
//// AbstractRefactoringAction.super.actionPerformed(ev);
87
//// if (ev == RefactoringActionsFactory.DEFAULT_EVENT) {
88
//// invokedExternally = false;
89
//// }
90
// }
91

92     private static String JavaDoc trim(String JavaDoc arg) {
93         arg = org.openide.util.Utilities.replaceString(arg, "&", ""); // NOI18N
94
return org.openide.util.Utilities.replaceString(arg, "...", ""); // NOI18N
95
}
96     
97     protected abstract boolean enabled(Node[] activatedNodes);
98     
99     /** Should be implemented by subclasses to provide instance of RefactoringUI
100      * implementation for the implemented refactoring. The method should initialize
101      * the RefactoringUI implementation with the provided array of nodes and textComponent.
102      * @param nodes Array of activated nodes that the refactoring should be performed
103      * on.
104      * @param selectedElement org.netbeans.jmi.javamodel.Element at cursor position if the action is invoked in editor or <code>null</code>.
105      * @return Implementation of RefactoringUI for the refactoring this action is
106      * supposed to invoke.
107      */

108 // protected abstract RefactoringUI createRefactoringUI(Node[] nodes, Element selectedElement);
109

110 // protected RefactoringUI createRefactoringUI(Element selectedElement,int startOffset,int endOffset) {
111
// return createRefactoringUI(activatedNodes, selectedElement);
112
// }
113

114     public final void processContext(Node[] activatedNodes, JTextComponent JavaDoc textComponent) {
115         this.activatedNodes = activatedNodes;
116
117         setCaretPosition(textComponent);
118         boolean e = enabled(activatedNodes);
119         //if (e != isEnabled()) {
120
setEnabled(e);
121         //}
122
}
123     
124     private void setCaretPosition(JTextComponent JavaDoc textComponent) {
125         caretPosition = -1;
126         if (activatedNodes == null || textComponent == null || invokedExternally)
127             return;
128         // #56228: need to get EditorCookie from DataObject rather than directly
129
DataObject dobj = (DataObject) activatedNodes[0].getCookie(DataObject.class);
130         if (dobj != null) {
131             EditorCookie ec = (EditorCookie) dobj.getCookie(EditorCookie.class);
132             if (ec != null) {
133                 TopComponent activetc = TopComponent.getRegistry().getActivated();
134                 if (activetc instanceof CloneableEditorSupport.Pane) {
135                     JEditorPane JavaDoc pane=((CloneableEditorSupport.Pane)activetc).getEditorPane();
136
137                     if (textComponent.equals(pane)) {
138                         caretPosition = textComponent.getCaretPosition();
139                         startSelectionPosition = textComponent.getSelectionStart();
140                         endSelectionPosition = textComponent.getSelectionEnd();
141                         
142                         //fix of issue 62849
143
if (startSelectionPosition != endSelectionPosition && endSelectionPosition == caretPosition) {
144                             caretPosition--;
145                         }
146                     }
147                 }
148             }
149         }
150     }
151
152     public final void run() {
153 // Element selectedElement=null;
154
// MDRepository rep = JavaModel.getJavaRepository();
155
// RefactoringUI ui = null;
156
//
157
// rep.beginTrans(true);
158
// try {
159
//// JavaMetamodel.getManager().setClassPath(
160
//// ClassPathFactory.createClassPath(RefactoringClassPathImplementation.getDefault())
161
//// );
162
// if (caretPosition != -1) {
163
// FileObject fo;
164
// DataObject dobj = (DataObject)activatedNodes[0].getCookie(DataObject.class);
165
// while (dobj instanceof DataShadow) {
166
// dobj = ((DataShadow) dobj).getOriginal();
167
// }
168
// fo=dobj.getPrimaryFile();
169
// JavaModel.setClassPath(fo);
170
// selectedElement = JavaMetamodel.getManager().getElementByOffset(fo,caretPosition);
171
// if (selectedElement !=null && startSelectionPosition != endSelectionPosition) {
172
// Element startElement,endElement;
173
//
174
// ui = createRefactoringUI(selectedElement, startSelectionPosition, endSelectionPosition);
175
// }
176
// }
177
// if (ui == null) {
178
// ui = createRefactoringUI(activatedNodes, selectedElement);
179
// }
180
// } finally {
181
// rep.endTrans();
182
// }
183
// TopComponent activetc = TopComponent.getRegistry().getActivated();
184
// if (activetc instanceof CloneableEditorSupport.Pane) {
185
// new RefactoringPanel(ui, activetc);
186
// } else {
187
// new RefactoringPanel(ui);
188
// }
189
}
190     
191     public boolean enable(Node[]a) {
192         return enabled(a);
193     }
194     
195     public org.openide.util.HelpCtx getHelpCtx() {
196         return HelpCtx.DEFAULT_HELP;
197     }
198     
199     protected void firePropertyChange(String JavaDoc name, Object JavaDoc oldValue, Object JavaDoc newValue) {
200         if (PROP_ENABLED.equals(name)) {
201             if (newValue != null) {
202                 super.firePropertyChange(name, oldValue, newValue);
203             }
204         } else {
205             super.firePropertyChange(name, oldValue, newValue);
206         }
207     }
208
209     protected boolean asynchronous() {
210         return false;
211     }
212
213 // public void performAction() {
214
// super.performAction();
215
// }
216

217     private static final class DelegateTextAction extends TextAction JavaDoc {
218     public static final long serialVersionUID = 1L;
219         
220         public DelegateTextAction() {
221             super("");
222         }
223         
224         public void actionPerformed(ActionEvent JavaDoc a) {
225         }
226         
227         public JTextComponent JavaDoc getTextComponent() {
228             return getFocusedComponent();
229         }
230     };
231
232 }
233
Popular Tags