KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > tool > common > Browser


1
2 /*
3  * Enhydra Java Application Server Project
4  *
5  * The contents of this file are subject to the Enhydra Public License
6  * Version 1.1 (the "License"); you may not use this file except in
7  * compliance with the License. You may obtain a copy of the License on
8  * the Enhydra web site ( http://www.enhydra.org/ ).
9  *
10  * Software distributed under the License is distributed on an "AS IS"
11  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
12  * the License for the specific terms governing rights and limitations
13  * under the License.
14  *
15  * The Initial Developer of the Enhydra Application Server is Lutris
16  * Technologies, Inc. The Enhydra Application Server and portions created
17  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
18  * All Rights Reserved.
19  *
20  * Contributor(s):
21  *
22  */

23 package org.enhydra.tool.common;
24
25 // Toolbox imports
26
import org.enhydra.tool.ToolBoxInfo;
27
28 // Standard imports
29
import java.awt.Component JavaDoc;
30 import java.io.File JavaDoc;
31 import java.util.ResourceBundle JavaDoc;
32 import java.util.Properties JavaDoc;
33 public class Browser {
34     static ResourceBundle JavaDoc res =
35         ResourceBundle.getBundle("org.enhydra.tool.common.Res"); // nores
36
private final String JavaDoc MSIE = "IEXPLORE.EXE"; // nores
37
private final String JavaDoc NETSCAPE = "netscape"; // nores
38
private Component JavaDoc owner = null;
39
40     public void open(String JavaDoc target) {
41         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
42         String JavaDoc browser = getBrowser();
43         File JavaDoc file = null;
44
45         if (browser == null) {
46
47             //
48
} else {
49             file = new File JavaDoc(browser);
50         }
51         if ((file == null) || (!file.isFile())) {
52             System.err.println("Help browser not found.");
53         } else {
54             buf.append(file.getAbsolutePath());
55             buf.append(' ');
56             buf.append(target);
57             try {
58                 Runtime.getRuntime().exec(buf.toString());
59             } catch (java.io.IOException JavaDoc e) {
60                 e.printStackTrace(System.err);
61             }
62         }
63     }
64
65     public void setOwner(Component JavaDoc o) {
66         owner = o;
67     }
68
69     public Component JavaDoc getOwner() {
70         return owner;
71     }
72
73     public String JavaDoc getBrowser() {
74         PathHandle path = null;
75         Properties JavaDoc props = null;
76
77         if (File.pathSeparatorChar == '\\') {
78             path = PathHandle.createPathHandle(MSIE);
79         } else {
80             path = PathHandle.createPathHandle(NETSCAPE);
81         }
82         try {
83             props = ToolBoxInfo.loadProperties();
84             path = PathHandle.createPathHandle(props.getProperty("browser"));
85         } catch (ToolException e) {
86             e.printStackTrace(System.err);
87         }
88         if (!path.isFile()) {
89             path = selectBrowser();
90             if (path.isFile()) {
91                 setBrowser(path.getPath());
92             }
93         }
94         return path.getPath();
95     }
96
97     public void setBrowser(String JavaDoc browser) {
98         Properties JavaDoc props = null;
99
100         try {
101             props = ToolBoxInfo.loadProperties();
102             props.setProperty("browser", browser);
103             ToolBoxInfo.storeProperties(props);
104         } catch (ToolException e) {
105             e.printStackTrace(System.err);
106         }
107     }
108
109     private PathHandle selectBrowser() {
110         PathHandle path = null;
111         File JavaDoc choice = null;
112         ExtensionFilter filter = null;
113
114         filter = new ExtensionFilter();
115         filter.setDescriptionTitle(res.getString("Browser"));
116         if (File.separatorChar == '\\') {
117             filter.addExtension("exe"); // nores
118
path = PathHandle.createPathHandle(MSIE);
119         } else {
120             path = PathHandle.createPathHandle(NETSCAPE);
121         }
122         choice = SwingUtil.getFileChoice(getOwner(), path.getPath(), filter,
123                                          res.getString("Select_browser"));
124         path = PathHandle.createPathHandle(choice);
125         return path;
126     }
127
128 }
129
Popular Tags