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 39 40 41 import org.openide.src.Identifier; 42 import org.openide.src.Import; 43 import org.openide.src.SourceElement; 44 import org.openide.src.SourceException; 45 import org.openide.util.NbBundle; 46 import org.openide.util.Utilities; 47 48 import org.openide.cookies.LineCookie; 49 import org.openide.loaders.DataObject; 50 import org.openide.text.Line; 51 import org.openide.ErrorManager; 52 53 import java.util.TreeSet ; 54 import java.lang.reflect.Modifier ; 55 import org.netbeans.editor.ext.java.*; 56 import org.netbeans.modules.editor.java.*; 57 58 import org.netbeans.modules.tasklist.core.ConfPanel; 59 import org.netbeans.modules.tasklist.core.TLUtils; 60 import org.netbeans.modules.tasklist.client.Suggestion; 61 import org.netbeans.modules.tasklist.client.SuggestionPerformer; 62 63 64 69 class ReplaceSymbolPerformer implements SuggestionPerformer { 70 71 private int lineno; 72 private int col; 73 private Line line; 74 private DataObject dobj; 75 private Document doc; 76 private String oldSymbol; 77 private String newSymbol; 78 private String beforeDesc; 79 private String afterDesc; 80 private JCClass importClass; 81 private boolean makeMethod; 82 83 ReplaceSymbolPerformer(int lineno, int col, Line line, DataObject dobj, 84 Document doc, 85 String oldSymbol, String newSymbol, 86 String beforeDesc, String afterDesc, 87 JCClass importClass, boolean makeMethod) { 88 this.lineno = lineno; 89 this.col = col; 90 this.line = line; 91 this.dobj = dobj; 92 this.doc = doc; 93 this.oldSymbol = oldSymbol; 94 this.newSymbol = newSymbol; 95 this.beforeDesc = beforeDesc; 96 this.afterDesc = afterDesc; 97 this.importClass = importClass; 98 this.makeMethod = false; 99 100 } 101 102 public void perform(Suggestion s) { 104 if (makeMethod) { 105 makeMethod(); 106 } else { 107 replaceSymbol(); 108 if (importClass != null) { 109 ImportPerformer.importClass(doc, importClass); 110 } 111 } 112 } 113 114 public boolean hasConfirmation() { 115 return true; 116 } 117 118 public Object getConfirmation(Suggestion s) { 119 String filename = 120 dobj.getPrimaryFile().getNameExt(); 121 StringBuffer sb = new StringBuffer (200); 122 Line l = line; 123 String text = l.getText(); 124 125 int fd = TLUtils.firstDiff(oldSymbol, newSymbol); 127 int ldo = oldSymbol.length()- TLUtils.lastDiff(oldSymbol, newSymbol); 128 int ldn = newSymbol.length() - TLUtils.lastDiff(oldSymbol, newSymbol); 129 sb.append("<html>"); TLUtils.appendSurroundingLine(sb, l, -1); 131 JPUtils.replaceSymbol(sb, text, col, oldSymbol, oldSymbol, 132 true, fd, ldo); 133 TLUtils.appendSurroundingLine(sb, l, +1); 134 sb.append("</html>"); String beforeContents = sb.toString(); 136 137 sb.setLength(0); 138 sb.append("<html>"); 139 if (importClass != null) { 140 sb.append("<b>import "); sb.append(importClass.toString()); 142 sb.append(";</b><br>...<br>"); } 144 TLUtils.appendSurroundingLine(sb, l, -1); 145 JPUtils.replaceSymbol(sb, text, col, oldSymbol, newSymbol, 146 true, fd, ldn); 147 TLUtils.appendSurroundingLine(sb, l, +1); 148 sb.append("</html>"); String afterContents = sb.toString(); 150 return new ConfPanel(beforeDesc, beforeContents, afterDesc, 151 afterContents, filename, lineno, null); 152 } 153 154 155 boolean replaceSymbol() { 156 if (!(doc instanceof StyledDocument)) { 159 return false; 160 } 161 162 lineno--; 164 165 StyledDocument sdoc = (StyledDocument)doc; 166 Element e = sdoc.getParagraphElement(0).getParentElement(); 167 if (e == null) { 168 e = sdoc.getDefaultRootElement(); 170 } 171 Element elm = e.getElement(lineno); 172 if (elm == null) { 173 return false; 174 } 175 int offset = elm.getStartOffset(); 176 int endOffset = elm.getEndOffset(); 177 178 try { 179 String text = sdoc.getText(offset, endOffset-offset); 180 182 int symLen = oldSymbol.length(); 188 int texLen = text.length(); 189 int n = texLen-symLen; 190 while (true) { 191 if (n < 0) { 192 break; 193 } 194 195 if (text.startsWith(oldSymbol, n)) { 196 if ((n > 0) && 198 Character.isJavaIdentifierPart(text.charAt(n-1))) { 199 n--; 200 continue; 201 } 202 if ((n+symLen+1 < texLen-1) && 203 Character.isJavaIdentifierPart(text.charAt(n+symLen))) { 204 n--; continue; 207 } 208 209 int pos = offset + n; 210 sdoc.remove(pos, symLen); 211 sdoc.insertString(pos, newSymbol, null); 212 } 214 n--; 215 } 216 } catch (BadLocationException ex) { 217 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 218 } 219 return false; 220 } 221 222 223 233 boolean makeMethod() { 234 if (!(doc instanceof StyledDocument)) { 238 return false; 239 } 240 241 lineno--; 243 244 StyledDocument sdoc = (StyledDocument)doc; 245 246 Element e = sdoc.getParagraphElement(0).getParentElement(); 247 if (e == null) { 248 e = sdoc.getDefaultRootElement(); 250 } 251 Element elm = e.getElement(lineno); 252 if (elm == null) { 253 return false; 254 } 255 int offset = elm.getStartOffset(); 256 int endOffset = elm.getEndOffset(); 257 258 try { 259 String text = sdoc.getText(offset, endOffset-offset); 260 col--; 262 if (text.substring(col).startsWith(oldSymbol)) { 263 int pos = offset + col + oldSymbol.length(); 264 sdoc.insertString(pos, "()", null); 265 } else if (text.substring(col+1).startsWith(oldSymbol)) { 266 int pos = offset + col+1 + oldSymbol.length(); 269 sdoc.insertString(pos, "()", null); 270 } else { 271 int x = text.indexOf(oldSymbol, col+2); int nx = x+oldSymbol.length(); 278 if ((x+oldSymbol.length() < text.length()-1) || 279 !Character.isJavaIdentifierPart(text.charAt(nx))) { 280 int pos = offset + nx; 281 sdoc.insertString(pos, "()", null); 282 } 283 } 284 } catch (BadLocationException ex) { 285 ErrorManager.getDefault().notify(ErrorManager.WARNING, ex); 286 } 287 return false; 288 } 289 290 291 292 } 293 294 | Popular Tags |