1 package org.columba.mail.gui.composer.html; 2 3 import java.io.IOException ; 4 import java.io.Reader ; 5 import java.io.StringReader ; 6 import java.nio.charset.Charset ; 7 import java.util.logging.Logger ; 8 9 import javax.swing.text.BadLocationException ; 10 import javax.swing.text.ChangedCharSetException ; 11 import javax.swing.text.Document ; 12 import javax.swing.text.EditorKit ; 13 import javax.swing.text.html.HTML ; 14 import javax.swing.text.html.HTMLDocument ; 15 16 import org.columba.mail.gui.composer.AbstractEditorController; 17 import org.columba.mail.gui.composer.ComposerController; 18 import org.frapuccino.htmleditor.api.IFormatChangedListener; 19 20 public class HtmlEditorController2 extends AbstractEditorController implements 21 org.frapuccino.htmleditor.api.IHtmlEditorController { 22 23 24 private static final Logger LOG = Logger 25 .getLogger("org.columba.mail.gui.composer.html"); 26 27 private org.frapuccino.htmleditor.api.IHtmlEditorController editor; 28 29 31 public HtmlEditorController2(ComposerController controller) { 32 super(controller); 33 34 editor = new org.frapuccino.htmleditor.HtmlEditorController(); 35 setView(editor.getView()); 37 } 38 39 40 43 public void toggleBold() { 44 editor.toggleBold(); 45 } 46 47 50 public void toggleItalic() { 51 editor.toggleItalic(); 52 } 53 54 57 public void toggleUnderline() { 58 editor.toggleUnderline(); 59 } 60 61 64 public void toggleStrikeout() { 65 editor.toggleStrikeout(); 66 } 67 68 71 public void toggleTeleTyper() { 72 editor.toggleTeleTyper(); 73 } 74 75 public void setCenterJustify() { 76 editor.setCenterJustify(); 77 } 78 79 public void setLeftJustify() { 80 editor.setLeftJustify(); 81 } 82 83 public void setRightJustify() { 84 editor.setRightJustify(); 85 } 86 87 94 public void setParagraphFormat(HTML.Tag tag) { 95 editor.setParagraphFormat(tag); 96 } 97 98 101 public void insertBreak() { 102 editor.insertBreak(); 103 } 104 105 108 public void setViewText(String text) { 109 try { 112 loadHtmlIntoView(text, false); 113 } catch (ChangedCharSetException ccse) { 114 try { 116 loadHtmlIntoView(text, true); 117 } catch (IOException e) { 118 LOG.severe("Error setting view content, " 119 + "even after ignore charset spec: " + e.getMessage()); 120 } 121 } catch (IOException e) { 122 LOG.severe("Error setting view content: " + e.getMessage()); 124 } 125 } 126 127 140 private void loadHtmlIntoView(String text, boolean ignoreCharset) 141 throws IOException { 142 Document doc = getView().getDocument(); 144 145 try { 146 doc.remove(0, doc.getLength()); 148 149 if ((text == null) || (text.equals(""))) { 151 return; 152 } 153 154 if (ignoreCharset) { 156 ((HTMLDocument ) getView().getDocument()).putProperty( 157 "IgnoreCharsetDirective", Boolean.TRUE); 158 } 159 160 Reader r = new StringReader (text); 161 EditorKit kit = getView().getEditorKit(); 162 kit.read(r, doc, 0); } catch (BadLocationException e) { 164 LOG.severe("Error deleting old view content: " + e.getMessage()); 165 166 return; 167 } 168 } 169 170 public void addFormatChangedListener(IFormatChangedListener listener) { 171 editor.addFormatChangedListener(listener); 172 } 173 174 public void removeFormatChangedListener(IFormatChangedListener listener) { 175 editor.removeFormatChangedListener(listener); 176 } 177 178 public void setCharset(Charset charset) { 179 editor.setCharset(charset); 180 } 181 182 } 183 | Popular Tags |