KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Point JavaDoc;
22 import java.awt.Toolkit JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.List JavaDoc;
26 import javax.swing.SwingUtilities JavaDoc;
27 import javax.swing.text.JTextComponent JavaDoc;
28 import javax.swing.text.Position JavaDoc;
29 import javax.swing.text.StyledDocument JavaDoc;
30 import org.netbeans.api.java.source.UiUtils;
31 import org.openide.filesystems.FileObject;
32 import org.openide.text.Annotation;
33 import org.openide.text.NbDocument;
34 import org.openide.util.NbBundle;
35
36 /**
37  *
38  * @author Jan Lahoda
39  */

40 class IsOverriddenAnnotation extends Annotation {
41     
42     private StyledDocument JavaDoc document;
43     private Position JavaDoc pos;
44     private String JavaDoc shortDescription;
45     private AnnotationType type;
46     private List JavaDoc<ElementDescription> declarations;
47     
48     public IsOverriddenAnnotation(StyledDocument JavaDoc document, Position JavaDoc pos, AnnotationType type, String JavaDoc shortDescription, List JavaDoc<ElementDescription> declarations) {
49         this.document = document;
50         this.pos = pos;
51         this.type = type;
52         this.shortDescription = shortDescription;
53         this.declarations = declarations;
54     }
55     
56     public String JavaDoc getShortDescription() {
57         return shortDescription;
58     }
59
60     public String JavaDoc getAnnotationType() {
61         switch(type) {
62             case IS_OVERRIDDEN:
63
64                 return "org-netbeans-modules-editor-java-is_overridden";
65             case HAS_IMPLEMENTATION:
66
67                 return "org-netbeans-modules-editor-java-has_implementations";
68             case IMPLEMENTS:
69
70                 return "org-netbeans-modules-editor-java-implements";
71             case OVERRIDES:
72
73                 return "org-netbeans-modules-editor-java-overrides";
74             default:
75
76                 throw new IllegalStateException JavaDoc("Currently not implemented: " + type);
77         }
78     }
79     
80     public void attach() {
81         NbDocument.addAnnotation(document, pos, -1, this);
82     }
83     
84     public void detachImpl() {
85         NbDocument.removeAnnotation(document, this);
86     }
87     
88     public String JavaDoc toString() {
89         return "[IsOverriddenAnnotation: " + shortDescription + "]";
90     }
91     
92     public Position JavaDoc getPosition() {
93         return pos;
94     }
95     
96     public String JavaDoc debugDump() {
97         List JavaDoc<String JavaDoc> elementNames = new ArrayList JavaDoc<String JavaDoc>();
98         
99         for(ElementDescription desc : declarations) {
100             elementNames.add(desc.getDisplayName());
101         }
102         
103         Collections.sort(elementNames);
104         
105         return "IsOverriddenAnnotation: type=" + type.name() + ", elements:" + elementNames.toString();
106     }
107     
108     void mouseClicked(JTextComponent JavaDoc c, Point JavaDoc p) {
109         Point JavaDoc position = new Point JavaDoc(p);
110         
111         SwingUtilities.convertPointToScreen(position, c);
112         
113         performGoToAction(type, declarations, position, shortDescription);
114     }
115     
116     static void performGoToAction(AnnotationType type, List JavaDoc<ElementDescription> declarations, Point JavaDoc position, String JavaDoc shortDescription/*XXX*/) {
117         if (type == AnnotationType.IMPLEMENTS || type == AnnotationType.OVERRIDES) {
118             if (declarations.size() == 1) {
119                 ElementDescription desc = declarations.get(0);
120                 FileObject file = desc.getSourceFile();
121                 
122                 if (file != null) {
123                     UiUtils.open(file, desc.getHandle());
124                 } else {
125                     Toolkit.getDefaultToolkit().beep();
126                 }
127                 
128                 return ;
129             }
130         }
131         
132         String JavaDoc caption;
133         
134         switch (type) {
135             case IMPLEMENTS:
136                 caption = NbBundle.getMessage(IsOverriddenAnnotation.class, "CAP_Implements");
137                 break;
138             case OVERRIDES:
139                 caption = NbBundle.getMessage(IsOverriddenAnnotation.class, "CAP_Overrides");
140                 break;
141             case HAS_IMPLEMENTATION:
142             case IS_OVERRIDDEN:
143                 caption = shortDescription;
144                 break;
145             default:
146                 throw new IllegalStateException JavaDoc("Currently not implemented: " + type);
147         }
148         
149         PopupUtil.showPopup(new IsOverriddenPopup(caption, declarations), caption, position.x, position.y, true, 0);
150     }
151 }
Popular Tags