KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > j2ee > blueprints > ui > HtmlBrowserWithScrollPosition


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.j2ee.blueprints.ui;
21
22 import java.io.*;
23 import java.awt.*;
24 import java.awt.event.*;
25 import java.net.*;
26 import javax.swing.*;
27 import javax.swing.event.*;
28 import javax.swing.text.*;
29 import javax.swing.text.html.*;
30 import javax.swing.Timer JavaDoc;
31
32 import org.openide.ErrorManager;
33 import org.openide.awt.HtmlBrowser;
34
35 public class HtmlBrowserWithScrollPosition extends JPanel implements HyperlinkListener {
36
37     // for displaying in the external browser
38
private HtmlBrowser.URLDisplayer displayer = HtmlBrowser.URLDisplayer.getDefault();
39     /** Timer to use for scrolling this tab */
40     private Timer JavaDoc scrollTimer = null;
41     private JLabel statusBar;
42     // html rendering pane
43
protected JEditorPane html;
44     
45     public HtmlBrowserWithScrollPosition() {
46         setLayout(new BorderLayout());
47
48         // construct html pane
49
html = new JEditorPane();
50         html.setEditable(false);
51         html.addHyperlinkListener(this);
52
53         add(new JScrollPane(html), BorderLayout.CENTER);
54         
55         statusBar = new JLabel(" "); // NOI18N
56
statusBar.setBackground(new java.awt.Color JavaDoc(80, 80, 80));
57         add(statusBar, BorderLayout.SOUTH);
58
59     }
60
61     // override the method in HyperLinkListener
62
public void hyperlinkUpdate(HyperlinkEvent e) {
63         HyperlinkEvent.EventType type = e.getEventType();
64         if (type == HyperlinkEvent.EventType.ACTIVATED) {
65             setURL(e.getURL());
66         } else if (type == HyperlinkEvent.EventType.ENTERED) {
67             statusBar.setText(e.getURL().toString());
68         } else if (type == HyperlinkEvent.EventType.EXITED) {
69             statusBar.setText(" "); // NOI18N
70
}
71     }
72  
73     protected void setURL(URL u) {
74         if (u!=null) {
75             String JavaDoc protocol = u.getProtocol();
76             // spawn the external browser for these types of links
77
if (protocol != null && (protocol.equals("http") || protocol.equals("https"))) {
78                 displayer.showURL(u);
79             } else {
80                 Cursor currentC = html.getCursor();
81                 Cursor busyC = Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR);
82                 html.setCursor(busyC);
83                 try {
84                     html.setPage(u);
85                 } catch (IOException e) {
86                     statusBar.setText(java.util.ResourceBundle.getBundle("org/netbeans/modules/j2ee/blueprints/ui/Bundle").getString("doc_not_found"));
87                 } finally {
88                     html.setCursor(currentC);
89                 }
90             }
91         }
92     }
93     
94     /**
95      * Returns the current position within this document, suitable for
96      * passing to setBrowserScrollPosition.
97      *
98      * @return the scroll position
99      */

100     public int getScrollPosition() {
101         int result = 0;
102         // If this browser has a scroll pane, use it to determine
103
// the current position.
104
Component c = getComponent(0);
105         if(c instanceof JScrollPane) {
106             JScrollPane pane = (JScrollPane)c;
107             result = pane.getVerticalScrollBar().getValue();
108         }
109         return result;
110     }
111     
112     /**
113      * Sets the current scroll position in this browser.
114      * If the document hasn't fully loaded, this method will retry
115      * for five seconds, once every 0.1 seconds.
116      *
117      * @param position The new scroll position, as returned from a
118      * previous call to getScrollPosition().
119      */

120     public void setScrollPosition(final int position) {
121         if(this.scrollTimer != null) {
122             this.scrollTimer.stop();
123         }
124         this.scrollTimer = new Timer JavaDoc(100,
125             new ActionListener() {
126                 int timeout = 50;
127                 public void actionPerformed(ActionEvent e) {
128                     boolean done = true;
129                     // If this browser has a scroll pane, use it to set
130
// the current position.
131
Component c = getComponent(0);
132                     if(c instanceof JScrollPane) {
133                         JScrollPane pane = (JScrollPane)c;
134                         JScrollBar bar = pane.getVerticalScrollBar();
135                         if(position <= bar.getMaximum()) {
136                             bar.setValue(position);
137                         }
138                         else {
139                             // Couldn't set yet. Retry later.
140
done = false;
141                         }
142                     }
143                     timeout--;
144                     if(timeout <= 0) done = true;
145                     if(done) {
146                         scrollTimer.stop();
147                         scrollTimer = null;
148                     }
149                 }
150             }
151         );
152         this.scrollTimer.start();
153     }
154 }
155
Popular Tags