2014-11-03 02:11:03 +01:00
|
|
|
package dorkbox.util.tray;
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
import java.awt.Dimension;
|
|
|
|
import java.awt.MouseInfo;
|
|
|
|
import java.awt.Point;
|
2014-11-03 02:11:03 +01:00
|
|
|
import java.awt.event.MouseAdapter;
|
|
|
|
import java.awt.event.MouseEvent;
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
import javax.swing.JPopupMenu;
|
|
|
|
import javax.swing.SwingUtilities;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
|
|
|
import dorkbox.util.DelayTimer;
|
|
|
|
|
|
|
|
public class SystemTrayMenuPopup extends JPopupMenu {
|
2014-11-24 17:40:06 +01:00
|
|
|
private static final long serialVersionUID = 1L;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
private DelayTimer timer;
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
public SystemTrayMenuPopup() {
|
|
|
|
super();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
this.timer = new DelayTimer("PopupMenuHider", true, new DelayTimer.Callback() {
|
|
|
|
@Override
|
|
|
|
public void execute() {
|
|
|
|
SwingUtilities.invokeLater(new Runnable() {
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
Point location = MouseInfo.getPointerInfo().getLocation();
|
|
|
|
Point locationOnScreen = getLocationOnScreen();
|
|
|
|
Dimension size = getSize();
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
if (location.x >= locationOnScreen.x && location.x < locationOnScreen.x + size.width
|
|
|
|
&& location.y >= locationOnScreen.y && location.y < locationOnScreen.y + size.height) {
|
2014-11-03 02:11:03 +01:00
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
|
|
|
} else {
|
|
|
|
setVisible(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
addMouseListener(new MouseAdapter() {
|
|
|
|
@Override
|
|
|
|
public void mouseExited(MouseEvent event) {
|
|
|
|
// wait before checking if mouse is still on the menu
|
|
|
|
SystemTrayMenuPopup.this.timer.delay(SystemTrayMenuPopup.this.timer.getDelay());
|
2014-11-03 02:11:03 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-11-24 17:40:06 +01:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setVisible(boolean b) {
|
|
|
|
this.timer.cancel();
|
|
|
|
|
|
|
|
if (b) {
|
|
|
|
// if the mouse isn't inside the popup in 5 seconds, close the popup
|
|
|
|
this.timer.delay(5000L);
|
|
|
|
}
|
|
|
|
|
|
|
|
super.setVisible(b);
|
|
|
|
}
|
2014-11-16 22:01:27 +01:00
|
|
|
}
|