KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > OverrideAnnotation


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 package org.netbeans.modules.java;
20
21 import java.lang.reflect.Modifier JavaDoc;
22 import java.text.MessageFormat JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
24 import org.netbeans.jmi.javamodel.JavaClass;
25 import org.netbeans.jmi.javamodel.Method;
26 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
27 import org.openide.cookies.SourceCookie;
28 import org.openide.loaders.DataObject;
29 import org.openide.text.Line;
30 import org.openide.text.NbDocument;
31 import org.openide.util.NbBundle;
32
33 class OverrideAnnotation extends LineSetAnnotation {
34
35
36     private static final String JavaDoc OVERRIDE_ANNOTATION_TYPE = "org-netbeans-modules-java-override_annotation"; // NOI18N
37
private static final String JavaDoc IMPLEMENT_ANNOTATION_TYPE = "org-netbeans-modules-java-implement_annotation"; // NOI18N
38

39     private Descriptor descriptor;
40     private int attachedToLine = -1;
41
42     private OverrideAnnotation (Descriptor descriptor) {
43         this.descriptor = descriptor;
44     }
45
46
47     public String JavaDoc getAnnotationType() {
48         return this.descriptor.isOverriding()? OVERRIDE_ANNOTATION_TYPE : IMPLEMENT_ANNOTATION_TYPE;
49     }
50
51     public String JavaDoc getShortDescription() {
52         String JavaDoc bundleKey = this.descriptor.isOverriding() ? "TXT_Overrides" : "TXT_Implements"; //NOI18N
53
return MessageFormat.format(NbBundle.getMessage(OverrideAnnotation.class,bundleKey),
54                 new Object JavaDoc[] {this.descriptor.getMethodName (), this.descriptor.getOriginalClassQName ()});
55     }
56
57     public void attachToLineSet(Line.Set lines) {
58         attachToLineSet(lines, this.descriptor.getLine());
59     }
60     
61     private void attachToLineSet(Line.Set lines, int line) {
62         if (line < 0) return;
63         Line docline = lines.getCurrent(line);
64         Line.Part part=docline.createPart(0,0);
65         attach(part);
66         attachedToLine = line;
67     }
68     
69     public void updateLine(Line.Set lines) {
70         int line = this.descriptor.getLine();
71         if (line != attachedToLine) {
72             detach();
73             attachToLineSet(lines, line);
74         }
75     }
76
77     public boolean equals (Object JavaDoc other) {
78         if (other instanceof OverrideAnnotation) {
79             OverrideAnnotation otherAnnotation = (OverrideAnnotation) other;
80             return this.descriptor.equals (otherAnnotation.descriptor);
81         }
82         return false;
83     }
84
85     Descriptor getDescriptor () {
86         return this.descriptor;
87     }
88
89     public static OverrideAnnotation forDescriptor (Descriptor desc) {
90         return new OverrideAnnotation (desc);
91     }
92
93     public static class Descriptor {
94
95         private final String JavaDoc overridenMethod;
96         private final String JavaDoc originalMethod;
97         private final boolean isImplementing;
98
99         public Descriptor (Method originalMethod, Method overridenMethod) {
100             this.originalMethod = originalMethod.refMofId();
101             this.overridenMethod = overridenMethod.refMofId();
102             JavaClass declaring = (JavaClass) originalMethod.getDeclaringClass();
103             isImplementing = declaring != null && (declaring.isInterface() ||
104                     Modifier.isAbstract (originalMethod.getModifiers()));
105         }
106
107         public final Method getOriginalMethod () {
108             return (Method) JavaMetamodel.getDefaultRepository().getByMofId(this.originalMethod);
109         }
110
111         public final Method getOverridenMethod () {
112             return (Method) JavaMetamodel.getDefaultRepository().getByMofId(this.overridenMethod);
113         }
114
115         public final boolean isOverriding () {
116             return ! this.isImplementing();
117         }
118
119         public final boolean isImplementing () {
120             return isImplementing;
121         }
122
123         public final String JavaDoc getMethodName () {
124             try {
125                 return getOverridenMethod().getName();
126             } catch (NullPointerException JavaDoc e) {
127                 return "";
128             }
129         }
130
131         public final String JavaDoc getOriginalClassQName () {
132             try {
133                 return getOriginalMethod().getDeclaringClass().getName();
134             } catch (NullPointerException JavaDoc e) {
135                 return "";
136             }
137         }
138
139         public final boolean equals (Object JavaDoc other) {
140             if (other instanceof OverrideAnnotation.Descriptor) {
141                 OverrideAnnotation.Descriptor oa = (OverrideAnnotation.Descriptor) other;
142                 return (this.originalMethod == null ? oa.originalMethod == null : this.originalMethod.equals (oa.originalMethod)) &&
143                        (this.overridenMethod == null ? oa.overridenMethod == null : this.overridenMethod.equals (oa.overridenMethod));
144             }
145             return false;
146         }
147
148         public final int hashCode () {
149             return this.overridenMethod == null ? 0 : this.originalMethod.hashCode();
150         }
151
152         /** returns -1 if the document is not accessible */
153         final int getLine () {
154             Method overridenMethod = getOverridenMethod();
155             if (overridenMethod == null) return -1;
156             DataObject dobj = JavaMetamodel.getManager().getDataObject(overridenMethod.getResource());
157             if (dobj == null) return -1;
158             SourceCookie.Editor seditor = (SourceCookie.Editor) dobj.getCookie (SourceCookie.Editor.class);
159             if (seditor == null) return -1;
160             StyledDocument JavaDoc doc = seditor.getDocument();
161             if (doc == null) return -1;
162
163             int offset = JavaMetamodel.getManager().getElementPosition(overridenMethod).getBegin().getOffset();
164             return NbDocument.findLineNumber(doc,offset);
165         }
166
167     }
168 }
169
Popular Tags