r2679 - in trunk: . src/main/java/org/chorem/jtimer/ui/systray src/main/java/org/chorem/jtimer/ui/widget
Author: echatellier Date: 2009-10-26 10:04:48 +0100 (Mon, 26 Oct 2009) New Revision: 2679 Added: trunk/src/main/java/org/chorem/jtimer/ui/widget/JPopupTrayIcon.java Modified: trunk/changelog.txt trunk/src/main/java/org/chorem/jtimer/ui/systray/SystrayManager.java Log: Fix systray popup menu not closed on focus lost Modified: trunk/changelog.txt =================================================================== --- trunk/changelog.txt 2009-10-15 09:29:18 UTC (rev 2678) +++ trunk/changelog.txt 2009-10-26 09:04:48 UTC (rev 2679) @@ -3,6 +3,7 @@ jTimer (1.3.1) stable; urgency=low + * Fix systray popup menu not closed on focus lost * Force task selection on rigth clic * Fix duration edition (task and alerts) * Fix NPE related to non available systray Modified: trunk/src/main/java/org/chorem/jtimer/ui/systray/SystrayManager.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/ui/systray/SystrayManager.java 2009-10-15 09:29:18 UTC (rev 2678) +++ trunk/src/main/java/org/chorem/jtimer/ui/systray/SystrayManager.java 2009-10-26 09:04:48 UTC (rev 2679) @@ -42,6 +42,7 @@ import org.chorem.jtimer.data.DataEventListener; import org.chorem.jtimer.entities.TimerProject; import org.chorem.jtimer.entities.TimerTask; +import org.chorem.jtimer.ui.widget.JPopupTrayIcon; import org.jdesktop.application.ApplicationContext; import org.jdesktop.application.ResourceManager; import org.jdesktop.application.ResourceMap; @@ -166,13 +167,10 @@ SystemTray tray = SystemTray.getSystemTray(); // construct a TrayIcon - trayIcon = new TrayIcon(idleImage, resourceMap.getString("tooltipIdleText"), null); + trayIcon = new JPopupTrayIcon(idleImage, resourceMap.getString("tooltipIdleText"), popup); trayIcon.setImageAutoSize(true); trayIcon.addMouseListener(this); - // add listener - //tray.addPropertyChangeListener("trayIcons", this); - // add the tray image try { tray.add(trayIcon); @@ -338,7 +336,7 @@ trayIcon.setImage(idleDetectImage); } } - + /** * Called by main application UI after idle detect. */ @@ -359,49 +357,6 @@ } /* - * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent) - * - public void propertyChange(PropertyChangeEvent evt) { - - // get event name - String event = evt.getPropertyName(); - - if (log.isDebugEnabled()) { - log.debug("Property change on system tray : " + event); - } - - if (evt.getNewValue() == null) { - if (log.isDebugEnabled()) { - log.debug("Disable tray icon"); - } - trayIcon = null; - } - else { - SystemTray tray = (SystemTray)evt.getSource(); - - boolean alreadyInTray = false; - TrayIcon[] installedIcons = tray.getTrayIcons(); - for (TrayIcon installedIcon : installedIcons) { - if (installedIcon.equals(trayIcon)) { - alreadyInTray = true; - } - } - - if (alreadyInTray) { - if (log.isDebugEnabled()) { - log.debug("Tray icon already installed"); - } - } - else { - if (log.isDebugEnabled()) { - log.debug("Install tray icons"); - } - EventQueue.invokeLater(this); - } - } - }/ - - /* * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent) */ public void actionPerformed(ActionEvent e) { @@ -537,12 +492,6 @@ @Override public void mouseReleased(MouseEvent e) { - // use sun workarround http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6285881 - if (e.getClickCount() == 1 && e.getButton() == MouseEvent.BUTTON3) { - popup.setLocation(e.getX(), e.getY()); - popup.setInvoker(popup); - popup.setVisible(true); - } } /* Added: trunk/src/main/java/org/chorem/jtimer/ui/widget/JPopupTrayIcon.java =================================================================== --- trunk/src/main/java/org/chorem/jtimer/ui/widget/JPopupTrayIcon.java (rev 0) +++ trunk/src/main/java/org/chorem/jtimer/ui/widget/JPopupTrayIcon.java 2009-10-26 09:04:48 UTC (rev 2679) @@ -0,0 +1,178 @@ +/* *##% + * Copyright (C) 2009 Code Lutin + * + * This program 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. + * + * 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.chorem.jtimer.ui.widget; + +import java.awt.Dimension; +import java.awt.Frame; +import java.awt.GraphicsEnvironment; +import java.awt.Image; +import java.awt.Point; +import java.awt.TrayIcon; +import java.awt.Window; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; + +import javax.swing.JDialog; +import javax.swing.JPopupMenu; +import javax.swing.JWindow; +import javax.swing.RootPaneContainer; +import javax.swing.event.PopupMenuEvent; +import javax.swing.event.PopupMenuListener; + +/** + * JPopupMenu compatible TrayIcon based on Alexander Potochkin's JXTrayIcon + * (http://weblogs.java.net/blog/alexfromsun/archive/2008/02/jtrayicon_updat.htm...) + * but uses a JWindow instead of a JDialog to workaround some bugs on linux. + * + * Use code based from, with some modifications : + * https://fishfarm.dev.java.net/source/browse/fishfarm/trunk/FishFarm/src/net/java/fishfarm/ui/JPopupTrayIcon.java?rev=198&view=markup + * + * @author Michael Bien + * @author Chatellier Eric + */ +public class JPopupTrayIcon extends TrayIcon implements MouseListener, + PopupMenuListener { + + private JPopupMenu menu; + + private Window window; + + private final static boolean IS_WINDOWS = System.getProperty("os.name") + .toLowerCase().contains("windows"); + + public JPopupTrayIcon(Image image, String tooltip, JPopupMenu popup) { + super(image, tooltip); + addMouseListener(this); + setJPopupMenu(popup); + } + + protected void showJPopupMenu(MouseEvent e) { + if (e.isPopupTrigger() && menu != null) { + if (window == null) { + + if (IS_WINDOWS) { + window = new JDialog((Frame) null); + ((JDialog) window).setUndecorated(true); + } else { + window = new JWindow((Frame) null); + } + window.setAlwaysOnTop(true); + Dimension size = menu.getPreferredSize(); + + Point centerPoint = GraphicsEnvironment + .getLocalGraphicsEnvironment().getCenterPoint(); + if (e.getY() > centerPoint.getY()) { + window.setLocation(e.getX(), e.getY() - size.height); + } else { + window.setLocation(e.getX(), e.getY()); + } + + window.setVisible(true); + + menu.show(((RootPaneContainer) window).getContentPane(), 0, 0); + + // popup works only for focused windows + window.toFront(); + } + } + } + + public JPopupMenu getJPopupMenu() { + return menu; + } + + public void setJPopupMenu(JPopupMenu menu) { + if (this.menu != null) { + this.menu.removePopupMenuListener(this); + } + this.menu = menu; + menu.addPopupMenuListener(this); + } + + /* + * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeVisible(javax.swing.event.PopupMenuEvent) + */ + @Override + public void popupMenuWillBecomeVisible(PopupMenuEvent e) { + + } + + /* + * @see javax.swing.event.PopupMenuListener#popupMenuWillBecomeInvisible(javax.swing.event.PopupMenuEvent) + */ + @Override + public void popupMenuWillBecomeInvisible(PopupMenuEvent e) { + if (window != null) { + window.dispose(); + window = null; + } + } + + /* + * @see javax.swing.event.PopupMenuListener#popupMenuCanceled(javax.swing.event.PopupMenuEvent) + */ + @Override + public void popupMenuCanceled(PopupMenuEvent e) { + if (window != null) { + window.dispose(); + window = null; + } + } + + /* + * @see java.awt.event.MouseListener#mouseClicked(java.awt.event.MouseEvent) + */ + @Override + public void mouseClicked(MouseEvent e) { + + } + + /* + * @see java.awt.event.MouseListener#mouseEntered(java.awt.event.MouseEvent) + */ + @Override + public void mouseEntered(MouseEvent e) { + + } + + /* + * @see java.awt.event.MouseListener#mouseExited(java.awt.event.MouseEvent) + */ + @Override + public void mouseExited(MouseEvent e) { + + } + + /* + * @see java.awt.event.MouseListener#mousePressed(java.awt.event.MouseEvent) + */ + @Override + public void mousePressed(MouseEvent e) { + showJPopupMenu(e); + + } + + /* + * @see java.awt.event.MouseListener#mouseReleased(java.awt.event.MouseEvent) + */ + @Override + public void mouseReleased(MouseEvent e) { + showJPopupMenu(e); + } +} Property changes on: trunk/src/main/java/org/chorem/jtimer/ui/widget/JPopupTrayIcon.java ___________________________________________________________________ Added: svn:keywords + "Author Date Id Revision HeadURL"
participants (1)
-
echatellier@users.chorem.org