001: package org.columba.core.gui.htmlviewer;
002:
003: import java.awt.BorderLayout;
004: import java.awt.event.ComponentEvent;
005: import java.awt.event.ComponentListener;
006: import java.util.logging.Logger;
007:
008: import javax.swing.JComponent;
009: import javax.swing.JPanel;
010:
011: import org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin;
012: import org.columba.core.logging.Logging;
013: import org.jdesktop.jdic.browser.WebBrowser;
014:
015: /**
016: * JDIC-enabled web browser component used by the Message Viewer in component
017: * mail.
018: * <p>
019: * Note: Java Proxy support/configuration doesn't has any effect. This component
020: * uses your system's proxy settings. For example, when using Firefox, you have
021: * to set your proxy in Firefox and these same options are also used in Columba.
022: * <p>
023: * Javascript support can be used to access DOM. This way we can for example
024: * print the HTML page using:
025: * <code>webBrowser.executeScript("window.print();");</code>
026: * <p>
027: * TODO (@author fdietz): how to use images in Message Viewer, we can't set a
028: * base URL and load images from columba.jar?
029: *
030: * @author Frederik Dietz
031: *
032: */
033: public class JDICHTMLViewerPlugin extends JPanel implements
034: IHTMLViewerPlugin {
035:
036: /** JDK 1.4+ logging framework logger, used for logging. */
037: private static final Logger LOG = Logger
038: .getLogger("org.columba.core.gui.htmlviewer");
039:
040: private WebBrowser browser;
041:
042: private boolean initialized = false;
043:
044: public JDICHTMLViewerPlugin() {
045: super ();
046:
047: try {
048: WebBrowser.setDebug(true);
049:
050: browser = new WebBrowser(true);
051:
052: // turn of focus stealing (workaround should be removed in the
053: // future!)
054: browser.setFocusable(false);
055:
056: setLayout(new BorderLayout());
057: add(browser, BorderLayout.CENTER);
058:
059: } catch (Error e) {
060: LOG.severe("Error while initializing JDIC native browser: "
061: + e.getMessage());
062: if (Logging.DEBUG)
063: e.printStackTrace();
064: } catch (Exception e) {
065: LOG
066: .severe("Exception error while initializing JDIC native browser: "
067: + e.getMessage());
068: if (Logging.DEBUG)
069: e.printStackTrace();
070: }
071:
072: addComponentListener(new ComponentListener() {
073:
074: public void componentHidden(ComponentEvent e) {
075: browser.setVisible(false);
076: browser = null;
077: }
078:
079: public void componentMoved(ComponentEvent e) {
080: // TODO Auto-generated method stub
081:
082: }
083:
084: public void componentResized(ComponentEvent e) {
085: // TODO Auto-generated method stub
086:
087: }
088:
089: public void componentShown(ComponentEvent e) {
090: // TODO Auto-generated method stub
091:
092: }
093:
094: });
095:
096: }
097:
098: public void view(String htmlSource) {
099: browser.setContent(htmlSource);
100: }
101:
102: public JComponent getComponent() {
103: return this ;
104: }
105:
106: public String getSelectedText() {
107: try {
108: String selectedTextFromScript = browser
109: .executeScript("(window.getSelection) ? window.getSelection() : "
110: + "(document.getSelection) ? document.getSelection() : "
111: + "(document.selection) ? document.selection.createRange().text : "
112: + "null");
113: return selectedTextFromScript;
114: } catch (Exception e) {
115: e.printStackTrace();
116: }
117:
118: return "";
119: }
120:
121: public boolean initialized() {
122: return true;
123: }
124:
125: public JComponent getContainer() {
126: return this ;
127: }
128:
129: public String getText() {
130: return browser.getContent();
131: }
132:
133: /**
134: * @see org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin#setCaretPosition(int)
135: */
136: public void setCaretPosition(int position) {
137: // TODO
138: }
139:
140: /**
141: * @see org.columba.core.gui.htmlviewer.api.IHTMLViewerPlugin#moveCaretPosition(int)
142: */
143: public void moveCaretPosition(int position) {
144: // TODO
145: }
146:
147: }
|