KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > gui > htmlviewer > JDICHTMLViewerPlugin


1 package org.columba.core.gui.htmlviewer;
2
3 import java.awt.BorderLayout JavaDoc;
4 import java.awt.event.ComponentEvent JavaDoc;
5 import java.awt.event.ComponentListener JavaDoc;
6 import java.util.logging.Logger JavaDoc;
7
8 import javax.swing.JComponent JavaDoc;
9 import javax.swing.JPanel JavaDoc;
10
11 import org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin;
12 import org.columba.core.logging.Logging;
13 import org.jdesktop.jdic.browser.WebBrowser;
14
15 /**
16  * JDIC-enabled web browser component used by the Message Viewer in component
17  * mail.
18  * <p>
19  * Note: Java Proxy support/configuration doesn't has any effect. This component
20  * uses your system's proxy settings. For example, when using Firefox, you have
21  * to set your proxy in Firefox and these same options are also used in Columba.
22  * <p>
23  * Javascript support can be used to access DOM. This way we can for example
24  * print the HTML page using:
25  * <code>webBrowser.executeScript("window.print();");</code>
26  * <p>
27  * TODO (@author fdietz): how to use images in Message Viewer, we can't set a
28  * base URL and load images from columba.jar?
29  *
30  * @author Frederik Dietz
31  *
32  */

33 public class JDICHTMLViewerPlugin extends JPanel JavaDoc implements IHTMLViewerPlugin {
34
35     /** JDK 1.4+ logging framework logger, used for logging. */
36     private static final Logger JavaDoc LOG = Logger
37             .getLogger("org.columba.core.gui.htmlviewer");
38
39     private WebBrowser browser;
40
41     private boolean initialized = false;
42
43     public JDICHTMLViewerPlugin() {
44         super();
45
46         try {
47             WebBrowser.setDebug(true);
48
49             browser = new WebBrowser(true);
50
51             // turn of focus stealing (workaround should be removed in the
52
// future!)
53
browser.setFocusable(false);
54
55             setLayout(new BorderLayout JavaDoc());
56             add(browser, BorderLayout.CENTER);
57
58         } catch (Error JavaDoc e) {
59             LOG.severe("Error while initializing JDIC native browser: "
60                     + e.getMessage());
61             if (Logging.DEBUG)
62                 e.printStackTrace();
63         } catch (Exception JavaDoc e) {
64             LOG
65                     .severe("Exception error while initializing JDIC native browser: "
66                             + e.getMessage());
67             if (Logging.DEBUG)
68                 e.printStackTrace();
69         }
70
71         addComponentListener(new ComponentListener JavaDoc() {
72
73             public void componentHidden(ComponentEvent JavaDoc e) {
74                 browser.setVisible(false);
75                 browser = null;
76             }
77
78             public void componentMoved(ComponentEvent JavaDoc e) {
79                 // TODO Auto-generated method stub
80

81             }
82
83             public void componentResized(ComponentEvent JavaDoc e) {
84                 // TODO Auto-generated method stub
85

86             }
87
88             public void componentShown(ComponentEvent JavaDoc e) {
89                 // TODO Auto-generated method stub
90

91             }
92
93         });
94
95     }
96
97     public void view(String JavaDoc htmlSource) {
98         browser.setContent(htmlSource);
99     }
100
101     public JComponent JavaDoc getComponent() {
102         return this;
103     }
104
105     public String JavaDoc getSelectedText() {
106         try {
107             String JavaDoc selectedTextFromScript = browser
108                     .executeScript("(window.getSelection) ? window.getSelection() : "
109                             + "(document.getSelection) ? document.getSelection() : "
110                             + "(document.selection) ? document.selection.createRange().text : "
111                             + "null");
112             return selectedTextFromScript;
113         } catch (Exception JavaDoc e) {
114             e.printStackTrace();
115         }
116
117         return "";
118     }
119
120     public boolean initialized() {
121         return true;
122     }
123
124     public JComponent JavaDoc getContainer() {
125         return this;
126     }
127
128     public String JavaDoc getText() {
129         return browser.getContent();
130     }
131
132     /**
133      * @see org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin#setCaretPosition(int)
134      */

135     public void setCaretPosition(int position) {
136         // TODO
137
}
138
139     /**
140      * @see org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin#moveCaretPosition(int)
141      */

142     public void moveCaretPosition(int position) {
143         // TODO
144
}
145
146 }
147
Popular Tags