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

📄 actionmenubar.java

📁 这是linux下ssl vpn的实现程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package com.sshtools.ui.awt;

import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FontMetrics;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.Panel;
import java.awt.Point;
import java.awt.SystemColor;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.image.FilteredImageSource;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.Enumeration;
import java.util.Vector;

import com.sshtools.ui.awt.tooltips.ToolTipManager;

/**
 * A menubar implementation
 * 
 * @author $Autho$
 * @version $Revision: 1.5 $
 */
public class ActionMenuBar extends Panel implements FocusListener, ComponentListener, PropertyChangeListener {

    Vector menus = new Vector();
    Window menuWindow;
    MenuCanvas menuCanvas;
    MenuAction currentMenu;
    Frame parentFrame;
    Color disabledForeground = SystemColor.textInactiveText;
    boolean toolTipsEnabled;
    String icons;
    String iconType = ActionButton.SMALL_ICONS;
    int imageTextGap = 4;
    Separator separator;
    Color baseBackground = null;
    Color baseForeground = null;

    public ActionMenuBar() {
        super();
        separator = new Separator(Separator.HORIZONTAL);
        separator.setPreferredSize(new Dimension(0, 5));
        setLayout(new ToolLayout(separator));
        setBackground(SystemColor.control);
        setForeground(SystemColor.controlText);
        //    add(separator);
    }

    public void setImageTextGap(int imageTextGap) {
        invalidate();
        this.imageTextGap = imageTextGap;
        validate();
        repaint();
    }

    public void setIconType(String iconType) {
        invalidate();
        this.iconType = iconType;
        validate();
        repaint();
    }

    public void setToolTipsEnabled(boolean toolTipsEnabled) {
        this.toolTipsEnabled = toolTipsEnabled;
        if (!toolTipsEnabled) {
            ToolTipManager.getInstance().hide();
        }
    }

    public void setDisabledForeground(Color disabledForeground) {
        this.disabledForeground = disabledForeground;
        repaint();
    }

    public void addActionMenu(ActionMenu actionMenu) {
        final MenuAction menuAction = new MenuAction(actionMenu);
        ActionButton button = new ActionButton(menuAction) {
            public boolean isFocusTraversable() {
                return true;
            }

            public boolean isFocusable() {
                return true;
            }

        };
        button.addItemListener(new ItemListener() {

            public void itemStateChanged(ItemEvent e) {
                if (currentMenu != null) {
                    hideMenuWindow();
                    showMenuWindow(menuAction);
                }
            }

        });
        for (Enumeration e = actionMenu.children(); e.hasMoreElements();) {
            Action action = (Action) e.nextElement();
            action.addPropertyChangeListener(this);
        }
        menuAction.setButton(button);
        if(baseForeground != null) {
            button.setBaseForeground(baseForeground);
        }
        if(baseBackground != null) {
            button.setBaseBackground(baseBackground);
        }
        menus.addElement(menuAction);
        button.addFocusListener(this);
        add(button);
    }

    public void remove(int index) {
        synchronized (getTreeLock()) {
            super.remove(index);
            MenuAction menuAction = (MenuAction) menus.elementAt(index);
            menuAction.button.removeFocusListener(this);
            menus.removeElementAt(index);
        }
    }

    /*
     * (non-Javadoc)
     * 
     * @see java.lang.Object#finalize()
     */
    protected void finalize() throws Throwable {
        super.finalize();
        if (menuWindow != null) {
            parentFrame.removeComponentListener(this);
        }
    }

    void showMenuWindow(MenuAction menu) {
        currentMenu = menu;
        if (menuWindow == null) {
            parentFrame = UIUtil.getFrameAncestor(this);
            if (parentFrame == null) {
                parentFrame = UIUtil.getSharedFrame();
            }
            parentFrame.addComponentListener(this);
            menuWindow = new Window(parentFrame);
            menuCanvas = new MenuCanvas();
            if(baseBackground != null) {
                menuCanvas.setBaseBackground(baseBackground);
            }
            if(baseForeground != null) {
                menuCanvas.setBaseForeground(baseForeground);
            }
            menuCanvas.itemHeight = currentMenu.button.getSize().height;
            menuWindow.setLayout(new GridLayout(1, 1));
            menuWindow.add(menuCanvas);
        }
        int x = currentMenu.button.getLocationOnScreen().x;
        int y = currentMenu.button.getLocationOnScreen().y;
        y += currentMenu.button.getSize().height;
        menuWindow.setLocation(x, y);
        Dimension d = menuCanvas.getPreferredSize();
        menuCanvas.setMenu(currentMenu.menu);
        menuWindow.setSize(0, 0);
        menuWindow.pack();
        menuWindow.setVisible(true);
        currentMenu.button.setPressed(true);

    }

