Java Code Examples for javax.swing.JMenuItem#getAction()

The following examples show how to use javax.swing.JMenuItem#getAction() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PluginsManager.java    From audiveris with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void menuSelected (MenuEvent e)
{
    boolean enabled = StubsController.getCurrentStub() != null;

    for (int i = 0; i < menu.getItemCount(); i++) {
        JMenuItem item = menu.getItem(i);

        // Beware of separators (for which returned menuItem is null)
        if (item != null) {
            item.setEnabled(enabled);

            // Indicate which plugin is the default (if any)
            Action action = item.getAction();

            if (action instanceof PluginAction) {
                Plugin plugin = ((PluginAction) action).getPlugin();
                item.setText(
                        plugin.getId() + ((plugin == defaultPlugin) ? " (default)" : ""));
            }
        }
    }
}
 
Example 2
Source File: CompletionActionsMainMenu.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/** Sets the state of JMenuItem*/
protected @Override void setMenu(){
    
    ActionMap am = getContextActionMap();
    Action action = null;
    if (am != null) {
        action = am.get(getActionName());
    }
    
    JMenuItem presenter = getMenuPresenter();
    Action presenterAction = presenter.getAction();
    if (presenterAction == null){
        presenter.setAction(this);
        presenter.setToolTipText(null); /* bugfix #62872 */ 
        menuInitialized = false;
    } 
    else {
        if (!this.equals(presenterAction)){
            presenter.setAction(this);
            presenter.setToolTipText(null); /* bugfix #62872 */
            menuInitialized = false;
        }
    }

    if (!menuInitialized){
        Mnemonics.setLocalizedText(presenter, getMenuItemText());
        menuInitialized = true;
    }

    presenter.setEnabled(action != null);
    JTextComponent comp = Utilities.getFocusedComponent();
    if (comp != null && comp instanceof JEditorPane){
        addAccelerators(this, presenter, comp);
    } else {
        presenter.setAccelerator(getDefaultAccelerator());
    }

}
 
Example 3
Source File: DynamicMenuEnabler.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void menuSelected(MenuEvent event) {
    JMenu menu = (JMenu) event.getSource();
    for (Component component : menu.getMenuComponents()) {
        if (component instanceof JMenuItem) {
            JMenuItem item   = (JMenuItem) component;
            Action    action = item.getAction();
            if (action instanceof Command) {
                ((Command) action).adjust();
            }
        }
    }
}
 
Example 4
Source File: Command.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public static void adjustMenuTree(JPopupMenu menu) {
    for (Component component : menu.getComponents()) {
        if (component instanceof JMenu) {
            adjustMenuTree((JMenu) component);
        } else if (component instanceof JMenuItem) {
            JMenuItem item   = (JMenuItem) component;
            Action    action = item.getAction();
            if (action instanceof Command) {
                ((Command) action).adjust();
            }
        }
    }
}
 
Example 5
Source File: Command.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
public static void adjustMenuTree(JMenu menu) {
    for (Component component : menu.getMenuComponents()) {
        if (component instanceof JMenu) {
            adjustMenuTree((JMenu) component);
        } else if (component instanceof JMenuItem) {
            JMenuItem item   = (JMenuItem) component;
            Action    action = item.getAction();
            if (action instanceof Command) {
                ((Command) action).adjust();
            }
        }
    }
}
 
Example 6
Source File: InterListMenu.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void mouseEntered (MouseEvent e)
{
    JMenuItem item = (JMenuItem) e.getSource();
    InterAction action = (InterAction) item.getAction();
    action.publish();
}
 
Example 7
Source File: InterMenu.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void mouseEntered (MouseEvent e)
{
    JMenuItem item = (JMenuItem) e.getSource();
    RelationAction relationAction = (RelationAction) item.getAction();
    relationAction.publish();
}
 
Example 8
Source File: InterMenu.java    From audiveris with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void mouseReleased (MouseEvent e)
{
    JMenuItem item = (JMenuItem) e.getSource();
    RelationAction ra = (RelationAction) item.getAction();
    SIGraph sig = ra.getInter().getSig();
    Relation relation = ra.getRelation();
    String relStr = relation.getName();

    if (OMR.gui.displayConfirmation("Remove " + relStr + " relation?")) {
        interController.unlink(sig, relation);
    }
}
 
Example 9
Source File: MenuKeysDispatcher.java    From PyramidShader with GNU General Public License v3.0 5 votes vote down vote up
private static void setActionsMenu(JDialog dialog, MenuElement menu) {
        MenuElement[] subItems = menu.getSubElements();
        InputMap inputMap = dialog.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
        for (MenuElement menuElement : subItems) {
            if (menuElement instanceof JMenuItem) {
                final JMenuItem menuItem = (JMenuItem) menuElement;
                if (menuItem.getAccelerator() != null) {
                    String key = "hackAction" + counter++;
                    inputMap.put(menuItem.getAccelerator(), key);

                    if (menuItem.getAction() == null) {
                        dialog.getRootPane().getActionMap().put(key,
                                new AbstractAction() {
                            @Override
                            public void actionPerformed(ActionEvent e) {
                                menuItem.doClick();
                            }
                        });
                    } else {
                        dialog.getRootPane().getActionMap().put(key,
                                menuItem.getAction());
                    }

//                     System.out.println(key + " : "
//                     + menuItem.getText()+ " : "
//                     + menuItem.getActionCommand() + " : "
//                     + menuItem.getAccelerator() + " : "
//                     + menuItem.getAction());
                }
            }
            // the original code had an else condition here, which is wrong.
            // JMenu is a subclass of JMenuItem, which prevents sub-menus from being traversed.
            if (menuElement.getSubElements().length > 0) {
                setActionsMenu(dialog, menuElement);
            }
        }
    }
 
Example 10
Source File: MenuEditLayer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static void installAcceleratorPreview(JMenuItem item) {
    if(item instanceof JMenu) return;
    //detect accelerator key
    boolean already_has_accel = false;
    if(item.getAccelerator() != null) already_has_accel = true;
    if(item.getAction() != null && item.getAction().getValue(Action.ACCELERATOR_KEY) != null) already_has_accel = true;

    
    
    boolean already_has_accel_border = false;
    if(item.getBorder() == accel_border) {
        already_has_accel_border = true;
        //uninstall if needed
        if(already_has_accel) {
            item.setBorder(null);
            return;
        }
    }
    
    if(item.getBorder() instanceof CompoundBorder) {
        CompoundBorder comp = (CompoundBorder)item.getBorder();
        if(comp.getInsideBorder() == accel_border) {
            already_has_accel_border = true;
            //uninstall if needed
            if(already_has_accel) {
                item.setBorder(comp.getOutsideBorder());
                return;
            }
        }
    }
    
    if(already_has_accel_border) return;
    if(already_has_accel) return;
    
    
    if(item.getBorder() == null) {
        item.setBorder(accel_border);
        return;
    }
    
    item.setBorder(BorderFactory.createCompoundBorder(
                item.getBorder(),accel_border));
}