1 19 20 package org.netbeans.modules.editor; 21 22 import java.awt.Component ; 23 import java.awt.event.ActionEvent ; 24 import java.awt.event.ItemEvent ; 25 import java.awt.event.ItemListener ; 26 import java.awt.event.KeyEvent ; 27 import java.util.Arrays ; 28 import java.util.Collections ; 29 import java.util.HashMap ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 import java.util.Map ; 33 import java.util.MissingResourceException ; 34 import java.util.ResourceBundle ; 35 import javax.swing.JMenu ; 36 import javax.swing.JMenuItem ; 37 import javax.swing.JPopupMenu ; 38 import javax.swing.Action ; 39 import javax.swing.JCheckBoxMenuItem ; 40 import javax.swing.KeyStroke ; 41 import javax.swing.text.Document ; 42 import javax.swing.text.JTextComponent ; 43 import javax.swing.text.TextAction ; 44 import javax.swing.text.Keymap ; 45 import org.netbeans.editor.ActionFactory; 46 import org.netbeans.editor.EditorUI; 47 import org.netbeans.editor.ext.ExtKit; 48 import org.openide.awt.DynamicMenuContent; 49 import org.openide.loaders.DataFolder; 50 import org.openide.util.actions.SystemAction; 51 import org.openide.util.actions.Presenter; 52 import org.openide.actions.UndoAction; 53 import org.openide.actions.RedoAction; 54 import org.openide.windows.TopComponent; 55 import org.netbeans.editor.BaseKit; 56 import org.netbeans.editor.Utilities; 57 import org.netbeans.editor.BaseAction; 58 import org.netbeans.editor.MacroDialogSupport; 59 import org.netbeans.editor.Settings; 60 import org.netbeans.editor.SettingsNames; 61 import org.netbeans.editor.ext.ExtSettingsNames; 62 import org.netbeans.modules.editor.impl.ActionsList; 63 import org.netbeans.modules.editor.impl.PopupMenuActionsProvider; 64 import org.netbeans.modules.editor.options.BaseOptions; 65 import org.netbeans.modules.editor.options.OptionUtilities; 66 import org.netbeans.modules.editor.options.AllOptionsFolder; 67 import org.netbeans.modules.editor.options.MacrosEditorPanel; 68 import org.openide.ErrorManager; 69 import org.openide.NotifyDescriptor; 70 import org.openide.awt.Mnemonics; 71 import org.openide.filesystems.FileObject; 72 import org.openide.filesystems.FileStateInvalidException; 73 import org.openide.util.ContextAwareAction; 74 import org.openide.util.Lookup; 75 import org.openide.util.NbBundle; 76 77 83 84 public class NbEditorKit extends ExtKit { 85 86 87 public static final String SYSTEM_ACTION_CLASS_NAME_PROPERTY = "systemActionClassName"; 89 static final long serialVersionUID =4482122073483644089L; 90 91 private static final Map contentTypeTable; 92 93 94 public static final String generateGoToPopupAction = "generate-goto-popup"; 96 97 public static final String generateFoldPopupAction = "generate-fold-popup"; 99 private static final NbUndoAction nbUndoActionDef = new NbUndoAction(); 100 private static final NbRedoAction nbRedoActionDef = new NbRedoAction(); 101 102 private Map systemAction2editorAction = new HashMap (); 103 104 static { 105 contentTypeTable = new HashMap (); 106 contentTypeTable.put("org.netbeans.modules.properties.syntax.PropertiesKit", "text/x-properties"); contentTypeTable.put("org.netbeans.modules.web.core.syntax.JSPKit", "text/x-jsp"); contentTypeTable.put("org.netbeans.modules.css.text.syntax.CSSEditorKit", "text/css"); contentTypeTable.put("org.netbeans.modules.xml.css.editor.CSSEditorKit", "text/css"); contentTypeTable.put("org.netbeans.modules.xml.text.syntax.DTDKit", "text/x-dtd"); contentTypeTable.put("org.netbeans.modules.xml.text.syntax.XMLKit", "text/xml"); contentTypeTable.put("org.netbeans.modules.corba.idl.editor.coloring.IDLKit", "text/x-idl"); } 114 115 public NbEditorKit() { 116 super(); 117 NbEditorSettingsInitializer.init(); 119 } 120 121 public Document createDefaultDocument() { 122 Document doc = new NbEditorDocument(this.getClass()); 123 Object mimeType = doc.getProperty("mimeType"); if (mimeType == null){ 125 doc.putProperty("mimeType", getContentType()); } 127 return doc; 128 } 129 130 138 protected void toolTipAnnotationsLock(Document doc) { 139 } 140 141 146 protected void toolTipAnnotationsUnlock(Document doc) { 147 } 148 149 protected EditorUI createEditorUI() { 150 return new NbEditorUI(); 151 } 152 153 protected Action [] createActions() { 154 Action [] nbEditorActions = new Action [] { 155 new NbBuildPopupMenuAction(), 156 new NbStopMacroRecordingAction(), 157 nbUndoActionDef, 158 nbRedoActionDef, 159 new NbBuildToolTipAction(), 160 new NbToggleLineNumbersAction(), 161 new ToggleToolbarAction(), 162 new NbGenerateGoToPopupAction(), 163 new GenerateFoldPopupAction() 164 }; 165 return TextAction.augmentList(super.createActions(), nbEditorActions); 166 } 167 168 169 protected void addSystemActionMapping(String editorActionName, Class systemActionClass) { 170 Action a = getActionByName(editorActionName); 171 if (a != null) { 172 a.putValue(SYSTEM_ACTION_CLASS_NAME_PROPERTY, systemActionClass.getName()); 173 } 174 systemAction2editorAction.put(systemActionClass.getName(), editorActionName); 175 } 176 177 protected void updateActions() { 178 addSystemActionMapping(cutAction, org.openide.actions.CutAction.class); 179 addSystemActionMapping(copyAction, org.openide.actions.CopyAction.class); 180 addSystemActionMapping(pasteAction, org.openide.actions.PasteAction.class); 181 addSystemActionMapping(deleteNextCharAction, org.openide.actions.DeleteAction.class); 183 addSystemActionMapping(showPopupMenuAction, org.openide.actions.PopupAction.class); 184 185 addSystemActionMapping(findAction, org.openide.actions.FindAction.class); 186 addSystemActionMapping(replaceAction, org.openide.actions.ReplaceAction.class); 187 addSystemActionMapping(gotoAction, org.openide.actions.GotoAction.class); 188 189 addSystemActionMapping(undoAction, org.openide.actions.UndoAction.class); 190 addSystemActionMapping(redoAction, org.openide.actions.RedoAction.class); 191 } 192 193 private boolean isInheritorOfNbEditorKit(){ 194 Class clz = this.getClass(); 195 while(clz.getSuperclass() != null){ 196 clz = clz.getSuperclass(); 197 if (NbEditorKit.class == clz) return true; 198 } 199 return false; 200 } 201 202 public String getContentType() { 203 if (isInheritorOfNbEditorKit()){ 204 ErrorManager.getDefault().log(ErrorManager.WARNING, 205 "Warning: KitClass "+this.getClass().getName()+" doesn't override the method getContentType."); } 207 return (contentTypeTable.containsKey(this.getClass().getName())) ? 208 (String )contentTypeTable.get(this.getClass().getName()) : "text/"+this.getClass().getName().replace('.','_'); } 210 211 private static ResourceBundle getBundleFromName (String name) { 212 ResourceBundle bundle = null; 213 if (name != null) { 214 try { 215 bundle = NbBundle.getBundle (name); 216 } catch (MissingResourceException mre) { 217 } 219 } 220 return bundle; 221 } 222 223 224 public static class ToggleToolbarAction extends BaseAction { 225 226 public ToggleToolbarAction() { 227 super(ExtKit.toggleToolbarAction); 228 putValue ("helpID", ToggleToolbarAction.class.getName ()); } 230 231 public void actionPerformed(ActionEvent evt, JTextComponent target) { 232 boolean toolbarVisible = AllOptionsFolder.getDefault().isToolbarVisible(); 233 AllOptionsFolder.getDefault().setToolbarVisible(!toolbarVisible); 234 } 235 236 public JMenuItem getPopupMenuItem(JTextComponent target) { 237 JCheckBoxMenuItem item = new JCheckBoxMenuItem ( 238 NbBundle.getBundle(BaseOptions.class).getString("PROP_base_toolbarVisible"), 239 AllOptionsFolder.getDefault().isToolbarVisible()); 240 item.addItemListener( new ItemListener () { 241 public void itemStateChanged(ItemEvent e) { 242 actionPerformed(null,null); 243 } 244 }); 245 return item; 246 } 247 248 protected Class getShortDescriptionBundleClass() { 249 return BaseKit.class; 250 } 251 252 } 253 254 255 public class NbBuildPopupMenuAction extends BuildPopupMenuAction { 256 257 static final long serialVersionUID =-8623762627678464181L; 258 259 protected JPopupMenu createPopupMenu(JTextComponent component) { 260 return new org.openide.awt.JPopupMenuPlus(); 263 } 264 265 protected JPopupMenu buildPopupMenu(JTextComponent component) { 266 EditorUI ui = Utilities.getEditorUI(component); 267 if (!ui.hasExtComponent()) { 268 return null; 269 } 270 271 JPopupMenu pm = createPopupMenu(component); 272 273 String mimeType = NbEditorUtilities.getMimeType(component); 274 List l = PopupMenuActionsProvider.getPopupMenuItems(mimeType); 275 276 if (l.isEmpty()){ 277 l = (List )Settings.getValue(Utilities.getKitClass(component), 278 (ui == null || ui.hasExtComponent()) 279 ? ExtSettingsNames.POPUP_MENU_ACTION_NAME_LIST 280 : ExtSettingsNames.DIALOG_POPUP_MENU_ACTION_NAME_LIST 281 ); 282 } 283 284 if (l != null) { 285 for (Iterator i = l.iterator(); i.hasNext(); ) { 286 Object obj = i.next(); 287 288 if (obj == null || obj instanceof javax.swing.JSeparator ) { 289 addAction(component, pm, (String )null); 290 } else if (obj instanceof String ) { 291 addAction(component, pm, (String )obj); 292 } else if (obj instanceof Action ) { 293 addAction(component, pm, (Action )obj); 294 } else if (obj instanceof DataFolder) { 295 pm.add(new LayerSubFolderMenu(component, ((DataFolder) obj).getPrimaryFile())); 296 } 297 } 298 } 299 300 return pm; 301 } 302 303 private Lookup getContextLookup(java.awt.Component component){ 304 Lookup lookup = null; 305 for (java.awt.Component c = component; c != null; c = c.getParent()) { 306 if (c instanceof Lookup.Provider) { 307 lookup = ((Lookup.Provider)c).getLookup (); 308 if (lookup != null) { 309 break; 310 } 311 } 312 } 313 return lookup; 314 } 315 316 private Action translateContextLookupAction(Lookup contextLookup, Action action) { 317 if (action instanceof ContextAwareAction && contextLookup != null){ 318 action = ((org.openide.util.ContextAwareAction)action) 319 .createContextAwareInstance(contextLookup); 320 } 321 return action; 322 } 323 324 private JMenuItem createLocalizedMenuItem(Action action) { 325 JMenuItem item; 326 if (action instanceof Presenter.Popup) { 327 item = ((Presenter.Popup)action).getPopupPresenter(); 328 } else { 329 item = new JMenuItem (action); 330 Mnemonics.setLocalizedText(item, item.getText()); 331 if (item.getIcon() != null) item.setIcon(null); } 333 return item; 334 } 335 336 private void assignAccelerator(Keymap km, Action action, JMenuItem item) { 337 if (item.getAccelerator() == null){ 338 KeyStroke ks = (KeyStroke )action.getValue(Action.ACCELERATOR_KEY); 339 if (ks!=null) { 340 item.setAccelerator(ks); 341 } else { 342 if (km != null) { 344 KeyStroke [] keys = km.getKeyStrokesForAction(action); 345 if (keys != null && keys.length > 0) { 346 item.setAccelerator(keys[0]); 347 } 348 } 349 } 350 } 351 } 352 353 protected void addAction(JTextComponent component, JPopupMenu popupMenu, Action action) { 354 Lookup contextLookup = getContextLookup(component); 355 356 if (contextLookup == null && 358 systemAction2editorAction.containsKey(action.getClass().getName())){ 359 addAction(component, popupMenu, (String ) systemAction2editorAction.get(action.getClass().getName())); 360 return; 361 } 362 363 action = translateContextLookupAction(contextLookup, action); 364 365 if (action != null) { 366 JMenuItem item = createLocalizedMenuItem(action); 367 if (item instanceof DynamicMenuContent) { 368 Component [] cmps = ((DynamicMenuContent)item).getMenuPresenters(); 369 for (int i = 0; i < cmps.length; i++) { 370 popupMenu.add(cmps[i]); 371 } 372 } else { 373 item.setEnabled(action.isEnabled()); 374 Object helpID = action.getValue ("helpID"); if (helpID != null && (helpID instanceof String )) { 376 item.putClientProperty ("HelpID", helpID); } 378 assignAccelerator(component.getKeymap(), action, item); 379 debugPopupMenuItem(item, action); 380 popupMenu.add(item); 381 } 382 } 383 } 384 385 private void addTopComponentActions(JTextComponent component, JPopupMenu popupMenu) { 386 Lookup contextLookup = getContextLookup(component); 387 TopComponent tc = NbEditorUtilities.getOuterTopComponent(component); 389 if (tc != null) { 390 Action [] actions = tc.getActions(); 392 Component [] comps = org.openide.util.Utilities.actionsToPopup(actions, contextLookup).getComponents(); 393 for (int i = 0; i < comps.length; i++) { 394 popupMenu.add(comps[i]); 395 } 396 } 397 } 398 399 protected void addAction(JTextComponent component, JPopupMenu popupMenu, 400 String actionName) { 401 if (actionName != null) { if (TopComponent.class.getName().equals(actionName)) { 404 addTopComponentActions(component, popupMenu); 405 return; 406 407 } else { 409 Class saClass = null; 411 try { 412 ClassLoader loader = (ClassLoader )Lookup.getDefault().lookup(ClassLoader .class); 413 saClass = Class.forName(actionName, false, loader); 414 } catch (Throwable t) { 415 } 416 417 if (saClass != null && SystemAction.class.isAssignableFrom(saClass)) { 418 Lookup contextLookup = getContextLookup(component); 419 Action action = SystemAction.get(saClass); 420 action = translateContextLookupAction(contextLookup, action); 421 422 JMenuItem item = createLocalizedMenuItem(action); 423 if (item != null) { 424 if (item instanceof DynamicMenuContent) { 425 Component [] cmps = ((DynamicMenuContent)item).getMenuPresenters(); 426 for (int i = 0; i < cmps.length; i++) { 427 popupMenu.add(cmps[i]); 428 } 429 } else { 430 if (!(item instanceof JMenu )) { 431 assignAccelerator( 432 (Keymap )Lookup.getDefault().lookup(Keymap .class), 433 action, 434 item 435 ); 436 } 437 debugPopupMenuItem(item, action); 438 popupMenu.add(item); 439 } 440 } 441 442 return; 443 } 444 } 445 446 } 447 448 super.addAction(component, popupMenu, actionName); 449 450 } 451 452 453 } 454 455 public class NbStopMacroRecordingAction extends ActionFactory.StopMacroRecordingAction { 456 457 private BaseOptions bo; 458 459 private Map getKBMap(){ 460 Map ret; 461 List list = bo.getKeyBindingList(); 462 if( list.size() > 0 && 463 ( list.get( 0 ) instanceof Class || list.get( 0 ) instanceof String ) 464 ) { 465 list.remove( 0 ); } 467 ret = OptionUtilities.makeKeyBindingsMap(list); 468 return ret; 469 } 470 471 protected MacroDialogSupport getMacroDialogSupport(Class kitClass){ 472 return new NbMacroDialogSupport(kitClass); 473 } 474 475 476 private class NbMacroDialogSupport extends MacroDialogSupport{ 477 478 public NbMacroDialogSupport( Class kitClass ) { 479 super(kitClass); 480 } 481 482 public void actionPerformed(ActionEvent evt) { 483 bo = BaseOptions.getOptions(NbEditorKit.this.getClass()); 484 Map oldMacroMap = null; 485 Map oldKBMap = null; 486 if (bo != null){ 487 oldMacroMap = bo.getMacroMap(); 488 oldKBMap = getKBMap(); 489 } 490 491 super.actionPerformed(evt); 492 493 if (bo != null){ 494 Map newMacroMap = bo.getMacroMap(); 495 bo.setMacroDiffMap(OptionUtilities.getMapDiff(oldMacroMap, newMacroMap, true)); 496 bo.setKeyBindingsDiffMap(OptionUtilities.getMapDiff(oldKBMap, getKBMap(), true)); 497 bo.setMacroMap(newMacroMap,false); 498 bo.setKeyBindingList(bo.getKeyBindingList(), false); 499 } 500 } 501 502 protected int showConfirmDialog(String macroName){ 503 NotifyDescriptor confirm = new NotifyDescriptor.Confirmation( 504 NbBundle.getMessage(MacrosEditorPanel.class,"MEP_Overwrite", macroName), 505 NotifyDescriptor.YES_NO_CANCEL_OPTION, 506 NotifyDescriptor.WARNING_MESSAGE 507 ); 508 org.openide.DialogDisplayer.getDefault().notify(confirm); 509 return ((Integer )confirm.getValue()).intValue(); 510 } 511 512 } 513 514 } 515 516 public static class NbUndoAction extends ActionFactory.UndoAction { 517 518 public void actionPerformed(ActionEvent evt, JTextComponent target) { 519 UndoAction ua = (UndoAction)SystemAction.get(UndoAction.class); 521 if (ua != null && ua.isEnabled()) { 522 ua.actionPerformed(evt); 523 } 524 } 525 526 } 527 528 public static class NbRedoAction extends ActionFactory.RedoAction { 529 530 public void actionPerformed(ActionEvent evt, JTextComponent target) { 531 RedoAction ra = (RedoAction)SystemAction.get(RedoAction.class); 533 if (ra != null && ra.isEnabled()) { 534 ra.actionPerformed(evt); 535 } 536 } 537 538 } 539 540 541 public class NbToggleLineNumbersAction extends ActionFactory.ToggleLineNumbersAction { 542 543 544 public NbToggleLineNumbersAction() { 545 } 546 547 protected boolean isLineNumbersVisible() { 548 return AllOptionsFolder.getDefault().getLineNumberVisible(); 549 } 550 551 protected void toggleLineNumbers() { 552 boolean numbersVisible = AllOptionsFolder.getDefault().getLineNumberVisible(); 553 AllOptionsFolder.getDefault().setLineNumberVisible(!numbersVisible); 554 } 555 556 public void actionPerformed(ActionEvent evt, JTextComponent target) { 557 toggleLineNumbers(); 558 } 559 560 561 } 562 563 public static class NbGenerateGoToPopupAction extends BaseAction { 564 565 public NbGenerateGoToPopupAction() { 566 super(generateGoToPopupAction); 567 putValue(BaseAction.NO_KEYBINDING, Boolean.TRUE); 568 } 569 570 public void actionPerformed(ActionEvent evt, JTextComponent target) { 571 } 572 573 protected Class getShortDescriptionBundleClass() { 574 return NbEditorKit.class; 575 } 576 577 } 578 579 580 public static class NbBuildToolTipAction extends BuildToolTipAction { 581 582 public void actionPerformed(ActionEvent evt, JTextComponent target) { 583 if (target != null) { 584 NbToolTip.buildToolTip(target); 585 } 586 } 587 588 } 589 590 public static class GenerateFoldPopupAction extends BaseAction { 591 592 private boolean addSeparatorBeforeNextAction; 593 594 public GenerateFoldPopupAction() { 595 super(generateFoldPopupAction); 596 putValue(BaseAction.NO_KEYBINDING, Boolean.TRUE); 597 } 598 599 protected Class getShortDescriptionBundleClass() { 600 return NbEditorKit.class; 601 } 602 603 public void actionPerformed(ActionEvent evt, JTextComponent target) { 604 } 605 606 private void addAcceleretors(Action a, JMenuItem item, JTextComponent target){ 607 Keymap km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() : 609 target.getKeymap(); 610 if (km != null) { 611 KeyStroke [] keys = km.getKeyStrokesForAction(a); 612 if (keys != null && keys.length > 0) { 613 boolean added = false; 614 for (int i = 0; i<keys.length; i++){ 615 if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) || 616 keys[i].getKeyCode() == KeyEvent.VK_ADD){ 617 item.setAccelerator(keys[i]); 618 added = true; 619 break; 620 } 621 } 622 if (added == false) item.setAccelerator(keys[0]); 623 } 624 } 625 } 626 627 protected String getItemText(JTextComponent target, String actionName, Action a) { 628 String itemText; 629 if (a instanceof BaseAction) { 630 itemText = ((BaseAction)a).getPopupMenuText(target); 631 } else { 632 itemText = actionName; 633 } 634 return itemText; 635 } 636 637 638 protected void addAction(JTextComponent target, JMenu menu, 639 String actionName) { 640 if (addSeparatorBeforeNextAction) { 641 addSeparatorBeforeNextAction = false; 642 menu.addSeparator(); 643 } 644 645 BaseKit kit = (target == null) ? BaseKit.getKit(BaseKit.class) : Utilities.getKit(target); 646 if (!(kit instanceof BaseKit)) { kit = BaseKit.getKit(BaseKit.class); 648 target = null; 649 } 650 if (kit == null) return; 651 boolean foldingEnabled = (target == null) ? false : 652 ((Boolean )Settings.getValue(Utilities.getKitClass(target), SettingsNames.CODE_FOLDING_ENABLE)).booleanValue(); 653 Action a = kit.getActionByName(actionName); 654 if (a != null) { 655 JMenuItem item = null; 656 if (a instanceof BaseAction) { 657 item = ((BaseAction)a).getPopupMenuItem(target); 658 } 659 if (item == null) { 660 String itemText = getItemText(target, actionName, a); 661 if (itemText != null) { 662 item = new JMenuItem (itemText); 663 item.addActionListener(a); 664 Mnemonics.setLocalizedText(item, itemText); 665 addAcceleretors(a, item, target); 666 item.setEnabled(a.isEnabled() && foldingEnabled); 667 Object helpID = a.getValue ("helpID"); if (helpID != null && (helpID instanceof String )) 669 item.putClientProperty ("HelpID", helpID); } 671 } 672 673 if (item != null) { 674 menu.add(item); 675 } 676 677 } else { menu.addSeparator(); 679 } 680 } 681 682 protected void setAddSeparatorBeforeNextAction(boolean addSeparator) { 683 this.addSeparatorBeforeNextAction = addSeparator; 684 } 685 686 protected void addAdditionalItems(JTextComponent target, JMenu menu){ 687 setAddSeparatorBeforeNextAction(false); 688 } 689 690 public JMenuItem getPopupMenuItem(JTextComponent target) { 691 String menuText = org.openide.util.NbBundle.getBundle (NbEditorKit.class). 692 getString("Menu/View/CodeFolds"); 693 JMenu menu = new JMenu (menuText); 694 Mnemonics.setLocalizedText(menu, menuText); 695 setAddSeparatorBeforeNextAction(false); 696 addAction(target, menu, BaseKit.collapseFoldAction); 697 addAction(target, menu, BaseKit.expandFoldAction); 698 setAddSeparatorBeforeNextAction(true); 699 addAction(target, menu, BaseKit.collapseAllFoldsAction); 700 addAction(target, menu, BaseKit.expandAllFoldsAction); 701 setAddSeparatorBeforeNextAction(true); 703 if (target != null) addAdditionalItems(target, menu); 704 return menu; 705 } 706 707 } 708 709 710 private static final class LayerSubFolderMenu extends JMenu { 711 712 private static String getLocalizedName(FileObject f) { 713 try { 714 return f.getFileSystem().getStatus().annotateName( 715 f.getNameExt(), 716 Collections.singleton(f)); 717 } catch (FileStateInvalidException e) { 718 return f.getNameExt(); 719 } 720 } 721 722 public LayerSubFolderMenu(JTextComponent target, FileObject folder) { 723 this(target, getLocalizedName(folder), ActionsList.convert(Arrays.asList(folder.getChildren()))); 724 } 725 726 private LayerSubFolderMenu(JTextComponent target, String text, List items) { 727 super(); 728 Mnemonics.setLocalizedText(this, text); 729 730 for (Iterator i = items.iterator(); i.hasNext(); ) { 731 Object obj = i.next(); 732 733 if (obj == null || obj instanceof javax.swing.JSeparator ) { 734 addSeparator(); 735 } else if (obj instanceof String ) { 736 addAction(target, this, (String )obj); 737 } else if (obj instanceof Action ) { 738 addAction(target, this, (Action )obj); 739 } else if (obj instanceof DataFolder) { 740 this.add(new LayerSubFolderMenu(target, ((DataFolder) obj).getPrimaryFile())); 741 } 742 } 743 } 744 745 private static void addAcceleretors(Action a, JMenuItem item, JTextComponent target) { 746 Keymap km = (target == null) ? BaseKit.getKit(BaseKit.class).getKeymap() : 748 target.getKeymap(); 749 if (km != null) { 750 KeyStroke [] keys = km.getKeyStrokesForAction(a); 751 if (keys != null && keys.length > 0) { 752 boolean added = false; 753 for (int i = 0; i<keys.length; i++){ 754 if ((keys[i].getKeyCode() == KeyEvent.VK_MULTIPLY) || 755 keys[i].getKeyCode() == KeyEvent.VK_ADD){ 756 item.setAccelerator(keys[i]); 757 added = true; 758 break; 759 } 760 } 761 if (added == false) { 762 item.setAccelerator(keys[0]); 763 } 764 }else if (a!=null){ 765 KeyStroke ks = (KeyStroke )a.getValue(Action.ACCELERATOR_KEY); 766 if (ks!=null) { 767 item.setAccelerator(ks); 768 } 769 } 770 } 771 } 772 773 private static String getItemText(JTextComponent target, String actionName, Action a) { 774 String itemText; 775 if (a instanceof BaseAction) { 776 itemText = ((BaseAction)a).getPopupMenuText(target); 777 } else { 778 Object value = a.getValue(BaseAction.POPUP_MENU_TEXT); 779 itemText = (value instanceof String ) ? (String )value : actionName; 780 } 781 return itemText; 782 } 783 784 private static void addAction(JTextComponent target, JMenu menu, String actionName) { 785 assert target != null : "The parameter target must not be null"; assert menu != null : "The parameter menu must not be null"; assert actionName != null : "The parameter actionName must not be null"; 789 BaseKit kit = Utilities.getKit(target); 790 if (kit == null) return; 791 Action a = kit.getActionByName(actionName); 792 if (a != null) { 793 addAction(target, menu, a); 794 } 795 } 796 797 798 private static void addAction(JTextComponent target, JMenu menu, Action action) { 799 assert target != null : "The parameter target must not be null"; assert menu != null : "The parameter menu must not be null"; assert action != null : "The parameter action must not be null"; 803 JMenuItem item = null; 804 if (action instanceof BaseAction) { 805 item = ((BaseAction)action).getPopupMenuItem(target); 806 } 807 808 if (item == null) { 809 String actionName = (String ) action.getValue(Action.NAME); 810 String itemText = getItemText(target, actionName, action); 811 if (itemText != null) { 812 item = new JMenuItem (itemText); 813 item.addActionListener(action); 814 Mnemonics.setLocalizedText(item, itemText); 815 addAcceleretors(action, item, target); 816 item.setEnabled(action.isEnabled()); 817 Object helpID = action.getValue ("helpID"); if (helpID != null && (helpID instanceof String )) { 819 item.putClientProperty ("HelpID", helpID); } 821 } 822 } 823 824 if (item != null) { 825 menu.add(item); 826 } 827 } 828 } 829 } 830 | Popular Tags |