    void hideMenuWindow() {
        if (menuWindow != null) {
            menuWindow.setVisible(false);
            if (currentMenu != null) {
                currentMenu.button.setPressed(false);
            }
            currentMenu = null;
        }
    }

    /**
     * @return
     */
    public int getMenuCount() {
        return menus.size();
    }

    class MenuAction extends AbstractAction {

        private ActionMenu menu;
        private ActionButton button;

        MenuAction(ActionMenu menu) {
            super(menu.getDisplayName());
            this.menu = menu;
        }

        /*
         * (non-Javadoc)
         * 
         * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
         */
        public void actionPerformed(ActionEvent e) {
            if ((e.getModifiers() & MouseEvent.BUTTON1_MASK) != 0) {
                showMenuWindow(this);
            } else {
                hideMenuWindow();
            }
        }

        void setButton(ActionButton button) {
            this.button = button;
        }

    }

    class MenuCanvas extends Canvas implements MouseMotionListener, MouseListener {

        ActionMenu menu;
        int borderWidth = 1;
        Insets insets = new Insets(1, 16, 1, 16);
        Color borderColor = SystemColor.controlShadow;
        int sel = -1;
        Color selectionBackground = SystemColor.controlLtHighlight;
        Color selectionForeground = SystemColor.textText;
        int itemHeight = 16;
        String toolTipText;
        Image backingStore;

        MenuCanvas() {
            addMouseMotionListener(this);
            addMouseListener(this);
            setBackground(SystemColor.control);
            setForeground(SystemColor.controlText);
        }
        
        void setBaseBackground(Color base) {
            if(base == null) {
                selectionBackground = SystemColor.controlHighlight;
                setBackground(SystemColor.control);
            }
            else {
                selectionBackground = base.darker();
                setBackground(base);
            }
        }
        
        void setBaseForeground(Color base) {
            if(base == null) {
                borderColor = SystemColor.controlShadow;
                selectionForeground = SystemColor.controlText;
                setForeground(SystemColor.controlText);
            }
            else {
                borderColor=  base.darker();
                selectionForeground = base.brighter();
                setForeground(base);
            }
        }

        public Dimension getPreferredSize() {
            if (menu == null || getGraphics() == null) {
                return new Dimension(0, 0);
            }
            int w = 0;
            int h = 0;
            int iw = 0;
            FontMetrics fm = getFontMetrics(getFont());
            for (Enumeration e = menu.children(); e.hasMoreElements();) {
                Action action = (Action) e.nextElement();
                if(action.getName().equals(ActionMenu.SEPARATOR.getName())) {
                    h += separator.getPreferredSize().height;
                }
                else {
	                String imagePath = null;
	                if (iconType.equals(ActionButton.SMALL_ICONS)) {
	                    imagePath = (String) action.getValue(Action.SMALL_IMAGE_PATH);
	                } else if (iconType.equals(ActionButton.LARGE_ICONS)) {
	                    imagePath = (String) action.getValue(Action.IMAGE_PATH);
	                }
	                String n = (String) action.getValue(Action.SHORT_DESCRIPTION);
	                n = n == null ? action.getName() : n;
	                w = Math.max(w, fm.stringWidth(n));
	                itemHeight = Math.max(itemHeight, fm.getHeight());
	                if (imagePath != null) {
	                    Image img = UIUtil.loadImage(action.getClass(), imagePath);
	                    if (img != null) {
	                        UIUtil.waitFor(img, this);
	                        iw = Math.max(iw, img.getWidth(this));
	                        itemHeight = Math.max(itemHeight, img.getHeight(this));
	                    }
	                }
	                h += itemHeight;
                }
            }
            if (iw != 0) {
                w += iw;
                w += imageTextGap;
            }
            if (insets != null) {
                w += insets.left + insets.right;
                h += insets.top + insets.bottom;
            }
            w += borderWidth * 2;
            h += borderWidth * 2;
            return new Dimension(w, h);
        }

        public void update(Graphics g) {
            paint(g);
        }

        public Dimension getMinimumSize() {
            return getPreferredSize();

⌨️ 快捷键说明

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