File: SimpleBrowser.java

package info (click to toggle)
libjdic-java 0.9.5-2
  • links: PTS, VCS
  • area: contrib
  • in suites: lenny
  • size: 2,704 kB
  • ctags: 2,027
  • sloc: java: 8,666; xml: 3,843; cpp: 3,435; ansic: 653; sh: 318; makefile: 213
file content (166 lines) | stat: -rwxr-xr-x 6,888 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2004 Sun Microsystems, Inc. All rights reserved. Use is
 * subject to license terms.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Lesser GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA.
 */

import java.awt.*;
import javax.swing.*;
import java.net.URL;
import java.net.MalformedURLException;
import org.jdesktop.jdic.browser.*;

/**
 * JDIC API demo main class.
 * <p>
 * SimpleBrowser demonstrate the usage of JDIC API package org.jdesktop.jdic.browser
 * (Browser component).
 */

public class SimpleBrowser {
    // Below method reads, changes javascript variables and attributes of DOM elements.
    private static void testDOMAPI(WebBrowser webBrowser) {
        System.out.println("===========================================================");
        System.out.println("=== Test setContent()/getContent()/executeScript() APIs ===");
        System.out.println("===========================================================");

        // Add Chinese/Korean/Japan characters to the content to test unicode
        // support.
        String HTML_CONTENT =
            "<html>" +
            "<head>" +
              "<script>" +
                "var counter = 100;" +
                "function scriptMethod() { " +
                    "alert('scriptMethod() within the loaded page'); " +
                "}" +
              "</script>" +
            "</head>" +
            "<title>Test Page for JDIC Browser Component</title>" +
            "" +
            "<body>" +
            "<div id='theDiv'>This page content is set using setContent() API</div>" +
            "</body>" +
            "</html>";

        System.out.println("===============================");
        System.out.println("=== To test executeScript() ===");
        System.out.println("===============================");
        String jscript = "alert(\"alert 'statement' test\");" +
            "document.bgColor='blue';";
        String result = webBrowser.executeScript(jscript);
        System.out.println("Execution of: " + jscript + " returns: " + result);

        System.out.println("============================");
        System.out.println("=== To test getContent() ===");
        System.out.println("============================");
        String content = webBrowser.getContent();
        System.out.println("getContent() returns: " + content);

        System.out.println("============================");
        System.out.println("=== To test setContent() ===");
        System.out.println("============================");
        System.out.println("To setContent(): " + HTML_CONTENT);
        webBrowser.setContent(HTML_CONTENT);

        jscript = "scriptMethod();";
        System.out.println("Execute JavaScript method within the current page ...");
        result = webBrowser.executeScript(jscript);

        String retContent = webBrowser.getContent();
        System.out.println("getContent() returns: " + retContent);

        // getContent may return the same content in different upper/lower
        // case or single/double quotation mark.
        // Here we assume the content is equal if the content lengh is equal.
        if (retContent != null
            && retContent.length() == HTML_CONTENT.length()) {
            System.out.println("=== SUCCEED: getContent() correctly returns "
                + "the content set by setContent() !!!");
        } else {
            System.out.println("=== ERROR: getContent() doesn't return the "
                + "content set by setContent() ???");
        }

    }

    public static void main(String[] args) {
        JFrame frame = new JFrame("JDIC API Demo - SimpleBrowser");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        final WebBrowser webBrowser = new WebBrowser();

        //Use below code to check the status of the navigation process,
        //or register a listener for the notification events.
        webBrowser.addWebBrowserListener(
            new WebBrowserListener() {
            boolean isFirstPage = true;

            public void downloadStarted(WebBrowserEvent event) {;}
            public void downloadCompleted(WebBrowserEvent event) {;}
            public void downloadProgress(WebBrowserEvent event) {;}
            public void downloadError(WebBrowserEvent event) {;}
            public void documentCompleted(WebBrowserEvent event) {
                // Uncomment below code to test getContent()/setContent()/
                // executeScript() APIs.
                // As the setContent() call will invoke this event, which falls
                // into a loop, so check if this event is fired by the first
                // loaded page.
                /*
                if (isFirstPage) {
                    testDOMAPI(webBrowser);
                    isFirstPage = false;
                }
                */
            }
            public void titleChange(WebBrowserEvent event) {;}
            public void statusTextChange(WebBrowserEvent event) {;}
            public void windowClose(WebBrowserEvent event) {
                if(JOptionPane.YES_OPTION==JOptionPane.showConfirmDialog(
                    webBrowser,
                    "The webpage you are viewing is trying to close the window.\n Do you want to close this window?",
                    "Warning",
                    JOptionPane.YES_NO_OPTION,
                    JOptionPane.QUESTION_MESSAGE))
                {
                     System.exit(0);
                }
            }
        });

        try {
            webBrowser.setURL(new URL("http://java.net"));
            // Below Chinese website tests unicode support.
            //webBrowser.setURL(new URL("http://www.google.com/intl/zh-CN/"));

            // Print out debug messages in the command line.
            //webBrowser.setDebug(true);
        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
            return;
        }

        JPanel panel = new JPanel();
        panel.setLayout(new BorderLayout());
        panel.setPreferredSize(new Dimension(700, 500));
        panel.add(webBrowser, BorderLayout.CENTER);

        frame.getContentPane().add(panel, BorderLayout.CENTER);
        frame.pack();
        frame.setVisible(true);
    }
}