/*
 * PlatformUtil.java  $Revision: 1.2 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;

/**
 * @version $Revision: 1.2 $ $Date: 2007/10/03 04:39:39 $
 * @author rob
 */
public class PlatformUtil
{
// Static variables
// Member variables
// Constructors and initialization
    // prevent instantiation
    private PlatformUtil()
    {
    }

// Interface implemenations
// Public methods
    /** Opens the url, typically in a web browser. */
    public static void openURL(URL url) throws IOException
    {
        openURL(url.toString());
    }

    /** Opens the url, typically in a web browser. */
    public static void openURL(String url) throws IOException
    {
        String[] command;

        boolean windows = false;
        String os = System.getProperty("os.name");
        if (os != null)
        {
            windows = os.toLowerCase().startsWith("windows");
        }

        if (windows)
        {
            command = new String[] {"cmd.exe", "/c", "start", "\"\"", url};
        }
        else
        {
            command = new String[] {"bash", "-c",
                                    "for browser in mozilla netscape opera ; do nobrow=`which $browser 2>&1 | grep \"no $browser in\"` ; if [ $? != 0 ] ; then $browser '" + url + "' ; break ; fi ; done"
            };
        }

        Runtime.getRuntime().exec(command);
    }

    public static void openFile(File file) throws IOException
    {
        // open a file on Windows NT4,2k,XP
        String[] command = {"cmd.exe",
                            "/c",
                            "start",
                            "\"\"",
                            file.toString()};

        Runtime.getRuntime().exec(command);
    }

    public static boolean isOpenFileSupported()
    {
        boolean ret = false;
        String os = System.getProperty("os.name");
        if (os != null)
        {
            ret = os.toLowerCase().startsWith("windows");
        }
        return ret;
    }

    public static boolean isOpenURLSupported()
    {
        boolean ret = false;
        String os = System.getProperty("os.name");
        if (os != null)
        {
            ret = os.toLowerCase().startsWith("windows") ||
                  os.toLowerCase().startsWith("linux") ||
                  os.toLowerCase().startsWith("sunos") ||
                  os.toLowerCase().startsWith("solaris");
        }
        return ret;
    }

// Protected methods
// Private methods
// Inner classes
}
