KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > extbrowser > ExtWebBrowser


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.extbrowser;
21
22 import java.beans.*;
23 import org.openide.ErrorManager;
24
25 import org.openide.execution.NbProcessDescriptor;
26 import org.openide.util.NbBundle;
27 import org.openide.awt.HtmlBrowser;
28 import org.openide.util.Utilities;
29
30 /** Factory and descriptions for external browser
31  */

32
33 public class ExtWebBrowser implements HtmlBrowser.Factory, java.io.Serializable JavaDoc, PropertyChangeListener {
34
35     private static final long serialVersionUID = -3021027901671504127L;
36
37     public static final String JavaDoc PROP_NAME = "name"; // NOI18N
38

39     /** Browser executable property name */
40     public static final String JavaDoc PROP_BROWSER_EXECUTABLE = "browserExecutable"; // NOI18N
41

42     /** DDE server property name */
43     public static final String JavaDoc PROP_DDESERVER = "dDEServer"; // NOI18N
44

45 // /** Browser start timeout property name */
46
// public static final String PROP_BROWSER_START_TIMEOUT = "browserStartTimeout"; // NOI18N
47

48     /** DDE activate timeout property name */
49     public static final String JavaDoc PROP_DDE_ACTIVATE_TIMEOUT = "activateTimeout"; // NOI18N
50

51     /** DDE openURL timeout property name */
52     public static final String JavaDoc PROP_DDE_OPENURL_TIMEOUT = "openurlTimeout"; // NOI18N
53

54     /** Name of DDE server corresponding to Netscape Navigator 4.x */
55     public static final String JavaDoc NETSCAPE = "NETSCAPE"; // NOI18N
56
/** Name of DDE server corresponding to Internet Explorer */
57     public static final String JavaDoc IEXPLORE = "IEXPLORE"; // NOI18N
58
/** Name of DDE server corresponding to Mozilla */
59     public static final String JavaDoc MOZILLA = "MOZILLA"; // NOI18N
60
/** Name of DDE server corresponding to Firefox */
61     public static final String JavaDoc FIREFOX = "FIREFOX"; // NOI18N
62
/** Name of DDE server corresponding to Netscape 6.x */
63     public static final String JavaDoc NETSCAPE6 = "NETSCAPE6"; // NOI18N
64

65 // /** Default timeout for starting the browser */
66
// protected static final int DEFAULT_BROWSER_START_TIMEOUT = 5000;
67

68     /** Default for DDE activate timeout property */
69     protected static final int DEFAULT_ACTIVATE_TIMEOUT = 2000;
70
71     /** Default for DDE openURL timeout property */
72     protected static final int DEFAULT_OPENURL_TIMEOUT = 3000;
73     
74     /** storage for DDE server property */
75     protected String JavaDoc ddeServer;
76     
77     /** storage for DDE activate timeout property */
78     protected int activateTimeout = DEFAULT_ACTIVATE_TIMEOUT;
79
80 // /** storage for starting browser timeout property */
81
// protected int browserStartTimeout = DEFAULT_BROWSER_START_TIMEOUT;
82

83     /** storage for DDE openURL timeout property */
84     protected int openurlTimeout = DEFAULT_OPENURL_TIMEOUT;
85
86     /** Logger for extbrowser module. */
87     private static ErrorManager err = ErrorManager.getDefault().getInstance("org.netbeans.modules.extbrowser"); // NOI18N
88

89     protected String JavaDoc name;
90     
91     public static ErrorManager getEM () {
92         return err;
93     }
94         
95     /** Holds value of property browserExecutable. */
96     protected NbProcessDescriptor browserExecutable;
97     
98     protected transient PropertyChangeSupport pcs;
99
100     /** Creates new Browser */
101     public ExtWebBrowser () {
102         init();
103     }
104
105     /** initialize object */
106     private void init () {
107         if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
108             err.log(ErrorManager.INFORMATIONAL, getClass().getName() + " " + System.currentTimeMillis() + "> init");
109         }
110         pcs = new PropertyChangeSupport(this);
111         if (Utilities.isWindows()) {
112             pcs.addPropertyChangeListener(this);
113         }
114     }
115
116     /**
117      * Gets DDE server name
118      * @return server name of DDEserver.
119      * <CODE>null</CODE> when no server is selected (means default web browser).
120      */

