KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > webclient > WebclientBrowserComponent


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.webclient;
21
22 import java.awt.*;
23 import java.awt.event.*;
24 import java.beans.PropertyChangeListener JavaDoc;
25 import java.beans.PropertyChangeEvent JavaDoc;
26 // import javax.swing.*;
27

28 import org.openide.windows.TopComponent;
29
30 import org.mozilla.webclient.*;
31
32 import org.openide.util.NbBundle;
33 import org.openide.util.RequestProcessor;
34
35 /**
36  * Adds some functionality to Mozilla browser.
37  *
38  * @author Radim.Kubacki@sun.com
39  */

40 class WebclientBrowserComponent extends Frame {
41
42     private static final int DEFAULT_WIDTH = 400;
43     private static final int DEFAULT_HEIGHT = 350;
44
45     private static final boolean debug = true;
46     
47     private WebclientBrowserImpl browserImpl;
48     /** java.awt.Canvas for drawing of pages. */
49     private BrowserControlCanvas browser;
50     /** toolbar button */
51     private Button bPrev;
52     /** toolbar button */
53     private Button bNext;
54     /** toolbar button */
55     private Button bHome;
56     /** toolbar button */
57     private Button bReload;
58     /** toolbar button */
59     private Button bStop;
60     
61     private TextField tfLocation;
62     
63     private BrowserListener bListener;
64     
65     /** Creates new MozillaBrowserComponent */
66     public WebclientBrowserComponent (WebclientBrowserImpl browserImpl) {
67         super ();
68         if (debug) System.out.println ("Creating MozillaBrowserComponent"); // NOI18N
69
setLayout (new BorderLayout ());
70         this.browserImpl = browserImpl;
71         
72         try {
73             browser = (BrowserControlCanvas)
74                     browserImpl.getBrowserControl ().queryInterface(BrowserControl.BROWSER_CONTROL_CANVAS_NAME);
75             add (browser, "Center"); // NOI18N
76
browser.setVisible (true);
77             
78             bListener = new BrowserListener ();
79             addWindowListener(bListener);
80             
81             initToolbar ();
82             setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
83         }
84         catch (ClassNotFoundException JavaDoc ex) {
85             ex.printStackTrace ();
86         }
87         catch (NullPointerException JavaDoc ex) {
88             ex.printStackTrace ();
89         }
90     }
91    
92     private void initToolbar () {
93         Panel toolbar = new Panel ();
94         toolbar.add (bPrev = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Prev")));
95         toolbar.add (bNext = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Next")));
96         toolbar.add (bStop = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Stop")));
97         toolbar.add (bReload = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Reload")));
98         toolbar.add (bHome = new Button (NbBundle.getMessage(WebclientBrowserComponent.class,"LBL_Home")));
99         toolbar.add (tfLocation = new TextField (30));
100         add (toolbar, "North"); // NOI18N
101
bPrev.addActionListener(bListener);
102         bNext.addActionListener(bListener);
103         bStop.addActionListener(bListener);
104         bReload.addActionListener(bListener);
105         bHome.addActionListener(bListener);
106         tfLocation.addActionListener(bListener);
107         browserImpl.addPropertyChangeListener(bListener);
108         refreshToolbar ();
109     }
110     
111     /** Updates toolbar buttons and location */
112     void refreshToolbar () {
113         bPrev.setEnabled(browserImpl.isBackward());
114         bNext.setEnabled(browserImpl.isForward());
115         java.net.URL JavaDoc url = browserImpl.getURL();
116         if (url != null) {
117             if (debug) System.out.println("setText to "+url); // NOI18N
118
tfLocation.setText(url.toString());
119         }
120     }
121
122     /**
123      * Initialize rest of Webclient interfaces.
124      */

125     public void addNotify() {
126         super.addNotify ();
127         browserImpl.initialize ();
128     }
129     
130     /**
131     * Returns preferred size.
132     */

133     public java.awt.Dimension JavaDoc getPreferredSize () {
134         java.awt.Dimension JavaDoc superPref = super.getPreferredSize ();
135         return new java.awt.Dimension JavaDoc (
136                    Math.max (DEFAULT_WIDTH, superPref.width),
137                    Math.max (DEFAULT_HEIGHT, superPref.height)
138                );
139     }
140
141     /**
142      * browser implementation does most of work
143      */

144     private void delete () {
145         browser.setVisible (false);
146         browser = null;
147     }
148     
149     private class BrowserListener
150     implements ActionListener, WindowListener, PropertyChangeListener JavaDoc, Runnable JavaDoc {
151         public void actionPerformed (ActionEvent e) {
152             if (e.getSource () == bPrev) {
153                 browserImpl.backward();
154             }
155             else if (e.getSource () == bNext) {
156                 browserImpl.forward();
157             }
158             else if (e.getSource () == bHome) {
159                 try {
160                     java.net.URL JavaDoc homePage = new java.net.URL JavaDoc (org.openide.awt.HtmlBrowser.getHomePage());
161                     browserImpl.setURL(homePage);
162                 }
163                 catch (java.net.MalformedURLException JavaDoc ex) {
164                     browserImpl.setStatusMessage(NbBundle.getMessage(WebclientBrowserComponent.class,"MSG_Cannot_get_home_page"));
165                 }
166             }
167             else if (e.getSource () == bStop) {
168                 browserImpl.stopLoading();
169             }
170             else if (e.getSource () == bReload) {
171                 browserImpl.reloadDocument ();
172             }
173             else if (e.getSource () == tfLocation) {
174                 try {
175                     browserImpl.setURL (new java.net.URL JavaDoc (tfLocation.getText()));
176                 }
177                 catch (java.net.MalformedURLException JavaDoc ex) {
178                     tfLocation.setText ("");
179                 }
180             }
181         }
182         
183         public void propertyChange (PropertyChangeEvent JavaDoc evt) {
184             String JavaDoc property = evt.getPropertyName ();
185             if (property == null) {
186                 RequestProcessor.postRequest (this);
187                 return;
188             }
189
190             if (property.equals (browserImpl.PROP_URL) ||
191                 property.equals (browserImpl.PROP_TITLE) ||
192                 property.equals (browserImpl.PROP_BACKWARD) ||
193                 property.equals (browserImpl.PROP_FORWARD))
194                 RequestProcessor.postRequest (this);
195         }
196
197         
198         private void close () {
199             // System.out.println("destroying the BrowserControl");
200
// WebclientBrowserComponent.this.delete ();
201
// should close the BrowserControlCanvas
202
browserImpl.destroy ();
203         }
204         
205         public void windowClosing(WindowEvent e) {
206             setVisible (false);
207         }
208         public void windowClosed(WindowEvent e) {}
209         public void windowActivated(java.awt.event.WindowEvent JavaDoc windowEvent) {}
210         public void windowDeactivated(java.awt.event.WindowEvent JavaDoc windowEvent) {}
211         public void windowDeiconified(java.awt.event.WindowEvent JavaDoc windowEvent) {}
212         public void windowIconified(java.awt.event.WindowEvent JavaDoc windowEvent) {}
213         public void windowOpened(java.awt.event.WindowEvent JavaDoc windowEvent) {}
214         
215         public void run() {
216             refreshToolbar();
217         }
218         
219     }
220 }
Popular Tags