1 19 20 package org.netbeans.modules.editor; 21 22 import java.awt.event.InputEvent ; 23 import java.awt.event.KeyEvent ; 24 import java.util.List ; 25 import javax.swing.Action ; 26 import javax.swing.ActionMap ; 27 import javax.swing.Icon ; 28 import javax.swing.ImageIcon ; 29 import javax.swing.JCheckBoxMenuItem ; 30 import javax.swing.JEditorPane ; 31 import javax.swing.JMenuItem ; 32 import javax.swing.KeyStroke ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 import javax.swing.text.JTextComponent ; 36 import javax.swing.text.Keymap ; 37 import org.netbeans.api.editor.mimelookup.MimeLookup; 38 import org.netbeans.api.editor.mimelookup.MimePath; 39 import org.netbeans.api.editor.settings.KeyBindingSettings; 40 import org.netbeans.api.editor.settings.MultiKeyBinding; 41 import org.netbeans.editor.BaseKit; 42 import org.netbeans.editor.Registry; 43 import org.netbeans.editor.Settings; 44 import org.netbeans.editor.SettingsNames; 45 import org.netbeans.editor.Utilities; 46 import org.netbeans.editor.ext.ExtKit; 47 import org.netbeans.modules.editor.options.BaseOptions; 48 import org.openide.awt.Mnemonics; 49 import org.openide.util.HelpCtx; 50 import org.openide.util.Lookup; 51 import org.openide.util.NbBundle; 52 import org.openide.util.actions.Presenter; 53 54 62 public abstract class MainMenuAction extends GlobalContextAction implements Presenter.Menu, ChangeListener { 63 64 public static final Icon BLANK_ICON = new ImageIcon (org.openide.util.Utilities.loadImage("org/netbeans/modules/editor/resources/empty.gif")); 65 public boolean menuInitialized = false; 66 67 private final Icon forcedIcon; 68 69 private boolean forceIcon; 70 71 72 public MainMenuAction() { 73 this(true, null); 75 } 76 77 public MainMenuAction (boolean forceIcon, Icon forcedIcon) { 78 Registry.addChangeListener(this); 81 this.forceIcon = forceIcon; 82 this.forcedIcon = forcedIcon; 83 } 84 85 public void resultChanged(org.openide.util.LookupEvent ev){ 86 setMenu(); 87 } 88 89 public void stateChanged(ChangeEvent e) { 90 setMenu(); 91 } 92 93 public org.openide.util.HelpCtx getHelpCtx() { 94 return HelpCtx.DEFAULT_HELP; 95 } 96 97 public String getName() { 98 return getMenuItemText(); 99 } 100 101 102 private static JTextComponent getComponent(){ 103 return Utilities.getFocusedComponent(); 104 } 105 106 107 private static Action getActionByName(String actionName){ 108 BaseKit bKit = getKit(); 109 if (bKit!=null){ 110 Action action = bKit.getActionByName(actionName); 111 return action; 112 } 113 return null; 114 } 115 116 117 protected static void addAccelerators(Action a, JMenuItem item, JTextComponent target){ 118 if (target == null || a==null || item==null) return; 119 120 Action kitAction = getActionByName((String )a.getValue(Action.NAME)); 122 if (kitAction!=null) a = kitAction; 123 Keymap km = target.getKeymap(); 125 126 if (km != null) { 127 KeyStroke [] keys = km.getKeyStrokesForAction(a); 128 KeyStroke itemAccelerator = item.getAccelerator(); 129 130 if (keys != null && keys.length > 0) { 131 if (itemAccelerator==null || !itemAccelerator.equals(keys[0])){ 132 item.setAccelerator(keys[0]); 133 } 134 }else{ 135 if (itemAccelerator!=null && kitAction!=null){ 136 item.setAccelerator(null); 137 } 138 } 139 } 140 } 141 142 143 private static BaseKit getKit(){ 144 JTextComponent component = getComponent(); 145 return (component == null) ? null : Utilities.getKit(component); 146 } 147 148 public boolean isEnabled() { 149 return false; 150 } 151 152 private static Object getSettingValue(BaseKit kit, String settingName) { 153 return Settings.getValue(kit.getClass(), settingName); 154 } 155 156 159 private static boolean getSettingBoolean(BaseKit kit, String settingName) { 160 Boolean val = (Boolean )getSettingValue(kit, settingName); 161 return (val != null) ? val.booleanValue() : false; 162 } 163 164 166 protected Action getGlobalKitAction(){ 167 return null; 168 } 169 170 171 172 protected void setMenu(){ 173 ActionMap am = getContextActionMap(); 174 Action action = null; 175 JMenuItem presenter = getMenuPresenter(); 176 if (am!=null){ 177 action = am.get(getActionName()); 178 if (action == null){ 179 action = getGlobalKitAction(); 180 } 181 Action presenterAction = presenter.getAction(); 182 if (presenterAction == null){ 183 if (action != null){ 184 presenter.setAction(action); 185 presenter.setToolTipText(null); 186 menuInitialized = false; 187 } 188 }else{ 189 if ((action!=null && !action.equals(presenterAction))){ 190 presenter.setAction(action); 191 presenter.setToolTipText(null); 192 menuInitialized = false; 193 }else if (action == null){ 194 presenter.setEnabled(false); 195 } 196 } 197 } 198 199 if (!menuInitialized){ 200 Mnemonics.setLocalizedText(presenter, getMenuItemText()); 201 menuInitialized = true; 202 } 203 204 presenter.setEnabled(action != null); 205 JTextComponent comp = Utilities.getFocusedComponent(); 206 if (comp != null && comp instanceof JEditorPane ){ 207 addAccelerators(action, presenter, comp); 208 } else { 209 presenter.setAccelerator(getDefaultAccelerator()); 210 } 211 212 if (forceIcon) { 213 presenter.setIcon(forcedIcon); 214 } 215 } 216 217 218 protected abstract String getMenuItemText(); 219 220 221 protected abstract String getActionName(); 222 223 224 protected KeyStroke getDefaultAccelerator(){ 225 Lookup ml = MimeLookup.getLookup(MimePath.get("text/x-java")); KeyBindingSettings kbs = (KeyBindingSettings) ml.lookup(KeyBindingSettings.class); 227 if (kbs != null){ 228 List lst = kbs.getKeyBindings(); 229 if (lst != null){ 230 for (int i=0; i<lst.size(); i++){ 231 MultiKeyBinding mkb = (MultiKeyBinding)lst.get(i); 232 String an = mkb.getActionName(); 233 if (an != null && an.equals(getActionName())){ 234 if (mkb.getKeyStrokeCount() == 1){ return mkb.getKeyStroke(0); 236 } 237 } 238 } 239 } 240 } 241 return null; 242 } 243 244 public static class ShowToolBarAction extends MainMenuAction{ 245 246 private static JCheckBoxMenuItem SHOW_TOOLBAR_MENU; 247 248 public ShowToolBarAction(){ 249 super(false, null); 250 SHOW_TOOLBAR_MENU = new JCheckBoxMenuItem (getMenuItemText()); 251 setMenu(); 252 } 253 254 260 261 protected void setMenu(){ 262 super.setMenu(); 263 SHOW_TOOLBAR_MENU.setState(isToolbarVisible()); 264 } 265 266 public JMenuItem getMenuPresenter() { 267 return SHOW_TOOLBAR_MENU; 268 } 269 270 private static boolean isToolbarVisible(){ 271 BaseKit kit = getKit(); 272 if (kit==null) return false; 273 return getSettingBoolean(kit, BaseOptions.TOOLBAR_VISIBLE_PROP); 274 } 275 276 protected String getMenuItemText(){ 277 return NbBundle.getBundle(MainMenuAction.class).getString( 278 "show_editor_toolbar_main_menu_view_item"); } 280 281 protected String getActionName() { 282 return ExtKit.toggleToolbarAction; 283 } 284 285 } 286 287 288 public static class ShowLineNumbersAction extends MainMenuAction{ 289 290 private JCheckBoxMenuItem SHOW_LINE_MENU; 291 292 public ShowLineNumbersAction(){ 293 super(false, null); 294 SHOW_LINE_MENU = new JCheckBoxMenuItem (getMenuItemText()); 295 setMenu(); 296 } 297 298 protected void setMenu(){ 299 super.setMenu(); 300 SHOW_LINE_MENU.setState(isLineNumbersVisible()); 301 } 302 303 protected String getMenuItemText(){ 304 return NbBundle.getBundle(MainMenuAction.class).getString( 305 "show_line_numbers_main_menu_view_item"); } 307 308 public String getName() { 309 return getMenuItemText(); 310 } 311 312 public javax.swing.JMenuItem getMenuPresenter() { 313 return SHOW_LINE_MENU; 314 } 315 316 private static boolean isLineNumbersVisible(){ 317 BaseKit kit = getKit(); 318 if (kit==null) return false; 319 return getSettingBoolean(kit, SettingsNames.LINE_NUMBER_VISIBLE); 320 } 321 322 protected String getActionName() { 323 return ExtKit.toggleLineNumbersAction; 324 } 325 326 } 327 328 329 public static class GoToSourceAction extends MainMenuAction{ 330 331 private JMenuItem GOTO_SOURCE_MENU; 332 333 public GoToSourceAction(){ 334 super(); 335 GOTO_SOURCE_MENU = new JMenuItem (getMenuItemText()); 336 setMenu(); 337 } 338 339 protected String getMenuItemText(){ 340 return NbBundle.getBundle(MainMenuAction.class).getString( 341 "goto_source_main_menu_edit_item"); } 343 344 public JMenuItem getMenuPresenter() { 345 return GOTO_SOURCE_MENU; 346 } 347 348 protected String getActionName() { 349 return ExtKit.gotoSourceAction; 350 } 351 352 protected KeyStroke getDefaultAccelerator(){ 353 return KeyStroke.getKeyStroke(KeyEvent.VK_O, InputEvent.ALT_MASK); 354 } 355 356 } 357 358 359 public static class GoToSuperAction extends MainMenuAction{ 360 361 private JMenuItem GOTO_SUPER_MENU; 362 363 public GoToSuperAction(){ 364 super(); 365 GOTO_SUPER_MENU = new JMenuItem (getMenuItemText()); 366 setMenu(); 367 } 368 369 protected String getMenuItemText(){ 370 return NbBundle.getBundle(MainMenuAction.class).getString( 371 "goto_super_implementation_main_menu_edit_item"); } 373 374 public JMenuItem getMenuPresenter() { 375 return GOTO_SUPER_MENU; 376 } 377 378 protected String getActionName() { 379 return ExtKit.gotoSuperImplementationAction; 380 } 381 382 protected KeyStroke getDefaultAccelerator(){ 383 return KeyStroke.getKeyStroke(KeyEvent.VK_B, InputEvent.CTRL_MASK); 384 } 385 386 } 387 388 public static class GoToDeclarationAction extends MainMenuAction{ 389 390 private JMenuItem GOTO_DECL_MENU; 391 392 public GoToDeclarationAction(){ 393 super(); 394 GOTO_DECL_MENU = new JMenuItem (getMenuItemText()); 395 setMenu(); 396 } 397 398 protected String getMenuItemText(){ 399 return NbBundle.getBundle(MainMenuAction.class).getString( 400 "goto_declaration_main_menu_edit_item"); } 402 403 public JMenuItem getMenuPresenter() { 404 return GOTO_DECL_MENU; 405 } 406 407 protected String getActionName() { 408 return ExtKit.gotoDeclarationAction; 409 } 410 411 protected KeyStroke getDefaultAccelerator(){ 412 return KeyStroke.getKeyStroke(KeyEvent.VK_G, InputEvent.ALT_MASK); 413 } 414 415 } 416 417 419 public static final class JumpBackAction extends MainMenuAction { 420 421 private JMenuItem jumpBackMenuItem; 422 423 public JumpBackAction () { 424 super(); 425 jumpBackMenuItem = new JMenuItem (getMenuItemText()); 426 setMenu(); 427 } 428 429 protected String getMenuItemText () { 430 return NbBundle.getBundle(JumpBackAction.class).getString( 431 "jump_back_main_menu_item"); } 433 434 public JMenuItem getMenuPresenter () { 435 return jumpBackMenuItem; 436 } 437 438 protected String getActionName () { 439 return BaseKit.jumpListPrevAction; 440 } 441 442 protected KeyStroke getDefaultAccelerator () { 443 return KeyStroke.getKeyStroke(KeyEvent.VK_K, InputEvent.ALT_MASK); 444 } 445 446 } 448 450 public static final class JumpForwardAction extends MainMenuAction { 451 452 private JMenuItem jumpForwardMenuItem; 453 454 public JumpForwardAction () { 455 super(); 456 jumpForwardMenuItem = new JMenuItem (getMenuItemText()); 457 setMenu(); 458 } 459 460 protected String getMenuItemText () { 461 return NbBundle.getBundle(JumpForwardAction.class).getString( 462 "jump_forward_main_menu_item"); } 464 465 public JMenuItem getMenuPresenter () { 466 return jumpForwardMenuItem; 467 } 468 469 protected String getActionName () { 470 return BaseKit.jumpListNextAction; 471 } 472 473 protected KeyStroke getDefaultAccelerator () { 474 return KeyStroke.getKeyStroke(KeyEvent.VK_L, InputEvent.ALT_MASK); 475 } 476 477 } 479 481 public static final class FormatAction extends MainMenuAction { 482 483 private JMenuItem formatMenuItem; 484 485 public FormatAction () { 486 super(); 487 formatMenuItem = new JMenuItem (getMenuItemText()); 488 setMenu(); 489 } 490 491 protected String getMenuItemText () { 492 return NbBundle.getBundle(FormatAction.class).getString( 493 "format_main_menu_item"); } 495 496 public JMenuItem getMenuPresenter () { 497 return formatMenuItem; 498 } 499 500 protected String getActionName () { 501 return BaseKit.formatAction; 502 } 503 504 } 506 508 public static final class ShiftLineLeftAction extends MainMenuAction { 509 510 private JMenuItem menuItem; 511 512 public ShiftLineLeftAction () { 513 super(); 514 menuItem = new JMenuItem (getMenuItemText()); 515 setMenu(); 516 } 517 518 protected String getMenuItemText () { 519 return NbBundle.getBundle(ShiftLineLeftAction.class).getString( 520 "shift_line_left_main_menu_item"); } 522 523 public JMenuItem getMenuPresenter () { 524 return menuItem; 525 } 526 527 protected String getActionName () { 528 return BaseKit.shiftLineLeftAction; 529 } 530 531 } 533 535 public static final class ShiftLineRightAction extends MainMenuAction { 536 537 private JMenuItem menuItem; 538 539 public ShiftLineRightAction () { 540 super(); 541 menuItem = new JMenuItem (getMenuItemText()); 542 setMenu(); 543 } 544 545 protected String getMenuItemText () { 546 return NbBundle.getBundle(ShiftLineRightAction.class).getString( 547 "shift_line_right_main_menu_item"); } 549 550 public JMenuItem getMenuPresenter () { 551 return menuItem; 552 } 553 554 protected String getActionName () { 555 return BaseKit.shiftLineRightAction; 556 } 557 558 } 560 562 public static final class CommentAction extends MainMenuAction { 563 564 private JMenuItem menuItem; 565 566 public CommentAction () { 567 super(); 568 menuItem = new JMenuItem (getMenuItemText()); 569 setMenu(); 570 } 571 572 protected String getMenuItemText () { 573 return NbBundle.getBundle(CommentAction.class).getString( 574 "comment_main_menu_item"); } 576 577 public JMenuItem getMenuPresenter () { 578 return menuItem; 579 } 580 581 protected String getActionName () { 582 return ExtKit.commentAction; 583 } 584 585 } 587 589 public static final class UncommentAction extends MainMenuAction { 590 591 private JMenuItem menuItem; 592 593 public UncommentAction () { 594 super(); 595 menuItem = new JMenuItem (getMenuItemText()); 596 setMenu(); 597 } 598 599 protected String getMenuItemText () { 600 return NbBundle.getBundle(UncommentAction.class).getString( 601 "uncomment_main_menu_item"); } 603 604 public JMenuItem getMenuPresenter () { 605 return menuItem; 606 } 607 608 protected String getActionName () { 609 return ExtKit.uncommentAction; 610 } 611 612 } 614 616 public static final class WordMatchNextAction extends MainMenuAction { 617 618 private JMenuItem menuItem; 619 620 public WordMatchNextAction () { 621 super(); 622 menuItem = new JMenuItem (getMenuItemText()); 623 setMenu(); 624 } 625 626 protected String getMenuItemText () { 627 return NbBundle.getBundle(WordMatchNextAction.class).getString( 628 "word_match_next_main_menu_item"); } 630 631 public JMenuItem getMenuPresenter () { 632 return menuItem; 633 } 634 635 protected String getActionName () { 636 return BaseKit.wordMatchNextAction; 637 } 638 639 } 641 643 public static final class WordMatchPrevAction extends MainMenuAction { 644 645 private JMenuItem menuItem; 646 647 public WordMatchPrevAction () { 648 super(); 649 menuItem = new JMenuItem (getMenuItemText()); 650 setMenu(); 651 } 652 653 protected String getMenuItemText () { 654 return NbBundle.getBundle(WordMatchPrevAction.class).getString( 655 "word_match_previous_main_menu_item"); } 657 658 public JMenuItem getMenuPresenter () { 659 return menuItem; 660 } 661 662 protected String getActionName () { 663 return BaseKit.wordMatchPrevAction; 664 } 665 666 } 668 670 public static final class FindNextAction extends MainMenuAction { 671 672 private JMenuItem menuItem; 673 674 public FindNextAction () { 675 super(true, BLANK_ICON); 676 menuItem = new JMenuItem (getMenuItemText()); 677 setMenu(); 678 } 679 680 protected String getMenuItemText () { 681 return NbBundle.getBundle(FindNextAction.class).getString( 682 "find_next_main_menu_item"); } 684 685 public JMenuItem getMenuPresenter () { 686 return menuItem; 687 } 688 689 protected String getActionName () { 690 return BaseKit.findNextAction; 691 } 692 693 } 695 696 698 public static final class FindPreviousAction extends MainMenuAction { 699 700 private JMenuItem menuItem; 701 702 public FindPreviousAction () { 703 super(true, BLANK_ICON); 704 menuItem = new JMenuItem (getMenuItemText()); 705 setMenu(); 706 } 707 708 protected String getMenuItemText () { 709 return NbBundle.getBundle(FindNextAction.class).getString( 710 "find_previous_main_menu_item"); } 712 713 public JMenuItem getMenuPresenter () { 714 return menuItem; 715 } 716 717 protected String getActionName () { 718 return BaseKit.findPreviousAction; 719 } 720 721 } 723 725 public static final class FindSelectionAction extends MainMenuAction { 726 727 private JMenuItem menuItem; 728 729 public FindSelectionAction () { 730 super(true, BLANK_ICON); 731 menuItem = new JMenuItem (getMenuItemText()); 732 setMenu(); 733 } 734 735 protected String getMenuItemText () { 736 return NbBundle.getBundle(FindNextAction.class).getString( 737 "find_selection_main_menu_item"); } 739 740 public JMenuItem getMenuPresenter () { 741 return menuItem; 742 } 743 744 protected String getActionName () { 745 return BaseKit.findSelectionAction; 746 } 747 748 } 750 752 public static final class StartMacroRecordingAction extends MainMenuAction { 753 754 private JMenuItem menuItem; 755 756 public StartMacroRecordingAction () { 757 super(true, BLANK_ICON); 758 menuItem = new JMenuItem (getMenuItemText()); 759 setMenu(); 760 } 761 762 protected String getMenuItemText () { 763 return NbBundle.getBundle(StartMacroRecordingAction.class).getString( 764 "start_macro_recording_main_menu_item"); } 766 767 public JMenuItem getMenuPresenter () { 768 return menuItem; 769 } 770 771 protected String getActionName () { 772 return BaseKit.startMacroRecordingAction; 773 } 774 775 } 777 779 public static final class StopMacroRecordingAction extends MainMenuAction { 780 781 private JMenuItem menuItem; 782 783 public StopMacroRecordingAction () { 784 super(true, BLANK_ICON); 785 menuItem = new JMenuItem (getMenuItemText()); 786 setMenu(); 787 } 788 789 protected String getMenuItemText () { 790 return NbBundle.getBundle(StopMacroRecordingAction.class).getString( 791 "stop_macro_recording_main_menu_item"); } 793 794 public JMenuItem getMenuPresenter () { 795 return menuItem; 796 } 797 798 protected String getActionName () { 799 return BaseKit.stopMacroRecordingAction; 800 } 801 802 } 804 806 public static final class RemoveTrailingSpacesAction extends MainMenuAction { 807 808 private JMenuItem menuItem; 809 810 public RemoveTrailingSpacesAction() { 811 super(true, null); 812 menuItem = new JMenuItem (getMenuItemText()); 813 setMenu(); 814 } 815 816 protected String getMenuItemText () { 817 return NbBundle.getBundle(RemoveTrailingSpacesAction.class).getString( 818 "remove_trailing_spaces_main_menu_item"); } 820 821 public JMenuItem getMenuPresenter () { 822 return menuItem; 823 } 824 825 protected String getActionName () { 826 return BaseKit.removeTrailingSpacesAction; 827 } 828 829 } 831 833 public static final class PasteFormattedAction extends MainMenuAction { 834 835 private JMenuItem menuItem; 836 837 public PasteFormattedAction() { 838 super(true, BLANK_ICON); 839 menuItem = new JMenuItem (getMenuItemText()); 840 setMenu(); 841 } 842 843 protected String getMenuItemText () { 844 return NbBundle.getBundle(PasteFormattedAction.class).getString( 845 "paste_formatted_main_menu_item"); } 847 848 public JMenuItem getMenuPresenter () { 849 return menuItem; 850 } 851 852 protected String getActionName () { 853 return BaseKit.pasteFormatedAction; 854 } 855 856 } 858 } 859 | Popular Tags |