KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.List JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Collections JavaDoc;
31 import java.util.Comparator JavaDoc;
32 import java.util.Iterator JavaDoc;
33 import org.openide.ErrorManager;
34 import org.openide.cookies.SourceCookie;
35 import org.openide.explorer.view.*;
36 import org.openide.nodes.*;
37 import org.netbeans.modules.java.*;
38 import org.openide.src.*;
39 import org.openide.text.NbDocument;
40
41
42 import org.openide.src.Identifier;
43 import org.openide.src.Import;
44 import org.openide.src.SourceElement;
45 import org.openide.src.SourceException;
46 import org.openide.util.NbBundle;
47 import org.openide.util.Utilities;
48
49 import org.openide.cookies.LineCookie;
50 import org.openide.loaders.DataObject;
51 import org.openide.text.Line;
52 import org.openide.ErrorManager;
53
54 import java.util.TreeSet JavaDoc;
55 import java.lang.reflect.Modifier JavaDoc;
56 import org.netbeans.editor.ext.java.*;
57 import org.netbeans.modules.editor.java.*;
58
59 import org.netbeans.modules.tasklist.core.ConfPanel;
60 import org.netbeans.modules.tasklist.core.TLUtils;
61 import org.netbeans.modules.tasklist.client.Suggestion;
62 import org.netbeans.modules.tasklist.client.SuggestionPerformer;
63
64
65 /**
66  * This class performs symbol replacement suggestions
67  *
68  * @author Tor Norbye
69  */

70 class CreateMethodPerformer implements SuggestionPerformer {
71     
72     private DataObject dobj;
73     private Document doc;
74     private String JavaDoc symbol;
75     private String JavaDoc location;
76     private String JavaDoc args;
77     private boolean makePublic;
78
79     CreateMethodPerformer(DataObject dobj,
80                           String JavaDoc symbol, String JavaDoc location,
81                           String JavaDoc args, boolean makePublic) {
82         this.dobj = dobj;
83         this.symbol = symbol;
84         this.location = location;
85         this.args = args;
86         this.makePublic = makePublic;
87     }
88
89     // Yay - it's a casing-error
90
public void perform(Suggestion s) {
91         SourceCookie sc = null;
92         sc = (SourceCookie)dobj.getCookie(SourceCookie.Editor.class);
93         if (sc == null) {
94             return;
95         }
96         SourceElement se = sc.getSource();
97         if (se == null) {
98             return;
99         }
100         ClassElement[] classes = se.getClasses();
101         if (classes == null) {
102             return;
103         }
104         String JavaDoc none =
105             NbBundle.getMessage(CreateMethodPerformer.class, "NoArgs");//NOI18N
106
for (int i = 0; i < classes.length; i++) {
107             if (classes[i].isInner()) {
108                 // Inner class - probably not the one we're looking for
109
} else {
110                 MethodElement el = null;
111                 try {
112                     el = new MethodElement();
113                     el.setName(Identifier.create(symbol));
114                     if (makePublic) {
115                         int mask = Modifier.PUBLIC;
116                         el.setModifiers(mask);
117                     }
118                     // TODO Figure out which modifier I need. If it's in
119
// my own package, it should be package private
120
// (the default) otherwise, set it to public
121
//el.setModifiers();
122

123                     // Do I get a return type out of the compiler error?
124
// Can I tell from the context?
125

126                     //el.setReturn(Type ret);
127
// XXX el.addJavaDoc(" /**@todo Implement the body of " + symbol + "(" + args + ") */");
128
MethodParameter[] params;
129                     ArrayList JavaDoc plist = new ArrayList JavaDoc();
130                     int begin = 0;
131                     while (true) {
132                         int next = args.indexOf(',', begin);
133                         String JavaDoc arg = null;
134                         if (next == -1) {
135                             arg = args.substring(begin);
136                         } else {
137                             arg = args.substring(begin, next);
138                         }
139                         if ((arg.length() > 0) && !args.equals(none)) {
140                             plist.add(arg);
141                         }
142                         if (next != -1) {
143                             begin = next+1;
144                         } else {
145                             break;
146                         }
147                     }
148                     params = new MethodParameter[plist.size()];
149                     Type[] types = new Type[plist.size()];
150                     for (int j = 0; j < params.length; j++) {
151                         Type type = Type.parse(plist.get(j).toString());
152                         types[j] = type;
153                         boolean fin = false; // final?
154
// How do we name the parameters?
155
String JavaDoc name = "param"+(j+1);
156                         params[j] = new MethodParameter(name, type, fin);
157                     }
158                     el.setParameters(params);
159
160                     el.setBody("\n/**@todo Implement this method*/\nthrow new java.lang.UnsupportedOperationException(\n \"Method " + symbol + "("+args+") not yet implemented.\");\n");
161                     classes[i].addMethod(el);
162
163                     MethodElement nel = classes[i].getMethod(Identifier.create(symbol), types);
164                     if (nel != null) {
165                         // Show the source code
166
SourceCookie.Editor editor =
167                            (SourceCookie.Editor)dobj.getCookie(SourceCookie.Editor.class);
168                         javax.swing.text.Element JavaDoc textElement = editor.sourceToText(nel);
169                         if (textElement != null) {
170                             StyledDocument document = editor.getDocument();
171                             if (document != null) {
172                                 int offset = textElement.getStartOffset();
173                                 int lineNumber = NbDocument.findLineNumber(document, offset);
174                                 Line line = editor.getLineSet().getCurrent(lineNumber);
175                                 line.show(line.SHOW_GOTO);
176                             }
177                         }
178                     }
179
180                     
181                 } catch (SourceException e) {
182                     e.printStackTrace();
183                 }
184                 break; // Only for the first top level class
185
/// XXX That is not right!
186
}
187         }
188     }
189
190      public boolean hasConfirmation() {
191          return true;
192      }
193
194     /**
195      * @todo Instead of hardcoding the method text here, do the same
196      * as in perform() where we create a MethodElement, and then just
197      * do getRawText() or getBody() on the memory methodelement (don't
198      * actually add it to the ClassElement obviously.)
199      */

200      public Object JavaDoc getConfirmation(Suggestion s) {
201          String JavaDoc none =
202             NbBundle.getMessage(CreateMethodPerformer.class, "NoArgs");//NOI18N
203
if (args.equals(none)) {
204              args = "";
205          }
206          String JavaDoc beforeDesc =
207              NbBundle.getMessage(ErrorSuggester.class,
208                                  "CreateMethodDesc", location); // NOI18N
209
String JavaDoc afterDesc =
210              NbBundle.getMessage(ErrorSuggester.class,
211                                  "CreateMethodAfter"); // NOI18N
212
String JavaDoc beforeContents = "<html><body><b>" + (makePublic? "public " : "") + "void " + symbol + "</b>(" + args + ") {<br><i>&nbsp;&nbsp;&nbsp;&nbsp;/**@todo Implement this method*/</i><br><b>&nbsp;&nbsp;&nbsp;&nbsp;throw new </b>java.lang.<b>UnsupportedOperationException</b>(<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\"Method " + symbol + "("+args+") not yet implemented.\");<br>}</body></html>";
213          String JavaDoc filename =
214              dobj.getPrimaryFile().getNameExt();
215          return new ConfPanel(beforeDesc, beforeContents, afterDesc,
216                               null, filename, -1, null);
217      }
218
219     
220
221 }
222
223
Popular Tags