Author: jflesch
Date: 2007-06-23 21:06:34 +0000 (Sat, 23 Jun 2007)
New Revision: 13742

Added:
   trunk/apps/Thaw/src/thaw/gui/SysTrayIcon.java
   trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java
Modified:
   trunk/apps/Thaw/src/thaw/core/MainWindow.java
   trunk/apps/Thaw/src/thaw/core/PluginManager.java
   trunk/apps/Thaw/src/thaw/gui/Table.java
   trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw.properties
   trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
Log:
Add a basic tray icon

Modified: trunk/apps/Thaw/src/thaw/core/MainWindow.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/MainWindow.java       2007-06-23 20:47:40 UTC 
(rev 13741)
+++ trunk/apps/Thaw/src/thaw/core/MainWindow.java       2007-06-23 21:06:34 UTC 
(rev 13742)
@@ -20,8 +20,8 @@
 import javax.swing.JToolBar;
 import javax.swing.JTextField;
 import javax.swing.WindowConstants;
+import java.awt.event.WindowListener;

-
 import thaw.gui.TabbedPane;
 import thaw.gui.IconBox;

@@ -51,7 +51,7 @@
  *
  * @author <a href="mailto:jflesch at nerim.net">Jerome Flesch</a>
  */
-public class MainWindow implements java.awt.event.ActionListener, 
java.awt.event.WindowListener,
+public class MainWindow implements java.awt.event.ActionListener, 
WindowListener,
                                   java.util.Observer {

        public final static int DEFAULT_SIZE_X = 790;
@@ -208,6 +208,15 @@
        }


+       public void addWindowListener(WindowListener wl) {
+               mainWindow.addWindowListener(wl);
+       }
+
+       public void removeWindowListener(WindowListener wl) {
+               mainWindow.removeWindowListener(wl);
+       }
+
+
        public void connectionHasChanged() {
                core.getConnectionManager().addObserver(this);
        }
@@ -222,6 +231,19 @@
        }


+       public boolean isVisible() {
+               return mainWindow.isVisible();
+       }
+
+       public void setIconified() {
+               mainWindow.setExtendedState(JFrame.ICONIFIED);
+       }
+
+       public void setNonIconified() {
+               mainWindow.setExtendedState(JFrame.NORMAL);
+       }
+
+
        public JFrame getMainFrame() {
                return mainWindow;
        }
