trayicon for sun java 6 use now a fancy jpopupmenu instead of a old

stylish awt popupmenu
cusax-fix
Damien Roth 17 years ago
parent 0c1458eb17
commit 4448bf0d83

@ -192,7 +192,7 @@ public TrayIconPeer createTrayIcon(ImageIcon icon, String tooltip,
SecurityException
{
return new AWTTrayIconPeer(trayIconClass, (icon == null) ? null
: icon.getImage(), tooltip, (PopupMenu) popup);
: icon.getImage(), tooltip, (JPopupMenu) popup);
}
public boolean isSwing()

@ -11,7 +11,10 @@
import java.lang.reflect.*;
import javax.swing.*;
import net.java.sip.communicator.impl.systray.*;
import net.java.sip.communicator.impl.systray.jdic.SystemTray.*;
import net.java.sip.communicator.service.gui.*;
/**
* @author Lubomir Marinov
@ -100,6 +103,8 @@ static class AWTTrayIconPeer
implements TrayIconPeer
{
private final Method addActionListener;
private final Method addMouseListener;
private final Method displayMessage;
@ -112,7 +117,7 @@ static class AWTTrayIconPeer
private final Method setIconAutoSize;
public AWTTrayIconPeer(Class<?> clazz, Image image, String tooltip,
PopupMenu popup)
JPopupMenu popup)
throws IllegalArgumentException,
UnsupportedOperationException,
HeadlessException,
@ -122,10 +127,13 @@ public AWTTrayIconPeer(Class<?> clazz, Image image, String tooltip,
try
{
constructor = clazz.getConstructor(new Class<?>[]
{ Image.class, String.class, PopupMenu.class });
{ Image.class, String.class });
addActionListener =
clazz.getMethod("addActionListener", new Class<?>[]
{ ActionListener.class });
addMouseListener =
clazz.getMethod("addMouseListener", new Class<?>[]
{ MouseListener.class });
messageTypeClass =
Class.forName("java.awt.TrayIcon$MessageType");
displayMessage =
@ -149,7 +157,9 @@ public AWTTrayIconPeer(Class<?> clazz, Image image, String tooltip,
try
{
impl = constructor.newInstance(new Object[]
{ image, tooltip, popup });
{ image, tooltip });
addMouseListener(new AWTMouseAdapter(popup));
}
catch (IllegalAccessException ex)
{
@ -194,6 +204,24 @@ public void addActionListener(ActionListener listener)
: cause);
}
}
public void addMouseListener(MouseListener listener)
{
try
{
addMouseListener.invoke(getImpl(), new Object[] { listener });
}
catch (IllegalAccessException ex)
{
throw new UndeclaredThrowableException(ex);
}
catch (InvocationTargetException ex)
{
Throwable cause = ex.getCause();
throw new UndeclaredThrowableException((cause == null) ? ex
: cause);
}
}
public void addBalloonActionListener(ActionListener listener)
{
@ -339,4 +367,69 @@ public void setIconAutoSize(boolean autoSize)
getImpl().setIconAutoSize(autoSize);
}
}
/**
* Extended mouse adapter to show the JpopupMenu in java 6
* Based on : http://weblogs.java.net/blog/ixmal/archive/2006/05/using_jpopupmen.html
*
* @author Damien Roth
*/
static class AWTMouseAdapter
extends MouseAdapter
{
private static int MARGIN = 15;
private JPopupMenu popup;
private boolean popupVisible = false;
private Component invoker;
public AWTMouseAdapter(JPopupMenu p)
{
this.popup = p;
// Get the MainFrame
ExportedWindow win = SystrayActivator.getUIService()
.getExportedWindow(ExportedWindow.MAIN_WINDOW);
/*
* The JPopupMenu need to have a invoker defined to work correctly.
* It can be the popup menu itself, but if the mainframe is used, the popup
* automatically close when the mainframe lost the focus
*/
if(win == null || win.getSource() == null || !(win.getSource() instanceof JFrame))
this.invoker = p;
else
this.invoker = (Component) win.getSource();
}
public void mouseReleased(MouseEvent e)
{
// Trick used here, the isVisible function always return false
// So we manage this manually
if (e.getButton() == MouseEvent.BUTTON3 && !popupVisible)
{
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
int x = e.getX(), y = e.getY();
// Use a margin to avoid the menu hiding the icon
if (x < MARGIN)
x = MARGIN;
else if (x > screen.getWidth()-MARGIN)
x = (int) screen.getWidth()-MARGIN;
if (y < MARGIN)
y = MARGIN;
else if (y > screen.getHeight()-MARGIN)
y = (int) screen.getWidth()-MARGIN;
popup.setLocation(x, y);
popup.setInvoker(invoker);
popup.setVisible(true);
popupVisible = true;
}
else
{
popup.setVisible(false);
popupVisible = false;
}
}
}
}

@ -96,7 +96,8 @@ private static void addSeparator(Object trayMenu)
public static Object createTrayMenu(SystrayServiceJdicImpl tray, boolean swing)
{
Object trayMenu = swing ? new JPopupMenu() : new PopupMenu();
swing = true;
JPopupMenu trayMenu = new JPopupMenu();
ActionListener listener = new ActionListener()
{
public void actionPerformed(ActionEvent event)
@ -124,22 +125,11 @@ private static Object createTrayMenuItem(String name, String textID, String icon
ActionListener listener, boolean swing)
{
String text = Resources.getString(textID);
Object trayMenuItem;
if (swing)
{
JMenuItem menuItem = new JMenuItem(text, Resources.getImage(iconID));
menuItem.setName(name);
menuItem.addActionListener(listener);
trayMenuItem = menuItem;
}
else
{
MenuItem menuItem = new MenuItem(text);
menuItem.setName(name);
menuItem.addActionListener(listener);
trayMenuItem = menuItem;
}
return trayMenuItem;
JMenuItem menuItem = new JMenuItem(text, Resources.getImage(iconID));
menuItem.setName(name);
menuItem.addActionListener(listener);
return menuItem;
}
public static boolean isVisible(Object trayMenu)

Loading…
Cancel
Save