Statistics
| Revision:

root / trunk / libraries / libIverUtiles / src / com / iver / utiles / BrowserControl.java @ 9654

History | View | Annotate | Download (7.52 KB)

1
/*
2
 * Created on 18-oct-2004
3
 *
4
 * TODO To change the template for this generated file go to
5
 * Window - Preferences - Java - Code Generation - Code and Comments
6
 */
7
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
8
 *
9
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
10
 *
11
 * This program is free software; you can redistribute it and/or
12
 * modify it under the terms of the GNU General Public License
13
 * as published by the Free Software Foundation; either version 2
14
 * of the License, or (at your option) any later version.
15
 *
16
 * This program is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 * along with this program; if not, write to the Free Software
23
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
24
 *
25
 * For more information, contact:
26
 *
27
 *  Generalitat Valenciana
28
 *   Conselleria d'Infraestructures i Transport
29
 *   Av. Blasco Ib??ez, 50
30
 *   46010 VALENCIA
31
 *   SPAIN
32
 *
33
 *      +34 963862235
34
 *   gvsig@gva.es
35
 *      www.gvsig.gva.es
36
 *
37
 *    or
38
 *
39
 *   IVER T.I. S.A
40
 *   Salamanca 50
41
 *   46005 Valencia
42
 *   Spain
43
 *
44
 *   +34 963163400
45
 *   dac@iver.es
46
 */
47
package com.iver.utiles;
48

    
49
/**
50
 * @author Steven Spencer
51
 *
52
 * TODO To change the template for this generated type comment go to
53
 * Window - Preferences - Java - Code Generation - Code and Comments
54
 */
