From ceb9bfcc07ec65ac6baa754cd5d23546471b9e5e Mon Sep 17 00:00:00 2001 From: mathiasdm <mathiasdm@mail.i2p> Date: Tue, 14 Dec 2010 07:19:37 +0000 Subject: [PATCH] Refactoring and splitting logic from interface. --- apps/desktopgui/TODO | 3 +- .../i2p/desktopgui/ExternalTrayManager.java | 52 +++++++ .../i2p/desktopgui/InternalTrayManager.java | 93 ++++++++++++ .../src/net/i2p/desktopgui/TrayManager.java | 136 ++---------------- .../i2p/desktopgui/util/BrowseException.java | 23 +++ .../net/i2p/desktopgui/util/I2PDesktop.java | 35 +++++ 6 files changed, 220 insertions(+), 122 deletions(-) create mode 100644 apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java create mode 100644 apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java create mode 100644 apps/desktopgui/src/net/i2p/desktopgui/util/BrowseException.java create mode 100644 apps/desktopgui/src/net/i2p/desktopgui/util/I2PDesktop.java diff --git a/apps/desktopgui/TODO b/apps/desktopgui/TODO index 5f38de78ad..98e768db99 100644 --- a/apps/desktopgui/TODO +++ b/apps/desktopgui/TODO @@ -2,12 +2,13 @@ HIGH PRIORITY: - Allow desktopgui to start, stop and restart I2P. - DONE - Correct logging system - DONE - Internationalisation: - * Add strings + * Add strings - DONE * Add Windows support * Might need some kind of trigger to reload the menu (for live language switching) * Language choice is not actually set as a parameter in I2P config? As a result, desktopgui always starts with the default, unless you manually set the language. - Modify installer to set I2P directory parameter; or use $I2P? +- Fix tabs versus spaces ;-) UNKNOWN: - API to allow applications to add themselves to the menu? * registerApplication(); -- should return a positive number on success, -1 on failure diff --git a/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java new file mode 100644 index 0000000000..702807ba2a --- /dev/null +++ b/apps/desktopgui/src/net/i2p/desktopgui/ExternalTrayManager.java @@ -0,0 +1,52 @@ +package net.i2p.desktopgui; + +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.TrayIcon; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.SwingWorker; + +import net.i2p.desktopgui.router.RouterManager; +import net.i2p.util.Log; + +public class ExternalTrayManager extends TrayManager { + + private final static Log log = new Log(ExternalTrayManager.class); + + protected ExternalTrayManager() {} + + @Override + public PopupMenu getMainMenu() { + PopupMenu popup = new PopupMenu(); + MenuItem startItem = new MenuItem(_("Start I2P")); + startItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + new SwingWorker<Object, Object>() { + + @Override + protected Object doInBackground() throws Exception { + RouterManager.start(); + return null; + } + + @Override + protected void done() { + trayIcon.displayMessage(_("Starting"), _("I2P is starting!"), TrayIcon.MessageType.INFO); + //Hide the tray icon. + //We cannot stop the desktopgui program entirely, + //since that risks killing the I2P process as well. + tray.remove(trayIcon); + } + + }.execute(); + } + + }); + popup.add(startItem); + return popup; + } +} diff --git a/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java new file mode 100644 index 0000000000..efa7e31a1b --- /dev/null +++ b/apps/desktopgui/src/net/i2p/desktopgui/InternalTrayManager.java @@ -0,0 +1,93 @@ +package net.i2p.desktopgui; + +import java.awt.MenuItem; +import java.awt.PopupMenu; +import java.awt.event.ActionEvent; +import java.awt.event.ActionListener; + +import javax.swing.SwingWorker; + +import net.i2p.desktopgui.router.RouterManager; +import net.i2p.desktopgui.util.BrowseException; +import net.i2p.desktopgui.util.I2PDesktop; +import net.i2p.util.Log; + +public class InternalTrayManager extends TrayManager { + + private final static Log log = new Log(InternalTrayManager.class); + + protected InternalTrayManager() {} + + @Override + public PopupMenu getMainMenu() { + PopupMenu popup = new PopupMenu(); + + MenuItem browserLauncher = new MenuItem(_("Launch I2P Browser")); + browserLauncher.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + new SwingWorker<Object, Object>() { + + @Override + protected Object doInBackground() throws Exception { + return null; + } + + @Override + protected void done() { + try { + I2PDesktop.browse("http://localhost:7657"); + } catch (BrowseException e1) { + log.log(Log.WARN, "Failed to open browser!", e1); + } + } + + }.execute(); + } + }); + MenuItem restartItem = new MenuItem(_("Restart I2P")); + restartItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + new SwingWorker<Object, Object>() { + + @Override + protected Object doInBackground() throws Exception { + RouterManager.restart(); + return null; + } + + }.execute(); + + } + + }); + MenuItem stopItem = new MenuItem(_("Stop I2P")); + stopItem.addActionListener(new ActionListener() { + + @Override + public void actionPerformed(ActionEvent arg0) { + new SwingWorker<Object, Object>() { + + @Override + protected Object doInBackground() throws Exception { + RouterManager.shutDown(); + return null; + } + + }.execute(); + + } + + }); + + popup.add(browserLauncher); + popup.addSeparator(); + popup.add(restartItem); + popup.add(stopItem); + + return popup; + } +} diff --git a/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java b/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java index 3c58999445..1bc8b1b055 100644 --- a/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java +++ b/apps/desktopgui/src/net/i2p/desktopgui/TrayManager.java @@ -20,29 +20,37 @@ import javax.swing.SwingWorker; import net.i2p.desktopgui.i18n.DesktopguiTranslator; import net.i2p.desktopgui.router.RouterManager; +import net.i2p.desktopgui.util.BrowseException; import net.i2p.desktopgui.util.ConfigurationManager; +import net.i2p.desktopgui.util.I2PDesktop; import net.i2p.util.Log; /** * Manages the tray icon life. */ -public class TrayManager { +public abstract class TrayManager { private static TrayManager instance = null; ///The tray area, or null if unsupported - private SystemTray tray = null; + protected SystemTray tray = null; ///Our tray icon, or null if unsupported - private TrayIcon trayIcon = null; + protected TrayIcon trayIcon = null; private final static Log log = new Log(TrayManager.class); /** * Instantiate tray manager. */ - private TrayManager() {} + protected TrayManager() {} public static TrayManager getInstance() { if(instance == null) { - instance = new TrayManager(); + boolean inI2P = ConfigurationManager.getInstance().getBooleanConfiguration("startWithI2P", false); + if(inI2P) { + instance = new InternalTrayManager(); + } + else { + instance = new ExternalTrayManager(); + } } return instance; } @@ -66,121 +74,7 @@ public class TrayManager { * Build a popup menu, adding callbacks to the different items. * @return popup menu */ - public PopupMenu getMainMenu() { - boolean inI2P = ConfigurationManager.getInstance().getBooleanConfiguration("startWithI2P", false); - - PopupMenu popup = new PopupMenu(); - MenuItem browserLauncher = new MenuItem(_("Launch I2P Browser")); - browserLauncher.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent arg0) { - new SwingWorker<Object, Object>() { - - @Override - protected Object doInBackground() throws Exception { - return null; - } - - @Override - protected void done() { - if(Desktop.isDesktopSupported()) { - Desktop desktop = Desktop.getDesktop(); - if(desktop.isSupported(Action.BROWSE)) { - try { - desktop.browse(new URI("http://localhost:7657")); - } catch (IOException e) { - log.log(Log.WARN, "Failed to open browser!", e); - } catch (URISyntaxException e) { - log.log(Log.WARN, "Failed to open browser!", e); - } - } - else { - trayIcon.displayMessage(_("Browser not found"), - _("The default browser for your system was not found."), - TrayIcon.MessageType.WARNING); - } - } - } - - }.execute(); - } - }); - popup.add(browserLauncher); - popup.addSeparator(); - MenuItem startItem = new MenuItem(_("Start I2P")); - startItem.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent arg0) { - new SwingWorker<Object, Object>() { - - @Override - protected Object doInBackground() throws Exception { - RouterManager.start(); - return null; - } - - @Override - protected void done() { - trayIcon.displayMessage(_("Starting"), _("I2P is starting!"), TrayIcon.MessageType.INFO); - //Hide the tray icon. - //We cannot stop the desktopgui program entirely, - //since that risks killing the I2P process as well. - tray.remove(trayIcon); - } - - }.execute(); - } - - }); - if(!inI2P) { - popup.add(startItem); - } - MenuItem restartItem = new MenuItem(_("Restart I2P")); - restartItem.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent arg0) { - new SwingWorker<Object, Object>() { - - @Override - protected Object doInBackground() throws Exception { - RouterManager.restart(); - return null; - } - - }.execute(); - - } - - }); - if(inI2P) { - popup.add(restartItem); - } - MenuItem stopItem = new MenuItem(_("Stop I2P")); - stopItem.addActionListener(new ActionListener() { - - @Override - public void actionPerformed(ActionEvent arg0) { - new SwingWorker<Object, Object>() { - - @Override - protected Object doInBackground() throws Exception { - RouterManager.shutDown(); - return null; - } - - }.execute(); - - } - - }); - if(inI2P) { - popup.add(stopItem); - } - return popup; - } + public abstract PopupMenu getMainMenu(); /** * Get tray icon image from the desktopgui resources in the jar file. @@ -192,7 +86,7 @@ public class TrayManager { return image; } - private static String _(String s) { + protected static String _(String s) { return DesktopguiTranslator._(s); } } diff --git a/apps/desktopgui/src/net/i2p/desktopgui/util/BrowseException.java b/apps/desktopgui/src/net/i2p/desktopgui/util/BrowseException.java new file mode 100644 index 0000000000..ec120a416a --- /dev/null +++ b/apps/desktopgui/src/net/i2p/desktopgui/util/BrowseException.java @@ -0,0 +1,23 @@ +package net.i2p.desktopgui.util; + +public class BrowseException extends Exception { + + private static final long serialVersionUID = 1L; + + public BrowseException() { + super(); + } + + public BrowseException(String s) { + super(s); + } + + public BrowseException(String s, Throwable t) { + super(s, t); + } + + public BrowseException(Throwable t) { + super(t); + } + +} \ No newline at end of file diff --git a/apps/desktopgui/src/net/i2p/desktopgui/util/I2PDesktop.java b/apps/desktopgui/src/net/i2p/desktopgui/util/I2PDesktop.java new file mode 100644 index 0000000000..3a35d5c318 --- /dev/null +++ b/apps/desktopgui/src/net/i2p/desktopgui/util/I2PDesktop.java @@ -0,0 +1,35 @@ +package net.i2p.desktopgui.util; + +import java.awt.Desktop; +import java.awt.TrayIcon; +import java.awt.Desktop.Action; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; + +import net.i2p.desktopgui.router.RouterManager; +import net.i2p.util.Log; + +public class I2PDesktop { + + private final static Log log = new Log(I2PDesktop.class); + + public static void browse(String url) throws BrowseException { + if(Desktop.isDesktopSupported()) { + Desktop desktop = Desktop.getDesktop(); + if(desktop.isSupported(Action.BROWSE)) { + try { + desktop.browse(new URI(url)); + } catch (Exception e) { + throw new BrowseException(); + } + } + else { + throw new BrowseException(); + } + } + else { + throw new BrowseException(); + } + } +} \ No newline at end of file -- GitLab