121     public String JavaDoc getDDEServer () {
122         return ddeServer;
123     }
124     
125     /**
126      * Sets DDE server name
127      * @param ddeServer name of DDE server or <CODE>null</CODE>
128      */

129     public void setDDEServer (String JavaDoc ddeServer) {
130         if ((ddeServer != null) && !ddeServer.equals(this.ddeServer)) {
131             String JavaDoc old = this.ddeServer;
132             this.ddeServer = ddeServer;
133             pcs.firePropertyChange (PROP_DDESERVER, old, ddeServer);
134             getEM().log("DDEServer changed to: " + ddeServer); // NOI18N
135
}
136     }
137    
138 // /** Getter for property browserStartTimeout.
139
// * @return Value of property browserStartTimeout.
140
// *
141
// */
142
// public int getBrowserStartTimeout() {
143
// return browserStartTimeout;
144
// }
145
//
146
// /** Setter for property browserStartTimeout.
147
// * @param browserStartTimeout New value of property browserStartTimeout.
148
// *
149
// */
150
// public void setBrowserStartTimeout(int browserStartTimeout) {
151
// if (browserStartTimeout != this.browserStartTimeout) {
152
// int oldVal = this.browserStartTimeout;
153
// this.browserStartTimeout = browserStartTimeout;
154
// pcs.firePropertyChange(PROP_BROWSER_START_TIMEOUT, oldVal, browserStartTimeout);
155
// }
156
// }
157

158     /** Getter for property openurlTimeout.
159      * @return Value of property openurlTimeout.
160      *
161      */

162     public int getOpenurlTimeout() {
163         return openurlTimeout;
164     }
165     
166     /** Setter for property openurlTimeout.
167      * @param openurlTimeout New value of property openurlTimeout.
168      *
169      */

170     public void setOpenurlTimeout(int openurlTimeout) {
171         if (openurlTimeout != this.openurlTimeout) {
172             int oldVal = this.openurlTimeout;
173             this.openurlTimeout = openurlTimeout;
174             pcs.firePropertyChange(PROP_DDE_OPENURL_TIMEOUT, oldVal, openurlTimeout);
175         }
176     }
177     
178     /** Getter for property activeTimeout.
179      * @return Value of property activeTimeout.
180      *
181      */

182     public int getActivateTimeout() {
183         return activateTimeout;
184     }
185     
186     /** Setter for property activeTimeout.
187      * @param activeTimeout New value of property activeTimeout.
188      *
189      */

190     public void setActivateTimeout(int activateTimeout) {
191         if (activateTimeout != this.activateTimeout) {
192             int oldVal = this.activateTimeout;
193             this.activateTimeout = activateTimeout;
194             pcs.firePropertyChange(PROP_DDE_ACTIVATE_TIMEOUT, oldVal, activateTimeout);
195         }
196     }
197
198     // getter for browser name - should be overriden in subclasses
199
public String JavaDoc getName() {
200         return name;
201     }
202     
203     /** Setter for browser name
204      */

205     public void setName(String JavaDoc name) {
206         if ((name != null) && (!name.equals(this.name))) {
207             String JavaDoc oldVal = this.name;
208             this.name = name;
209             pcs.firePropertyChange(PROP_NAME, oldVal, name);
210         }
211     }
212     
213     /** Getter for property browserExecutable.
214      * @return Value of property browserExecutable.
215      */

216     public NbProcessDescriptor getBrowserExecutable () {
217         if (browserExecutable == null || "".equals(browserExecutable.getProcessName())) { // NOI18N
218
return defaultBrowserExecutable();
219         }
220         return browserExecutable;
221     }
222
223     /** Setter for property browserExecutable.
224      * @param browserExecutable New value of property browserExecutable.
225      */

