KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > AbstractEditorController


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.mail.gui.composer;
17
18 import java.awt.Font JavaDoc;
19 import java.util.Observable JavaDoc;
20 import java.util.Observer JavaDoc;
21
22 import javax.swing.JComponent JavaDoc;
23 import javax.swing.JTextPane JavaDoc;
24
25 import org.columba.core.config.Config;
26 import org.columba.core.xml.XmlElement;
27
28 /**
29  * This class serves as a common super class for the Editor Controllers used in
30  * Composer: TextEditorController and HtmlEditorController. As such, it defines
31  * the common interface needed by mainly the ComposerController.
32  * <p>
33  * It extends Observable to allow all actions to enable/disable themselves on
34  * text selection changes.
35  *
36  * @author Karl Peder Olesen (karlpeder), 2003-09-06
37  */

38 public abstract class AbstractEditorController extends Observable JavaDoc implements Observer JavaDoc {
39
40     /** Reference to the controller */
41     private ComposerController controller;
42
43     private JTextPane JavaDoc view;
44
45       // name of font
46
private String JavaDoc name;
47
48     // size of font
49
private String JavaDoc size;
50
51     // currently used font
52
private Font JavaDoc font;
53
54     // font configuration
55
private XmlElement textFontElement;
56     private XmlElement fonts;
57
58     // overwrite look and feel font settings
59
private boolean overwrite;
60
61     /**
62      * Default constructor. Stores a reference to the controller
63      *
64      * @param ctrl
65      * Controller controlling this object
66      */

67     public AbstractEditorController(ComposerController ctrl) {
68         controller = ctrl;
69
70         XmlElement options = Config.getInstance().get("options").getElement("/options");
71         XmlElement guiElement = options.getElement("gui");
72         fonts = guiElement.getElement("fonts");
73
74         if (fonts == null) {
75             fonts = guiElement.addSubElement("fonts");
76         }
77
78         overwrite = Boolean.valueOf(fonts.getAttribute("overwrite", "true"))
79                            .booleanValue();
80
81         // register for configuration changes
82
fonts.addObserver(this);
83
84         textFontElement = fonts.getElement("text");
85
86         if (textFontElement == null) {
87             textFontElement = fonts.addSubElement("text");
88         }
89
90         if (overwrite) {
91             name = "Default";
92             size = "12";
93
94             font = new Font JavaDoc(name, Font.PLAIN, Integer.parseInt(size));
95         } else {
96             name = textFontElement.getAttribute("name", "Default");
97             size = textFontElement.getAttribute("size", "12");
98
99             font = new Font JavaDoc(name, Font.PLAIN, Integer.parseInt(size));
100         }
101     }
102
103     public void setView(JTextPane JavaDoc view) {
104         this.view = view;
105     }
106
107     public JTextPane JavaDoc getView() {
108         return view;
109     }
110
111     /**
112      * Returns the controller
113      */

114     public ComposerController getController() {
115         return controller;
116     }
117
118     // ********** Methods necessary to hide view from clients ********
119

120     /**
121      * Sets the text of the editor view
122      *
123      * @param text
124      * New text, which replaces the current view text
125      */

126     public abstract void setViewText(String JavaDoc text);
127
128     /** **************** FocusOwner implementation **************************** */
129
130     // the following lines add cut/copy/paste/undo/redo/selectall
131
// actions support using the Columba action objects.
132
//
133
// This means that we only have a single instance of these
134
// specific actions, which is shared by all menuitems and
135
// toolbar buttons.
136
/**
137      * @see org.columba.core.gui.focus.FocusOwner#isCutActionEnabled()
138      */

139     public boolean isCutActionEnabled() {
140         if (view.getSelectedText() == null) {
141             return false;
142         }
143
144         if (view.getSelectedText().length() > 0) {
145             return true;
146         }
147
148         return false;
149     }
150
151     /**
152      * @see org.columba.core.gui.focus.FocusOwner#isCopyActionEnabled()
153      */

154     public boolean isCopyActionEnabled() {
155         if (view.getSelectedText() == null) {
156             return false;
157         }
158
159         if (view.getSelectedText().length() > 0) {
160             return true;
161         }
162
163         return false;
164     }
165
166     /**
167      * @see org.columba.core.gui.focus.FocusOwner#isPasteActionEnabled()
168      */

169     public boolean isPasteActionEnabled() {
170         return true;
171     }
172
173     /**
174      * @see org.columba.core.gui.focus.FocusOwner#isDeleteActionEnabled()
175      */

176     public boolean isDeleteActionEnabled() {
177         if (view.getSelectedText() == null) {
178             return false;
179         }
180
181         if (view.getSelectedText().length() > 0) {
182             return true;
183         }
184
185         return false;
186     }
187
188     /**
189      * @see org.columba.core.gui.focus.FocusOwner#isSelectAllActionEnabled()
190      */

191     public boolean isSelectAllActionEnabled() {
192         return true;
193     }
194
195     /**
196      * @see org.columba.core.gui.focus.FocusOwner#isRedoActionEnabled()
197      */

198     public boolean isRedoActionEnabled() {
199         // TODO (@author karlpeder): Implementation of undo/redo missing
200
return false;
201     }
202
203     /**
204      * @see org.columba.core.gui.focus.FocusOwner#isUndoActionEnabled()
205      */

206     public boolean isUndoActionEnabled() {
207         // TODO (@author karlpeder): Implementation of undo/redo missing
208
return false;
209     }
210
211     /**
212      * @see org.columba.core.gui.focus.FocusOwner#cut()
213      */

214     public void cut() {
215         view.cut();
216     }
217
218     /**
219      * @see org.columba.core.gui.focus.FocusOwner#copy()
220      */

221     public void copy() {
222         view.copy();
223     }
224
225     /**
226      * @see org.columba.core.gui.focus.FocusOwner#paste()
227      */

228     public void paste() {
229         view.paste();
230     }
231
232     /**
233      * @see org.columba.core.gui.focus.FocusOwner#delete()
234      */

235     public void delete() {
236         view.replaceSelection("");
237     }
238
239     /**
240      * @see org.columba.core.gui.focus.FocusOwner#redo()
241      */

242     public void redo() {
243         // TODO (@author karlpeder): Implementation of undo/redo missing
244
}
245
246     /**
247      * @see org.columba.core.gui.focus.FocusOwner#undo()
248      */

249     public void undo() {
250         // TODO (@author karlpeder): Implementation of undo/redo missing
251
}
252
253     /**
254      * @see org.columba.core.gui.focus.FocusOwner#selectAll()
255      */

256     public void selectAll() {
257         view.selectAll();
258     }
259
260     /**
261      * @see org.columba.core.gui.focus.FocusOwner#getComponent()
262      */

263     public JComponent JavaDoc getComponent() {
264         return view;
265     }
266
267     /**
268      * @see org.columba.mail.gui.composer.AbstractEditorController#getViewUIComponent()
269      */

270     public JTextPane JavaDoc getViewUIComponent() {
271         return getView();
272     }
273
274     /**
275      * @see org.columba.mail.gui.composer.AbstractEditorController#setViewEnabled(boolean)
276      */

277     public void setViewEnabled(boolean enabled) {
278         getView().setEnabled(enabled);
279     }
280
281     public void setCaretPosition(int position) {
282         getView().setCaretPosition(position);
283     }
284
285     public void moveCaretPosition(int position) {
286         getView().moveCaretPosition(position);
287     }
288
289     /**
290      * @see org.columba.mail.gui.composer.AbstractEditorController#getViewFont()
291      */

292     public Font JavaDoc getViewFont() {
293         return getView().getFont();
294     }
295
296     /**
297      * @see org.columba.mail.gui.composer.AbstractEditorController#setViewFont(java.awt.Font)
298      */

299     public void setViewFont(Font JavaDoc f) {
300         getView().setFont(f);
301     }
302
303     /**
304      * @see org.columba.mail.gui.composer.AbstractEditorController#getViewText()
305      */

306     public String JavaDoc getViewText() {
307         return getView().getText();
308     }
309
310     /**
311      * @see org.columba.mail.gui.composer.AbstractEditorController#updateComponents(boolean)
312      */

313     public void updateComponents(boolean b) {
314         if (b) {
315             if (this.getController().getModel().getBodyText() != null) {
316                 this.setViewText(this.getController().getModel().getBodyText());
317             }
318         } else {
319             if (getView().getText() != null) {
320                 this.getController().getModel()
321                         .setBodyText(getView().getText());
322             }
323         }
324     }
325
326     /**
327      * Gets fired when configuration changes occur.
328      *
329      * @see org.columba.core.gui.config.GeneralOptionsDialog
330      *
331      * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
332      */

333     public void update(Observable JavaDoc arg0, Object JavaDoc arg1) {
334         // fonts
335
overwrite = Boolean.valueOf(fonts.getAttribute("overwrite", "true"))
336                            .booleanValue();
337
338         if (overwrite == false) {
339             // use default font settings
340
name = "Default";
341             size = "12";
342
343             font = new Font JavaDoc(name, Font.PLAIN, Integer.parseInt(size));
344         } else {
345             // overwrite look and feel font settings
346
name = textFontElement.getAttribute("name", "Default");
347             size = textFontElement.getAttribute("size", "12");
348
349             font = new Font JavaDoc(name, Font.PLAIN, Integer.parseInt(size));
350
351             setViewFont(font);
352         }
353     }
354 }
355
Popular Tags