Statistics
| Revision:

gvsig-raster / org.gvsig.raster.wms / branches / org.gvsig.raster.wms_dataaccess_refactoring / org.gvsig.raster.wms.app.wmsclient / src / main / java / org / gvsig / raster / wms / app / wmsclient / infobypoint / BrowserControl.java @ 2435

History | View | Annotate | Download (7.7 KB)

1
/**
2
 * gvSIG. Desktop Geographic Information System.
3
 *
4
 * Copyright (C) 2007-2013 gvSIG Association.
5
 *
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 3
9
 * of the License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA  02110-1301, USA.
20
 *
21
 * For any additional information, do not hesitate to contact us
22
 * at info AT gvsig.com, or visit our website www.gvsig.com.
23
 */
24
/*
25
 * Created on 18-oct-2004
26
 *
27
 * TODO To change the template for this generated file go to
28
 * Window - Preferences - Java - Code Generation - Code and Comments
29
 */
30
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
31
 *
32
 * Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
33
 *
34
 * This program is free software; you can redistribute it and/or
35
 * modify it under the terms of the GNU General Public License
36
 * as published by the Free Software Foundation; either version 2
37
 * of the License, or (at your option) any later version.
38
 *
39
 * This program is distributed in the hope that it will be useful,
40
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
41
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
42
 * GNU General Public License for more details.
43
 *
44
 * You should have received a copy of the GNU General Public License
45
 * along with this program; if not, write to the Free Software
46
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
47
 *
48
 * For more information, contact:
49
 *
50
 *  Generalitat Valenciana
51
 *   Conselleria d'Infraestructures i Transport
52
 *   Av. Blasco Ib??ez, 50
53
 *   46010 VALENCIA
54
 *   SPAIN
55
 *
56
 *      +34 963862235
57
 *   gvsig@gva.es
58
 *      www.gvsig.gva.es
59
 *
60
 *    or
61
 *
62
 *   IVER T.I. S.A
63
 *   Salamanca 50
64
 *   46005 Valencia
65
 *   Spain
66
 *
67
 *   +34 963163400
68
 *   dac@iver.es
69
 */
70
package org.gvsig.raster.wms.app.wmsclient.infobypoint;
71
import java.io.IOException;
72
import java.util.ArrayList;
73
import java.util.List;
74

    
75
import org.slf4j.Logger;
76
import org.slf4j.LoggerFactory;
77

    
78
 
