PageRenderTime 35ms CodeModel.GetById 15ms app.highlight 13ms RepoModel.GetById 2ms app.codeStats 0ms

/jEdit/tags/jedit-4-5-pre1/org/gjt/sp/jedit/gui/tray/JTrayIcon.java

#
Java | 135 lines | 88 code | 15 blank | 32 comment | 9 complexity | b7098b41706bd1148c2df15d98ebd70e MD5 | raw file
  1/*
  2 * jEdit - Programmer's Text Editor
  3 * :tabSize=8:indentSize=8:noTabs=false:
  4 * :folding=explicit:collapseFolds=1:
  5 *
  6 * Copyright Š 2011 Matthieu Casanova
  7 *
  8 * This program is free software; you can redistribute it and/or
  9 * modify it under the terms of the GNU General Public License
 10 * as published by the Free Software Foundation; either version 2
 11 * of the License, or any later version.
 12 * This program is distributed in the hope that it will be useful,
 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 15 * GNU General Public License for more details.
 16 *
 17 * You should have received a copy of the GNU General Public License
 18 * along with this program; if not, write to the Free Software
 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 20 */
 21
 22package org.gjt.sp.jedit.gui.tray;
 23
 24//{{{ Imports
 25import java.awt.Frame;
 26import java.awt.Image;
 27import java.awt.TrayIcon;
 28
 29import java.awt.event.MouseAdapter;
 30import java.awt.event.MouseEvent;
 31import java.awt.event.MouseListener;
 32
 33import javax.swing.JDialog;
 34import javax.swing.JPopupMenu;
 35import javax.swing.event.PopupMenuEvent;
 36import javax.swing.event.PopupMenuListener;
 37
 38import org.gjt.sp.jedit.GUIUtilities;
 39//}}}
 40
 41/**
 42 * A TrayIcon that accepts Swing JPopupMenu.
 43 * @author Matthieu Casanova
 44 * @since jEdit 4.5pre1
 45 */
 46public class JTrayIcon extends TrayIcon
 47{
 48	private JDialog parent;
 49	private JPopupMenu menu;
 50	private MouseListener mouseListener;
 51	private PopupMenuListener popupMenuListener;
 52
 53	//{{{ JTrayIcon constructor
 54	public JTrayIcon(Image image, String tooltip)
 55	{
 56		super(image, tooltip, null);
 57
 58	} //}}}
 59
 60	//{{{ getMenu() method
 61	public JPopupMenu getMenu()
 62	{
 63		return menu;
 64	} //}}}
 65
 66	//{{{ setMenu() method
 67	public void setMenu(JPopupMenu menu)
 68	{
 69		if (menu == null)
 70		{
 71
 72			if (mouseListener != null)
 73			{
 74				removeMouseListener(mouseListener);
 75				mouseListener = null;
 76			}
 77			if (popupMenuListener != null)
 78			{
 79				this.menu.removePopupMenuListener(popupMenuListener);
 80				popupMenuListener = null;
 81			}
 82			parent = null;
 83		}
 84		else
 85		{
 86			parent = new JDialog((Frame) null);
 87			parent.setUndecorated(true);
 88			parent.setAlwaysOnTop(true);
 89			if (mouseListener == null)
 90			{
 91				mouseListener = new MyMouseListener();
 92				addMouseListener(mouseListener);
 93			}
 94			popupMenuListener = new MyPopupMenuListener();
 95			menu.addPopupMenuListener(popupMenuListener);
 96		}
 97		this.menu = menu;
 98	} //}}}
 99
100	//{{{ MyMouseListener class
101	private class MyMouseListener extends MouseAdapter
102	{
103		@Override
104		public void mouseClicked(MouseEvent e)
105		{
106			if (GUIUtilities.isPopupTrigger(e))
107			{
108				parent.setLocation(e.getX(), e.getY() - menu.getPreferredSize().height);
109				parent.setVisible(true);
110				menu.show(parent, 0, 0);
111			}
112		}
113	} //}}}
114
115	//{{{ MyPopupMenuListener class
116	private class MyPopupMenuListener implements PopupMenuListener
117	{
118		@Override
119		public void popupMenuWillBecomeVisible(PopupMenuEvent e)
120		{
121		}
122
123		@Override
124		public void popupMenuWillBecomeInvisible(PopupMenuEvent e)
125		{
126			parent.setVisible(false);
127		}
128
129		@Override
130		public void popupMenuCanceled(PopupMenuEvent e)
131		{
132			parent.setVisible(false);
133		}
134	} //}}}
135}