KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > tools > OverrideAction


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-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.java.tools;
21
22 import java.awt.Dialog JavaDoc;
23 import java.awt.event.ActionEvent JavaDoc;
24 import java.beans.PropertyChangeEvent JavaDoc;
25 import java.beans.PropertyChangeListener JavaDoc;
26 import java.text.MessageFormat JavaDoc;
27 import java.util.*;
28 import java.util.Iterator JavaDoc;
29 import javax.swing.JEditorPane JavaDoc;
30 import javax.swing.text.JTextComponent JavaDoc;
31 import javax.swing.text.TextAction JavaDoc;
32 import org.netbeans.api.mdr.MDRepository;
33 import org.netbeans.jmi.javamodel.ClassDefinition;
34 import org.netbeans.jmi.javamodel.ClassMember;
35 import org.netbeans.jmi.javamodel.Element;
36 import org.netbeans.jmi.javamodel.JavaClass;
37 import org.netbeans.jmi.javamodel.Method;
38 import org.netbeans.jmi.javamodel.Resource;
39 import org.netbeans.modules.java.JavaDataObject;
40 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
41 import org.openide.*;
42 import org.openide.cookies.EditorCookie;
43 import org.openide.loaders.DataObject;
44 import org.openide.loaders.DataShadow;
45 import org.openide.nodes.*;
46 import org.openide.text.CloneableEditorSupport;
47 import org.openide.text.PositionBounds;
48 import org.openide.text.PositionRef;
49
50 import org.openide.util.HelpCtx;
51 import org.openide.util.NbBundle;
52 import org.openide.util.actions.NodeAction;
53 import org.openide.windows.TopComponent;
54
55 /** This action allows the user to override some of the methods present in the
56  * selected class.
57  * The action displays a modal dialog with several lists, then overrides
58  * one or more methods the user selects.
59  */

