Package slash.navigation.gui.helpers

Source Code of slash.navigation.gui.helpers.JMenuHelper

/*
    This file is part of RouteConverter.

    RouteConverter is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    RouteConverter is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with RouteConverter; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

    Copyright (C) 2007 Christian Pesch. All Rights Reserved.
*/

package slash.navigation.gui.helpers;

import slash.navigation.gui.Application;

import javax.swing.*;
import java.awt.*;
import java.util.ResourceBundle;

import static javax.swing.KeyStroke.getKeyStroke;
import static slash.common.io.Transfer.trim;
import static slash.navigation.gui.helpers.UIHelper.loadIcon;

/**
* A helper for simplified {@link JMenu} operations.
*
* @author Christian Pesch
*/

public class JMenuHelper {
    private static String getString(String key) {
        return Application.getInstance().getContext().getBundle().getString(key);
    }

    private static String getOptionalString(String key) {
        ResourceBundle bundle = Application.getInstance().getContext().getBundle();
        return bundle.containsKey(key) ? bundle.getString(key) : null;
    }

    public static JMenu createMenu(String name) {
        JMenu menu = new JMenu(getString(name + "-menu"));
        menu.setName(name);
        String mnemonic = trim(getOptionalString(name + "-menu-mnemonic"));
        if (mnemonic != null && mnemonic.length() > 0)
            menu.setMnemonic(mnemonic.charAt(0));
        return menu;
    }

    private static int getMnemonicAmpersandIndex(String text) {
        int i = -1;
        do {
            i = text.indexOf('&', i + 1);
            if (i >= 0 && (i + 1) < text.length()) {
                // before ' '
                if (text.charAt(i + 1) == ' ') {
                    continue;
                    // before ', and after '
                } else if (text.charAt(i + 1) == '\'' && i > 0 && text.charAt(i - 1) == '\'') {
                    continue;
                }
                // ampersand is marking mnemonics
                return i;
            }
        } while (i >= 0);
        return -1;
    }

    private static void setMnemonic(AbstractButton item, char mnemonic) {
        item.setMnemonic(mnemonic);
        String text = item.getText();
        int ampersandIndex = getMnemonicAmpersandIndex(text);
        if (ampersandIndex != -1) {
            item.setText(text.substring(0, ampersandIndex) + text.substring(ampersandIndex + 1));
            item.setDisplayedMnemonicIndex(ampersandIndex);
        }
    }

    public static void setMnemonic(AbstractButton button, String key) {
        String mnemonic = trim(getOptionalString(key));
        if (mnemonic != null && mnemonic.length() > 0)
            setMnemonic(button, mnemonic.charAt(0));
    }

    public static JMenuItem createItem(String name) {
        Action action = Application.getInstance().getContext().getActionManager().get(name);
        JMenuItem item = new JMenuItem(action);
        initializeItem(name, item);
        return item;
    }

    public static JRadioButtonMenuItem createRadioItem(String name) {
        Action action = Application.getInstance().getContext().getActionManager().get(name);
        JRadioButtonMenuItem item = new JRadioButtonMenuItem(action);
        initializeItem(name, item);
        return item;
    }

    private static void initializeItem(String name, JMenuItem item) {
        item.setName(name);
        item.setText(getString(name + "-action"));
        String tooltip = trim(getOptionalString(name + "-action-tooltip"));
        if (tooltip != null)
            item.setToolTipText(tooltip);
        setMnemonic(item, name + "-action-mnemonic");
        String keystroke = trim(getOptionalString(name + "-action-keystroke"));
        if (keystroke != null)
            item.setAccelerator(getKeyStroke(keystroke));
        String iconUrl = trim(getOptionalString(name + "-action-icon"));
        if (iconUrl != null)
            item.setIcon(loadIcon(iconUrl));
        String disabledIconUrl = trim(getOptionalString(name + "-action-disabled-icon"));
        if (disabledIconUrl != null)
            item.setDisabledIcon(loadIcon(disabledIconUrl));
    }

    public static void registerAction(AbstractButton component, String name) {
        String text = component.getText();
        String toolTipText = component.getToolTipText();
        Icon icon = component.getIcon();
        component.setAction(Application.getInstance().getContext().getActionManager().get(name));
        component.setText(text);
        component.setToolTipText(toolTipText);
        component.setIcon(icon);
    }

    public static void registerKeyStroke(JComponent component, String name) {
        String keystroke = getString(name + "-action-keystroke");
        component.getInputMap().put(getKeyStroke(keystroke), name);
        component.getActionMap().put(name, Application.getInstance().getContext().getActionManager().get(name));
    }

    public static JMenu findMenu(JMenuBar menuBar, String menuName) {
        for (int i = 0; i < menuBar.getMenuCount(); i++) {
            JMenu menu = menuBar.getMenu(i);
            if (menuName.equals(menu.getName()))
                return menu;
        }
        return null;
    }

    public static JMenu findMenu(JMenuBar menuBar, String menuName, String subMenuName) {
        return findMenuComponent(menuBar, menuName, subMenuName, JMenu.class);
    }

    public static JMenuItem findItem(JMenuBar menuBar, String menuName, String menuItemName) {
        return findMenuComponent(menuBar, menuName, menuItemName, JMenuItem.class);
    }

    public static Component findMenuComponent(JPopupMenu menu, String menuComponentName) {
        for (int i = 0; i < menu.getComponentCount(); i++) {
            Component component = menu.getComponent(i);
            if (menuComponentName.equals(component.getName()))
                return component;
        }
        return null;
    }

    public static <T extends Component> T findMenuComponent(JMenu menu, String menuComponentName, Class<T> componentClass) {
        for (int i = 0; i < menu.getMenuComponentCount(); i++) {
            Component component = menu.getMenuComponent(i);
            if (menuComponentName.equals(component.getName()) && componentClass.isInstance(component))
                return componentClass.cast(component);
        }
        return null;
    }

    public static <T extends Component> T findMenuComponent(JMenuBar menuBar, String menuName, String menuComponentName, Class<T> componentClass) {
        JMenu menu = findMenu(menuBar, menuName);
        if (menu != null) {
            return findMenuComponent(menu, menuComponentName, componentClass);
        }
        return null;
    }
}
TOP

Related Classes of slash.navigation.gui.helpers.JMenuHelper

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.