KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > test > gui > web > util > BrowserUtils


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.test.gui.web.util;
21
22 import org.netbeans.jellytools.actions.OptionsViewAction;
23
24
25 import org.netbeans.jellytools.NbDialogOperator;
26 import org.netbeans.jellytools.OptionsOperator;
27 import org.netbeans.jellytools.Bundle;
28
29 import org.netbeans.jellytools.properties.ComboBoxProperty;
30 import org.netbeans.jellytools.properties.TextFieldProperty;
31 import org.netbeans.jellytools.properties.PropertySheetTabOperator;
32 import org.netbeans.jellytools.properties.PropertySheetOperator;
33
34 import org.netbeans.jemmy.operators.JTextComponentOperator;
35
36 import java.io.File JavaDoc;
37
38 public class BrowserUtils {
39     private static String JavaDoc iSep = "|";
40     private String JavaDoc sampleUserAgentNS7="User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.2) Gecko/20030208 Netscape/7.02";
41     private String JavaDoc sampleUserAgentNS4="User-Agent: Mozilla/4.78 [en] (X11; U; Linux 2.4.7-10smp i686)";
42     private String JavaDoc sampleUserAgentIE6="User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)";
43
44     public static final int NETSCAPE4 = 0;
45     public static final int NETSCAPE6 = 1;
46     public static final int NETSCAPE7 = 2;
47     public static final int MSIE6 = 3;
48     public static final int UNKNOWN = 32;
49
50
51     public BrowserUtils() {
52     }
53
54     public static void setExternalUnixBrowser() {
55     String JavaDoc browser = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","CTL_UnixBrowserName");
56     setBrowser(browser);
57     }
58     public static void setExternalWinBrowser() {
59     String JavaDoc browser = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","CTL_WinBrowserName");
60     setBrowser(browser);
61     }
62     public static void setSwingBrowser() {
63     String JavaDoc browser = Bundle.getString("org.netbeans.beaninfo.Bundle","CTL_SwingBrowser");
64     setBrowser(browser);
65     }
66     public static void setCLBrowser() {
67     String JavaDoc browser = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","CTL_SimpleExtBrowser");
68     setBrowser(browser);
69     }
70     public static void setCLBrowserCommand(String JavaDoc command) {
71     OptionsOperator oo = OptionsOperator.invoke();
72     String JavaDoc ideConfiguration = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration");
73     String JavaDoc sets = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration/ServerAndExternalToolSettings");
74     String JavaDoc browsers = Bundle.getString("org.netbeans.core.Bundle", "Services/Browsers");
75     String JavaDoc cl = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","CTL_SimpleExtBrowser");
76     oo.selectOption(ideConfiguration + iSep + sets + iSep + browsers + iSep + cl);
77     PropertySheetOperator pso = PropertySheetOperator.invoke();
78         PropertySheetTabOperator psto = new PropertySheetTabOperator(pso);
79     String JavaDoc pnameBrowserExecutable = Bundle.getString("org.netbeans.modules.extbrowser.Bundle" ,"PROP_browserExecutable");
80     TextFieldProperty pr = new TextFieldProperty(psto, pnameBrowserExecutable);
81     if (!pr.getValue().equals(command)) {
82         pr.setValue(command);
83     }
84     }
85     
86
87     public static void setEBUBrowserCommand(String JavaDoc command) {
88     OptionsOperator oo = OptionsOperator.invoke();
89     String JavaDoc ideConfiguration = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration");
90     String JavaDoc sets = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration/ServerAndExternalToolSettings");
91     String JavaDoc browsers = Bundle.getString("org.netbeans.core.Bundle", "Services/Browsers");
92     String JavaDoc cl = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","CTL_UnixBrowserName");
93     oo.selectOption(ideConfiguration + iSep + sets + iSep + browsers + iSep + cl);
94     PropertySheetOperator pso = PropertySheetOperator.invoke();
95         PropertySheetTabOperator psto = new PropertySheetTabOperator(pso);
96     String JavaDoc pnameBrowserExecutable = Bundle.getString("org.netbeans.modules.extbrowser.Bundle" ,"PROP_browserExecutable");
97     TextFieldProperty pr = new TextFieldProperty(psto, pnameBrowserExecutable);
98     if (!pr.getValue().equals(command)) {
99         pr.setValue(command);
100     }
101     }
102
103
104     public static void setBrowser(String JavaDoc name) {
105     //new OptionsViewAction().perform();
106
OptionsOperator oo = OptionsOperator.invoke();
107     String JavaDoc ideConfiguration = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration");
108     String JavaDoc system = Bundle.getString("org.netbeans.core.Bundle", "UI/Services/IDEConfiguration/System");
109     String JavaDoc systemSettings = Bundle.getString("org.netbeans.core.Bundle", "Services/org-netbeans-core-IDESettings.settings");
110     oo.selectOption(ideConfiguration + iSep + system + iSep + systemSettings);
111     PropertySheetOperator pso = PropertySheetOperator.invoke();
112         PropertySheetTabOperator psto = new PropertySheetTabOperator(pso);
113     String JavaDoc pnameWebBrowser = Bundle.getString("org.netbeans.core.Bundle" ,"PROP_WWW_BROWSER");
114     ComboBoxProperty pr = new ComboBoxProperty(psto, pnameWebBrowser);
115     if (!pr.getValue().equals(name)) {
116         pr.setValue(name);
117     }
118     }
119     public static boolean handleSwingBrowserDialog() {
120     String JavaDoc title = Bundle.getString("org.openide.Bundle","NTF_InformationTitle");
121     try {
122         NbDialogOperator dialog = new NbDialogOperator(title);
123         dialog.ok();
124         return true;
125     }catch(Exception JavaDoc e) {
126         return false;
127     }
128     }
129
130     
131
132
133
134     /**
135        DDE servers section
136     */

137     public static void setDDEServerExplorer() {
138     System.out.println("#####\nNot implemented yet\n#####");
139     }
140     public static void setDDEServerNetscape() {
141     System.out.println("#####\nNot implemented yet\n#####");
142     }
143     
144     public static void setDDEServerNetscape6() {
145     System.out.println("#####\nNot implemented yet\n#####");
146     }
147     /* End of DDE servers section */
148
149     /**
150        Get Browsers section
151      */

152
153     public static String JavaDoc getIEInPath() {
154     System.out.println("#####\nNot implemented yet\n#####");
155     return null;
156     }
157     public static String JavaDoc getIEFullPath() {
158     System.out.println("#####\nNot implemented yet\n#####");
159     return null;
160     }
161     public static String JavaDoc getNetscapeFullPath() {
162     String JavaDoc[] paths = null;
163     String JavaDoc command = null;
164     if(System.getProperty("os.name").indexOf("Windows")!=-1) {
165             System.out.println("##########\nThis test must be extended for Windows platform\n#######");
166         return null;
167         }else {
168         paths = new String JavaDoc[] {"/usr/bin/netscape","/usr/local/bin/netscape","/bin/netscape"};
169     }
170     for(int i=0;i<paths.length;i++) {
171         if((new File JavaDoc(paths[i])).exists()) {
172         command = paths[i] + " {URL}";
173         i = paths.length;
174         }
175     }
176     if(command == null) {
177         StringBuffer JavaDoc reason = new StringBuffer JavaDoc("Nothing of following commands found on your system : ");
178         for(int i=0;i<paths.length;i++) {
179         reason.append(paths[i] + ";");
180         }
181         System.out.println("##########\n" + reason.toString() + "\n##########");
182         return null;
183     }
184     return command;
185     }
186     public static String JavaDoc getNetscapeInPath() {
187     String JavaDoc netscapeWin = "netscape.exe";
188     String JavaDoc netscapeUx = "netscape";
189     if(System.getProperty("os.name").indexOf("Windows")!=-1) {
190         return netscapeWin;
191         }else {
192         return netscapeUx;
193     }
194     }
195     public static String JavaDoc getNetscape6FullPath() {
196     String JavaDoc[] paths = null;
197     String JavaDoc command = null;
198     if(System.getProperty("os.name").indexOf("Windows")!=-1) {
199             System.out.println("##########\nThis test must be extended for Windows platform\n#######");
200         return null;
201         }else {
202         paths = new String JavaDoc[] {"/usr/local/netscape6/netscape","/usr/bin/netscape6","/usr/local/bin/netscape6","/bin/netscape6"};
203     }
204     for(int i=0;i<paths.length;i++) {
205         if((new File JavaDoc(paths[i])).exists()) {
206         command = paths[i] + " {URL}";
207         i = paths.length;
208         }
209     }
210     if(command == null) {
211         StringBuffer JavaDoc reason = new StringBuffer JavaDoc("Nothing of following commands found on your system : ");
212         for(int i=0;i<paths.length;i++) {
213         reason.append(paths[i] + ";");
214         }
215         System.out.println("##########\n" + reason.toString() + "\n##########");
216         return null;
217     }
218     return command;
219     }
220     public static String JavaDoc getNetscape6InPath() {
221     String JavaDoc netscapeWin = "netscp6.exe";
222     String JavaDoc netscapeUx = "netscape6";
223     if(System.getProperty("os.name").indexOf("Windows")!=-1) {
224         return netscapeWin;
225         }else {
226         return netscapeUx;
227     }
228     }
229
230
231
232     /* Errors handling section */
233
234     /**
235        Handle error in CL browser configuration
236     */

237     public static boolean handleErrorInCLBrowser() {
238     String JavaDoc title = Bundle.getString("org.openide.Bundle","NTF_WarningTitle");
239     String JavaDoc text = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","EXC_Invalid_Processor");
240     NbDialogOperator op = null;
241     try {
242         op = new NbDialogOperator(title);
243     }catch(Exception JavaDoc e) {
244         System.out.println(title + " dialog not found");
245         return false;
246     }
247     // JTextComponentOperator tco = new JTextComponentOperator(op,0);
248
// System.out.println("#####TEXT:" + tco.getText());
249
op.ok();
250     return true;
251     }
252     /**
253        Handle error in CL browser configuration
254     */

255     public static boolean handleErrorInUnixBrowser() {
256     String JavaDoc title = Bundle.getString("org.openide.Bundle","NTF_WarningTitle");
257     String JavaDoc text = Bundle.getString("org.netbeans.modules.extbrowser.Bundle","MSG_Cant_run_netscape"); //{0} - name of command, for example netscape88
258
try {
259         NbDialogOperator op = new NbDialogOperator(title);
260         op.ok();
261         return true;
262     }catch(Exception JavaDoc e) {
263         return false;
264     }
265     }
266
267     public static int getBrowserVersion(String JavaDoc userAgent) {
268     if(userAgent.indexOf("MSIE 6")!=-1) {
269         return MSIE6;
270     }
271     if(userAgent.indexOf("Mozilla/4")!=-1) {
272         return NETSCAPE4;
273     }
274     if(userAgent.indexOf("Mozilla/5")!=-1) {
275         if(userAgent.indexOf("Netscape/7")!=-1) {
276         return NETSCAPE7;
277         }
278         if(userAgent.indexOf("Netscape6/6")!=-1) {
279         return NETSCAPE6;
280         }
281     }
282     return UNKNOWN;
283     }
284
285
286     public static String JavaDoc getBrowserDescription(int version) {
287     String JavaDoc descr = null;
288     switch(version) {
289     case NETSCAPE4:
290         descr = "Netscape4.x browser";
291         break;
292     case NETSCAPE6:
293         descr = "Netscape6.x browser";
294         break;
295     case NETSCAPE7:
296         descr = "Netscape7.x browser";
297         break;
298     case MSIE6:
299         descr = "Internet Explorer 6";
300         break;
301     case UNKNOWN:
302         descr = "This browser is unknown by BrowserUtils";
303         break;
304     default:
305         descr = "Can't understand version " + version;
306     }
307     return descr;
308     }
309 }
310
311
Popular Tags