1 package org.enhydra.kelp.ant.swing; 2 3 4 import javax.swing.*; 5 6 import java.awt.*; import java.awt.event.*; 9 import java.net.URL ; 10 import java.io.IOException ; 11 12 import javax.swing.event.*; 13 import javax.swing.text.html.*; 14 15 public class HTMLViewer extends JDialog 17 implements HyperlinkListener { 18 private static JDialog dialog = null; 20 JEditorPane editorPane; 21 LifoURL historyBack = new LifoURL(100); 24 LifoURL historyForward = new LifoURL(100); 25 26 public HTMLViewer(String title, URL url) { 27 super(); 28 this.setTitle(title); 29 init(url); 30 } 31 32 public HTMLViewer(Dialog parent, String title, URL url) { 33 super(parent, title); 34 init(url); 35 } 36 37 private void init(URL url) { 38 GridBagLayout gridBagLayout = new GridBagLayout(); 39 BorderLayout layOut = new BorderLayout(); 40 JPanel editorMainPane = new JPanel(); 41 editorMainPane.setLayout(layOut); 42 43 45 65 JLabel emptyLabel = new JLabel(); 66 setConstr(gridBagLayout, emptyLabel, 2,0,1,1); 67 editorMainPane.add(emptyLabel); 68 69 70 editorPane = createEditorPane(url); 72 editorPane.addHyperlinkListener(this); 73 JScrollPane editorScrollPane = new JScrollPane(editorPane); 74 editorScrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS); 75 76 editorScrollPane.setPreferredSize(new Dimension(750, 500)); 77 editorScrollPane.setMinimumSize(new Dimension(750, 500)); 78 80 editorMainPane.setSize(new Dimension(800, 600)); 81 editorMainPane.add(editorScrollPane); 83 84 setContentPane(editorMainPane); 85 86 87 } 88 89 private void setConstr(GridBagLayout gbl, Component comp, int gx, int gy, int gw, int gh) { 90 gbl.setConstraints(comp, new GridBagConstraints(gx,gy,gw,gh,0,0, 91 GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0,0,0,0), 94 0,0)); } 96 97 public void hyperlinkUpdate(HyperlinkEvent e) { 98 if (e.getEventType() == HyperlinkEvent.EventType.ACTIVATED) { 99 editorPane = (JEditorPane) e.getSource(); 100 if (e instanceof HTMLFrameHyperlinkEvent) { 101 HTMLFrameHyperlinkEvent evt = (HTMLFrameHyperlinkEvent)e; 102 HTMLDocument doc = (HTMLDocument)editorPane.getDocument(); 103 doc.processHTMLFrameHyperlinkEvent(evt); 104 } else { 105 try { 106 historyBack.add(editorPane.getPage()); 107 editorPane.setPage(e.getURL()); 109 } catch (Throwable t) { 110 t.printStackTrace(); 111 } 112 } 113 } 114 } 115 116 private JEditorPane createEditorPane(URL url) { 117 JEditorPane editorPane = new JEditorPane(); 118 editorPane.setEditable(false); 119 displayURL(url, editorPane); 127 131 return editorPane; 132 } 133 134 private void displayURL(URL url, JEditorPane editorPane) { 135 try { 136 editorPane.setPage(url); 137 } catch (IOException e) { 138 System.err.println("Attempted to read a bad URL: " + url); 139 } 140 } 141 142 152 162 public static void createAndShow(Dialog parent, String title, URL url) { 163 if(parent == null){ 164 dialog = new HTMLViewer(title, url); 165 }else{ 166 dialog = new HTMLViewer(parent, title, url); 167 } 168 dialog.addWindowListener(new WindowAdapter() { 169 public void windowClosing(WindowEvent e) { 170 dialog.dispose(); 171 } 172 }); 173 174 dialog.pack(); 175 dialog.show(); 176 } 178 } 179
| Popular Tags
|