package org.protege.editor.core.ui.util;

import org.protege.editor.core.platform.OSUtils;

import javax.swing.*;
import java.lang.reflect.Method;
/*
* Copyright (C) 2007, University of Manchester
*
*
*/

/**
 * Author: drummond<br>
 * http://www.cs.man.ac.uk/~drummond/<br><br>

 * The University Of Manchester<br>
 * Bio Health Informatics Group<br>
 * Date: Aug 12, 2008<br><br>
 */
public class NativeBrowserLauncher {

    private static final String errMsg = "Sorry, could not launch web browser";

    public static void openURL(String url) {
        try {
            if (OSUtils.isOSX()) {
                Class<?> fileMgr = Class.forName("com.apple.eio.FileManager");
                Method openURL = fileMgr.getDeclaredMethod("openURL", String.class);
                openURL.invoke(null, url);
            }
            else if (OSUtils.isWindows()){
                Runtime.getRuntime().exec(new String[] {"rundll32", "url.dll,FileProtocolHandler", url});
            }
            else { //assume Unix or Linux
                String[] browsers = {
                        "xdg-open", "firefox", "opera", "konqueror", "epiphany", "mozilla", "netscape" };
                String browser = null;
                for (int count = 0; count < browsers.length && browser == null; count++)
                    if (Runtime.getRuntime().exec(
                            new String[] {"which", browsers[count]}).waitFor() == 0)
                        browser = browsers[count];
                if (browser == null)
                    throw new Exception("Could not find web browser");
                else
                    Runtime.getRuntime().exec(new String[] {browser, url});
            }
        }
        catch (Exception e) {
            JOptionPane.showMessageDialog(null, errMsg + ":\n" + e.getLocalizedMessage());
        }
    }
}
