001    /******************************************************************************
002     * Copyright (C) MActor Developers. All rights reserved.                        *
003     * ---------------------------------------------------------------------------*
004     * This file is part of MActor.                                               *
005     *                                                                            *
006     * MActor is free software; you can redistribute it and/or modify             *
007     * it under the terms of the GNU General Public License as published by       *
008     * the Free Software Foundation; either version 2 of the License, or          *
009     * (at your option) any later version.                                        *
010     *                                                                            *
011     * MActor is distributed in the hope that it will be useful,                  *
012     * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
013     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *
014     * GNU General Public License for more details.                               *
015     *                                                                            *
016     * You should have received a copy of the GNU General Public License          *
017     * along with MActor; if not, write to the Free Software                      *
018     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA *
019     ******************************************************************************/
020    package org.mactor.ui.gui;
021    
022    import java.lang.reflect.Method;
023    import java.net.URL;
024    
025    import javax.swing.JOptionPane;
026    import javax.swing.event.HyperlinkEvent;
027    import javax.swing.event.HyperlinkListener;
028    
029    public class BrowserUtil {
030            private static final String errMsg = "Error attempting to launch web browser";
031            public static HyperlinkListener createLinkListener() {
032                    return new HyperlinkListener() {
033                            public void hyperlinkUpdate(HyperlinkEvent e) {
034                                    if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) {
035                                            URL target = e.getURL();
036                                            openURL(target);
037                                    }
038                            }
039                    };
040            }
041            // This flowwing code was fetched from:
042            // http://www.centerkey.com/java/browser/
043            //
044            public static void openURL(URL url) {
045                    String osName = System.getProperty("os.name");
046                    try {
047                            if (osName.startsWith("Mac OS")) {
048                                    Class fileMgr = Class.forName("com.apple.eio.FileManager");
049                                    Method openURL = fileMgr.getDeclaredMethod("openURL", new Class[] { String.class });
050                                    openURL.invoke(null, new Object[] { url });
051                            } else if (osName.startsWith("Windows"))
052                                    Runtime.getRuntime().exec("rundll32 url.dll,FileProtocolHandler " + url);
053                            else { // assume Unix or Linux
054                                    String[] browsers = { "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
055                                    String browser = null;
056                                    for (int count = 0; count < browsers.length && browser == null; count++)
057                                            if (Runtime.getRuntime().exec(new String[] { "which", browsers[count] }).waitFor() == 0)
058                                                    browser = browsers[count];
059                                    if (browser == null)
060                                            throw new Exception("Could not find web browser");
061                                    else
062                                            Runtime.getRuntime().exec(new String[] { browser, url.toExternalForm() });
063                            }
064                    } catch (Exception e) {
065                            JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
066                    }
067            }
068    }