226     public void setBrowserExecutable (NbProcessDescriptor browserExecutable) {
227         if ((browserExecutable != null) && (!browserExecutable.equals(this.browserExecutable))) {
228             NbProcessDescriptor oldVal = this.browserExecutable;
229             this.browserExecutable = browserExecutable;
230             pcs.firePropertyChange(PROP_BROWSER_EXECUTABLE, oldVal, browserExecutable);
231         }
232         if (browserExecutable == null) {
233             NbProcessDescriptor oldVal = this.browserExecutable;
234             this.browserExecutable = defaultBrowserExecutable();
235             pcs.firePropertyChange(PROP_BROWSER_EXECUTABLE, oldVal, browserExecutable);
236         }
237     }
238
239     /* when browserExecutable is changed, ddeServer has to be changed accordingly */
240     public void propertyChange(PropertyChangeEvent evt) {
241         if (evt.getPropertyName().equals(ExtWebBrowser.PROP_BROWSER_EXECUTABLE)) {
242             Object JavaDoc np = evt.getNewValue();
243             if ((np != null) && (np instanceof NbProcessDescriptor)) {
244                 String JavaDoc processName = ((NbProcessDescriptor)np).getProcessName();
245                 if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
246                     err.log(ErrorManager.INFORMATIONAL, "" + System.currentTimeMillis() + "> propertychange: " + processName);
247                 }
248                 if ((processName != null) && (processName.trim().length() > 0)) {
249                     if (processName.toUpperCase().indexOf("IEXPLORE.EXE") > -1) { // NOI18N
250
setDDEServer(IEXPLORE);
251                     } else if (processName.toUpperCase().indexOf("MOZILLA.EXE") > -1) { // NOI18N
252
setDDEServer(MOZILLA);
253                     } else if (processName.toUpperCase().indexOf("FIREFOX.EXE") > -1) { // NOI18N
254
setDDEServer(FIREFOX);
255                     } else if (processName.toUpperCase().indexOf("NETSCP6.EXE") > -1) { // NOI18N
256
setDDEServer(NETSCAPE6);
257                     } else if (processName.toUpperCase().indexOf("NETSCP.EXE") > -1) { // NOI18N
258
setDDEServer(NETSCAPE6);
259                     } else if (processName.toUpperCase().indexOf("NETSCAPE.EXE") > -1) { // NOI18N
260
setDDEServer(NETSCAPE);
261                     } else {
262                         setDDEServer(null);
263                     }
264                 }
265             }
266         }
267     }
268
269     /** Default command for browser execution.
270      * Can be overriden to return browser that suits to platform and settings.
271      *
272      * @return netscape without any argument.
273      */

