KickJava   Java API By Example, From Geeks To Geeks.

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


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.event.ActionEvent JavaDoc;
23 import java.util.logging.Level JavaDoc;
24 import javax.swing.AbstractAction JavaDoc;
25 import javax.swing.Action JavaDoc;
26 import javax.swing.text.BadLocationException JavaDoc;
27 import javax.swing.text.Document JavaDoc;
28 import javax.swing.text.JTextComponent JavaDoc;
29 import org.netbeans.editor.AnnotationDesc;
30 import org.netbeans.editor.Annotations;
31 import org.netbeans.editor.BaseDocument;
32 import org.netbeans.editor.ImplementationProvider;
33 import org.netbeans.editor.JumpList;
34 import org.netbeans.editor.Utilities;
35 import org.openide.ErrorManager;
36 import org.openide.filesystems.FileObject;
37 import org.openide.loaders.DataObject;
38
39 /**
40  *
41  * @author Jan Lahoda
42  */

43 public final class IsOverriddenAnnotationAction extends AbstractAction JavaDoc {
44     
45     public IsOverriddenAnnotationAction() {
46         setEnabled(true);
47     }
48     
49     public void actionPerformed(ActionEvent JavaDoc e) {
50         if (!invokeDefaultAction((JTextComponent JavaDoc) e.getSource())) {
51             Action JavaDoc actions[] = ImplementationProvider.getDefault().getGlyphGutterActions((JTextComponent JavaDoc) e.getSource());
52             
53             if (actions == null)
54                 return ;
55             
56             int nextAction = 0;
57             
58             while (nextAction < actions.length && actions[nextAction] != this)
59                 nextAction++;
60             
61             nextAction++;
62             
63             if (actions.length > nextAction) {
64                 Action JavaDoc a = actions[nextAction];
65                 if (a!=null && a.isEnabled()){
66                     a.actionPerformed(e);
67                 }
68             }
69         }
70     }
71     
72     private FileObject getFile(JTextComponent JavaDoc component) {
73         Document JavaDoc doc = component.getDocument();
74         DataObject od = (DataObject) doc.getProperty(Document.StreamDescriptionProperty);
75         
76         if (od == null) {
77             return null;
78         }
79         
80         return od.getPrimaryFile();
81     }
82     
83     private IsOverriddenAnnotation findAnnotation(JTextComponent JavaDoc component, AnnotationDesc desc, int offset) {
84         FileObject file = getFile(component);
85         
86         if (file == null) {
87             if (ErrorManager.getDefault().isLoggable(ErrorManager.WARNING)) {
88                 ErrorManager.getDefault().log(ErrorManager.WARNING, "component=" + component + " does not have a file specified in the document.");
89             }
90             return null;
91         }
92         
93         AnnotationsHolder ah = AnnotationsHolder.get(file);
94
95         if (ah == null) {
96             IsOverriddenAnnotationHandler.LOG.log(Level.INFO, "component=" + component + " does not have attached a IsOverriddenAnnotationHandler");
97
98             return null;
99         }
100
101         for(IsOverriddenAnnotation a : ah.getAnnotations()) {
102             if ( a.getPosition().getOffset() == offset
103                 && desc.getShortDescription().equals(a.getShortDescription())) {
104                 return a;
105             }
106         }
107         
108         return null;
109     }
110     
111     boolean invokeDefaultAction(final JTextComponent JavaDoc comp) {
112         final Document JavaDoc doc = comp.getDocument();
113         
114         if (doc instanceof BaseDocument) {
115             final int currentPosition = comp.getCaretPosition();
116             final Annotations annotations = ((BaseDocument) doc).getAnnotations();
117             final IsOverriddenAnnotation[] annotation = new IsOverriddenAnnotation[1];
118             final Point JavaDoc[] p = new Point JavaDoc[1];
119             
120             doc.render(new Runnable JavaDoc() {
121                 public void run() {
122                     try {
123                         int line = Utilities.getLineOffset((BaseDocument) doc, currentPosition);
124                         int startOffset = Utilities.getRowStartFromLineOffset((BaseDocument) doc, line);
125                         AnnotationDesc desc = annotations.getActiveAnnotation(line);
126                         p[0] = comp.modelToView(startOffset).getLocation();
127                         annotation[0] = findAnnotation(comp, desc, startOffset);
128                     } catch (BadLocationException JavaDoc ex) {
129                         ErrorManager.getDefault().notify(ex);
130                     }
131                 }
132             });
133             
134             if (annotation[0] == null)
135                 return false;
136             
137             JumpList.checkAddEntry(comp, currentPosition);
138             
139             annotation[0].mouseClicked(comp, p[0]);
140             
141             return true;
142         }
143         
144         return false;
145     }
146     
147 }
Popular Tags