⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menutools.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir 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.
 *
 * Caliph & Emir 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 Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2005 by Werner Klieber (werner@klieber.info)
 * http://caliph-emir.sourceforge.net
 */
package at.wklieber.tools;

import at.wklieber.Settings;
import at.wklieber.gui.GenericListener;
import org.apache.log4j.Category;
import org.apache.log4j.Logger;
import org.jdom.Attribute;
import org.jdom.Element;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * used to extract the Accelerator from a string.
 * e.g. "&File" -> "File", "F"
 */
class MenuEntryStruct {
    private String text = "";
    private char mnemonic = 0;

    public MenuEntryStruct(String property1) {
        init(property1, null);
    }

    public MenuEntryStruct(String property1, String accelerator1) {
        init(property1, accelerator1);
    }

    private void init(String property1, String accelerator1) {
        text = property1;


        if (accelerator1 != null && accelerator1.length() > 0) {
            mnemonic = accelerator1.charAt(0);
        } else { // try to extract from property name
            int shortCut = text.indexOf("&");
            mnemonic = 0;
            if ((shortCut > -1) && (text.length() > shortCut)) {
                String prefix = "";
                if (shortCut > 0) {
                    prefix = text.substring(0, shortCut);
                }
                mnemonic = text.charAt(shortCut + 1);
                String postfix = text.substring(shortCut + 1);

                text = prefix + postfix;
            }
        }
    }

    public String getText() {
        return text;
    }

    public char getMnemonic() {
        return mnemonic;
    }

    public String toString() {
        return "Text: <" + text + ">, Mnemonic: <" + (int) mnemonic + ">";
    }
}  // end class

//----------------------------------------------------------------------------------

public class MenuTools {
    static Category cat = Logger.getLogger(MenuTools.class.getName());
    private static Console console = Console.getReference();

    //private static MenuTools java2dTools = null;

    public static final int TYPE_MENUBAR = 0;
    public static final int TYPE_POPUP = 1;


    private final int ITEM_PLAIN = 0;// Item types
    private final int ITEM_CHECK = 1;
    private final int ITEM_RADIO = 2;

    JMenuBar menuBar = null;
    JPopupMenu popupMenu = null;
    Container toolBarContainer = null;
    Map menuEntries = null;  // contains all menu main entries: File, Edit, Search, ....
    Map toolbarEntries = null; // a set of toolbars, according to the menu main entries
    String imageDir = null;  // the base dir, where all images are located

    private Object parentClass = null;