274     protected NbProcessDescriptor defaultBrowserExecutable () {
275         String JavaDoc b = "netscape"; // NOI18N
276
if (err.isLoggable(ErrorManager.INFORMATIONAL)) {
277             err.log(ErrorManager.INFORMATIONAL, "" + System.currentTimeMillis() + "> ExtBrowser: defaultBrowserExecutable: ");
278         }
279         if (Utilities.isWindows()) {
280             b = "iexplore"; // NOI18N
281
String JavaDoc params = ""; // NOI18N
282
try {
283                 // finds HKEY_CLASSES_ROOT\\".html" and respective HKEY_CLASSES_ROOT\\<value>\\shell\\open\\command
284
// we will ignore all params here
285
b = NbDdeBrowserImpl.getDefaultOpenCommand ();
286                 String JavaDoc [] args = Utilities.parseParameters(b);
287
288                 if (args == null || args.length == 0) {
289                     throw new NbBrowserException ();
290                 }
291                 b = args[0];
292                 if (args[0].toUpperCase().indexOf("IEXPLORE.EXE") > -1) { // NOI18N
293
setDDEServer(IEXPLORE);
294                     params = "-nohome "; // NOI18N
295
} else if (args[0].toUpperCase().indexOf("MOZILLA.EXE") > -1) { // NOI18N
296
setDDEServer(MOZILLA);
297                 } else if (args[0].toUpperCase().indexOf("FIREFOX.EXE") > -1) { // NOI18N
298
setDDEServer(FIREFOX);
299                 } else if (args[0].toUpperCase().indexOf("NETSCP6.EXE") > -1) { // NOI18N
300
setDDEServer(NETSCAPE6);
301                 } else if (args[0].toUpperCase().indexOf("NETSCP.EXE") > -1) { // NOI18N
302
setDDEServer(NETSCAPE6);
303                 } else if (args[0].toUpperCase().indexOf("NETSCAPE.EXE") > -1) { // NOI18N
304
setDDEServer(NETSCAPE);
305                 }
306                 params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
307                 return new NbProcessDescriptor(b, params);
308                 
309             } catch (NbBrowserException e) {
310                 try {
311                     b = NbDdeBrowserImpl.getBrowserPath("IEXPLORE"); // NOI18N
312
if ((b != null) && (b.trim().length() > 0)) {
313                         setDDEServer(IEXPLORE);
314                         params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
315                         return new NbProcessDescriptor(b, params);
316                     }
317
318                     b = NbDdeBrowserImpl.getBrowserPath("MOZILLA"); // NOI18N
319
if ((b != null) && (b.trim().length() > 0)) {
320                         setDDEServer(MOZILLA);
321                         params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
322                         return new NbProcessDescriptor(b, params);
323                     }
324
325                     b = NbDdeBrowserImpl.getBrowserPath("FIREFOX"); // NOI18N
326
if ((b != null) && (b.trim().length() > 0)) {
327                         setDDEServer(FIREFOX);
328                         params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
329                         return new NbProcessDescriptor(b, params);
330                     }
331
332                     b = NbDdeBrowserImpl.getBrowserPath("Netscp"); // NOI18N
333
if ((b != null) && (b.trim().length() > 0)) {
334                         setDDEServer(NETSCAPE6);
335                         params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
336                         return new NbProcessDescriptor(b, params);
337                     }
338                     
339                     b = NbDdeBrowserImpl.getBrowserPath("Netscp6"); // NOI18N
340
if ((b != null) && (b.trim().length() > 0)) {
341                         setDDEServer(NETSCAPE6);
342                         params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
343                         return new NbProcessDescriptor(b, params);
344                     }
345
346                     b = NbDdeBrowserImpl.getBrowserPath("Netscape"); // NOI18N
347
if ((b != null) && (b.trim().length() > 0)) {
348                         setDDEServer(NETSCAPE);
349                         params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
350                         return new NbProcessDescriptor(b, params);
351                     }
352                     
353                 } catch (NbBrowserException e2) {
354                     setDDEServer(IEXPLORE);
355                     b = "C:\\Program Files\\Internet Explorer\\iexplore.exe"; // NOI18N
356
}
357             } catch (UnsatisfiedLinkError JavaDoc e) {
358                 // someone is customizing this on non-Win platform
359
b = "iexplore"; // NOI18N
360
}
361             params += "{" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "}";
362             return new NbProcessDescriptor (b, params);
363
364         // Unix but not MacOSX
365
} else if (Utilities.isUnix() && !Utilities.isMac()) {
366             
367             // Linux -> Mozilla should be default
368
if (Utilities.getOperatingSystem() == Utilities.OS_LINUX) {
369                 b = "mozilla"; // NOI18N
370
java.io.File JavaDoc f = new java.io.File JavaDoc ("/usr/local/mozilla/mozilla"); // NOI18N
371
if (f.exists()) {
372                     b = f.getAbsolutePath();
373                 } else {
374                     f = new java.io.File JavaDoc ("/usr/bin/firefox"); // NOI18N
375
if (f.exists()) {
376                         b = f.getAbsolutePath();
377                     }
378                 }
379             // Solaris -> Netscape should be default
380
} else if (Utilities.getOperatingSystem() == Utilities.OS_SOLARIS) {
381                 b = "netscape"; // NOI18N
382
java.io.File JavaDoc f = new java.io.File JavaDoc ("/usr/dt/bin/sun_netscape"); // NOI18N
383
if (f.exists()) {
384                     b = f.getAbsolutePath();
385                 }
386             }
387
388             return new NbProcessDescriptor( b,
389                 "-remote \"openURL({" + ExtWebBrowser.UnixBrowserFormat.TAG_URL + "})\"", // NOI18N
390
NbBundle.getMessage(ExtWebBrowser.class, "MSG_BrowserExecutorHint")
391             );
392             
393         // OS/2
394
} else if (Utilities.getOperatingSystem () == Utilities.OS_OS2) {
395             return new NbProcessDescriptor(
396                 "Netscape.exe", // NOI18N
397
// {URL}
398
" {" + UnixBrowserFormat.TAG_URL + "}", // NOI18N
399
NbBundle.getBundle(ExtWebBrowser.class).getString("MSG_BrowserExecutorHint")
400             );
401             
402         // Mac OS
403
} else if (Utilities.isMac()) {
404             return new NbProcessDescriptor(
405                 "/usr/bin/open", // NOI18N
406
// {URL}
407
" {" + UnixBrowserFormat.TAG_URL + "}", // NOI18N
408
NbBundle.getBundle(ExtWebBrowser.class).getString("MSG_BrowserExecutorHint")
409             );
410             
411         // Other
412
} else {
413             return new NbProcessDescriptor(
414                 // empty string for process
415
"", // NOI18N
416
// {URL}
417
" {" + UnixBrowserFormat.TAG_URL + "}", // NOI18N
418
NbBundle.getBundle(ExtWebBrowser.class).getString("MSG_BrowserExecutorHint")
419             );
420         }
421     }
422
423     /**
424      * Returns a new instance of BrowserImpl implementation.
425      */

