KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > javaparser > CastPerformer


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.tasklist.javaparser;
21
22 import javax.swing.text.*;
23 import javax.swing.event.*;
24 import java.awt.*;
25 import java.awt.event.*;
26 import javax.swing.*;
27 import org.openide.ErrorManager;
28 import org.openide.explorer.view.*;
29 import org.openide.nodes.*;
30 import org.netbeans.modules.java.*;
31
32 import org.openide.loaders.DataObject;
33 import org.openide.text.Line;
34
35 import org.netbeans.editor.ext.java.*;
36 import org.netbeans.modules.editor.java.*;
37
38 import org.netbeans.modules.tasklist.core.ConfPanel;
39 import org.netbeans.modules.tasklist.core.TLUtils;
40 import org.netbeans.modules.tasklist.client.Suggestion;
41 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
42
43
44 /**
45  * This class performs casting to a required type
46  * <p>
47  * @todo I have both the class name, and the fully qualified class name.
48  * If I use the FQCN the source code gets pretty ugly (especially
49  * in the presense of long package paths, e.g.
50  * SuggestionImpl s = (org.netbeans.modules.tasklist.suggestions.SuggestionImpl)it.next();
51  * So for now, I'm just casting to the class name (SuggestionImpl) above.
52  * But should I add an import statement too?
53  *
54  * @author Tor Norbye
55  */

56 class CastPerformer implements SuggestionPerformer {
57     
58     private int column;
59     private Line line;
60     private DataObject dobj;
61     private Document doc;
62     private String JavaDoc reqType;
63     private String JavaDoc reqClass;
64     private String JavaDoc beforeDesc;
65
66     CastPerformer(int column, Line line, DataObject dobj,
67                   Document doc,
68                   String JavaDoc reqType,
69                   String JavaDoc reqClass,
70                   String JavaDoc beforeDesc) {
71         this.column = column;
72         this.line = line;
73         this.dobj = dobj;
74         this.doc = doc;
75         this.reqType = reqType;
76         this.reqClass = reqClass;
77         this.beforeDesc = beforeDesc;
78     }
79
80     public boolean hasConfirmation() {
81         return true;
82     }
83     
84     public Object JavaDoc getConfirmation(Suggestion s) {
85         String JavaDoc filename =
86             dobj.getPrimaryFile().getNameExt();
87         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(200);
88         Line l = line;
89         String JavaDoc text = l.getText();
90         
91         // Underline differences
92
sb.append("<html>"); // NOI18N
93
TLUtils.appendSurroundingLine(sb, l, -1);
94         TLUtils.appendHTMLString(sb, text.substring(0, column));
95         sb.append("<b>("); // NOI18N
96
sb.append(reqClass);
97         sb.append(")</b>"); // NOI18N
98
TLUtils.appendHTMLString(sb, text.substring(column));
99         //sb.append("<br>");
100
TLUtils.appendSurroundingLine(sb, l, +1);
101         sb.append("</html>"); // NOI18N
102
String JavaDoc beforeContents = sb.toString();
103         int lineno = line.getLineNumber();
104         return new ConfPanel(beforeDesc, beforeContents, null,
105                              null, filename, lineno, null);
106     }
107     
108     public void perform(Suggestion s) {
109         if (!(doc instanceof StyledDocument)) {
110             return;
111         }
112         
113         int lineno = line.getLineNumber();
114         StyledDocument sdoc = (StyledDocument)doc;
115         
116         Element e = sdoc.getParagraphElement(0).getParentElement();
117         if (e == null) {
118             // try default root (should work for text/plain)
119
e = sdoc.getDefaultRootElement();
120         }
121         Element elm = e.getElement(lineno);
122         if (elm == null) {
123             return;
124         }
125         int offset = elm.getStartOffset();
126         
127         try {
128             int pos = offset + column;
129             sdoc.insertString(pos, "(" + reqClass + ")", null);
130
131             // MakeMethodPerformer has this issue; is this relevant
132
// for incompatible types too?
133
// Interestingly, at least on OSX with jdk1.3, I may get
134
// a "wrong" column, e.g. it will point at "foo" in
135
// "foo".length
136
// ^
137
// So I've gotta start searching forward
138
} catch (BadLocationException ex) {
139             ErrorManager.getDefault().notify(ErrorManager.WARNING, ex);
140         }
141     }
142 }
143
144
Popular Tags