60 public class OverrideAction extends NodeAction {
61     
62     private DelegateTextAction delegateAction = new DelegateTextAction();
63     
64     static String JavaDoc getString(String JavaDoc key) {
65         return NbBundle.getMessage(OverrideAction.class, key);
66     }
67     
68     public OverrideAction () {
69         putValue("noIconInMenu", Boolean.TRUE); // NOI18N
70
}
71     
72     public HelpCtx getHelpCtx() {
73         return null;
74     }
75     
76     public String JavaDoc getName() {
77         return getString("LAB_OverrideTool");
78     }
79     
80     public void performAction(final Node[] activatedNodes) {
81         Runnable JavaDoc run = new Runnable JavaDoc() {
82             public void run() {
83                 ClassDefinition el = getJavaClass(activatedNodes[0]);
84                 boolean isInterface = el instanceof JavaClass && ((JavaClass)el).isInterface();
85                 if (isInterface) {
86                     NotifyDescriptor nd = new NotifyDescriptor(
87                             getString("LAB_OverrideWarningDiagText"),
88                             getString("LAB_OverrideWarningDiagTitle"),
89                             NotifyDescriptor.DEFAULT_OPTION,
90                             NotifyDescriptor.WARNING_MESSAGE,
91                             null,
92                             null
93                     );
94                     DialogDisplayer.getDefault().notify(nd);
95                     return;
96                 }
97                 if (el == null) return;
98
99                 JMIInheritanceSupport support = new JMIInheritanceSupport(el);
100
101                 String JavaDoc title = MessageFormat.format(getString("LBL_OverridePanel2_Title"), //NOI18N
102
new Object JavaDoc[] { el.getName() } );
103                 OverridePanel2 panel = new OverridePanel2(support);
104                 final DialogDescriptor desc = new DialogDescriptor(panel, title, true, DialogDescriptor.OK_CANCEL_OPTION, DialogDescriptor.OK_OPTION, null);
105                 panel.addPropertyChangeListener(new PropertyChangeListener JavaDoc() {
106                     public void propertyChange(PropertyChangeEvent JavaDoc evt) {
107                         if (OverridePanel2.PROP_VALID.equals(evt.getPropertyName())) {
108                             desc.setValid(((Boolean JavaDoc) evt.getNewValue()).booleanValue());
109                         }
110                     }
111                 });
112                 HelpCtx ctx = new HelpCtx(OverrideAction.class.getName());
113                 desc.setHelpCtx(ctx);
114
115                 Dialog JavaDoc dialog = DialogDisplayer.getDefault().createDialog(desc);
116                 dialog.setVisible(true);
117                 dialog.dispose();
118
119                 if (desc.getValue().equals(DialogDescriptor.OK_OPTION)) {
120                     Map selMethods = panel.getSelection();
121                     Collection methods = support.sortMethods(selMethods);
122                     
123                     Method first = null;
124                     int index = 0;
125
126                     MDRepository repository = JavaMetamodel.getDefaultRepository();
127                     boolean failed = true;
128                     repository.beginTrans(true);
129                     try {
130                         if (!el.isValid()) {
131                             el = getJavaClass(activatedNodes[0]);
132                             support.setRootClass(el);
133                         }
134                         for (Iterator JavaDoc i = methods.iterator(); i.hasNext(); ) {
135                             Method m = (Method) i.next();
136                             m = support.overrideMethod(m, panel.isGenerateSuperCalls(), panel.isGenerateJavadoc());
137                             if (first == null) {
138                                 first = m;
139                                 index = el.getFeatures().indexOf(m);
140                             }
141                         }
142                         failed = false;
143                     } finally {
144                         repository.endTrans(failed);
145                     }
146
147                     if (first != null) {
148                         PositionBounds bounds = null;
149                         repository.beginTrans(false);
150                         try {
151                             if (!first.isValid()) {
152                                 if (!el.isValid())
153                                     el = getJavaClass(activatedNodes[0]);
154                                 List list = el.getFeatures();
155                                 if (list.size() > index) {
156                                     Iterator JavaDoc iter = list.iterator();
157                                     Object JavaDoc obj = null;
158                                     for (int x = 0; x <= index; x++)
159                                         obj = iter.next();
160                                     // Object obj = list.get(index);
161
if (obj instanceof Method) {
162                                         first = (Method)obj;
163                                     }
164                                 }
165                             }
166                             if (first.isValid()) {
167                                 bounds = JavaMetamodel.getManager().getElementPosition(first);
168                             }
169                         } finally {
170                             repository.endTrans();
171                         }
172                         if (bounds != null) {
173                             PositionRef beginPos = bounds.getBegin();
174                             CloneableEditorSupport editSupp = beginPos.getCloneableEditorSupport();
175                             JEditorPane JavaDoc[] panes = editSupp.getOpenedPanes();
176                             if (panes==null) {
177                                 editSupp.open();
178                             }
179                         }
180                     }
181                 }
182             }
183         };
184         JavaMetamodel.getManager().invokeAfterScanFinished(run, getString("LBL_OverridePanel2_Title"));
185     }
186     
187     protected boolean enable(Node[] activatedNodes) {
188         if (activatedNodes.length != 1) {
189             return false;
190         }
191         
192         Element elem = getElement(activatedNodes[0]);
193         return elem != null;
194     }
195     
196     private Element getElement(Node n) {
197         Element el = (Element) n.getLookup().lookup(Element.class);
198         if (el == null) {
199             JavaDataObject jdo = (JavaDataObject) n.getCookie(JavaDataObject.class);
200             if (jdo !=null)
201                 el = JavaMetamodel.getManager().getResource(jdo.getPrimaryFile());
202             if (el==null)
203                 return null;
204         }
205         
206         if (!((el instanceof Resource) || (el instanceof ClassMember) || (el instanceof ClassDefinition))) {
207             el = null;
208         }
209         return el;
210     }
211     
212     private ClassDefinition getJavaClass(Node n) {
213         // force reparse of the source if it was modified
214
MDRepository repository = JavaMetamodel.getDefaultRepository();
215         repository.beginTrans(true);
216         repository.endTrans();
217         
218         int caretPosition = -1;
219         EditorCookie ec = (EditorCookie) n.getCookie(EditorCookie.class);
220         if (ec != null) {
221             TopComponent activetc = TopComponent.getRegistry().getActivated();
222             if (activetc instanceof CloneableEditorSupport.Pane) {
223                 JEditorPane JavaDoc pane = ((CloneableEditorSupport.Pane)activetc).getEditorPane();
224                 JTextComponent JavaDoc textComponent = delegateAction.getTextComponent();
225                 if (textComponent == null) {
226                     return null;
227                 }
228                 
229                 if (textComponent.equals(pane)) {
230                     caretPosition = textComponent.getCaretPosition();
231                 }
232             }
233         }
234         Element el = null;
235         if (caretPosition != -1) {
236             DataObject dobj = (DataObject)n.getCookie(DataObject.class);
237             while (dobj instanceof DataShadow) {
238                 dobj = ((DataShadow) dobj).getOriginal();
239             }
240             el = JavaMetamodel.getManager().getElementByOffset(dobj.getPrimaryFile(), caretPosition);
241         } else {
242             el = getElement(n);
243         }
244         
245         if (el==null)
246             return null;
247
248         while (!(el instanceof ClassDefinition) && !(el instanceof Resource)) {
249             el = (Element)el.refImmediateComposite();
250         }
251
252         if (el instanceof Resource) {
253             for (Iterator JavaDoc iter = ((Resource) el).getClassifiers().iterator(); iter.hasNext(); ) {
254                 Object JavaDoc obj = iter.next();
255                 if (obj instanceof JavaClass) {
256                     return (JavaClass) obj;
257                 }
258             }
259             return null;
260         }
261         return (ClassDefinition) el;
262     }
263     
264     protected boolean asynchronous() {
265         return false;
266     }
267     
268     // ..........................................................................
269
private static final class DelegateTextAction extends TextAction JavaDoc {
270         
271         public DelegateTextAction() {
272             super("");
273         }
274         
275         public void actionPerformed(ActionEvent JavaDoc a) {
276         }
277         
278         public JTextComponent JavaDoc getTextComponent() {
279             return getFocusedComponent();
280         }
281     }; // DelegateTextAction
282

283 }
284
Popular Tags