1 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 ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Collections ; 31 import java.util.Comparator ; 32 import java.util.Iterator ; 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 ; 55 import java.lang.reflect.Modifier ; 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 70 class CreateMethodPerformer implements SuggestionPerformer { 71 72 private DataObject dobj; 73 private Document doc; 74 private String symbol; 75 private String location; 76 private String args; 77 private boolean makePublic; 78 79 CreateMethodPerformer(DataObject dobj, 80 String symbol, String location, 81 String 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 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 none = 105 NbBundle.getMessage(CreateMethodPerformer.class, "NoArgs"); for (int i = 0; i < classes.length; i++) { 107 if (classes[i].isInner()) { 108 } 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 123 126 MethodParameter[] params; 129 ArrayList plist = new ArrayList (); 130 int begin = 0; 131 while (true) { 132 int next = args.indexOf(',', begin); 133 String 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; String 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 SourceCookie.Editor editor = 167 (SourceCookie.Editor)dobj.getCookie(SourceCookie.Editor.class); 168 javax.swing.text.Element 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; } 187 } 188 } 189 190 public boolean hasConfirmation() { 191 return true; 192 } 193 194 200 public Object getConfirmation(Suggestion s) { 201 String none = 202 NbBundle.getMessage(CreateMethodPerformer.class, "NoArgs"); if (args.equals(none)) { 204 args = ""; 205 } 206 String beforeDesc = 207 NbBundle.getMessage(ErrorSuggester.class, 208 "CreateMethodDesc", location); String afterDesc = 210 NbBundle.getMessage(ErrorSuggester.class, 211 "CreateMethodAfter"); String beforeContents = "<html><body><b>" + (makePublic? "public " : "") + "void " + symbol + "</b>(" + args + ") {<br><i> /**@todo Implement this method*/</i><br><b> throw new </b>java.lang.<b>UnsupportedOperationException</b>(<br> \"Method " + symbol + "("+args+") not yet implemented.\");<br>}</body></html>"; 213 String 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 |