@@ -550,7 +572,7 @@

        public void showDialogAbout() {
                final JComponent[] labels = new JComponent[] {
-                       null,
+                       new JTextField("Thaw "+Main.VERSION),
                        new JLabel(I18n.getMessage("thaw.about.l2")),
                        new JLabel(I18n.getMessage("thaw.about.l3")),
                        new JLabel(I18n.getMessage("thaw.about.l4")),
@@ -569,8 +591,6 @@
                }
                */

-
-               labels[0] = new JTextField("Thaw "+Main.VERSION);
                ((JTextField)labels[0]).setFont(new Font("Dialog", Font.BOLD, 
30));
                ((JTextField)labels[0]).setEditable(false);


Modified: trunk/apps/Thaw/src/thaw/core/PluginManager.java
===================================================================
--- trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-06-23 20:47:40 UTC 
(rev 13741)
+++ trunk/apps/Thaw/src/thaw/core/PluginManager.java    2007-06-23 21:06:34 UTC 
(rev 13742)
@@ -26,6 +26,7 @@
                "thaw.plugins.FetchPlugin",
                "thaw.plugins.InsertPlugin",
                "thaw.plugins.StatusBar",
+               "thaw.plugins.TrayIcon",
                "thaw.plugins.ThemeSelector",
                "thaw.plugins.Hsqldb",
                "thaw.plugins.Signatures",
@@ -226,7 +227,8 @@
                        ((Plugin)plugins.get(className)).run(core);

                } catch(final Exception e) {
-                       Logger.warning(this, "runPlugin('"+className+"'): 
Exception: "+e);
+                       Logger.error(this, "runPlugin('"+className+"'): 
Exception: "+e);
+                       e.printStackTrace();
                        return false;
                }


Added: trunk/apps/Thaw/src/thaw/gui/SysTrayIcon.java
===================================================================
--- trunk/apps/Thaw/src/thaw/gui/SysTrayIcon.java                               
(rev 0)
+++ trunk/apps/Thaw/src/thaw/gui/SysTrayIcon.java       2007-06-23 21:06:34 UTC 
(rev 13742)
@@ -0,0 +1,133 @@
+package thaw.gui;
+
+import java.lang.reflect.Method;
+import javax.swing.ImageIcon;
+import java.awt.Image;
+import java.awt.event.MouseListener;
+import java.awt.PopupMenu;
+
+import thaw.core.Logger;
+
+/**
+ * Systray icon that must compile with java 1.4 AND 1.6
+ * Limitations:
+ * <ul>
+ * <li>Will only work with java 1.6 (will do nothing else)</li>
+ * <li>Only one icon</li>
+ * </ul>
+ * <br/>
+ */
+public class SysTrayIcon {
+       private Object systemTray;
+       private Object trayIcon;
+
+       public SysTrayIcon(ImageIcon icon) {
+               try {
+                       systemTray = 
Class.forName("java.awt.SystemTray").getMethod("getSystemTray", 
(Class[])null).invoke(null, (Object[])null);
+                       trayIcon = 
Class.forName("java.awt.TrayIcon").getConstructor(new Class[] {
+                               Image.class
+                       }).newInstance(new Object[] {
+                               icon.getImage()
+                       });
+
+                       
Class.forName("java.awt.TrayIcon").getMethod("setImageAutoSize", new Class[] {
+                               Boolean.TYPE
+                       }).invoke(trayIcon, new Object[] {
+                               new Boolean(true)
+                       });
+
+               } catch(Exception e) {
+                       Logger.notice(this, "Can't use Tray icon because: 
"+e.toString());
+                       if (e.getCause() != null)
+                               Logger.notice(this, "Cause: 
"+e.getCause().toString());
+                       Logger.notice(this, "Probably due to a JVM without the 
support for the tray icons");
+                       systemTray = null;
+                       trayIcon   = null;
+               }
+       }
+
+       public boolean canWork() {
+               return (systemTray != null);
+       }
+
+
+       public void setVisible(boolean v) {
+               if (!canWork())
+                       return;
+
+               String method = (v ? "add" : "remove");
+
+               try {
+                       Class.forName("java.awt.SystemTray").getMethod(method, 
new Class[] {
+                               Class.forName("java.awt.TrayIcon")
+                       }).invoke(systemTray, new Object[] {
+                               trayIcon
+                       });
+               } catch(Exception e) {
+                       Logger.warning(this, "Error while changing visibility 
of the icon : "+e.toString());
+               }
+       }
+
+
+       public void addMouseListener(MouseListener ml) {
+               if (!canWork())
+                       return;
+
+               try {
+                       
Class.forName("java.awt.TrayIcon").getMethod("addMouseListener", new Class[] {
+                               MouseListener.class
+                       }).invoke(trayIcon, new Object[] {
+                               ml
+                       });
+               } catch(Exception e) {
+                       Logger.warning(this, "Error while adding mouse listener 
: "+e.toString());
+               }
+       }
+
+       public void removeMouseListener(MouseListener ml) {
+               if (!canWork())
+                       return;
+
+               try {
+                       
Class.forName("java.awt.TrayIcon").getMethod("removeMouseListener", new Class[] 
{
+                               MouseListener.class
+                       }).invoke(trayIcon, new Object[] {
+                               ml
+                       });
+               } catch(Exception e) {
+                       Logger.warning(this, "Error while removing mouse 
listener : "+e.toString());
+               }
+       }
+
+       public void setToolTip(String tt) {
+               if (!canWork())
+                       return;
+
+               try {
+                       
Class.forName("java.awt.TrayIcon").getMethod("setToolTip", new Class[] {
+                               String.class
+                       }).invoke(trayIcon, new Object[] {
+                               tt
+                       });
+               } catch(Exception e) {
+                       Logger.warning(this, "Error while setting tooltip : 
"+e.toString());
+               }
+       }
+
+
+       public void setPopupMenu(PopupMenu m) {
+               if (!canWork())
+                       return;
+
+               try {
+                       
Class.forName("java.awt.TrayIcon").getMethod("setPopupMenu", new Class[] {
+                               PopupMenu.class
+                       }).invoke(trayIcon, new Object[] {
+                               m
+                       });
+               } catch(Exception e) {
+                       Logger.warning(this, "Error while setting popup menu : 
"+e.toString());
+               }
+       }
+
+}

Modified: trunk/apps/Thaw/src/thaw/gui/Table.java
===================================================================
--- trunk/apps/Thaw/src/thaw/gui/Table.java     2007-06-23 20:47:40 UTC (rev 
13741)
+++ trunk/apps/Thaw/src/thaw/gui/Table.java     2007-06-23 21:06:34 UTC (rev 
13742)
@@ -290,7 +290,7 @@
                        TableColumn c = m.getColumn(i);
                        String size = 
config.getValue(configPrefix+"_col_width_"+Integer.toString(i));

-                       if (size != null) {
+                       if (size != null && !("".equals(size))) {
                                c.setPreferredWidth(Integer.parseInt(size));
                        }
                }

Modified: trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2007-06-23 
20:47:40 UTC (rev 13741)
+++ trunk/apps/Thaw/src/thaw/i18n/source.thaw_fr.properties     2007-06-23 
21:06:34 UTC (rev 13742)
@@ -485,4 +485,4 @@
 thaw.plugin.transferLogs.chooseFile=Choisisez un fichier
 thaw.plugin.transferLogs.importedKey=Clef import?e

-
+thaw.plugin.trayIcon.pluginName=Ic?ne systray

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2007-06-23 20:47:40 UTC 
(rev 13741)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw.properties       2007-06-23 21:06:34 UTC 
(rev 13742)
@@ -495,3 +495,6 @@
 thaw.plugin.transferLogs.exportKeys=Export key list
 thaw.plugin.transferLogs.chooseFile=Choose a file
 thaw.plugin.transferLogs.importedKey=Imported key
+
+
+thaw.plugin.trayIcon.pluginName=Tray icon

Modified: trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties
===================================================================
--- trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2007-06-23 20:47:40 UTC 
(rev 13741)
+++ trunk/apps/Thaw/src/thaw/i18n/thaw_fr.properties    2007-06-23 21:06:34 UTC 
(rev 13742)
@@ -485,4 +485,4 @@
 thaw.plugin.transferLogs.chooseFile=Choisisez un fichier
 thaw.plugin.transferLogs.importedKey=Clef import\u00e9e

-
+thaw.plugin.trayIcon.pluginName=Ic\u00f4ne systray

Added: trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java
===================================================================
--- trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java                              
(rev 0)
+++ trunk/apps/Thaw/src/thaw/plugins/TrayIcon.java      2007-06-23 21:06:34 UTC 
(rev 13742)
@@ -0,0 +1,88 @@
+package thaw.plugins;
+
+import java.awt.event.MouseListener;
+import java.awt.event.MouseEvent;
+import java.awt.event.WindowListener;
+import java.awt.event.WindowEvent;
+
+import thaw.core.Core;
+import thaw.core.Logger;
+import thaw.core.I18n;
+
+import thaw.gui.SysTrayIcon;
+
+
+public class TrayIcon implements thaw.core.Plugin, MouseListener, 
WindowListener {
+       private Core core;
+       private SysTrayIcon icon;
+
+
+       public TrayIcon() {
+
+       }
+
+
+       public boolean run(Core core) {
+               this.core = core;
+
+               icon = new SysTrayIcon(thaw.gui.IconBox.blueBunny);
+               icon.setToolTip("Thaw "+thaw.core.Main.VERSION);
+               icon.addMouseListener(this);
+
+               core.getMainWindow().addWindowListener(this);
+
+               icon.setVisible(true);
+
+               return true;
+       }
+
+
+       public boolean stop() {
+               core.getMainWindow().addWindowListener(this);
+               icon.removeMouseListener(this);
+
+               icon.setVisible(false);
+
+               return true;
+       }
+
+       public String getNameForUser() {
+               return I18n.getMessage("thaw.plugin.trayIcon.pluginName");
+       }
+
+       public javax.swing.ImageIcon getIcon() {
+               return thaw.gui.IconBox.blueBunny;
+       }
+
+       public void switchMainWindowVisibility() {
+               boolean v = !core.getMainWindow().isVisible();
+
+               core.getMainWindow().setNonIconified();
+
+               core.getMainWindow().setVisible(v);
+
+               core.getMainWindow().setNonIconified();
+       }
+
+       public void windowActivated(WindowEvent e) { }
+       public void windowClosed(WindowEvent e) { }
+       public void windowClosing(WindowEvent e) { }
+       public void windowDeactivated(WindowEvent e) { }
+       public void windowDeiconified(WindowEvent e) { }
+
+        public void windowIconified(WindowEvent e) {
+               switchMainWindowVisibility();
+       }
+
+       public void windowOpened(WindowEvent e) { }
+
+
+       public void mouseClicked(MouseEvent e) {
+               switchMainWindowVisibility();
+       }
+
+       public void mouseEntered(MouseEvent e) { }
+       public void mouseExited(MouseEvent e) { }
+       public void mousePressed(MouseEvent e) { }
+       public void mouseReleased(MouseEvent e) { }
+}


Reply via email to