KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > html > HtmlEditorController2


1 package org.columba.mail.gui.composer.html;
2
3 import java.io.IOException JavaDoc;
4 import java.io.Reader JavaDoc;
5 import java.io.StringReader JavaDoc;
6 import java.nio.charset.Charset JavaDoc;
7 import java.util.logging.Logger JavaDoc;
8
9 import javax.swing.text.BadLocationException JavaDoc;
10 import javax.swing.text.ChangedCharSetException JavaDoc;
11 import javax.swing.text.Document JavaDoc;
12 import javax.swing.text.EditorKit JavaDoc;
13 import javax.swing.text.html.HTML JavaDoc;
14 import javax.swing.text.html.HTMLDocument JavaDoc;
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     /** JDK 1.4+ logging framework logger, used for logging. */
24     private static final Logger JavaDoc LOG = Logger
25             .getLogger("org.columba.mail.gui.composer.html");
26
27     private org.frapuccino.htmleditor.api.IHtmlEditorController editor;
28
29     // private JTextPane view;
30

31     public HtmlEditorController2(ComposerController controller) {
32         super(controller);
33
34         editor = new org.frapuccino.htmleditor.HtmlEditorController();
35         // view = editor.getView();
36
setView(editor.getView());
37     }
38
39     /** ************* Methods for setting html specific formatting ************ */
40     /**
41      * Toggle bold font in the view on/off
42      */

43     public void toggleBold() {
44         editor.toggleBold();
45     }
46
47     /**
48      * Toggle italic font in the view on/off
49      */

50     public void toggleItalic() {
51         editor.toggleItalic();
52     }
53
54     /**
55      * Toggle underline font in the view on/off
56      */

57     public void toggleUnderline() {
58         editor.toggleUnderline();
59     }
60
61     /**
62      * Toggle strikeout font in the view on/off
63      */

64     public void toggleStrikeout() {
65         editor.toggleStrikeout();
66     }
67
68     /**
69      * Toggle teletyper font (type written text) in the view on/off
70      */

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     /**
88      * Sets paragraph format for selected paragraphs or current paragraph if no
89      * text is selected
90      *
91      * @param tag
92      * Html tag specifying the format to set
93      */

94     public void setParagraphFormat(HTML.Tag JavaDoc tag) {
95         editor.setParagraphFormat(tag);
96     }
97
98     /**
99      * 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 JavaDoc text) {
109         // // This doesn't handle ChangedCharsetExceptions correctly.
110
// view.setText(text);
111
try {
112             loadHtmlIntoView(text, false);
113         } catch (ChangedCharSetException JavaDoc ccse) {
114             // try again, but ignore charset specification in the html
115
try {
116                 loadHtmlIntoView(text, true);
117             } catch (IOException JavaDoc e) {
118                 LOG.severe("Error setting view content, "
119                         + "even after ignore charset spec: " + e.getMessage());
120             }
121         } catch (IOException JavaDoc e) {
122             // other IOExceptions than ChangedCharsetException
123
LOG.severe("Error setting view content: " + e.getMessage());
124         }
125     }
126
127     /**
128      * Private utility for loading html into the view. Is called from
129      * setViewText. <br>
130      * The method works mostly as calling view.setText() directly, but is
131      * necessary to be able to handle ChangedCharsetExceptions
132      *
133      * @param text
134      * Text to load into the view
135      * @param ignoreCharset
136      * If set to true, charset specifications in the html will be
137      * ignore
138      * @throws IOException
139      */

140     private void loadHtmlIntoView(String JavaDoc text, boolean ignoreCharset)
141             throws IOException JavaDoc {
142         // clear existing text
143
Document JavaDoc doc = getView().getDocument();
144
145         try {
146             // delete old contents
147
doc.remove(0, doc.getLength());
148
149             // if no text is specified, we are done now
150
if ((text == null) || (text.equals(""))) {
151                 return;
152             }
153
154             // load contents into document
155
if (ignoreCharset) {
156                 ((HTMLDocument JavaDoc) getView().getDocument()).putProperty(
157                         "IgnoreCharsetDirective", Boolean.TRUE);
158             }
159
160             Reader JavaDoc r = new StringReader JavaDoc(text);
161             EditorKit JavaDoc kit = getView().getEditorKit();
162             kit.read(r, doc, 0); // this can throw a ChangedCharsetException
163
} catch (BadLocationException JavaDoc 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 JavaDoc charset) {
179         editor.setCharset(charset);
180     }
181
182 }
183
Popular Tags