55
import java.io.IOException;
56
import java.util.ArrayList;
57
import java.util.logging.Level;
58
import java.util.logging.Logger;
59

    
60

    
61
/**
62
* A simple, static class to display a URL in the system browser.
63

64

65
*
66
* Under Unix, the system browser is hard-coded to be 'netscape'.
67
* Netscape must be in your PATH for this to work.  This has been
68
* tested with the following platforms: AIX, HP-UX and Solaris.
69

70

71
*
72
* Under Windows, this will bring up the default browser under windows,
73
* usually either Netscape or Microsoft IE.  The default browser is
74
* determined by the OS.  This has been tested under Windows 95/98/NT.
75

76

77
*
78
* Examples:
79

80

81
*
82
BrowserControl.displayURL("http://www.javaworld.com")
83
*
84
BrowserControl.displayURL("file://c:\\docs\\index.html")
85
*
86
BrowserContorl.displayURL("file:///user/joe/index.html");
87
*
88

89
* Note - you must include the url type -- either "http://" or
90
* "file://".
91
*/
92
public class BrowserControl
93
{
94
        private static Logger logger = Logger.getLogger(BrowserControl.class.getName());
95
        public static final String OPERA = "Opera";
96
        public static final String NETSCAPE = "Netscape";
97
        public static final String MOZILLA = "Mozilla";
98
        public static final String GALEON = "Galeon";
99
        public static final String EPIPHANY = "Epiphany";
100
        public static final String FIREFOX = "Firefox";
101
        public static final String KONQUEROR = "Konqueror";
102
        private static String browserCommand = FIREFOX;
103
        private static ArrayList supportedBrowsers;
104

    
105
    /**
106
     * Display a file in the system browser.  If you want to display a
107
     * file, you must include the absolute path name.
108
     *
109
     * @param url the file's url (the url must start with either "http://"
110
or
111
     * "file://").
112
     */
113
    public static void displayURL(String url)
114
    {
115
        boolean windows = isWindowsPlatform();
116
        String cmd = null;
117
        try
118
        {
119
                if (windows)
120
                {
121
                        // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
122
                        cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
123
                        Process p = Runtime.getRuntime().exec(cmd);
124
                }
125
                else
126
                {
127
                        //
128
                        if (browserCommand.equals(NETSCAPE) ||
129
                                browserCommand.equals(FIREFOX) ||
130
                                browserCommand.equals(MOZILLA)) {
131
                                // Under Unix, Netscape has to be running for the "-remote"
132
                                // command to work.  So, we try sending the command and
133
                                // check for an exit value.  If the exit command is 0,
134
                                // it worked, otherwise we need to start the browser.
135
                                // cmd = 'netscape -remote openURL(http://www.javaworld.com)'
136
                                cmd = browserCommand.toLowerCase() + " -remote openURL(" + url + ")";
137
                                Process p = Runtime.getRuntime().exec(cmd);
138
                                try
139
                                {
140
                                        // wait for exit code -- if it's 0, command worked,
141
                                        // otherwise we need to start the browser up.
142
                                        int exitCode = p.waitFor();
143
                                        if (exitCode != 0)
144
                                        {
145
                                                // Command failed, start up the browser
146
                                                // cmd = 'netscape http://www.javaworld.com'
147
                                                cmd = browserCommand.toLowerCase() + " "  + url;
148
                                                p = Runtime.getRuntime().exec(cmd);
149
                                        }
150
                                }
151
                                catch(InterruptedException x)
152
                                {
153
                                        System.err.println("Error bringing up browser, cmd='" +
154
                                                        cmd + "'");
155
                                        System.err.println("Caught: " + x);
156
                                }
157
                        } else if (browserCommand.equals(KONQUEROR)) {
158
                                cmd = "konqueror "+ url;
159
                                Runtime.getRuntime().exec(cmd);
160
//                        } else if (browserCommand.equals(OPERA)) {
161
//                                TODO available if command is rigth, check it
162
//                                cmd = "opera "+ url;
163
//                                Runtime.getRuntime().exec(cmd);
164
                        } else if (browserCommand.equals(EPIPHANY)) {
165
                                cmd = "epiphany "+ url;
166
                                Runtime.getRuntime().exec(cmd);
167
//                        } else if (browserCommand.equals(GALEON)) {
168
//                                TODO available if command is rigth, check it
169
//                                cmd = "galeon "+ url;
170
//                                Runtime.getRuntime().exec(cmd);
171
                        } else {
172
                                cmd = cmd.replaceAll("%url|%URL", url);
173
                                logger.log(Level.FINEST, "Executing system runtime command: " + cmd);
174

    
175
                                Runtime.getRuntime().exec(cmd);
176
                        }
177

    
178

    
179
                }
180
        }
181
        catch(IOException x)
182
        {
183
            // couldn't exec browser
184
            System.err.println("Could not invoke browser, command=" + cmd);
185
            System.err.println("Caught: " + x);
186
        }
187

    
188
    }
189
    /**
190
     * Try to determine whether this application is running under Windows
191
     * or some other platform by examing the "os.name" property.
192
     *
193
     * @return true if this application is running under a Windows OS
194
     */
195
    public static boolean isWindowsPlatform()
196
    {
197
        String os = System.getProperty("os.name");
198
        if ( os != null && os.startsWith(WIN_ID))
199
            return true;
200
        else
201
            return false;
202
    }
203

    
204

    
205
    public static void setBrowserCommand(String browserCommand) {
206
            BrowserControl.browserCommand = browserCommand;
207
    }
208
    /**
209
     * Returns a list of supported browsers.
210
     * @return
211
     */
212
    public static ArrayList getSupportedBrowsers() {
213
            if (supportedBrowsers==null)
214
            {
215
                    supportedBrowsers = new ArrayList();
216
                    supportedBrowsers.add(KONQUEROR);
217
                    supportedBrowsers.add(FIREFOX);
218
                     supportedBrowsers.add(EPIPHANY);
219
                    // supportedBrowsers.add(GALEON);
220
                    supportedBrowsers.add(MOZILLA);
221
                    supportedBrowsers.add(NETSCAPE);
222
                    // supportedBrowsers.add(OPERA);
223
            }
224
            return supportedBrowsers;
225
    }
226
    // Used to identify the windows platform.
227
    private static final String WIN_ID = "Windows";
228
    // The default system browser under windows.
229
    private static final String WIN_PATH = "rundll32";
230
    // The flag to display a url.
231
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
232
    // The default browser under unix.
233
//    private static final String UNIX_PATH = "netscape";
234
    // The flag to display a url.
235
//    private static final String UNIX_FLAG = "-remote openURL";
236
}