1 package org.columba.core.gui.htmlviewer; 2 3 import java.awt.BorderLayout ; 4 import java.awt.event.ComponentEvent ; 5 import java.awt.event.ComponentListener ; 6 import java.util.logging.Logger ; 7 8 import javax.swing.JComponent ; 9 import javax.swing.JPanel ; 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 33 public class JDICHTMLViewerPlugin extends JPanel implements IHTMLViewerPlugin { 34 35 36 private static final Logger 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 browser.setFocusable(false); 54 55 setLayout(new BorderLayout ()); 56 add(browser, BorderLayout.CENTER); 57 58 } catch (Error e) { 59 LOG.severe("Error while initializing JDIC native browser: " 60 + e.getMessage()); 61 if (Logging.DEBUG) 62 e.printStackTrace(); 63 } catch (Exception 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 () { 72 73 public void componentHidden(ComponentEvent e) { 74 browser.setVisible(false); 75 browser = null; 76 } 77 78 public void componentMoved(ComponentEvent e) { 79 81 } 82 83 public void componentResized(ComponentEvent e) { 84 86 } 87 88 public void componentShown(ComponentEvent e) { 89 91 } 92 93 }); 94 95 } 96 97 public void view(String htmlSource) { 98 browser.setContent(htmlSource); 99 } 100 101 public JComponent getComponent() { 102 return this; 103 } 104 105 public String getSelectedText() { 106 try { 107 String 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 e) { 114 e.printStackTrace(); 115 } 116 117 return ""; 118 } 119 120 public boolean initialized() { 121 return true; 122 } 123 124 public JComponent getContainer() { 125 return this; 126 } 127 128 public String getText() { 129 return browser.getContent(); 130 } 131 132 135 public void setCaretPosition(int position) { 136 } 138 139 142 public void moveCaretPosition(int position) { 143 } 145 146 } 147 | Popular Tags |