1 19 20 package org.netbeans.modules.editor.java; 21 22 import java.awt.event.ActionEvent ; 23 import java.util.ArrayList ; 24 import java.util.List ; 25 import javax.swing.*; 26 import javax.swing.text.*; 27 import javax.swing.text.BadLocationException ; 28 import org.netbeans.editor.*; 29 import org.netbeans.editor.Utilities; 30 import org.netbeans.editor.ext.*; 31 import org.netbeans.editor.ext.java.*; 32 import org.netbeans.api.editor.fold.FoldHierarchy; 33 import org.netbeans.api.editor.fold.FoldUtilities; 34 import org.netbeans.api.java.queries.SourceLevelQuery; 35 import org.netbeans.editor.ext.ExtKit.CommentAction; 36 import org.netbeans.editor.ext.ExtKit.PrefixMakerAction; 37 import org.netbeans.editor.ext.ExtKit.UncommentAction; 38 import org.netbeans.lib.editor.codetemplates.api.CodeTemplateManager; 39 import org.netbeans.modules.editor.NbEditorKit; 40 import org.netbeans.modules.editor.NbEditorUtilities; 41 import org.netbeans.modules.java.editor.codegen.GenerateCodeAction; 42 import org.netbeans.modules.java.editor.imports.FastImportAction; 43 import org.netbeans.modules.java.editor.imports.JavaFixAllImports; 44 import org.netbeans.modules.java.editor.overridden.GoToSuperTypeAction; 45 import org.netbeans.modules.java.editor.rename.InstantRenameAction; 46 import org.openide.filesystems.FileObject; 47 import org.openide.loaders.DataObject; 48 import org.openide.awt.Mnemonics; 49 import org.openide.util.*; 50 51 57 58 public class JavaKit extends NbEditorKit { 59 60 public static final String JAVA_MIME_TYPE = "text/x-java"; 62 private static final String [] getSetIsPrefixes = new String [] { 63 "get", "set", "is" }; 65 66 69 public static final String makeGetterAction = "make-getter"; 71 74 public static final String makeSetterAction = "make-setter"; 76 79 public static final String makeIsAction = "make-is"; 81 82 public static final String addWatchAction = "add-watch"; 84 85 public static final String toggleBreakpointAction = "toggle-breakpoint"; 87 88 public static final String abbrevDebugLineAction = "abbrev-debug-line"; 90 91 public static final String fixImportsAction = "fix-imports"; 93 94 public static final String fastImportAction = "fast-import"; 96 97 99 public static final String tryCatchAction = "try-catch"; 101 public static final String javaDocShowAction = "javadoc-show-action"; 103 public static final String expandAllJavadocFolds = "expand-all-javadoc-folds"; 105 public static final String collapseAllJavadocFolds = "collapse-all-javadoc-folds"; 107 public static final String expandAllCodeBlockFolds = "expand-all-code-block-folds"; 109 public static final String collapseAllCodeBlockFolds = "collapse-all-code-block-folds"; 111 public static final String selectNextElementAction = "select-element-next"; 113 public static final String selectPreviousElementAction = "select-element-previous"; 115 static final long serialVersionUID =-5445829962533684922L; 116 117 118 public JavaKit(){ 119 org.netbeans.modules.java.editor.JavaEditorModule.init(); 120 } 121 122 public String getContentType() { 123 return JAVA_MIME_TYPE; 124 } 125 126 130 public Syntax createSyntax(Document doc) { 131 return new JavaSyntax(getSourceLevel((BaseDocument)doc)); 132 } 133 134 public Completion createCompletion(ExtEditorUI extEditorUI) { 135 return null; 136 } 137 138 public CompletionJavaDoc createCompletionJavaDoc(ExtEditorUI extEditorUI) { 139 return null; 140 } 141 142 @Override 143 public Document createDefaultDocument() { 144 Document doc = new JavaDocument(this.getClass()); 145 Object mimeType = doc.getProperty("mimeType"); if (mimeType == null){ 147 doc.putProperty("mimeType", getContentType()); } 149 return doc; 150 } 151 152 public String getSourceLevel(BaseDocument doc) { 153 DataObject dob = NbEditorUtilities.getDataObject(doc); 154 return dob != null ? SourceLevelQuery.getSourceLevel(dob.getPrimaryFile()) : null; 155 } 156 157 158 public Formatter createFormatter() { 159 return new JavaFormatter(this.getClass()); 160 } 161 162 protected void initDocument(BaseDocument doc) { 163 doc.addDocumentListener(new JavaDrawLayerFactory.LParenWatcher()); 166 doc.putProperty(SyntaxUpdateTokens.class, 167 new SyntaxUpdateTokens() { 168 169 private List tokenList = new ArrayList (); 170 171 public void syntaxUpdateStart() { 172 tokenList.clear(); 173 } 174 175 public List syntaxUpdateEnd() { 176 return tokenList; 177 } 178 179 public void syntaxUpdateToken(TokenID id, TokenContextPath contextPath, int offset, int length) { 180 if (JavaTokenContext.LINE_COMMENT == id) { 181 tokenList.add(new TokenInfo(id, contextPath, offset, length)); 182 } 183 } 184 } 185 ); 186 187 CodeTemplateManager.get(doc); 189 } 190 191 protected Action[] createActions() { 192 Action[] javaActions = new Action[] { 193 new JavaDefaultKeyTypedAction(), 194 new PrefixMakerAction(makeGetterAction, "get", getSetIsPrefixes), new PrefixMakerAction(makeSetterAction, "set", getSetIsPrefixes), new PrefixMakerAction(makeIsAction, "is", getSetIsPrefixes), new AbbrevDebugLineAction(), 198 new CommentAction("//"), new UncommentAction("//"), new JavaGenerateGoToPopupAction(), 201 new JavaInsertBreakAction(), 202 new JavaDeleteCharAction(deletePrevCharAction, false), 203 new ExpandAllJavadocFolds(), 204 new CollapseAllJavadocFolds(), 205 new ExpandAllCodeBlockFolds(), 206 new CollapseAllCodeBlockFolds(), 207 new JavaGenerateFoldPopupAction(), 208 new JavaGoToDeclarationAction(), 209 new JavaGoToSourceAction(), 210 new InstantRenameAction(), 211 new JavaFixImports(), 212 new GenerateCodeAction(), 213 new SelectCodeElementAction(selectNextElementAction, true), 214 new SelectCodeElementAction(selectPreviousElementAction, false), 215 new FastImportAction(), 216 new GoToSuperTypeAction(), 217 }; 218 219 return TextAction.augmentList(super.createActions(), javaActions); 220 } 221 222 public static class JavaDefaultKeyTypedAction extends ExtDefaultKeyTypedAction { 223 224 protected void insertString(BaseDocument doc, int dotPos, 225 Caret caret, String str, 226 boolean overwrite) throws BadLocationException { 227 char insertedChar = str.charAt(0); 228 if (insertedChar == '\"' || insertedChar == '\''){ 229 boolean inserted = BracketCompletion.completeQuote(doc, dotPos, caret, insertedChar); 230 if (inserted){ 231 caret.setDot(dotPos+1); 232 }else{ 233 super.insertString(doc, dotPos, caret, str, overwrite); 234 235 } 236 } else { 237 super.insertString(doc, dotPos, caret, str, overwrite); 238 BracketCompletion.charInserted(doc, dotPos, caret, insertedChar); 239 } 240 } 241 242 protected void replaceSelection(JTextComponent target, 243 int dotPos, 244 Caret caret, 245 String str, 246 boolean overwrite) 247 throws BadLocationException { 248 char insertedChar = str.charAt(0); 249 Document doc = target.getDocument(); 250 if (insertedChar == '\"' || insertedChar == '\''){ 251 if (doc != null) { 252 try { 253 boolean inserted = false; 254 int p0 = Math.min(caret.getDot(), caret.getMark()); 255 int p1 = Math.max(caret.getDot(), caret.getMark()); 256 if (p0 != p1) { 257 doc.remove(p0, p1 - p0); 258 } 259 int caretPosition = caret.getDot(); 260 if (doc instanceof BaseDocument){ 261 inserted = BracketCompletion.completeQuote( 262 (BaseDocument)doc, 263 caretPosition, 264 caret, insertedChar); 265 } 266 if (inserted){ 267 caret.setDot(caretPosition+1); 268 } else { 269 if (str != null && str.length() > 0) { 270 doc.insertString(p0, str, null); 271 } 272 } 273 } catch (BadLocationException e) { 274 e.printStackTrace(); 275 } 276 } 277 } else { 278 super.replaceSelection(target, dotPos, caret, str, overwrite); 279 if (doc instanceof BaseDocument){ 280 BracketCompletion.charInserted((BaseDocument)doc, caret.getDot()-1, caret, insertedChar); 281 } 282 } 283 } 284 } 285 286 287 public static class JavaGenerateGoToPopupAction extends NbGenerateGoToPopupAction { 288 289 public void actionPerformed(ActionEvent evt, JTextComponent target) { 290 } 291 292 private void addAcceleretors(Action a, JMenuItem item, JTextComponent target){ 293 Keymap km = target.getKeymap(); 295 if (km != null) { 296 297 KeyStroke[] keys = km.getKeyStrokesForAction(a); 298 if (keys != null && keys.length > 0) { 299 item.setAccelerator(keys[0]); 300 }else if (a!=null){ 301 KeyStroke ks = (KeyStroke)a.getValue(Action.ACCELERATOR_KEY); 302 if (ks!=null) { 303 item.setAccelerator(ks); 304 } 305 } 306 } 307 } 308 309 private void addAction(JTextComponent target, JMenu menu, Action a){ 310 if (a != null) { 311 String actionName = (String ) a.getValue(Action.NAME); 312 JMenuItem item = null; 313 if (a instanceof BaseAction) { 314 item = ((BaseAction)a).getPopupMenuItem(target); 315 } 316 if (item == null) { 317 String itemText = (String )a.getValue(ExtKit.TRIMMED_TEXT); 319 if (itemText == null){ 320 itemText = getItemText(target, actionName, a); 321 } 322 if (itemText != null) { 323 item = new JMenuItem(itemText); 324 Mnemonics.setLocalizedText(item, itemText); 325 item.addActionListener(a); 326 addAcceleretors(a, item, target); 327 item.setEnabled(a.isEnabled()); 328 Object helpID = a.getValue ("helpID"); if (helpID != null && (helpID instanceof String )) 330 item.putClientProperty ("HelpID", helpID); }else{ 332 if (ExtKit.gotoSourceAction.equals(actionName)){ 333 item = new JMenuItem(NbBundle.getBundle(JavaKit.class).getString("goto_source_open_source_not_formatted")); addAcceleretors(a, item, target); 335 item.setEnabled(false); 336 } 337 } 338 } 339 340 if (item != null) { 341 menu.add(item); 342 } 343 344 } 345 } 346 347 protected void addAction(JTextComponent target, JMenu menu, 348 String actionName) { 349 BaseKit kit = Utilities.getKit(target); 350 if (kit == null) return; 351 Action a = kit.getActionByName(actionName); 352 if (a!=null){ 353 addAction(target, menu, a); 354 } else { menu.addSeparator(); 356 } 357 } 358 359 protected String getItemText(JTextComponent target, String actionName, Action a) { 360 String itemText; 361 if (a instanceof BaseAction) { 362 itemText = ((BaseAction)a).getPopupMenuText(target); 363 } else { 364 itemText = actionName; 365 } 366 return itemText; 367 } 368 369 public JMenuItem getPopupMenuItem(final JTextComponent target) { 370 String menuText = NbBundle.getBundle(JavaKit.class).getString("generate-goto-popup"); JMenu jm = new JMenu(menuText); 372 addAction(target, jm, ExtKit.gotoSourceAction); 373 addAction(target, jm, ExtKit.gotoDeclarationAction); 374 addAction(target, jm, gotoSuperImplementationAction); 375 addAction(target, jm, ExtKit.gotoAction); 376 return jm; 377 } 378 379 } 380 381 382 public static class AbbrevDebugLineAction extends BaseAction { 383 384 public AbbrevDebugLineAction() { 385 super(abbrevDebugLineAction); 386 } 387 388 public void actionPerformed(ActionEvent evt, JTextComponent target) { 389 if (target != null) { 390 if (!target.isEditable() || !target.isEnabled()) { 391 target.getToolkit().beep(); 392 return; 393 } 394 BaseDocument doc = (BaseDocument)target.getDocument(); 395 StringBuffer sb = new StringBuffer ("System.out.println(\""); String title = (String )doc.getProperty(Document.TitleProperty); 397 if (title != null) { 398 sb.append(title); 399 sb.append(':'); 400 } 401 try { 402 sb.append(Utilities.getLineOffset(doc, target.getCaret().getDot()) + 1); 403 } catch (BadLocationException e) { 404 } 405 sb.append(' '); 406 407 BaseKit kit = Utilities.getKit(target); 408 if (kit == null) return; 409 Action a = kit.getActionByName(BaseKit.insertContentAction); 410 if (a != null) { 411 Utilities.performAction( 412 a, 413 new ActionEvent (target, ActionEvent.ACTION_PERFORMED, sb.toString()), 414 target 415 ); 416 } 417 } 418 } 419 420 } 421 422 423 public static class JavaInsertBreakAction extends InsertBreakAction { 424 425 static final long serialVersionUID = -1506173310438326380L; 426 427 protected Object beforeBreak(JTextComponent target, BaseDocument doc, Caret caret) { 428 int dotPos = caret.getDot(); 429 if (BracketCompletion.posWithinString(doc, dotPos)) { 430 try { 431 doc.insertString(dotPos, "\" + \"", null); dotPos += 3; 433 caret.setDot(dotPos); 434 return new Integer (dotPos); 435 } catch (BadLocationException ex) { 436 } 437 } else { 438 try { 439 if (BracketCompletion.isAddRightBrace(doc, dotPos)) { 440 int end = BracketCompletion.getRowOrBlockEnd(doc, dotPos); 441 doc.insertString(end, "}", null); doc.getFormatter().indentNewLine(doc, end); 443 caret.setDot(dotPos); 444 return Boolean.TRUE; 445 } 446 } catch (BadLocationException ex) { 447 } 448 } 449 return null; 450 } 451 452 protected void afterBreak(JTextComponent target, BaseDocument doc, Caret caret, Object cookie) { 453 if (cookie != null) { 454 if (cookie instanceof Integer ) { 455 int nowDotPos = caret.getDot(); 457 caret.setDot(nowDotPos+1); 458 } 459 } 460 } 461 462 } 463 464 465 public static class JavaDeleteCharAction extends ExtDeleteCharAction { 466 467 public JavaDeleteCharAction(String nm, boolean nextChar) { 468 super(nm, nextChar); 469 } 470 471 protected void charBackspaced(BaseDocument doc, int dotPos, Caret caret, char ch) 472 throws BadLocationException { 473 BracketCompletion.charBackspaced(doc, dotPos, caret, ch); 474 } 475 } 476 477 public static class ExpandAllJavadocFolds extends BaseAction{ 478 public ExpandAllJavadocFolds(){ 479 super(expandAllJavadocFolds); 480 putValue(SHORT_DESCRIPTION, NbBundle.getBundle(JavaKit.class).getString("expand-all-javadoc-folds")); 481 putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getBundle(JavaKit.class).getString("popup-expand-all-javadoc-folds")); 482 } 483 484 public void actionPerformed(ActionEvent evt, JTextComponent target) { 485 FoldHierarchy hierarchy = FoldHierarchy.get(target); 486 FoldUtilities.expand(hierarchy, JavaFoldManager.JAVADOC_FOLD_TYPE); 488 } 489 } 490 491 public static class CollapseAllJavadocFolds extends BaseAction{ 492 public CollapseAllJavadocFolds(){ 493 super(collapseAllJavadocFolds); 494 putValue(SHORT_DESCRIPTION, NbBundle.getBundle(JavaKit.class).getString("collapse-all-javadoc-folds")); 495 putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getBundle(JavaKit.class).getString("popup-collapse-all-javadoc-folds")); 496 } 497 498 public void actionPerformed(ActionEvent evt, JTextComponent target) { 499 FoldHierarchy hierarchy = FoldHierarchy.get(target); 500 FoldUtilities.collapse(hierarchy, JavaFoldManager.JAVADOC_FOLD_TYPE); 502 } 503 } 504 505 public static class ExpandAllCodeBlockFolds extends BaseAction{ 506 public ExpandAllCodeBlockFolds(){ 507 super(expandAllCodeBlockFolds); 508 putValue(SHORT_DESCRIPTION, NbBundle.getBundle(JavaKit.class).getString("expand-all-code-block-folds")); 509 putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getBundle(JavaKit.class).getString("popup-expand-all-code-block-folds")); 510 } 511 512 public void actionPerformed(ActionEvent evt, JTextComponent target) { 513 FoldHierarchy hierarchy = FoldHierarchy.get(target); 514 List types = new ArrayList (); 516 types.add(JavaFoldManager.CODE_BLOCK_FOLD_TYPE); 517 types.add(JavaFoldManager.IMPORTS_FOLD_TYPE); 518 FoldUtilities.expand(hierarchy, types); 519 } 520 } 521 522 public static class CollapseAllCodeBlockFolds extends BaseAction{ 523 public CollapseAllCodeBlockFolds(){ 524 super(collapseAllCodeBlockFolds); 525 putValue(SHORT_DESCRIPTION, NbBundle.getBundle(JavaKit.class).getString("collapse-all-code-block-folds")); 526 putValue(BaseAction.POPUP_MENU_TEXT, NbBundle.getBundle(JavaKit.class).getString("popup-collapse-all-code-block-folds")); 527 } 528 529 public void actionPerformed(ActionEvent evt, JTextComponent target) { 530 FoldHierarchy hierarchy = FoldHierarchy.get(target); 531 List types = new ArrayList (); 533 types.add(JavaFoldManager.CODE_BLOCK_FOLD_TYPE); 534 types.add(JavaFoldManager.IMPORTS_FOLD_TYPE); 535 FoldUtilities.collapse(hierarchy, types); 536 } 537 } 538 539 public static class JavaGenerateFoldPopupAction extends GenerateFoldPopupAction{ 540 541 protected void addAdditionalItems(JTextComponent target, JMenu menu){ 542 addAction(target, menu, collapseAllJavadocFolds); 543 addAction(target, menu, expandAllJavadocFolds); 544 setAddSeparatorBeforeNextAction(true); 545 addAction(target, menu, collapseAllCodeBlockFolds); 546 addAction(target, menu, expandAllCodeBlockFolds); 547 } 548 549 } 550 551 private static class JavaGoToDeclarationAction extends GotoDeclarationAction { 552 public @Override boolean gotoDeclaration(JTextComponent target) { 553 GoToSupport.goTo((BaseDocument) target.getDocument(), target.getCaretPosition(), false); 554 return true; 555 } 556 } 557 558 private static class JavaGoToSourceAction extends BaseAction { 559 560 static final long serialVersionUID =-6440495023918097760L; 561 562 public JavaGoToSourceAction() { 563 super(gotoSourceAction, 564 ABBREV_RESET | MAGIC_POSITION_RESET | UNDO_MERGE_RESET 565 | SAVE_POSITION 566 ); 567 putValue(TRIMMED_TEXT, LocaleSupport.getString("goto-source-trimmed")); } 569 570 public void actionPerformed(ActionEvent evt, JTextComponent target) { 571 if (target != null) { 572 GoToSupport.goTo((BaseDocument) target.getDocument(), target.getCaretPosition(), true); 573 } 574 } 575 576 public String getPopupMenuText(JTextComponent target) { 577 return NbBundle.getBundle(JavaKit.class).getString("goto_source_open_source_not_formatted"); } 579 580 protected Class getShortDescriptionBundleClass() { 581 return BaseKit.class; 582 } 583 } 584 585 private static class JavaFixImports extends BaseAction { 586 587 public JavaFixImports() { 588 super(fixImportsAction, 589 ABBREV_RESET | MAGIC_POSITION_RESET | UNDO_MERGE_RESET 590 | SAVE_POSITION 591 ); 592 putValue(TRIMMED_TEXT, NbBundle.getBundle(JavaKit.class).getString("fix-imports-trimmed")); 593 putValue(SHORT_DESCRIPTION, NbBundle.getBundle(JavaKit.class).getString("desc-fix-imports")); putValue(POPUP_MENU_TEXT, NbBundle.getBundle(JavaKit.class).getString("popup-fix-imports")); } 596 597 public void actionPerformed(ActionEvent evt, JTextComponent target) { 598 if (target != null) { 599 Document doc = target.getDocument(); 600 FileObject fo = ((DataObject) doc.getProperty(Document.StreamDescriptionProperty)).getPrimaryFile(); 601 602 JavaFixAllImports.getDefault().fixAllImports(fo); 603 } 604 } 605 } 606 607 } 608 | Popular Tags |