426     public HtmlBrowser.Impl createHtmlBrowserImpl() {
427         return new DelegatingWebBrowserImpl(this);
428     }
429
430     /**
431      * @param l new PropertyChangeListener */

432     public void addPropertyChangeListener (PropertyChangeListener l) {
433         if (pcs == null) {
434             pcs = new PropertyChangeSupport(this);
435         }
436         pcs.addPropertyChangeListener (l);
437     }
438     
439     /**
440      * @param l PropertyChangeListener to be removed */

441     public void removePropertyChangeListener (PropertyChangeListener l) {
442         if (pcs != null) {
443             pcs.removePropertyChangeListener (l);
444         }
445     }
446     
447     private void readObject (java.io.ObjectInputStream JavaDoc ois)
448     throws java.io.IOException JavaDoc, ClassNotFoundException JavaDoc {
449         ois.defaultReadObject ();
450         if (browserExecutable != null && browserExecutable.getArguments() != null) {
451             // replace old {params} with {URL}
452
String JavaDoc args = browserExecutable.getArguments();
453             int idx = args.indexOf("{params}"); // NOI18N
454
if (idx >= 0) {
455                 browserExecutable = new NbProcessDescriptor (
456                     browserExecutable.getProcessName(),
457                     args.substring(0, idx)+"-remote \"openURL({URL})"+args.substring(idx+8), // NOI18N
458
NbBundle.getMessage (ExtWebBrowser.class, "MSG_BrowserExecutorHint")
459                 );
460             }
461         }
462         init();
463     }
464
465     /** Default format that can format tags related to execution.
466      * Currently this is only the URL.
467      */

468     public static class UnixBrowserFormat extends org.openide.util.MapFormat {
469         
470         /** SVUID for serialization. */
471         private static final long serialVersionUID = -699340388834127437L;
472         
473         /** Tag used to pass URL */
474         public static final String JavaDoc TAG_URL = "URL"; // NOI18N
475

476         /** Creates UnixBrowserFormat for URL.
477          * @param url to specify URL
478          */

479         public UnixBrowserFormat (String JavaDoc url) {
480             super(new java.util.HashMap JavaDoc ());
481             java.util.Map JavaDoc map = getMap ();
482             map.put (TAG_URL, url);
483         }
484     }
485     
486 }
487
Popular Tags