added browser selection dialog

This commit is contained in:
hypercubus
2004-08-20 12:56:40 +00:00
committed by zzz
parent 0b084ece08
commit ffff6d701f
5 changed files with 111 additions and 42 deletions

View File

@ -22,13 +22,9 @@
<copy todir="./build/icons">
<fileset dir="./src/net/i2p/apps/systray/icons" />
</copy>
<copy todir="./build/jar_temp/doc">
<fileset dir="../doc" />
</copy>
<mkdir dir="./build/jar_temp" />
<copy todir="./build/jar_temp">
<fileset dir="./build/obj" includes="**/*.class" />
<fileset dir="./src/net/i2p/apps/systray" includes="doc/*" />
</copy>
<jar destfile="./build/lib/systray.jar" basedir="./build/jar_temp" includes="**/*">
<manifest>

View File

@ -0,0 +1,31 @@
/*
* I2P - An anonymous, secure, and fully-distributed communication network.
*
* BrowserChooser.java
* 2004 The I2P Project
* This code is public domain.
*/
package net.i2p.apps.systray;
import java.awt.FileDialog;
import java.awt.Frame;
/**
* A rather nasty AWT file chooser dialog (thanks, Kaffe!) which allows the user
* to select their preferred browser with.
*
* @author hypercubus
*/
public class BrowserChooser extends FileDialog {
public BrowserChooser(Frame owner, String windowTitle) {
super(owner, windowTitle);
initialize();
}
public void initialize(){
this.setSize(300,400);
this.show();
}
}

View File

