View Javadoc

1   /*
2    * @(#)$Id: HtmlPanel.java 689 2009-07-22 00:10:27Z bsigner $
3    *
4    * Author		:	Ueli Kurmann, igesture@uelikurmann.ch
5    *                  
6    *
7    * Purpose		: 
8    *
9    * -----------------------------------------------------------------------
10   *
11   * Revision Information:
12   *
13   * Date				Who			Reason
14   *
15   * 18.10.2008		ukurmann	Initial Release
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.tool.util;
28  
29  import java.awt.BorderLayout;
30  import java.awt.Dimension;
31  import java.net.URL;
32  import java.util.logging.Level;
33  import java.util.logging.Logger;
34  
35  import javax.swing.JEditorPane;
36  import javax.swing.JPanel;
37  import javax.swing.JScrollPane;
38  import javax.swing.SwingUtilities;
39  import javax.swing.plaf.basic.BasicHTML;
40  
41  import org.sigtec.util.Constant;
42  import org.sigtec.util.MIME;
43  
44  
45  /**
46   * Comment
47   * @version 1.0 18.10.2008
48   * @author Ueli Kurmann
49   */
50  public class HtmlPanel extends JPanel {
51  
52     private static Logger LOGGER = Logger.getLogger(HtmlPanel.class.getName());
53  
54     private JEditorPane htmlArea;
55  
56  
57     /**
58      * 
59      * @param url URL of the html document
60      * @param dimension dimension of the html area
61      */
62     public HtmlPanel(URL url, Dimension dimension) {
63        init(url, dimension);
64     }
65  
66  
67     private void init(final URL url, Dimension dimension) {
68  
69        final Dimension finalDimension = dimension;
70  
71        SwingUtilities.invokeLater(new Runnable() {
72  
73           @Override
74           public void run() {
75              setLayout(new BorderLayout());
76              setPreferredSize(finalDimension);
77              try {
78  
79                 if (url == null) {
80                    htmlArea = new JEditorPane();
81                 }
82                 else {
83                    htmlArea = new JEditorPane(url);
84                 }
85                 htmlArea.setEditable(false);
86                 htmlArea.setContentType(MIME.toString(MIME.HTML));
87                 htmlArea.putClientProperty(BasicHTML.documentBaseKey,
88                       HtmlPanel.class.getResource(Constant.SLASH));
89  
90                 // htmlArea.putClientProperty(JEditorPane.HONOR_DISPLAY_PROPERTIES,
91                 // Boolean.TRUE);
92  
93                 htmlArea.setPreferredSize(finalDimension);
94                 JScrollPane scrollPane = new JScrollPane(htmlArea);
95                 scrollPane.setPreferredSize(finalDimension);
96                 scrollPane.setAutoscrolls(true);
97                 add(scrollPane, BorderLayout.CENTER);
98              }
99              catch (Exception e) {
100                LOGGER.log(Level.SEVERE, Constant.EMPTY_STRING, e);
101             }
102 
103          }
104 
105       });
106 
107    }
108 
109 
110    public void setHtmlContent(String htmlCode) {
111       htmlArea.setText(htmlCode);
112    }
113 
114 }