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

📄 gnometrayiconservice.java

📁 JDesktop Integration Components (JDIC)
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is * subject to license terms. * * This program is free software; you can redistribute it and/or modify * it under the terms of the Lesser GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program 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 this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package org.jdesktop.jdic.tray.internal.impl;import org.jdesktop.jdic.tray.internal.TrayIconService;/** * The <code>GnomeTrayIconService</code> interface is the contract for a Gnome  * TrayIcon implementation. * */import java.awt.*;import javax.swing.*;import javax.swing.border.BevelBorder;import javax.swing.event.PopupMenuEvent;import javax.swing.event.PopupMenuListener;import java.awt.event.*;import java.util.*;public class GnomeTrayIconService extends GnomeTrayAppletService     implements TrayIconService {    private JPopupMenu menu;    private JDialog popupMenuParent;    private IconPanel iconPanel;    private Icon icon;            private HWToolTip tooltip;    private BalloonMessageWindow bmw;    private boolean autoSize;    private LinkedList actionList = new LinkedList();    private LinkedList balloonListeners = new LinkedList();    public GnomeTrayIconService() {        super();        initFrame();    }    void initFrame(){        iconPanel = new IconPanel();        frame.add(iconPanel);        frame.setFocusable(true);        frame.requestFocus();        bmw = new BalloonMessageWindow(frame);                initListeners();                popupMenuParent = new JDialog(frame, "JDIC Tray Icon");        popupMenuParent.setUndecorated(true);    }    void mousePressed(final MouseEvent e) {        if(tooltip != null)        	tooltip.setVisible(false);        if (e.isPopupTrigger()) {        	if (menu != null) {                Dimension d = menu.getPreferredSize();                Dimension s = Toolkit.getDefaultToolkit().getScreenSize();                final Point p = e.getPoint();                SwingUtilities.convertPointToScreen(p, (Component) e.getSource());                p.x = p.x + d.width > s.width ? p.x - d.width : p.x;                p.y = p.y + d.height > s.height ? p.y - d.height : p.y;                SwingUtilities.convertPointFromScreen(p, popupMenuParent);                popupMenuParent.setVisible(true);                SwingUtilities.invokeLater(new Runnable(){                    public void run(){                        menu.show(popupMenuParent.getContentPane(), p.x, p.y);                        popupMenuParent.toFront();                    }                });            }         }else {            SwingUtilities.invokeLater(new Runnable(){                public void run() {                  Thread actionThread = new Thread(){                    public void run() {                        ListIterator li = actionList.listIterator(0);                        while (li.hasNext()) {                        ActionListener al;                        al = (ActionListener) li.next();                        al.actionPerformed(new ActionEvent(GnomeTrayIconService.this,                                    ActionEvent.ACTION_PERFORMED, "PressAction", e.getWhen(),                                    0));                        }                    }                                    };                  actionThread.start();                }            });        }    }    void mouseEntered(MouseEvent e) {        if ((tooltip != null) && ((menu == null) || ((menu != null) && !menu.isShowing()))) {            Dimension d = tooltip.getSize();            Point p = frame.getLocationOnScreen();            Dimension size = tooltip.getPreferredSize();            tooltip.show(p.x, p.y - size.height - 5);        }    }    void mouseExited(MouseEvent e) {        if (tooltip != null) {            tooltip.setVisible(false);        }    }    void initListeners() {        MouseAdapter ma = new MouseAdapter() {            public void mousePressed(MouseEvent e) {                GnomeTrayIconService.this.mousePressed(e);            }            public void mouseEntered(MouseEvent e) {                GnomeTrayIconService.this.mouseEntered(e);            }            public void mouseExited(MouseEvent e) {                GnomeTrayIconService.this.mouseExited(e);            }        };        iconPanel.addMouseListener(ma);        frame.addMouseListener(ma);    // for some reason 1.4.2 needs        // to have mouse listener installed on frame instead of iconPanel      }    public void addNotify() {    	if(GnomeTrayAppletService.winMap.get(new Long(this.getWindow())) == null){    		super.init();    		initFrame();                		if(this.icon != null)    			this.setIcon(this.icon);    		if(this.menu != null)    			this.setPopupMenu(this.menu);    		    		if(this.tooltip != null)    			tooltip = new HWToolTip(this.tooltip.label.getTipText(), frame);    		    	}        GnomeSystemTrayService.dockWindow(this.getWindow());        frame.setVisible(true);    }    public void setPopupMenu(JPopupMenu m) {        menu = m;        if (m != null) {            m.setLightWeightPopupEnabled(false);            m.addPopupMenuListener(new PopupMenuListener(){				public void popupMenuWillBecomeVisible(PopupMenuEvent e) {				}				public void popupMenuWillBecomeInvisible(PopupMenuEvent e) {                    popupMenuParent.setVisible(false);                }				public void popupMenuCanceled(PopupMenuEvent e) {				}                            });            // in jdk1.4, the popup menu is still visible after the invoker window lost focus.            popupMenuParent.addWindowFocusListener(new WindowFocusListener() {                public void windowGainedFocus(WindowEvent e) {                }                public void windowLostFocus(WindowEvent e) {                    menu.setVisible(false);                }            });        }    }    public void setIcon(final Icon i) {        SwingUtilities.invokeLater(new Runnable(){           public void run(){            icon = i;            if (icon != null) {                int w = icon.getIconWidth();                int h = icon.getIconHeight();                reshape(0,0,w,h);                frame.setVisible(false);                frame.remove(iconPanel);                iconPanel = new IconPanel();                 frame.add(iconPanel);                frame.setVisible(true);            }            iconPanel.repaint();           }        });    }    public void setCaption(String s) {        // System.out.println("setCaption s =" + s);        if (tooltip == null) {            tooltip = new HWToolTip(s, frame);        } else {            tooltip.setCaption(s);        }    }    public void setIconAutoSize(boolean b) {        autoSize = b;        if (autoSize && (icon != null)) {            int w = icon.getIconWidth();            int h = icon.getIconHeight();            reshape(0,0,w,h);            frame.setVisible(false);            frame.remove(iconPanel);            iconPanel = new IconPanel();            frame.add(iconPanel);            frame.setVisible(true);        }    }    public void addActionListener(ActionListener l) {        actionList.add(l);    }    public void removeActionListener(ActionListener l) {        actionList.remove(l);    }    public Point getLocationOnScreen() {        Point p = null;        if (iconPanel != null) {            p = iconPanel.getLocationOnScreen();        }        return p;    }    /*     * This iconPanel paints the icon      *     */    class IconPanel extends JComponent {        Image img;        IconPanel() {}        public void paintComponent(Graphics g) {            Dimension d = getAppletSize();            g.clearRect(0, 0, d.width, d.height);            if (icon != null) {                int w = icon.getIconWidth();                int h = icon.getIconHeight();                if (!autoSize) {                    icon.paintIcon(this, g, 0, 0);                } else {                    /* Scale to the right size */                    if (img == null) {                        img = createImage(w, h);                    }                    icon.paintIcon(this, img.getGraphics(), 0, 0);                    g.drawImage(img, 0, 0, d.width, d.height, 0,0,w,h, this);                }            }          super.paintComponent(g);        }        boolean doesIconReferenceImage(Icon icon, Image image) {            Image iconImage = (icon != null && (icon instanceof ImageIcon)) ?                ((ImageIcon)icon).getImage() : null;            return (iconImage == image);        }        /**

⌨️ 快捷键说明

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