1 package SnowMailClient.view.html; 2 3 import snow.utils.storage.*; 4 import snow.utils.gui.*; 5 import snow.text.TextUtils; 6 import SnowMailClient.Language.Language; 7 8 import java.util.*; 9 import java.awt.BorderLayout ; 10 import java.awt.Color ; 11 import java.awt.Insets ; 12 import java.awt.event.*; 13 import javax.swing.*; 14 import javax.swing.border.*; 15 import javax.swing.event.*; 16 import javax.swing.text.html.*; 17 import javax.swing.text.*; 18 import java.text.*; 19 import java.io.*; 20 import java.net.*; 21 22 23 25 public class HTMLViewer extends JDialog 26 { 27 final private StyledEditorKit styledEditorKit = new StyledEditorKit(); 28 final private HTMLEditorKit hTMLEditorKit = new HTMLEditorKit(); 29 30 31 final JEditorPane jed = new JEditorPane(); 32 final JToggleButton tb = new JToggleButton("Html", true); 33 JButton closeBt = new JButton(Language.translate("Close")); 34 35 final JTextField linkLabel = new JTextField(""); 36 final JTextField linkField = new JTextField(); 37 final JLabel lineLabel = new JLabel(); 38 39 public static final int fontSize = UIManager.getFont("Label.font").getSize(); 40 41 final private SnowBackgroundPanel backgroundPanel = new SnowBackgroundPanel(new BorderLayout (0,0)); 42 43 final private static List<URL> history = new ArrayList<URL>(); 44 final private JButton homeButton = new JButton("Home"); 45 final private JButton backButton = new JButton("<"); 46 48 private URL homeURL = null; 49 50 public HTMLViewer( JFrame owner, 51 String title, 52 boolean showToggleTextButton, 53 boolean modal, boolean ok 54 ) 55 { 56 super(owner,title,modal); 57 58 59 getContentPane().add(backgroundPanel, BorderLayout.CENTER); 60 61 jed.setEditable(false); JScrollPane jsp = new JScrollPane(jed); 63 64 backgroundPanel.add(jsp, BorderLayout.CENTER); 65 jed.setOpaque(false); 66 jsp.setOpaque(false); 67 jsp.getViewport().setOpaque(false); 68 69 JPanel northPanel = new JPanel(); 70 GridLayout3 grid = new GridLayout3(6, northPanel); 71 grid.setColumnWeights(new double[]{1,1,1,1,100}); 72 getContentPane().add(northPanel, BorderLayout.NORTH); 73 grid.add(closeBt); 74 GUIUtils.setSmallDimensions(closeBt); 75 if(showToggleTextButton) 76 { 77 grid.add(tb); 78 GUIUtils.setSmallDimensions(tb); 79 } 80 else 81 { 82 grid.add(""); 83 } 84 grid.add( homeButton ); 85 homeButton.setMargin(new Insets (0,2,0,2)); 86 homeButton.setEnabled(false); 87 grid.add( backButton ); 88 backButton.setMargin(new Insets (0,2,0,2)); 89 91 92 93 grid.add(" URL: "); 94 grid.add(linkField, true); 95 96 homeButton.addActionListener(new ActionListener() 97 { 98 public void actionPerformed(ActionEvent e) 99 { 100 try 101 { 102 setURL(homeURL); 103 } 104 catch(Exception ee) 105 { 106 linkLabel.setText("Error: "+ee.getMessage()); 109 } 110 } 111 }); 112 113 backButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent ae) 114 { 115 if(history.size()<2) return; 116 117 URL previous = history.get(history.size()-2); 118 history.remove(history.size()-1); 119 history.remove(history.size()-1); try 121 { 122 setURL(previous); 123 } 124 catch(Exception ee) 125 { 126 linkLabel.setText("Error: "+ee.getMessage()); 129 } 130 } }); 131 132 133 closeBt.setBackground(Color.orange); 134 closeBt.addActionListener(new ActionListener() 135 { 136 public void actionPerformed(ActionEvent e) 137 { 138 setVisible(false); 139 dispose(); 140 } 141 }); 142 143 144 145 tb.addActionListener(new ActionListener() 146 { 147 public void actionPerformed(ActionEvent e) 148 { 149 setContent(jed.getText()); 150 } 151 }); 152 153 154 155 156 linkField.addActionListener(new ActionListener() 157 { 158 public void actionPerformed(ActionEvent e) 159 { 160 try 161 { 162 URL url = new URL( linkField.getText()); 163 setURL(url); 164 } 165 catch(Exception ee) 166 { 167 linkLabel.setText("Error: "+ee.getMessage()); 170 } 171 } 172 }); 173 174 175 JPanel southPanel = new JPanel(); 178 southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS)); 179 getContentPane().add(southPanel, BorderLayout.SOUTH); 180 181 final JLabel lineLabel = new JLabel(); 182 southPanel.add(lineLabel); 183 southPanel.add(linkLabel); 184 185 lineLabel.setBorder(BorderFactory.createCompoundBorder( 186 new EtchedBorder(EtchedBorder.LOWERED), 187 new EmptyBorder(1,1,1,1))); 188 lineLabel.setBackground(getBackground()); 190 191 linkLabel.setBorder(BorderFactory.createCompoundBorder( 192 new EtchedBorder(EtchedBorder.LOWERED), 193 new EmptyBorder(1,1,1,1))); 194 linkLabel.setEditable(false); 195 linkLabel.setBackground(getBackground()); 196 197 198 jed.addHyperlinkListener(new HyperlinkListener() 199 { 200 public void hyperlinkUpdate(HyperlinkEvent e) 201 { 202 linkLabel.setText(Language.translate("link")+": "+e.getURL()); 203 if(e.getEventType()==HyperlinkEvent.EventType.ACTIVATED) 206 { 207 try 208 { 209 setURL(e.getURL()); 210 } 211 catch(Exception ex) 212 { 213 ex.printStackTrace(); 214 } 215 } 216 } 217 }); 218 219 jed.addCaretListener(new CaretListener() 220 { 221 public void caretUpdate(javax.swing.event.CaretEvent ce) 222 { 223 int cp = jed.getCaretPosition(); 224 try 225 { 226 DefaultStyledDocument sd = (DefaultStyledDocument) jed.getDocument(); 227 Element elt = sd.getParagraphElement(cp); 228 lineLabel.setText(Language.translate("Line")+" "+(TextUtils.getPositionInParent(elt)+1)); 229 } 231 catch(Exception e) 232 { 233 } 235 } 236 }); 237 238 239 } 241 public void animateBackground(boolean animate) 242 { 243 if(animate) 244 { 245 backgroundPanel.startAnimation(); 246 } 247 else 248 { 249 backgroundPanel.stopAnimation(); 250 } 251 } 252 253 public void setContentAsText(String cont) 254 { 255 if(tb.isSelected()) 256 { 257 jed.setText(""); tb.setSelected(false); 259 } 260 jed.setEditorKit(this.styledEditorKit); 262 jed.setText(cont); 263 jed.setCaretPosition(0); 264 } 265 266 public void setContent(String cont) 267 { 268 if(tb.isSelected()) 269 { 270 jed.setText(""); 272 jed.setEditorKit(this.hTMLEditorKit); 273 try 274 { 275 jed.setText(cont); 276 jed.setCaretPosition(0); 277 } 278 catch(Exception e) 279 { 280 jed.setEditorKit(this.styledEditorKit); 282 jed.setText("HTML Error:"+e.getMessage()+"\n\n"+cont); 283 jed.setCaretPosition(0); 284 } 285 } 286 else 287 { 288 jed.setEditorKit(this.styledEditorKit); 290 jed.setText(cont); 291 jed.setCaretPosition(0); 292 } 293 } 294 295 public void setHomeURL(URL url) throws Exception 296 { 297 homeURL = url; 298 homeButton.setEnabled(url!=null); 299 setURL(url); 300 } 301 302 304 public void setURL(URL url) throws Exception 305 { 306 try 308 { 309 linkField.setText(""+url); 310 jed.setPage(url); 311 history.add(url); 312 } 314 catch(Exception e) 315 { 316 linkLabel.setText("Error: "+e.getMessage()); 317 } 318 } 319 320 public static void main(String [] arguments) 321 { 322 try 323 { 324 HTMLViewer hv = new HTMLViewer(new JFrame("Test"), "test", false, true, true); 325 hv.setHomeURL(new URL("file:///E:/projects/mail/client/published/snowmail.sn.funpic.de/index.htm")); 326 hv.setSize(1000,900); 327 hv.setVisible(true); 328 } 329 catch(Exception e) 330 { 331 e.fillInStackTrace(); 332 } 333 } 334 335 336 337 338 } | Popular Tags |