KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SnowMailClient > view > html > HTMLSecureViewer


1 package SnowMailClient.view.html;
2
3 import snow.utils.storage.*;
4 import snow.utils.gui.*;
5 import snow.text.*;
6 import SnowMailClient.html.*;
7 import SnowMailClient.Language.Language;
8
9 import java.awt.*;
10 import java.awt.event.*;
11 import javax.swing.*;
12 import javax.swing.border.*;
13 import javax.swing.event.*;
14 import javax.swing.text.html.*;
15 import javax.swing.text.*;
16 import java.text.*;
17 import java.io.*;
18 import java.net.*;
19
20
21 /** An HTML viewer
22     1) source view
23     2) extracted text
24     3) filtered HTML
25     4) full HTML
26 */

27 public class HTMLSecureViewer extends JDialog
28 {
29   final static private boolean debug = false;
30
31   final private StyledEditorKit styledEditorKit = new StyledEditorKit();
32   final private HTMLEditorKit hTMLEditorKit = new HTMLEditorKit();
33
34   final JTextPane textPane = new JTextPane();
35   final JToggleButton sourceButton = new JToggleButton(Language.translate("Source"));
36   final JToggleButton textButton = new JToggleButton(Language.translate("Text"));
37   final JToggleButton filteredHtmlButton = new JToggleButton(Language.translate("filtered HTML"));
38   final JToggleButton htmlButton = new JToggleButton(Language.translate("HTML"));
39   final JToggleButton htmlFromText = new JToggleButton(Language.translate("HTML from text"));
40   final JToggleButton closeButton = new JToggleButton(Language.translate("Close"));
41
42   final JTextField linkLabel = new JTextField("");
43   final JTextField linkField = new JTextField();
44
45   final static int fontSize = UIManager.getFont("Label.font").getSize();
46
47   String JavaDoc htmlSourceContent = "";
48
49   public HTMLSecureViewer( JFrame owner,
50                             String JavaDoc title )
51   {
52      super(owner,title,true);
53
54
55      textPane.setEditable(false); // important for HTML
56
if(debug) textPane.setEditable(true);
57
58      JScrollPane jsp = new JScrollPane(textPane);
59      getContentPane().add(jsp, BorderLayout.CENTER);
60      textPane.setOpaque(false);
61      jsp.setOpaque(false);
62      jsp.getViewport().setOpaque(false);
63
64      JPanel northPanel = new JPanel();
65      northPanel.setLayout(new BoxLayout(northPanel, BoxLayout.X_AXIS));
66      northPanel.setBorder(new EmptyBorder(2,4,2,4));
67      getContentPane().add(northPanel, BorderLayout.NORTH);
68      northPanel.add(closeButton);
69      northPanel.add(Box.createHorizontalGlue());
70
71      northPanel.add(sourceButton);
72      northPanel.add(textButton);
73      northPanel.add(filteredHtmlButton);
74      northPanel.add(htmlButton);
75      if(debug)
76      {
77        northPanel.add(htmlFromText);
78      }
79
80      ButtonGroup bg = new ButtonGroup();
81      bg.add(sourceButton);
82      bg.add(textButton);
83      bg.add(filteredHtmlButton);
84      bg.add(htmlButton);
85      bg.add(htmlFromText);
86
87      GUIUtils.setSmallDimensions(closeButton);
88      GUIUtils.setSmallDimensions(sourceButton);
89      GUIUtils.setSmallDimensions(textButton);
90      GUIUtils.setSmallDimensions(filteredHtmlButton);
91      GUIUtils.setSmallDimensions(htmlButton);
92      GUIUtils.setSmallDimensions(htmlFromText);
93
94
95      closeButton.setBackground(Color.orange);
96      closeButton.addActionListener(new ActionListener()
97      {
98        public void actionPerformed(ActionEvent e)
99        {
100           setVisible(false);
101           dispose();
102        }
103      });
104
105
106
107      this.sourceButton.addActionListener(new ActionListener()
108      {
109        public void actionPerformed(ActionEvent e)
110        {
111          displaySourceTextAction();
112        }
113      });
114
115      this.textButton.addActionListener(new ActionListener()
116      {
117        public void actionPerformed(ActionEvent e)
118        {
119          displayTextAction();
120        }
121      });
122
123      filteredHtmlButton.addActionListener(new ActionListener()
124      {
125        public void actionPerformed(ActionEvent e)
126        {
127          displayFilteredHTMLAction();
128        }
129      });
130
131      this.htmlButton.addActionListener(new ActionListener()
132      {
133        public void actionPerformed(ActionEvent e)
134        {
135          displayHTMLAction();
136        }
137      });
138
139      htmlFromText.addActionListener(new ActionListener()
140      {
141        public void actionPerformed(ActionEvent e)
142        {
143          displayHTMLFromTextAction();
144        }
145      });
146
147      linkField.addActionListener(new ActionListener()
148      {
149        public void actionPerformed(ActionEvent e)
150        {
151          try
152          {
153            URL url = new URL( linkField.getText());
154            textPane.setPage(url); // Attention: SECURITY !!!
155
}
156          catch(Exception JavaDoc ee)
157          {
158            ee.printStackTrace();
159            linkLabel.setText("Error: "+ee.getMessage());
160          }
161        }
162      });
163
164
165      // south
166
//
167
JPanel southPanel = new JPanel();
168      southPanel.setLayout(new BoxLayout(southPanel, BoxLayout.X_AXIS));
169      getContentPane().add(southPanel, BorderLayout.SOUTH);
170
171      final JLabel lineLabel = new JLabel();
172      southPanel.add(lineLabel);
173      southPanel.add(linkLabel);
174
175      lineLabel.setBorder(BorderFactory.createCompoundBorder(
176        new EtchedBorder(EtchedBorder.LOWERED),
177        new EmptyBorder(1,1,1,1)));
178      //lineLabel.setEditable(false);
179
lineLabel.setBackground(getBackground());
180
181      linkLabel.setBorder(BorderFactory.createCompoundBorder(
182        new EtchedBorder(EtchedBorder.LOWERED),
183        new EmptyBorder(1,1,1,1)));
184      linkLabel.setEditable(false);
185      linkLabel.setBackground(getBackground());
186
187
188      textPane.addHyperlinkListener(new HyperlinkListener()
189      {
190        public void hyperlinkUpdate(HyperlinkEvent e)
191        {
192           linkLabel.setText(Language.translate("link")+": "+e.getURL());
193           // System.out.println("descr="+e.getDescription());
194
// System.out.println("elt="+e.getSourceElement());
195
}
196      });
197
198
199      textPane.addCaretListener(new CaretListener()
200      {
201        public void caretUpdate(javax.swing.event.CaretEvent JavaDoc ce)
202        {
203           int cp = textPane.getCaretPosition();
204           try
205           {
206             DefaultStyledDocument sd = (DefaultStyledDocument) textPane.getDocument();
207             Element elt = sd.getParagraphElement(cp);
208
209             lineLabel.setText(Language.translate("Line")+" "+(TextUtils.getPositionInParent(elt)+1));
210             //Arrays sd.getRootElements()[0];
211
}
212           catch(Exception JavaDoc e)
213           {
214             e.printStackTrace();
215           }
216        }
217      });
218
219   } // Constructor
220

221   public void setHTMLSource(String JavaDoc html)
222   {
223     this.htmlSourceContent = html;
224     filteredHtmlButton.setSelected(true);
225     displayFilteredHTMLAction();
226   }
227
228   public void setText(String JavaDoc html)
229   {
230     this.htmlSourceContent = html;
231     this.sourceButton.setSelected(true);
232     displaySourceTextAction();
233   }
234
235   private void displaySourceTextAction()
236   {
237     textPane.setText("");
238     this.textPane.setEditorKit(styledEditorKit);
239     textPane.setText(htmlSourceContent);
240     textPane.setCaretPosition(0);
241   }
242
243   private void displayTextAction()
244   {
245     try
246     {
247       HTMLTextExtractor he = new HTMLTextExtractor( htmlSourceContent, true);
248
249       textPane.setText("");
250       this.textPane.setEditorKit(styledEditorKit);
251       textPane.setText(he.getTextOnly());
252       textPane.setCaretPosition(0);
253     }
254     catch(Exception JavaDoc e)
255     {
256       this.textPane.setEditorKit(styledEditorKit);
257       textPane.setText(Language.translate("Error")+": "+e.getMessage());
258     }
259   }
260
261   /** problem... some tags cause a display crash !! (just a beep is emmitted, the panel remains empty).
262
263   */

264   private void displayHTMLAction()
265   {
266 /* textPane.setText("");
267     this.textPane.setEditorKit(this.hTMLEditorKit);
268     String html = this.htmlSourceContent;
269 / //int start = StringUtils.indexOfStartTagIgnoreCase(html, "html")+5;
270     if(start>=0)
271     {
272       html = "`"+html.substring(start);
273       //System.out.println("***"+html.substring(0,10));
274     }*
275
276     textPane.setText(html);
277     textPane.setCaretPosition(0); */

278
279     try
280     {
281        HTMLCleaner hc = new HTMLCleaner(htmlSourceContent, false);
282        String JavaDoc fContent = hc.getCleanHTMLText();
283        //System.out.println(""+hc.getCleanHTMLText());
284

285        textPane.setText("");
286        this.textPane.setEditorKit(this.hTMLEditorKit);
287        textPane.setText(fContent);
288        textPane.setCaretPosition(0);
289     }
290     catch(Exception JavaDoc e)
291     {
292        this.textPane.setEditorKit(styledEditorKit);
293        textPane.setText(Language.translate("Error")+": "+e.getMessage());
294     }
295
296
297   }
298
299   private void displayHTMLFromTextAction()
300   {
301     String JavaDoc html = textPane.getText();
302     this.textPane.setEditorKit(this.hTMLEditorKit);
303     textPane.setText(html);
304     textPane.setCaretPosition(0);
305
306   }
307
308
309   private void displayFilteredHTMLAction()
310   {
311     try
312     {
313        HTMLCleaner hc = new HTMLCleaner(htmlSourceContent, true);
314        String JavaDoc fContent = hc.getCleanHTMLText();
315        //System.out.println(""+hc.getCleanHTMLText());
316

317        textPane.setText("");
318        this.textPane.setEditorKit(this.hTMLEditorKit);
319        textPane.setText(fContent);
320        textPane.setCaretPosition(0);
321     }
322     catch(Exception JavaDoc e)
323     {
324        this.textPane.setEditorKit(styledEditorKit);
325        textPane.setText(Language.translate("Error")+": "+e.getMessage());
326     }
327   }
328
329
330   public static void main(String JavaDoc[] aaa)
331   {
332     JFrame f = new JFrame("");
333     HTMLSecureViewer hs = new HTMLSecureViewer(f, "test");
334     hs.setHTMLSource("<h1>Hello</h1><br><h2>title2</h2>...");
335     hs.setSize(400, 600);
336     hs.setVisible(true);
337
338   }
339
340 } // HTMLSecureViewer
Popular Tags