KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SimpleBrowser


1 /*
2  * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
3  * subject to license terms.
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the Lesser GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18  * USA.
19  */

20
21 import java.awt.*;
22 import javax.swing.*;
23 import java.net.URL JavaDoc;
24 import java.net.MalformedURLException JavaDoc;
25 import org.jdesktop.jdic.browser.*;
26
27 /**
28  * JDIC API demo main class.
29  * <p>
30  * SimpleBrowser demonstrate the usage of JDIC API package org.jdesktop.jdic.browser
31  * (Browser component).
32  */

33
34 public class SimpleBrowser {
35     // Below method reads, changes javascript variables and attributes of DOM elements.
36
private static void testDOMAPI(WebBrowser webBrowser) {
37         System.out.println("===========================================================");
38         System.out.println("=== Test setContent()/getContent()/executeScript() APIs ===");
39         System.out.println("===========================================================");
40                        
41         // Add Chinese/Korean/Japan characters to the content to test unicode
42
// support.
43
String JavaDoc HTML_CONTENT =
44             "<html>" +
45             "<head>" +
46               "<script>" +
47                 "var counter = 100;" +
48                 "function scriptMethod() { " +
49                     "alert('scriptMethod() within the loaded page'); " +
50                 "}" +
51               "</script>" +
52             "</head>" +
53             "<title>Test Page for JDIC Browser Component</title>" +
54             "" +
55             "<body>" +
56             "<div id='theDiv'>This page content is set using setContent() API</div>" +
57             "</body>" +
58             "</html>";
59
60         System.out.println("===============================");
61         System.out.println("=== To test executeScript() ===");
62         System.out.println("===============================");
63         String JavaDoc jscript = "alert(\"alert 'statement' test\");" +
64             "document.bgColor='blue';";
65         String JavaDoc result = webBrowser.executeScript(jscript);
66         System.out.println("Execution of: " + jscript + " returns: " + result);
67
68         System.out.println("============================");
69         System.out.println("=== To test getContent() ===");
70         System.out.println("============================");
71         String JavaDoc content = webBrowser.getContent();
72         System.out.println("getContent() returns: " + content);
73
74         System.out.println("============================");
75         System.out.println("=== To test setContent() ===");
76         System.out.println("============================");
77         System.out.println("To setContent(): " + HTML_CONTENT);
78         webBrowser.setContent(HTML_CONTENT);
79
80         jscript = "scriptMethod();";
81         System.out.println("Execute JavaScript method within the current page ...");
82         result = webBrowser.executeScript(jscript);
83         
84         String JavaDoc retContent = webBrowser.getContent();
85         System.out.println("getContent() returns: " + retContent);
86         
87         // getContent may return the same content in different upper/lower
88
// case or single/double quotation mark.
89
// Here we assume the content is equal if the content lengh is equal.
90
if (retContent != null
91             && retContent.length() == HTML_CONTENT.length()) {
92             System.out.println("=== SUCCEED: getContent() correctly returns "
93                 + "the content set by setContent() !!!");
94         } else {
95             System.out.println("=== ERROR: getContent() doesn't return the "
96                 + "content set by setContent() ???");
97         }
98
99     }
100         
101     public static void main(String JavaDoc[] args) {
102         JFrame frame = new JFrame("JDIC API Demo - SimpleBrowser");
103         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
104
105         final WebBrowser webBrowser = new WebBrowser();
106         
107         //Use below code to check the status of the navigation process,
108
//or register a listener for the notification events.
109
webBrowser.addWebBrowserListener(
110             new WebBrowserListener() {
111             boolean isFirstPage = true;
112                         
113             public void downloadStarted(WebBrowserEvent event) {;}
114             public void downloadCompleted(WebBrowserEvent event) {;}
115             public void downloadProgress(WebBrowserEvent event) {;}
116             public void downloadError(WebBrowserEvent event) {;}
117             public void documentCompleted(WebBrowserEvent event) {
118                 // Uncomment below code to test getContent()/setContent()/
119
// executeScript() APIs.
120
// As the setContent() call will invoke this event, which falls
121
// into a loop, so check if this event is fired by the first
122
// loaded page.
123
/*
124                 if (isFirstPage) {
125                     testDOMAPI(webBrowser);
126                     isFirstPage = false;
127                 }
128                 */

129             }
130             public void titleChange(WebBrowserEvent event) {;}
131             public void statusTextChange(WebBrowserEvent event) {;}
132         });
133
134         try {
135             webBrowser.setURL(new URL JavaDoc("http://java.net"));
136             // Below Chinese website tests unicode support.
137
//webBrowser.setURL(new URL("http://www.google.com/intl/zh-CN/"));
138

139             // Print out debug messages in the command line.
140
//webBrowser.setDebug(true);
141
} catch (MalformedURLException JavaDoc e) {
142             System.out.println(e.getMessage());
143             return;
144         }
145         
146         JPanel panel = new JPanel();
147         panel.setLayout(new BorderLayout());
148         panel.setPreferredSize(new Dimension(700, 500));
149         panel.add(webBrowser, BorderLayout.CENTER);
150         
151         frame.getContentPane().add(panel, BorderLayout.CENTER);
152         frame.pack();
153         frame.setVisible(true);
154     }
155 }
156
Popular Tags