org/homelinux/largo/utils/BrowserLaunch.java
author Markus Bröker <mbroeker@largo.homelinux.org>
Fri, 17 Dec 2010 22:33:17 +0100
changeset 14 f12f77aa13b2
parent 13 f83884cc7d2f
child 16 55b0d5006e7b
permissions -rw-r--r--
serialVersionUID starts with 1L...
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     1
/**
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     2
 *  $Id: BrowserLaunch.java 125 2008-04-22 08:06:08Z mbroeker $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     3
 * $URL: http://localhost/svn/eclipse/Schachspiel/trunk/org/homelinux/largo/utils/BrowserLaunch.java $
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     4
 */
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     5
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     6
package org.homelinux.largo.utils;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     7
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     8
import java.lang.reflect.Method;
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
     9
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    10
import javax.swing.JOptionPane;
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    11
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    12
/**
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    13
 * Cross-Platform Browserlaunch
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    14
 */
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    15
public class BrowserLaunch {
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    16
    private static final String errMsg = "Error attempting to launch web browser";
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    17
13
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    18
    /**
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    19
     * This method opens the URL in a native platform browser
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    20
     */
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    21
    public void openURL(String url) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    22
        String osName = System.getProperty("os.name");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    23
        try {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    24
            if (osName.startsWith("Mac OS")) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    25
                Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    26
                Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    27
                    String.class
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    28
                });
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    29
                openURL.invoke(null, new Object[] {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    30
                    url
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    31
                });
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    32
            } else if (osName.startsWith("Windows"))
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    33
                Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    34
            else {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    35
                String[] browsers = {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    36
                        "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape"
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    37
                };
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    38
                String browser = null;
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    39
                for (int count = 0; count < browsers.length && browser == null; count++)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    40
                    if (Runtime.getRuntime().exec(new String[] {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    41
                            "which", browsers[count]
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    42
                    }).waitFor() == 0)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    43
                        browser = browsers[count];
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    44
                if (browser == null)
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    45
                    throw new Exception("Could not find web browser");
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    46
                else
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    47
                    Runtime.getRuntime().exec(new String[] {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    48
                            browser, url
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    49
                    });
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    50
            }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    51
        } catch (Exception e) {
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    52
            JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    53
        }
f83884cc7d2f Source Code re-formatted
Markus Bröker <mbroeker@largo.homelinux.org>
parents: 0
diff changeset
    54
    }
0
e0dbaef72362 svn copy of the chess engine
Markus Bröker <mbroeker@largo.homelinux.org>
parents:
diff changeset
    55
};