    /**
     * create a button with actiononlistener
     */
    public static JButton createImageButton(String iconFileName,
                                            String toolTip,
                                            Object actionListenerTargetClass,
                                            String actionListenerName) {
        JButton returnValue = null;

        try {
            ImageIcon icon = null;
            String iconDir = Settings.getReference().getIconsDir();
            icon = new ImageIcon(iconDir + iconFileName, toolTip);
            returnValue = createImageButton(icon, toolTip, actionListenerTargetClass, actionListenerName);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }

    public static JButton createImageButton(Icon icon,
                                            String toolTip,
                                            Object actionListenerTargetClass,
                                            String actionListenerName) {
        JButton returnValue = null;

        try {
            ActionListener action = createActionListener(actionListenerName, actionListenerTargetClass);

            JButton jButton = new JButton(icon);
            jButton.setToolTipText(toolTip);
            jButton.addActionListener(action);
            returnValue = jButton;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }


    /**
     * constructor for managing a menuBar List
     */
    public MenuTools(Object parentClass1, JMenuBar menuBar1, Container toolBarContainer1,
                     String imageLocation1) {
        init(parentClass1, menuBar1, toolBarContainer1, imageLocation1);
    } // end constructor


    /**
     * constructor for managing a popupMenu
     */
    public MenuTools(String imageLocation1) {
        init(null, null, null, imageLocation1);
    } // end constructor


    private void init(Object parentClass1, JMenuBar menuBar1, Container toolBarContainer1,
                      String imageLocation1) {
        parentClass = parentClass1;
        menuBar = menuBar1;
        toolBarContainer = toolBarContainer1;

        imageDir = "/at/wklieber/data/icons";
        //imageDir = FileTools.setUrlPrefix(imageDir);
        imageDir = FileTools.setFinalDirectorySlash(imageLocation1);


        popupMenu = new JPopupMenu();

        menuEntries = new HashMap();
        toolbarEntries = new HashMap();
    }


    /**
     * reads the complete structure from the IAccess configuratin
     */

    private String getAttributeValue(Element element1, String attrName1) {
        String returnValue = "";
        if (element1 == null || attrName1 == null || attrName1.length() == 0) {
            return returnValue;
        }
        try {
            Attribute attributeElement = element1.getAttribute(attrName1);
            if (attributeElement != null) {
                returnValue = attributeElement.getValue();
            }
        } catch (Exception e) {
            cat.error(e);
        }
        return returnValue;
    }


    String nameWithAccelerator(String entryName, String entryAccelerator) {
        String returnValue = entryName;

        if (entryName == null || entryAccelerator == null || entryAccelerator.length() == 0) {
            return returnValue;
        }

        try {
            int start = entryName.indexOf(entryAccelerator);
            if (start >= 0) {
                String prefix = "";
                if (start > 0) {
                    prefix = entryName.substring(0, start);
                }

                String postfix = entryName.substring(start);
                returnValue = prefix + "&" + postfix;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return returnValue;
    }

    /**
     * read menu settings from the xul file and add the entries to a menubar or pop-up menu
     *
     * @param dialogName1 the id in the xul file that specifies the menu to read in
     * @param type        specifies where the entries are added: menubar or popup menu
     */
    public void readMenuFromConfigFile(String dialogName1, int type) {
        try {
            IAccessFile config = Settings.getReference().getConfigAccess();
            String xmlRoot = "imbConfig/userInterface/menu/xul";

            java.util.List menuElements = config.getProperties(xmlRoot);
            if (menuElements == null || menuElements.size() < 1) {
                return;
            }

            // get xul tag
            Element xmlRootElement = (Element) menuElements.get(0);
            //cat.debug(xmlRootElement.getName());

            // get the actionlistener names


            java.util.List commandElements = xmlRootElement.getChildren("Command");
            Map actionListnerList = new HashMap(commandElements.size());
            for (Iterator commandIterator = commandElements.iterator(); commandIterator.hasNext();) {
                Element e = (Element) commandIterator.next();
                actionListnerList.put(getAttributeValue(e, "id"), getAttributeValue(e, "oncommand"));
            }
            //cat.debug(CollectionTools.printCollectionContent(actionListnerList));


            String xpath = "//xul/menubar[@id=\"" + dialogName1 + "\"]";
            java.util.List menuBarElements = XmlTools.xpathQuery(xmlRootElement, xpath, true);
            if (menuBarElements == null || menuBarElements.size() < 1) {
                return;
            }

            //get the dialogName element "menubar"
            Element dialogElement = (Element) menuBarElements.get(0);
            //cat.debug(XmlTools.documentToString(dialogElement));

            // now get the entries for the menubar: e.g. file, edit, ...
            java.util.List menuList = XmlTools.xpathQuery(dialogElement, "//menubar/menu", true);
            for (Iterator menuIterator = menuList.iterator(); menuIterator.hasNext();) {
                Element itemElement = (Element) menuIterator.next();
                String menuName = getAttributeValue(itemElement, "label");
                String menuAccelerator = getAttributeValue(itemElement, "accesskey");
                menuName = nameWithAccelerator(menuName, menuAccelerator);

                Element child = itemElement.getChild("menupopup");
                if (child == null) {
                    continue;
                }

                java.util.List entryList = child.getChildren("menuitem");
                //cat.debug("--->" + entryList.size());
                for (Iterator entryIterator = entryList.iterator(); entryIterator.hasNext();) {
                    Element entryElement = (Element) entryIterator.next();
                    //entryElement.detach();

                    String entryName = getAttributeValue(entryElement, "label");
                    String entryAccelerator = getAttributeValue(entryElement, "accesskey");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -