001: package org.columba.mail.gui.composer.html;
002:
003: import java.io.IOException;
004: import java.io.Reader;
005: import java.io.StringReader;
006: import java.nio.charset.Charset;
007: import java.util.logging.Logger;
008:
009: import javax.swing.text.BadLocationException;
010: import javax.swing.text.ChangedCharSetException;
011: import javax.swing.text.Document;
012: import javax.swing.text.EditorKit;
013: import javax.swing.text.html.HTML;
014: import javax.swing.text.html.HTMLDocument;
015:
016: import org.columba.mail.gui.composer.AbstractEditorController;
017: import org.columba.mail.gui.composer.ComposerController;
018: import org.frapuccino.htmleditor.api.IFormatChangedListener;
019:
020: public class HtmlEditorController2 extends AbstractEditorController
021: implements org.frapuccino.htmleditor.api.IHtmlEditorController {
022:
023: /** JDK 1.4+ logging framework logger, used for logging. */
024: private static final Logger LOG = Logger
025: .getLogger("org.columba.mail.gui.composer.html");
026:
027: private org.frapuccino.htmleditor.api.IHtmlEditorController editor;
028:
029: // private JTextPane view;
030:
031: public HtmlEditorController2(ComposerController controller) {
032: super (controller);
033:
034: editor = new org.frapuccino.htmleditor.HtmlEditorController();
035: // view = editor.getView();
036: setView(editor.getView());
037: }
038:
039: /** ************* Methods for setting html specific formatting ************ */
040: /**
041: * Toggle bold font in the view on/off
042: */
043: public void toggleBold() {
044: editor.toggleBold();
045: }
046:
047: /**
048: * Toggle italic font in the view on/off
049: */
050: public void toggleItalic() {
051: editor.toggleItalic();
052: }
053:
054: /**
055: * Toggle underline font in the view on/off
056: */
057: public void toggleUnderline() {
058: editor.toggleUnderline();
059: }
060:
061: /**
062: * Toggle strikeout font in the view on/off
063: */
064: public void toggleStrikeout() {
065: editor.toggleStrikeout();
066: }
067:
068: /**
069: * Toggle teletyper font (type written text) in the view on/off
070: */
071: public void toggleTeleTyper() {
072: editor.toggleTeleTyper();
073: }
074:
075: public void setCenterJustify() {
076: editor.setCenterJustify();
077: }
078:
079: public void setLeftJustify() {
080: editor.setLeftJustify();
081: }
082:
083: public void setRightJustify() {
084: editor.setRightJustify();
085: }
086:
087: /**
088: * Sets paragraph format for selected paragraphs or current paragraph if no
089: * text is selected
090: *
091: * @param tag
092: * Html tag specifying the format to set
093: */
094: public void setParagraphFormat(HTML.Tag tag) {
095: editor.setParagraphFormat(tag);
096: }
097:
098: /**
099: * Method for inserting a break (BR) element
100: */
101: public void insertBreak() {
102: editor.insertBreak();
103: }
104:
105: /**
106: * @see org.columba.mail.gui.composer.AbstractEditorController#setViewText(java.lang.String)
107: */
108: public void setViewText(String text) {
109: // // This doesn't handle ChangedCharsetExceptions correctly.
110: // view.setText(text);
111: try {
112: loadHtmlIntoView(text, false);
113: } catch (ChangedCharSetException ccse) {
114: // try again, but ignore charset specification in the html
115: try {
116: loadHtmlIntoView(text, true);
117: } catch (IOException e) {
118: LOG.severe("Error setting view content, "
119: + "even after ignore charset spec: "
120: + e.getMessage());
121: }
122: } catch (IOException e) {
123: // other IOExceptions than ChangedCharsetException
124: LOG.severe("Error setting view content: " + e.getMessage());
125: }
126: }
127:
128: /**
129: * Private utility for loading html into the view. Is called from
130: * setViewText. <br>
131: * The method works mostly as calling view.setText() directly, but is
132: * necessary to be able to handle ChangedCharsetExceptions
133: *
134: * @param text
135: * Text to load into the view
136: * @param ignoreCharset
137: * If set to true, charset specifications in the html will be
138: * ignore
139: * @throws IOException
140: */
141: private void loadHtmlIntoView(String text, boolean ignoreCharset)
142: throws IOException {
143: // clear existing text
144: Document doc = getView().getDocument();
145:
146: try {
147: // delete old contents
148: doc.remove(0, doc.getLength());
149:
150: // if no text is specified, we are done now
151: if ((text == null) || (text.equals(""))) {
152: return;
153: }
154:
155: // load contents into document
156: if (ignoreCharset) {
157: ((HTMLDocument) getView().getDocument()).putProperty(
158: "IgnoreCharsetDirective", Boolean.TRUE);
159: }
160:
161: Reader r = new StringReader(text);
162: EditorKit kit = getView().getEditorKit();
163: kit.read(r, doc, 0); // this can throw a ChangedCharsetException
164: } catch (BadLocationException e) {
165: LOG.severe("Error deleting old view content: "
166: + e.getMessage());
167:
168: return;
169: }
170: }
171:
172: public void addFormatChangedListener(IFormatChangedListener listener) {
173: editor.addFormatChangedListener(listener);
174: }
175:
176: public void removeFormatChangedListener(
177: IFormatChangedListener listener) {
178: editor.removeFormatChangedListener(listener);
179: }
180:
181: public void setCharset(Charset charset) {
182: editor.setCharset(charset);
183: }
184:
185: }
|