@ -184,7 +184,7 @@ public class ShellCommand {
* {@link #getErrorStream()}, respectively. Input can be passed to the
* <code>STDIN</code> of the shell process via {@link #getInputStream()}.
*
* @param shellCommand The command for the shell to execute.
* @param _shellCommand The command for the shell to execute.
*/
public void execute(String shellCommand) {
execute(shellCommand, NO_CONSUME_OUTPUT, NO_WAIT_FOR_EXIT_STATUS);
@ -199,7 +199,7 @@ public class ShellCommand {
* Input can be passed to the <code>STDIN</code> of the shell process via
* {@link #getInputStream()}.
*
* @param shellCommand The command for the shell to execute.
* @param _shellCommand The command for the shell to execute.
* @return <code>true</code> if the spawned shell process
* returns an exit status of 0 (indicating success),
* else <code>false</code>.
@ -221,7 +221,7 @@ public class ShellCommand {
* {@link #getErrorStream()}, respectively. Input can be passed to the
* <code>STDIN</code> of the shell process via {@link #getInputStream()}.
*
* @param shellCommand The command for the shell to execute.
* @param _shellCommand The command for the shell to execute.
* @param seconds The method will return <code>true</code> if this
* number of seconds elapses without the process
* returning an exit status. A value of <code>0</code>
@ -256,7 +256,7 @@ public class ShellCommand {
* without waiting for an exit status. Any output produced by the executed
* command will not be displayed.
*
* @param shellCommand The command for the shell to execute.
* @param _shellCommand The command for the shell to execute.
* @throws IOException
*/
public void executeSilent(String shellCommand) throws IOException {
@ -268,7 +268,7 @@ public class ShellCommand {
* all of the command's resulting shell processes have completed. Any output
* produced by the executed command will not be displayed.
*
* @param shellCommand The command for the shell to execute.
* @param _shellCommand The command for the shell to execute.
* @return <code>true</code> if the spawned shell process
* returns an exit status of 0 (indicating success),
* else <code>false</code>.
@ -287,7 +287,7 @@ public class ShellCommand {
* specified number of seconds has elapsed first. Any output produced by the
* executed command will not be displayed.
*
* @param shellCommand The command for the shell to execute.
* @param _shellCommand The command for the shell to execute.
* @param seconds The method will return <code>true</code> if this
* number of seconds elapses without the process
* returning an exit status. A value of <code>0</code>

View File

@ -8,6 +8,8 @@
package net.i2p.apps.systray;
import java.awt.Frame;
import snoozesoft.systray4j.SysTrayMenu;
import snoozesoft.systray4j.SysTrayMenuEvent;
import snoozesoft.systray4j.SysTrayMenuIcon;
@ -23,18 +25,26 @@ import snoozesoft.systray4j.SysTrayMenuListener;
*/
public class SysTray implements SysTrayMenuListener {
private SysTrayMenuItem itemExit = new SysTrayMenuItem("Exit systray", "exit");
private SysTrayMenuItem itemSetBrowser = new SysTrayMenuItem("Set preferred browser...", "setbrowser");
private SysTrayMenuIcon sysTrayMenuIcon = new SysTrayMenuIcon("../icons/iggy");
private SysTrayMenu sysTrayMenu = new SysTrayMenu(sysTrayMenuIcon, "I2P Console");
private static String _browserString;
private BrowserChooser _browserChooser;
private Frame _frame;
private SysTrayMenuItem _itemExit = new SysTrayMenuItem("Exit systray", "exit");
private SysTrayMenuItem _itemSelectBrowser = new SysTrayMenuItem("Select preferred browser...", "selectbrowser");
private SysTrayMenuIcon _sysTrayMenuIcon = new SysTrayMenuIcon("../icons/iggy");
private SysTrayMenu _sysTrayMenu = new SysTrayMenu(_sysTrayMenuIcon, "I2P Console");
public SysTray() {
sysTrayMenuIcon.addSysTrayMenuListener(this);
_sysTrayMenuIcon.addSysTrayMenuListener(this);
createSysTrayMenu();
}
public static void main(String[] args) {
new SysTray();
if (args.length == 1)
_browserString = args[0];
while(true)
try {
Thread.sleep(2 * 1000);
@ -46,27 +56,52 @@ public class SysTray implements SysTrayMenuListener {
public void iconLeftClicked(SysTrayMenuEvent e) {}
public void iconLeftDoubleClicked(SysTrayMenuEvent e) {
try {
new UrlLauncher().openUrl("http://localhost:7657");
} catch (Exception ex) {
// Pop up a dialog or something.
if (_browserString == null || _browserString.equals("browser default")) {
try {
new UrlLauncher().openUrl("http://localhost:7657");
} catch (Exception ex) {
setBrowser(promptForBrowser("Please select another browser"));
}
} else {
try {
new UrlLauncher().openUrl("http://localhost:7657", _browserString);
} catch (Exception ex) {
setBrowser(promptForBrowser("Please select another browser"));
}
}
}
public void menuItemSelected(SysTrayMenuEvent e) {
if (e.getActionCommand().equals("exit")) {
// exit systray
_browserChooser = null;
_frame = null;
_itemExit = null;
_itemSelectBrowser = null;
_sysTrayMenuIcon = null;
_sysTrayMenu = null;
System.exit(0);
} else if (e.getActionCommand().equals("start")) {
// Popup browser dialog
} else if (e.getActionCommand().equals("selectbrowser")) {
setBrowser(promptForBrowser("Select preferred browser"));
}
}
private void createSysTrayMenu() {
itemSetBrowser.addSysTrayMenuListener(this);
itemExit.addSysTrayMenuListener(this);
sysTrayMenu.addItem(itemExit);
sysTrayMenu.addSeparator();
sysTrayMenu.addItem(itemSetBrowser);
_itemSelectBrowser.addSysTrayMenuListener(this);
_itemExit.addSysTrayMenuListener(this);
_sysTrayMenu.addItem(_itemExit);
_sysTrayMenu.addSeparator();
_sysTrayMenu.addItem(_itemSelectBrowser);
}
private String promptForBrowser(String windowTitle) {
_frame = new Frame();
_browserChooser = new BrowserChooser(_frame, windowTitle);
return _browserChooser.getDirectory() + _browserChooser.getFile();
}
private void setBrowser(String browser) {
_browserString = browser;
// change "clientApp.3.args=browser" property in clients.config here.
// System.out.println("User chose browser: " + browser);
}
}

View File

@ -17,11 +17,11 @@ package net.i2p.apps.systray;
* Firefox, Netscape, Opera, and Safari.
*
* @author hypercubus
*
* TODO Add a method to allow opening a URL with a specific browser.
*/
public class UrlLauncher {
ShellCommand _shellCommand = new ShellCommand();
/**
* Discovers the operating system the installer is running under and tries
* to launch the given URL using the default browser for that platform; if
@ -30,52 +30,59 @@ public class UrlLauncher {
*/
public boolean openUrl(String url) throws Exception {
String osName = System.getProperty("os.name");
ShellCommand shellCommand = new ShellCommand();
String osName = System.getProperty("os.name");
if (osName.toLowerCase().indexOf("mac") > -1) {
if (osName.toLowerCase().startsWith("mac os x")) {
if (shellCommand.executeSilentAndWaitTimed("safari " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("safari " + url, 5))
return true;
} else {
return false;
}
if (shellCommand.executeSilentAndWaitTimed("iexplore " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("iexplore " + url, 5))
return true;
} else if (osName.startsWith("Windows")) {
if (shellCommand.executeSilentAndWaitTimed("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("\"C:\\Program Files\\Internet Explorer\\iexplore.exe\" " + url, 5))
return true;
} else {
if (shellCommand.executeSilentAndWaitTimed("konqueror " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("konqueror " + url, 5))
return true;
if (shellCommand.executeSilentAndWaitTimed("galeon " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("galeon " + url, 5))
return true;
}
if (shellCommand.executeSilentAndWaitTimed("firefox " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("firefox " + url, 5))
return true;
if (shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("opera -newpage " + url, 5))
return true;
if (shellCommand.executeSilentAndWaitTimed("mozilla " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("mozilla " + url, 5))
return true;
if (shellCommand.executeSilentAndWaitTimed("netscape " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("netscape " + url, 5))
return true;
if (shellCommand.executeSilentAndWaitTimed("links " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("links " + url, 5))
return true;
if (shellCommand.executeSilentAndWaitTimed("lynx " + url, 5))
if (_shellCommand.executeSilentAndWaitTimed("lynx " + url, 5))
return true;
return false;
}
public boolean openUrl(String url, String browser) throws Exception {
// System.out.println("Launching: '" + browser + " " + url + "'");
if (_shellCommand.executeSilentAndWaitTimed(browser + " " + url, 5))
return true;
return false;