/** * Sets a mnemomic for the specified button. * * @param b button * @param mnem mnemonics that have already been assigned */ public static void setMnemonic(final AbstractButton b, final StringBuilder mnem) { // do not set mnemonics for Mac! Alt+key used for special characters. if (Prop.MAC) return; // find and assign unused mnemomic final String label = b.getText(); final int ll = label.length(); for (int l = 0; l < ll; l++) { final char ch = Character.toLowerCase(label.charAt(l)); if (!letter(ch) || mnem.indexOf(Character.toString(ch)) != -1) continue; b.setMnemonic(ch); mnem.append(ch); break; } }
/** use this to set a buttons's mnemonic */ public void setMnemonic(AbstractButton button) { if (assignedModifier == getGroupModifier(MNEMONIC) && getKeyStroke() != null && KeyEvent.getKeyText(assignedKey).length() == 1) { button.setMnemonic( KeyEvent.getKeyText(assignedKey) .charAt(0)); // getKeyStroke().getKeyChar() seems not to work here } }
private void configureAbstractButton(AbstractButton button, String resource) { String title = resources.getString(resource); int i = title.indexOf('&'); int mnemonic = 0; if (i >= 0) { mnemonic = title.charAt(i + 1); title = title.substring(0, i) + title.substring(i + 1); button.setText(title); button.setMnemonic(Character.toUpperCase(mnemonic)); button.setDisplayedMnemonicIndex(i); } else button.setText(title); }
private JComponent addItem( final Component pComponent, final KToolbarImpl pToolbar, final Action pAction) { JComponent item = null; final String key = (String) pAction.getValue(Action.NAME); if (pAction instanceof KMenu) { final KMenu actionMenu = (KMenu) pAction; item = actionMenu.create(pComponent); pToolbar.add(item); } else if (pAction == KAction.SEPARATOR) { pToolbar.addSeparator(); } else if (pAction instanceof KComponentAction) { item = ((KComponentAction) pAction).getComponent(); pToolbar.add(item); } else { AbstractButton button; ActionGroup group = (ActionGroup) pAction.getValue(KAction.KEY_GROUP); if (group != null) { button = new JToggleButton(pAction); ((JToggleButton) button).setSelected(group.getSelected() == pAction); ButtonGroup bg = group.getButtonGroup(ResKey.TOOLBAR); bg.add(button); } else { button = new JButton(pAction); } item = button; final WidgetResources wr = ResourceAdapter.getInstance().getWidget(key, ResKey.TOOLBAR); Icon icon = wr.getIcon(); if (icon == null) { icon = DEF_ICON; } button.setIcon(icon); button.setToolTipText(wr.getToolTip()); button.setMargin(ZERO_INSETS); button.setRequestFocusEnabled(false); button.setFocusable(false); if (false) { button.setText(wr.getText()); button.setMnemonic(wr.getMnenomnic()); button.setDisplayedMnemonicIndex(wr.getMnenomnicIndex()); } else { button.setText(null); } } if (item != null) { pToolbar.add(item); } return item; }
/** @noinspection ALL */ private void $$$loadButtonText$$$(AbstractButton component, String text) { StringBuffer result = new StringBuffer(); boolean haveMnemonic = false; char mnemonic = '\0'; int mnemonicIndex = -1; for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == '&') { i++; if (i == text.length()) break; if (!haveMnemonic && text.charAt(i) != '&') { haveMnemonic = true; mnemonic = text.charAt(i); mnemonicIndex = result.length(); } } result.append(text.charAt(i)); } component.setText(result.toString()); if (haveMnemonic) { component.setMnemonic(mnemonic); component.setDisplayedMnemonicIndex(mnemonicIndex); } }
public void propertyChange(PropertyChangeEvent evt) { final AbstractButton button = (AbstractButton) evt.getSource(); m_existingMnemonics.remove(button.getMnemonic(), button); button.setMnemonic(0); setMnemonic(button); }
private void setMnemonic(final AbstractButton button) { final int existingMnemonic = button.getMnemonic(); if (existingMnemonic != 0) { m_existingMnemonics.add(existingMnemonic, button); return; } final String text = button.getText(); if (text == null) { return; } // Remove our text changed listener whilst changing text to prevent // recursion. m_textChangedListener.remove(button); button.setText(removeMnemonicMarkers(text)); m_textChangedListener.add(button); // Look for explicit mnemonic indicated by an underscore in the button's // text. We remove the underscores. int underscore = text.indexOf('_'); int numberOfUnderscores = 0; while (underscore >= 0 && underscore < text.length() - 1) { final int explicitMnemonic = toKey(text.charAt(underscore + 1), false); final AbstractButton existingExplicit = m_existingMnemonics.getExplicit(explicitMnemonic); // If there is an existing button with the same explicit mnemonic, it // takes precedence and we fall back to other heuristics. if (explicitMnemonic != 0 && existingExplicit == null || existingExplicit == button) { final AbstractButton oldButton = m_existingMnemonics.remove(explicitMnemonic); button.setMnemonic(explicitMnemonic); // Calling setDisplayedIndex() directly here doesn't work for text // change events since it is overwritten by AbstractButton.setText(), // based on the original text. I've submitted a bug to Sun. // // Instead, we dispatch the change in the AWT event dispatching thread, // which works for the common case that setText() is called from that // thread. final int index = underscore - numberOfUnderscores; SwingUtilities.invokeLater( new Runnable() { public void run() { button.setDisplayedMnemonicIndex(index); } }); m_existingMnemonics.addExplicit(explicitMnemonic, button); // If there is a different existing button with an implicit mnemonic, // we take precedence and we calculate a new mnemonic for it. if (oldButton != null && oldButton != button) { oldButton.setMnemonic(0); setMnemonic(oldButton); } return; } // Treat subsequent underscores as indications of alternative mnemonics. underscore = text.indexOf('_', underscore + 1); ++numberOfUnderscores; } // No explicit mnemonic, use heuristics. for (int i = 0; i < m_heuristics.length; ++i) { final int result = m_heuristics[i].apply(button.getText()); if (result != 0) { button.setMnemonic(result); m_existingMnemonics.add(result, button); return; } } }