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

The following examples show how to use javax.swing.JMenuItem#getAccelerator() . 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: NbEditorKit.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static void assignAccelerator(Keymap km, Action action, JMenuItem item) {
    if (item.getAccelerator() == null){
        KeyStroke ks = (KeyStroke)action.getValue(Action.ACCELERATOR_KEY);
        if (ks!=null) {
            item.setMnemonic(ks.getKeyCode());
        } else {
            // Try to get the accelerator from keymap
            if (km != null) {
                KeyStroke[] keys = km.getKeyStrokesForAction(action);
                if (keys != null && keys.length > 0) {
                    item.setMnemonic(keys[0].getKeyCode());
                }
            }
        }
    }
}
 
Example 2
Source File: MainMenuAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/** Adds accelerators to given JMenuItem taken from the action */
protected static void addAccelerators(Action a, JMenuItem item, JTextComponent target){
    if (target == null || a==null || item==null) return;
    
    // get accelerators from kitAction
    Action kitAction = getActionByName((String)a.getValue(Action.NAME));
    if (kitAction!=null) a = kitAction;
    // Try to get the accelerator, TopComponent action could be obsoleted
    Keymap km = target.getKeymap();

    if (km != null) {
        KeyStroke[] keys = km.getKeyStrokesForAction(a);
        KeyStroke itemAccelerator = item.getAccelerator();
        
        if (keys != null && keys.length > 0) {
            if (itemAccelerator==null || !itemAccelerator.equals(keys[0])){
                item.setAccelerator(keys[0]);
            }
        }else{
            if (itemAccelerator!=null && kitAction!=null){
                item.setAccelerator(null);
            }
        }
    }
}
 
Example 3
Source File: NbMenuItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
     * @param it
     * @return instance of NbMenuItem constructed from parameter it */
    public NbMenuItem(JMenuItem it) {
        setName(it.getText());//getLabel();
        this.accelerator = (it.getAccelerator() == null) ? null : it.getAccelerator().toString();
        this.mnemo = (char) it.getMnemonic();
//        System.out.println("NbMenuItem ["+name+"] - mnemo: ["+it.getMnemonic()+"]"); why are the mnemonic always in capital?
        this.enabled = it.isEnabled();
        this.checkbox = it instanceof JCheckBoxMenuItem;
        this.radiobutton = it instanceof JRadioButtonMenuItem;
        this.checked = it.isSelected();
    }
 
Example 4
Source File: NbMenuItem.java    From netbeans with Apache License 2.0 5 votes vote down vote up
/**
     * @param it
     * @return instance of NbMenuItem constructed from parameter it */
    public NbMenuItem(JMenuItem it) {
        setName(it.getText());//getLabel();
        this.accelerator = (it.getAccelerator() == null) ? null : it.getAccelerator().toString();
        this.mnemo = (char) it.getMnemonic();
//        System.out.println("NbMenuItem ["+name+"] - mnemo: ["+it.getMnemonic()+"]"); why are the mnemonic always in capital?
        this.enabled = it.isEnabled();
        this.checkbox = it instanceof JCheckBoxMenuItem;
        this.radiobutton = it instanceof JRadioButtonMenuItem;
        this.checked = it.isSelected();
    }
 
Example 5
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 6
Source File: DropTargetLayer.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private static int getAcceleratorWidth(JMenuItem item) {
    if(item instanceof JMenu) return 0;
    if(item.getAccelerator() != null) return 50;
    return MenuEditLayer.ACCEL_PREVIEW_WIDTH; // gutter space that we can click on to add an accelerator
}
 
Example 7
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));
}
 
Example 8
Source File: MenuChecker.java    From netbeans with Apache License 2.0 4 votes vote down vote up
NbMenu(JMenuItem menu, List<NbMenu> submenu) {
    name = menu.getText();//getLabel();
    accelerator = (menu.getAccelerator() == null) ? null : menu.getAccelerator().toString();
    mnemo = menu.getMnemonic();
    this.submenu = submenu;
}
 
Example 9
Source File: SeaGlassMenuItemUI.java    From seaglass with Apache License 2.0 4 votes vote down vote up
static Dimension getPreferredMenuItemSize(SeaGlassContext context, SeaGlassContext accContext, boolean useCheckAndArrow, JComponent c,
    Icon checkIcon, Icon arrowIcon, int defaultTextIconGap, String acceleratorDelimiter) {
    JMenuItem b = (JMenuItem) c;
    Icon icon = (Icon) b.getIcon();
    String text = b.getText();
    KeyStroke accelerator = b.getAccelerator();
    String acceleratorText = "";

    if (accelerator != null) {
        int modifiers = accelerator.getModifiers();
        if (modifiers > 0) {
            acceleratorText = KeyEvent.getKeyModifiersText(modifiers);
            acceleratorText += acceleratorDelimiter;
        }
        int keyCode = accelerator.getKeyCode();
        if (keyCode != 0) {
            acceleratorText += KeyEvent.getKeyText(keyCode);
        } else {
            acceleratorText += accelerator.getKeyChar();
        }
    }

    Font font = context.getStyle().getFont(context);
    FontMetrics fm = b.getFontMetrics(font);
    FontMetrics fmAccel = b.getFontMetrics(accContext.getStyle().getFont(accContext));

    resetRects();

    layoutMenuItem(context, fm, accContext, text, fmAccel, acceleratorText, icon, checkIcon, arrowIcon, b.getVerticalAlignment(), b
        .getHorizontalAlignment(), b.getVerticalTextPosition(), b.getHorizontalTextPosition(), viewRect, iconRect, textRect,
        acceleratorRect, checkIconRect, arrowIconRect, text == null ? 0 : defaultTextIconGap, defaultTextIconGap, useCheckAndArrow);
    // find the union of the icon and text rects
    r.setBounds(textRect);
    r = SwingUtilities.computeUnion(iconRect.x, iconRect.y, iconRect.width, iconRect.height, r);
    // To make the accelerator texts appear in a column,
    // find the widest MenuItem text and the widest accelerator text.

    // Get the parent, which stores the information.
    Container parent = b.getParent();

    if (parent instanceof JPopupMenu) {
        SeaGlassPopupMenuUI popupUI = (SeaGlassPopupMenuUI) SeaGlassLookAndFeel.getUIOfType(((JPopupMenu) parent).getUI(),
            SeaGlassPopupMenuUI.class);

        if (popupUI != null) {
            r.width = popupUI.adjustTextWidth(r.width);

            popupUI.adjustAcceleratorWidth(acceleratorRect.width);

            r.width += popupUI.getMaxAcceleratorWidth();
        }
    } else if (parent != null && !(b instanceof JMenu && ((JMenu) b).isTopLevelMenu())) {
        r.width += acceleratorRect.width;
    }

    if (useCheckAndArrow) {
        // Add in the checkIcon
        r.width += checkIconRect.width;
        r.width += defaultTextIconGap;

        // Add in the arrowIcon
        r.width += defaultTextIconGap;
        r.width += arrowIconRect.width;
    }

    r.width += 2 * defaultTextIconGap;

    Insets insets = b.getInsets();
    if (insets != null) {
        r.width += insets.left + insets.right;
        r.height += insets.top + insets.bottom;
    }

    // if the width is even, bump it up one. This is critical
    // for the focus dash line to draw properly
    if (r.width % 2 == 0) {
        r.width++;
    }

    // if the height is even, bump it up one. This is critical
    // for the text to center properly
    if (r.height % 2 == 0) {
        r.height++;
    }
    return r.getSize();
}