KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > editor > overridden > GoToSuperTypeAction


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.java.editor.overridden;
20
21 import com.sun.source.tree.Tree.Kind;
22 import com.sun.source.util.TreePath;
23 import java.awt.Point JavaDoc;
24 import java.awt.Toolkit JavaDoc;
25 import java.awt.event.ActionEvent JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.util.ArrayList JavaDoc;
28 import java.util.List JavaDoc;
29 import javax.lang.model.element.Element;
30 import javax.lang.model.element.ElementKind;
31 import javax.lang.model.element.ExecutableElement;
32 import javax.lang.model.element.TypeElement;
33 import javax.swing.SwingUtilities JavaDoc;
34 import javax.swing.text.BadLocationException JavaDoc;
35 import javax.swing.text.JTextComponent JavaDoc;
36 import org.netbeans.api.java.source.CancellableTask;
37 import org.netbeans.api.java.source.CompilationController;
38 import org.netbeans.api.java.source.JavaSource;
39 import org.netbeans.api.java.source.JavaSource.Phase;
40 import org.netbeans.editor.BaseAction;
41 import org.netbeans.editor.ext.ExtKit;
42 import org.netbeans.modules.editor.java.JavaKit;
43 import org.openide.util.Exceptions;
44 import org.openide.util.NbBundle;
45
46 /**
47  *
48  * @author Jan Lahoda
49  */

50 public class GoToSuperTypeAction extends BaseAction {
51     
52     public GoToSuperTypeAction() {
53         super(JavaKit.gotoSuperImplementationAction, SAVE_POSITION | ABBREV_RESET);
54         putValue(SHORT_DESCRIPTION, NbBundle.getBundle(JavaKit.class).getString("goto-super-implementation"));
55         String JavaDoc name = NbBundle.getBundle(JavaKit.class).getString("goto-super-implementation-trimmed");
56         putValue(ExtKit.TRIMMED_TEXT,name);
57         putValue(POPUP_MENU_TEXT, name);
58     }
59     
60     public void actionPerformed(ActionEvent JavaDoc evt, final JTextComponent JavaDoc target) {
61         JavaSource js = JavaSource.forDocument(target.getDocument());
62         
63         if (js == null) {
64             Toolkit.getDefaultToolkit().beep();
65         }
66         
67         final List JavaDoc<ElementDescription> result = new ArrayList JavaDoc<ElementDescription>();
68         final AnnotationType[] type = new AnnotationType[1];
69         
70         try {
71             js.runUserActionTask(new CancellableTask<CompilationController>() {
72                 public void cancel() {}
73                 public void run(CompilationController parameter) throws Exception JavaDoc {
74                     parameter.toPhase(Phase.RESOLVED); //!!!
75

76                     TreePath path = parameter.getTreeUtilities().pathFor(target.getCaretPosition());
77                     
78                     while (path != null && path.getLeaf().getKind() != Kind.METHOD) {
79                         path = path.getParentPath();
80                     }
81                     
82                     if (path == null) {
83                         Toolkit.getDefaultToolkit().beep();
84                         return ;
85                     }
86                     
87                     Element resolved = parameter.getTrees().getElement(path);
88                     
89                     if (resolved == null || resolved.getKind() != ElementKind.METHOD) {
90                         Toolkit.getDefaultToolkit().beep();
91                         return ;
92                     }
93                     
94                     ExecutableElement ee = (ExecutableElement) resolved;
95                     
96                     type[0] = IsOverriddenAnnotationHandler.detectOverrides(parameter, (TypeElement) ee.getEnclosingElement(), ee, result);
97                 }
98                 
99             }, true);
100             
101             if (type[0] == null) {
102                 Toolkit.getDefaultToolkit().beep();
103                 return ;
104             }
105             
106             Point JavaDoc p = new Point JavaDoc(target.modelToView(target.getCaretPosition()).getLocation());
107             
108             SwingUtilities.convertPointToScreen(p, target);
109             
110             IsOverriddenAnnotation.performGoToAction(type[0], result, p, "");
111         } catch (IOException JavaDoc e) {
112             Exceptions.printStackTrace(e);
113         } catch (BadLocationException JavaDoc e) {
114             Exceptions.printStackTrace(e);
115         }
116     }
117
118 }
119
Popular Tags