|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
// yacyTray.java
|
|
|
|
|
// (C) 2008 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
|
|
|
|
|
//
|
|
|
|
|
// This is a part of YaCy, a peer-to-peer based web search engine
|
|
|
|
@ -26,63 +27,73 @@
|
|
|
|
|
|
|
|
|
|
package de.anomic.yacy;
|
|
|
|
|
|
|
|
|
|
import java.awt.Image;
|
|
|
|
|
import java.awt.MenuItem;
|
|
|
|
|
import java.awt.PopupMenu;
|
|
|
|
|
import java.awt.Toolkit;
|
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
import java.awt.event.ActionListener;
|
|
|
|
|
import java.awt.event.ItemEvent;
|
|
|
|
|
import java.awt.event.ItemListener;
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.lang.reflect.Constructor;
|
|
|
|
|
|
|
|
|
|
import javax.swing.ImageIcon;
|
|
|
|
|
import javax.swing.JMenuItem;
|
|
|
|
|
import javax.swing.JPopupMenu;
|
|
|
|
|
import javax.swing.UIManager;
|
|
|
|
|
|
|
|
|
|
import org.jdesktop.jdic.tray.SystemTray;
|
|
|
|
|
import org.jdesktop.jdic.tray.TrayIcon;
|
|
|
|
|
//import org.jdesktop.jdic.tray.SystemTray;
|
|
|
|
|
//import org.jdesktop.jdic.tray.TrayIcon;
|
|
|
|
|
|
|
|
|
|
import de.anomic.plasma.plasmaSwitchboard;
|
|
|
|
|
import de.anomic.server.serverSystem;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class yacyTray implements ActionListener, ItemListener {
|
|
|
|
|
private static final boolean testing = false;
|
|
|
|
|
public final class yacyTray {
|
|
|
|
|
private static final boolean testing = true;
|
|
|
|
|
|
|
|
|
|
plasmaSwitchboard sb;
|
|
|
|
|
private static plasmaSwitchboard sb;
|
|
|
|
|
|
|
|
|
|
public static boolean isShown = false;
|
|
|
|
|
public static boolean lockBrowserPopup = true;
|
|
|
|
|
|
|
|
|
|
private long t1;
|
|
|
|
|
private static long t1;
|
|
|
|
|
|
|
|
|
|
private static SystemTray tray;
|
|
|
|
|
private static TrayIcon ti;
|
|
|
|
|
|
|
|
|
|
public yacyTray(final plasmaSwitchboard sb, final boolean showmenu) {
|
|
|
|
|
this.sb = sb;
|
|
|
|
|
tray = SystemTray.getDefaultSystemTray();
|
|
|
|
|
private static nativTrayIcon ti;
|
|
|
|
|
|
|
|
|
|
public static void init(final plasmaSwitchboard par_sb) {
|
|
|
|
|
sb = par_sb;
|
|
|
|
|
try {
|
|
|
|
|
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
|
|
|
|
|
} catch (final Exception e) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
if( Integer.parseInt(System.getProperty("java.version").substring(2,3)) >=5 )
|
|
|
|
|
System.setProperty("javax.swing.adjustPopupLocationToFit", "false");
|
|
|
|
|
|
|
|
|
|
JPopupMenu menu;
|
|
|
|
|
JMenuItem menuItem;
|
|
|
|
|
final boolean trayIcon = sb.getConfig("trayIcon", "false").equals("true");
|
|
|
|
|
if (trayIcon) {
|
|
|
|
|
System.setProperty("java.awt.headless", "false");
|
|
|
|
|
|
|
|
|
|
if(nativTrayIcon.isSupported()) {
|
|
|
|
|
final String iconpath = sb.getRootPath().toString() + "/addon/YaCy_TrayIcon.gif".replace("/", File.separator);
|
|
|
|
|
final ImageIcon i = new ImageIcon(iconpath);
|
|
|
|
|
ActionListener al = new ActionListener() {
|
|
|
|
|
public void actionPerformed(final ActionEvent e) {
|
|
|
|
|
trayClickAction();
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
ti = new nativTrayIcon(sb, iconpath, al, setupPopupMenu());
|
|
|
|
|
|
|
|
|
|
ti.addToSystemTray();
|
|
|
|
|
isShown = true;
|
|
|
|
|
} else {
|
|
|
|
|
System.setProperty("java.awt.headless", "true");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (final Exception e) {
|
|
|
|
|
System.setProperty("java.awt.headless", "true");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// the menu is disabled because of visibility conflicts on windows with jre6
|
|
|
|
|
// anyway the code might be a template for future use
|
|
|
|
|
if (showmenu) {
|
|
|
|
|
public static PopupMenu setupPopupMenu() {
|
|
|
|
|
// this is the popup menu
|
|
|
|
|
menu = new JPopupMenu("YaCy");
|
|
|
|
|
PopupMenu menu = new PopupMenu("YaCy");
|
|
|
|
|
MenuItem menuItem;
|
|
|
|
|
|
|
|
|
|
// YaCy Search
|
|
|
|
|
menuItem = new JMenuItem("YaCy Search");
|
|
|
|
|
menuItem = new MenuItem("YaCy Search");
|
|
|
|
|
menuItem.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(final ActionEvent e) {
|
|
|
|
|
openBrowser("");
|
|
|
|
@ -91,37 +102,22 @@ public class yacyTray implements ActionListener, ItemListener {
|
|
|
|
|
menu.add(menuItem);
|
|
|
|
|
|
|
|
|
|
// Quit
|
|
|
|
|
if (testing) {
|
|
|
|
|
menu.addSeparator();
|
|
|
|
|
menuItem = new JMenuItem("Quit");
|
|
|
|
|
menuItem = new MenuItem("Quit");
|
|
|
|
|
menuItem.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(final ActionEvent e) {
|
|
|
|
|
System.exit(0);
|
|
|
|
|
sb.terminate();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
menu.add(menuItem);
|
|
|
|
|
return menu;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Tray Icon
|
|
|
|
|
ti = new TrayIcon(i, "YaCy", menu);
|
|
|
|
|
} else {
|
|
|
|
|
ti = new TrayIcon(i, "YaCy");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ti.setIconAutoSize(true);
|
|
|
|
|
ti.addActionListener(new ActionListener() {
|
|
|
|
|
public void actionPerformed(final ActionEvent e) {
|
|
|
|
|
trayClickAction();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
tray.addTrayIcon(ti);
|
|
|
|
|
isShown = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void trayClickAction(){ //detect doubleclick
|
|
|
|
|
private static void trayClickAction(){ //detect doubleclick
|
|
|
|
|
if(System.currentTimeMillis() - t1 < 500){
|
|
|
|
|
if (lockBrowserPopup) {
|
|
|
|
|
displayBalloonMessage("YaCy","Please wait until YaCy is started.");
|
|
|
|
|
ti.displayBalloonMessage("YaCy","Please wait until YaCy is started.");
|
|
|
|
|
} else {
|
|
|
|
|
openBrowser("");
|
|
|
|
|
}
|
|
|
|
@ -129,22 +125,137 @@ public class yacyTray implements ActionListener, ItemListener {
|
|
|
|
|
} else { t1 = System.currentTimeMillis(); }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void openBrowser(final String browserPopUpPage){
|
|
|
|
|
private static void openBrowser(final String browserPopUpPage){
|
|
|
|
|
// no need for https, because we are on localhost
|
|
|
|
|
serverSystem.openBrowser("http://localhost:" + sb.getConfig("port", "8080") + "/" + browserPopUpPage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeTray(){
|
|
|
|
|
tray.removeTrayIcon(ti);
|
|
|
|
|
public static void removeTray(){
|
|
|
|
|
ti.removeFromSystemTray();
|
|
|
|
|
isShown = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void displayBalloonMessage(final String title, final String message){
|
|
|
|
|
ti.displayMessage(title, message, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void actionPerformed(final ActionEvent e) { }
|
|
|
|
|
class nativTrayIcon {
|
|
|
|
|
private plasmaSwitchboard sb;
|
|
|
|
|
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 nativTrayIcon(final plasmaSwitchboard sb, String IconPath, ActionListener al, PopupMenu menu) {
|
|
|
|
|
if(!this.isSupported())
|
|
|
|
|
return;
|
|
|
|
|
this.sb = sb;
|
|
|
|
|
|
|
|
|
|
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] = "YaCy";
|
|
|
|
|
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] = new 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);
|
|
|
|
|
|
|
|
|
|
public void itemStateChanged(final ItemEvent e) { }
|
|
|
|
|
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) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
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) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|