79
/**
80
* <p>A simple, static class to display a URL in the system browser.<br></p>
81
*
82
* <p>Under <b>Unix</b> systems, it will open one of the browser set through the
83
* <b>setBrowserCommand(String)</b>. The argument of this method
84
* must be a fully qualified command or one of the commands returned
85
* by <b>getSupportedBrowsers()</b>.<br>By default, the browser is Firefox,
86
* which is included in the list of supported browsers.<br></p>
87
*
88
* <p>Under <b>Windows</b>, this will bring up the default browser under windows,
89
* usually either Netscape or Microsoft IE.  The default browser is
90
* determined by the OS so no config is necessary.<br>This has been tested under Windows 95/98/NT/XP.</p>
91
* <p>Notice that <b>you must include</b> the url type -- either "http://" or  "file://".</p>
92
*
93
* <p>Under <b>Mac</b>, the usability of this class is pending of test. So probably it does not work</p>
94
* Examples:
95
* BrowserControl.displayURL("http://www.javaworld.com")
96
* BrowserControl.displayURL("file://c:\\docs\\index.html")
97
* BrowserContorl.displayURL("file:///user/joe/index.html");
98
*
99
* @author jaume dominguez faus - jaume.dominguez@iver.es
100
*/
101
public class BrowserControl
102
{
103
        private static Logger logger = LoggerFactory.getLogger(BrowserControl.class.getName());
104
         // Used to identify the windows platform.
105
    private static final String WIN_ID = "Windows";
106
    // The default system browser under windows.
107
    private static final String WIN_PATH = "rundll32";
108
    // The flag to display a url.
109
    private static final String WIN_FLAG = "url.dll,FileProtocolHandler";
110
    
111
        public static final String OPERA = "Opera";
112
        public static final String NETSCAPE = "Netscape";
113
        public static final String MOZILLA = "Mozilla";
114
        public static final String GALEON = "Galeon";
115
        public static final String EPIPHANY = "Epiphany";
116
        public static final String FIREFOX = "Firefox";
117
        public static final String CHROME = "google-chrome";
118
        public static final String KONQUEROR = "Konqueror";
119
        private String browserCommand = null;
120
        private List<String> supportedBrowsers;
121
        //public Preferences fPrefs = Preferences.userRoot().node( "gvsig.wcs-wizard" );
122
        
123
        public BrowserControl() {
124
                browserCommand = FIREFOX;//fPrefs.get("DefaultBrowser", FIREFOX);
125
        }
126
        
127
        private boolean throwBrowser(String browser, String url) {
128
                String url1 = url.replace("'", "\'");
129
                String url2 = "'" + url.replace("'", "\'") + "'";
130
                int exitCode = -1;
131
                String cmd = null;
132
                Process p = null;
133
                
134
                try {
135
                        cmd = browser.toLowerCase() + " " + url1;
136
                        logger.info("Trying..." + cmd);
137
                        p = exec(cmd);
138
                        exitCode = p.waitFor();
139
                } catch (IOException e) {
140
                        exitCode = -1;
141
                } catch (InterruptedException e) {
142
                        logger.info("Error bringing up browser, cmd='" + cmd + "'");
143
                }
144
                
145
                try {
146
                        if (exitCode != 0) {
147
                                cmd = browser.toLowerCase() + " "  + url2;
148
                                logger.info("Trying..." + cmd);
149
                                p = exec(cmd);
150
                                exitCode = p.waitFor();
151
                        }
152
                } catch (IOException e) {
153
                        exitCode = -1;
154
                } catch(InterruptedException x) {
155
                        logger.info("Error bringing up browser, cmd='" + cmd + "'");
156
                } 
157
                
158
                if(exitCode != 0)
159
                        return false;
160
                return true;
161
        }
162

    
163
    /**
164
     * Display a file in the system browser.  If you want to display a
165
     * file, you must include the absolute path name.
166
     *
167
     * @param url the file's url (the url must start with either "http://" or  "file://").
168
     */
169
    public void displayURL(String url) {
170
        boolean windows = isWindowsPlatform();
171
        String cmd = null;
172
        try {
173
                if (windows) {
174
                        // cmd = 'rundll32 url.dll,FileProtocolHandler http://...'
175
                        cmd = WIN_PATH + " " + WIN_FLAG + " " + url;
176
                        Runtime.getRuntime().exec(cmd);
177
                } else {
178
                        if(throwBrowser(browserCommand, url)) {
179
                                return;
180
                        } else {
181
                                int i = 0;
182
                                boolean throwed = false;
183
                                while(i < getSupportedBrowsers().size() && ! throwed) {
184
                                        throwed = throwBrowser(getSupportedBrowsers().get(i), url);
185
                                        i++;
186
                                }
187
                        }
188
                }
189
        } catch(IOException x) {
190
            // couldn't exec browser
191
                logger.warn("Could not invoke browser, command=" + cmd);
192
                logger.info("Caught: " + x);
193
        }
194

    
195
    }
196
    
197
    private Process exec(String cmd) throws IOException {
198
            logger.info(cmd);
199
                return Runtime.getRuntime().exec(cmd);
200
    }
201

    
202
    /**
203
     * Try to determine whether this application is running under Windows
204
     * or some other platform by examing the "os.name" property.
205
     *
206
     * @return true if this application is running under a Windows OS
207
     */
208
    public static boolean isWindowsPlatform()
209
    {
210
        String os = System.getProperty("os.name");
211
        if ( os != null && os.startsWith(WIN_ID))
212
            return true;
213
        else
214
            return false;
215
    }
216

    
217

    
218
    
219
    /**
220
     * Returns a list of supported browsers.
221
     * @return
222
     */
223
    public List<String> getSupportedBrowsers() {
224
            if (supportedBrowsers == null) {
225
                    supportedBrowsers = new ArrayList<String>();
226
                    supportedBrowsers.add(CHROME);
227
                    supportedBrowsers.add(FIREFOX);
228
                    supportedBrowsers.add(KONQUEROR);
229
                    supportedBrowsers.add(EPIPHANY);
230
                    supportedBrowsers.add(MOZILLA);
231
                    supportedBrowsers.add(NETSCAPE);
232
                    supportedBrowsers.add(OPERA);
233
            }
234
            return supportedBrowsers;
235
    }
236
}