1 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 ; 31 32 import org.openide.ErrorManager; 33 import org.openide.awt.HtmlBrowser; 34 35 public class HtmlBrowserWithScrollPosition extends JPanel implements HyperlinkListener { 36 37 private HtmlBrowser.URLDisplayer displayer = HtmlBrowser.URLDisplayer.getDefault(); 39 40 private Timer scrollTimer = null; 41 private JLabel statusBar; 42 protected JEditorPane html; 44 45 public HtmlBrowserWithScrollPosition() { 46 setLayout(new BorderLayout()); 47 48 html = new JEditorPane(); 50 html.setEditable(false); 51 html.addHyperlinkListener(this); 52 53 add(new JScrollPane(html), BorderLayout.CENTER); 54 55 statusBar = new JLabel(" "); statusBar.setBackground(new java.awt.Color (80, 80, 80)); 57 add(statusBar, BorderLayout.SOUTH); 58 59 } 60 61 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(" "); } 71 } 72 73 protected void setURL(URL u) { 74 if (u!=null) { 75 String protocol = u.getProtocol(); 76 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 100 public int getScrollPosition() { 101 int result = 0; 102 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 120 public void setScrollPosition(final int position) { 121 if(this.scrollTimer != null) { 122 this.scrollTimer.stop(); 123 } 124 this.scrollTimer = new Timer (100, 125 new ActionListener() { 126 int timeout = 50; 127 public void actionPerformed(ActionEvent e) { 128 boolean done = true; 129 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 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 |