From c73af39e547b8d9d53ec9b6373563b66e7b9e274 Mon Sep 17 00:00:00 2001 From: Lotus Date: Wed, 18 Jan 2012 20:47:09 +0100 Subject: [PATCH] refactoring of tray icon class, now uses Java 6 methods natively --- defaults/yacy.init | 10 +- htroot/Status_p.inc | 2 +- source/net/yacy/gui/Tray.java | 315 ++++++------------ source/net/yacy/search/Switchboard.java | 5 +- .../net/yacy/search/SwitchboardConstants.java | 8 +- source/net/yacy/yacy.java | 4 +- 6 files changed, 118 insertions(+), 226 deletions(-) diff --git a/defaults/yacy.init b/defaults/yacy.init index d4172ce96..6c82ffeb6 100644 --- a/defaults/yacy.init +++ b/defaults/yacy.init @@ -464,9 +464,10 @@ browserPopUpPage=index.html indexForward = # defines if the YaCy icon appears in the system tray on supported platforms -trayIcon=true -trayIcon.force=false -tray.label=YaCy +tray.icon.enabled=true +tray.icon.force=false +tray.icon.label=YaCy +tray.menu.enabled=true # index sharing attributes: by default, sharing is on. # If you want to use YaCy only for local indexing (robinson mode), @@ -974,9 +975,6 @@ memory.standardStrategy = true cgi.allow = false cgi.suffixes = cgi,pl -# whether this is a version for a web browser -browserintegration = false - # content integration settings content.phpbb3.urlstub = http:/// content.phpbb3.dbtype = mysql diff --git a/htroot/Status_p.inc b/htroot/Status_p.inc index 4d7b261b6..92073901a 100644 --- a/htroot/Status_p.inc +++ b/htroot/Status_p.inc @@ -62,7 +62,7 @@ :: Enabled [Disable] :: - Experimental + Experimental #(/tray)# diff --git a/source/net/yacy/gui/Tray.java b/source/net/yacy/gui/Tray.java index 5f2b5e86e..6238711f9 100644 --- a/source/net/yacy/gui/Tray.java +++ b/source/net/yacy/gui/Tray.java @@ -1,5 +1,5 @@ -// yacyTray.java -// (C) 2008 by David Wieditz; d.wieditz@gmx.de +// Tray.java +// (C) 2008-2012 by David Wieditz; d.wieditz@gmx.de // (C) 2008 by Florian Richter; Florian_Richter@gmx.de // first published 13.07.2008 on http://yacy.net // @@ -30,15 +30,14 @@ package net.yacy.gui; import java.awt.Image; import java.awt.MenuItem; import java.awt.PopupMenu; +import java.awt.SystemTray; import java.awt.Toolkit; +import java.awt.TrayIcon; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; -import java.lang.reflect.Constructor; -import java.lang.reflect.Method; import net.yacy.gui.framework.Browser; -import net.yacy.kelondro.logging.Log; import net.yacy.kelondro.util.OS; import net.yacy.search.Switchboard; import net.yacy.search.SwitchboardConstants; @@ -46,38 +45,42 @@ import net.yacy.search.SwitchboardConstants; public final class Tray { - protected static Switchboard sb; - - private static nativeTrayIcon ti; - private static boolean isIntegrated; // browser integration - private static boolean isShown = false; + private Switchboard sb; + + private TrayIcon ti; + private String trayLabel; + final private static boolean deutsch = System.getProperty("user.language","").equals("de"); final private static boolean french = System.getProperty("user.language","").equals("fr"); - - public static String trayLabel; - - public static boolean lockBrowserPopup = true; - - - public static void init(final Switchboard par_sb) { - sb = par_sb; - isIntegrated = sb.getConfigBool(SwitchboardConstants.BROWSERINTEGRATION, false); - trayLabel = sb.getConfig(SwitchboardConstants.TRAY_LABEL, "YaCy"); + + // states + private boolean isShown = false; + private boolean appIsReady = false; + private boolean menuEnabled = true; + + public Tray(final Switchboard sb_par) { + sb = sb_par; + menuEnabled = sb.getConfigBool(SwitchboardConstants.TRAY_MENU_ENABLED, true); + trayLabel = sb.getConfig(SwitchboardConstants.TRAY_ICON_LABEL, "YaCy"); try { - final boolean trayIcon = sb.getConfigBool(SwitchboardConstants.TRAY_ICON_ENABLED, false); - if (trayIcon && (OS.isWindows || sb.getConfigBool(SwitchboardConstants.TRAY_ICON_FORCED, false))) { - System.setProperty("java.awt.headless", "false"); + final boolean trayIconEnabled = sb.getConfigBool(SwitchboardConstants.TRAY_ICON_ENABLED, false); + final boolean trayIconForced = sb.getConfigBool(SwitchboardConstants.TRAY_ICON_FORCED, false); + if (trayIconEnabled && (OS.isWindows || trayIconForced)) { + System.setProperty("java.awt.headless", "false"); // we have to switch off headless mode, else all will fail - if(nativeTrayIcon.isSupported()) { - final String iconpath = sb.getAppPath().toString() + "/addon/YaCy_TrayIcon.png".replace("/", File.separator); + if(SystemTray.isSupported()) { + final String iconPath = sb.getAppPath().toString() + "/addon/YaCy_TrayIcon.png".replace("/", File.separator); ActionListener al = new ActionListener() { public void actionPerformed(final ActionEvent e) { - trayClickAction(); + doubleClickAction(); } }; - ti = new nativeTrayIcon(iconpath, al, setupPopupMenu()); - - ti.addToSystemTray(); + final Image i = Toolkit.getDefaultToolkit().getImage(iconPath); + final PopupMenu menu = (menuEnabled) ? getPopupMenu() : null; + ti = new TrayIcon(i, trayLabel, menu); + ti.setImageAutoSize(true); + ti.addActionListener(al); + SystemTray.getSystemTray().add(ti); isShown = true; } else { System.setProperty("java.awt.headless", "true"); @@ -88,38 +91,77 @@ public final class Tray { } } - public static PopupMenu setupPopupMenu() { + /** + * set all functions available + */ + public void setReady() { + appIsReady = true; + } + + public void remove() { + if (isShown){ + SystemTray.getSystemTray().remove(ti); + ti = null; + isShown = false; + } + } + + private void doubleClickAction() { + if (!appIsReady) { + String label; + if (deutsch) + label = "Bitte warten bis YaCy gestartet ist."; + else if (french) + label = "S'il vous plaît attendre jusqu'à YaCy est démarré."; + else + label = "Please wait until YaCy is started."; + //ti.displayMessage("YaCy",label); + ti.displayMessage("YaCy", label, TrayIcon.MessageType.INFO); + } else { + openBrowserPage(""); + } + } + + /** + * + * @param browserPopUpPage relative path to the webserver root + */ + private void openBrowserPage(final String browserPopUpPage) { + if(!menuEnabled) return; + // no need for https, because we are on localhost + Browser.openBrowser("http://localhost:" + sb.getConfig("port", "8090") + "/" + browserPopUpPage); + } + + private PopupMenu getPopupMenu() { String label; - // this is the popup menu + PopupMenu menu = new PopupMenu(); MenuItem menuItem; - - if(isIntegrated) return menu; - + // YaCy Search if (deutsch) - label = "YaCy Suche"; - else if (french) - label = "YaCy Recherche"; - else - label = "YaCy Search"; + label = "YaCy Suche"; + else if (french) + label = "YaCy Recherche"; + else + label = "YaCy Search"; menuItem = new MenuItem(label); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { - openBrowser(""); + openBrowserPage(""); } }); menu.add(menuItem); - - /* - * no prominent compare since google can not be displayed in a frame anymore + + + /* no prominent compare since google can not be displayed in a frame anymore // Compare YaCy if (deutsch) - label = "Vergleichs-Suche"; - else if (french) - label = "Comparer YaCy"; - else - label = "Compare YaCy"; + label = "Vergleichs-Suche"; + else if (french) + label = "Comparer YaCy"; + else + label = "Compare YaCy"; menuItem = new MenuItem(label); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { @@ -127,33 +169,33 @@ public final class Tray { } }); menu.add(menuItem); - */ - + */ + // Peer Administration if (deutsch) - label = "Peer Administration"; - else if (french) - label = "Peer Administration"; - else - label = "Peer Administration"; + label = "Peer Administration"; + else if (french) + label = "Peer Administration"; + else + label = "Peer Administration"; menuItem = new MenuItem(label); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { - openBrowser("Status.html"); + openBrowserPage("Status.html"); } }); menu.add(menuItem); - + // Separator menu.addSeparator(); // Quit if(deutsch) - label = "YaCy Beenden"; - else if(french) - label = "Arrêt YaCy"; - else - label = "Shutdown YaCy"; + label = "YaCy Beenden"; + else if(french) + label = "Arrêt YaCy"; + else + label = "Shutdown YaCy"; menuItem = new MenuItem(label); menuItem.addActionListener(new ActionListener() { public void actionPerformed(final ActionEvent e) { @@ -163,154 +205,5 @@ public final class Tray { menu.add(menuItem); return menu; } - - - protected static void trayClickAction(){ //doubleclick - if (lockBrowserPopup) { - String label; - if (deutsch) - label = "Bitte warten bis YaCy gestartet ist."; - else if (french) - label = "S'il vous plaît attendre jusqu'à YaCy est démarré."; - else - label = "Please wait until YaCy is started."; - ti.displayBalloonMessage("YaCy",label); - } else { - openBrowser(""); - } - } - - protected static void openBrowser(final String browserPopUpPage){ - if(isIntegrated) return; - // no need for https, because we are on localhost - Browser.openBrowser("http://localhost:" + sb.getConfig("port", "8090") + "/" + browserPopUpPage); - } - - public static void removeTray(){ - if (isShown){ - ti.removeFromSystemTray(); - isShown = false; - } - } - -} - -class nativeTrayIcon { - private Object SystemTray; - private Object TrayIcon; - private Class SystemTrayClass; - private Class TrayIconClass; - - public static boolean isSupported() { - try { - Class l_SystemTrayClass = Class.forName("java.awt.SystemTray"); - //Object SystemTray = SystemTrayClass.newInsta - Method isSupportedMethod = l_SystemTrayClass.getMethod("isSupported", (Class[])null); - Boolean isSupported = (Boolean)isSupportedMethod.invoke(null, (Object[])null); - return isSupported; - } catch (Throwable e) { - return false; - } - - } - - public nativeTrayIcon(String IconPath, ActionListener al, PopupMenu menu) { - if(!isSupported()) return; - - final Image i = Toolkit.getDefaultToolkit().getImage(IconPath); - - try { - this.TrayIconClass = Class.forName("java.awt.TrayIcon"); - this.SystemTrayClass = Class.forName("java.awt.SystemTray"); - - // with reflections: this.TrayIcon = new TrayIcon(i, "YaCy"); - Class partypes1[] = new Class[3]; - partypes1[0] = Image.class; - partypes1[1] = String.class; - partypes1[2] = PopupMenu.class; - Constructor TrayIconConstructor = TrayIconClass.getConstructor(partypes1); - - Object arglist1[] = new Object[3]; - arglist1[0] = i; - arglist1[1] = Tray.trayLabel; - arglist1[2] = menu; - this.TrayIcon = TrayIconConstructor.newInstance(arglist1); - - // with reflections: this.TrayIcon.setImageAutoSize(true) - Class partypes2[] = new Class[1]; - partypes2[0] = Boolean.TYPE; - Method setImageAutoSizeMethod = TrayIconClass.getMethod("setImageAutoSize", partypes2); - - Object arglist2[] = new Object[1]; - arglist2[0] = Boolean.TRUE; - setImageAutoSizeMethod.invoke(this.TrayIcon, arglist2); - - // with reflections: this.TrayIcon.addActionListener(al) - Class partypes3[] = new Class[1]; - partypes3[0] = ActionListener.class; - Method addActionListenerMethod = TrayIconClass.getMethod("addActionListener", partypes3); - - Object arglist3[] = new Object[1]; - arglist3[0] = al; - addActionListenerMethod.invoke(this.TrayIcon, arglist3); - - // with reflections: nativSystemTray = SystemTray.getDefaultSystemTray() - Method getDefaultSystemTrayMethod = SystemTrayClass.getMethod("getSystemTray", (Class[])null); - this.SystemTray = getDefaultSystemTrayMethod.invoke(null, (Object[])null); - - } catch (Throwable e) { - Log.logException(e); - this.TrayIcon = null; - } - } - - public void addToSystemTray() { - try { - // with reflections: this.SystemTray.add(this.TrayIcon) - Class partypes1[] = new Class[1]; - partypes1[0] = TrayIconClass; - Method addMethod = SystemTrayClass.getMethod("add", partypes1); - - Object arglist1[] = new Object[1]; - arglist1[0] = this.TrayIcon; - addMethod.invoke(this.SystemTray, arglist1); - } catch (Throwable e) { - Log.logException(e); - } - } - - public void removeFromSystemTray() { - try { - // with reflections: this.SystemTray.remove(this.TrayIcon) - Class partypes1[] = new Class[1]; - partypes1[0] = TrayIconClass; - Method removeMethod = SystemTrayClass.getMethod("remove", partypes1); - - Object arglist1[] = new Object[1]; - arglist1[0] = this.TrayIcon; - removeMethod.invoke(this.SystemTray, arglist1); - } catch (Throwable e) { - Log.logException(e); - } - } - - public void displayBalloonMessage(final String title, final String message) { - try { - // with reflections: this.TrayIcon.displayBalloonMessage(title, message, TrayIcon.MessageType.NONE) - Class partypes1[] = new Class[3]; - partypes1[0] = String.class; - partypes1[1] = String.class; - partypes1[2] = Class.forName("java.awt.TrayIcon.MessageType"); - Method displayBalloonMessageMethod = TrayIconClass.getMethod("displayBalloonMessage", partypes1); - - Object arglist1[] = new Object[1]; - arglist1[0] = title; - arglist1[1] = message; - arglist1[2] = null; - displayBalloonMessageMethod.invoke(this.TrayIcon, arglist1); - } catch (Throwable e) { - Log.logException(e); - } - } } diff --git a/source/net/yacy/search/Switchboard.java b/source/net/yacy/search/Switchboard.java index 7f6af6cb7..4a34e54c8 100644 --- a/source/net/yacy/search/Switchboard.java +++ b/source/net/yacy/search/Switchboard.java @@ -243,6 +243,7 @@ public final class Switchboard extends serverSwitch public LinkedBlockingQueue trail; public SeedDB peers; public WorkTables tables; + public Tray tray; public WorkflowProcessor indexingDocumentProcessor; public WorkflowProcessor indexingCondensementProcessor; @@ -294,7 +295,7 @@ public final class Switchboard extends serverSwitch } // init TrayIcon if possible - Tray.init(this); + tray = new Tray(this); // remote proxy configuration initRemoteProxy(); @@ -1597,7 +1598,7 @@ public final class Switchboard extends serverSwitch Domains.close(); AccessTracker.dumpLog(new File("DATA/LOG/queries.log")); UPnP.deletePortMapping(); - Tray.removeTray(); + tray.remove(); try { HTTPClient.closeConnectionManager(); } catch ( final InterruptedException e ) { diff --git a/source/net/yacy/search/SwitchboardConstants.java b/source/net/yacy/search/SwitchboardConstants.java index 7442a7148..6af1a5b91 100644 --- a/source/net/yacy/search/SwitchboardConstants.java +++ b/source/net/yacy/search/SwitchboardConstants.java @@ -443,10 +443,10 @@ public final class SwitchboardConstants { /** * system tray */ - public static final String TRAY_ICON_ENABLED = "trayIcon"; - public static final String TRAY_ICON_FORCED = "trayIcon.force"; - public static final String TRAY_LABEL = "tray.label"; - public static final String BROWSERINTEGRATION = "browserintegration"; + public static final String TRAY_ICON_ENABLED = "tray.icon.enabled"; + public static final String TRAY_ICON_FORCED = "tray.icon.force"; + public static final String TRAY_ICON_LABEL = "tray.icon.label"; + public static final String TRAY_MENU_ENABLED = "tray.menu.enabled"; /** * Segments diff --git a/source/net/yacy/yacy.java b/source/net/yacy/yacy.java index 77d6a4861..85ef9bc8b 100644 --- a/source/net/yacy/yacy.java +++ b/source/net/yacy/yacy.java @@ -330,8 +330,8 @@ public final class yacy { //Log.logException(e); } - // unlock yacyTray browser popup - Tray.lockBrowserPopup = false; + // enable browser popup, http server is ready now + sb.tray.setReady(); //regenerate Locales from Translationlist, if needed final File locale_source = sb.getAppPath("locale.source", "locales");