001: //The contents of this file are subject to the Mozilla Public License Version 1.1
002: //(the "License"); you may not use this file except in compliance with the
003: //License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
004: //
005: //Software distributed under the License is distributed on an "AS IS" basis,
006: //WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
007: //for the specific language governing rights and
008: //limitations under the License.
009: //
010: //The Original Code is "The Columba Project"
011: //
012: //The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: //Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014: //
015: //All Rights Reserved.
016: package org.columba.mail.gui.composer;
017:
018: import java.awt.Font;
019: import java.util.Observable;
020: import java.util.Observer;
021:
022: import javax.swing.JComponent;
023: import javax.swing.JTextPane;
024:
025: import org.columba.core.config.Config;
026: import org.columba.core.xml.XmlElement;
027:
028: /**
029: * This class serves as a common super class for the Editor Controllers used in
030: * Composer: TextEditorController and HtmlEditorController. As such, it defines
031: * the common interface needed by mainly the ComposerController.
032: * <p>
033: * It extends Observable to allow all actions to enable/disable themselves on
034: * text selection changes.
035: *
036: * @author Karl Peder Olesen (karlpeder), 2003-09-06
037: */
038: public abstract class AbstractEditorController extends Observable
039: implements Observer {
040:
041: /** Reference to the controller */
042: private ComposerController controller;
043:
044: private JTextPane view;
045:
046: // name of font
047: private String name;
048:
049: // size of font
050: private String size;
051:
052: // currently used font
053: private Font font;
054:
055: // font configuration
056: private XmlElement textFontElement;
057: private XmlElement fonts;
058:
059: // overwrite look and feel font settings
060: private boolean overwrite;
061:
062: /**
063: * Default constructor. Stores a reference to the controller
064: *
065: * @param ctrl
066: * Controller controlling this object
067: */
068: public AbstractEditorController(ComposerController ctrl) {
069: controller = ctrl;
070:
071: XmlElement options = Config.getInstance().get("options")
072: .getElement("/options");
073: XmlElement guiElement = options.getElement("gui");
074: fonts = guiElement.getElement("fonts");
075:
076: if (fonts == null) {
077: fonts = guiElement.addSubElement("fonts");
078: }
079:
080: overwrite = Boolean.valueOf(
081: fonts.getAttribute("overwrite", "true")).booleanValue();
082:
083: // register for configuration changes
084: fonts.addObserver(this );
085:
086: textFontElement = fonts.getElement("text");
087:
088: if (textFontElement == null) {
089: textFontElement = fonts.addSubElement("text");
090: }
091:
092: if (overwrite) {
093: name = "Default";
094: size = "12";
095:
096: font = new Font(name, Font.PLAIN, Integer.parseInt(size));
097: } else {
098: name = textFontElement.getAttribute("name", "Default");
099: size = textFontElement.getAttribute("size", "12");
100:
101: font = new Font(name, Font.PLAIN, Integer.parseInt(size));
102: }
103: }
104:
105: public void setView(JTextPane view) {
106: this .view = view;
107: }
108:
109: public JTextPane getView() {
110: return view;
111: }
112:
113: /**
114: * Returns the controller
115: */
116: public ComposerController getController() {
117: return controller;
118: }
119:
120: // ********** Methods necessary to hide view from clients ********
121:
122: /**
123: * Sets the text of the editor view
124: *
125: * @param text
126: * New text, which replaces the current view text
127: */
128: public abstract void setViewText(String text);
129:
130: /** **************** FocusOwner implementation **************************** */
131:
132: // the following lines add cut/copy/paste/undo/redo/selectall
133: // actions support using the Columba action objects.
134: //
135: // This means that we only have a single instance of these
136: // specific actions, which is shared by all menuitems and
137: // toolbar buttons.
138: /**
139: * @see org.columba.core.gui.focus.FocusOwner#isCutActionEnabled()
140: */
141: public boolean isCutActionEnabled() {
142: if (view.getSelectedText() == null) {
143: return false;
144: }
145:
146: if (view.getSelectedText().length() > 0) {
147: return true;
148: }
149:
150: return false;
151: }
152:
153: /**
154: * @see org.columba.core.gui.focus.FocusOwner#isCopyActionEnabled()
155: */
156: public boolean isCopyActionEnabled() {
157: if (view.getSelectedText() == null) {
158: return false;
159: }
160:
161: if (view.getSelectedText().length() > 0) {
162: return true;
163: }
164:
165: return false;
166: }
167:
168: /**
169: * @see org.columba.core.gui.focus.FocusOwner#isPasteActionEnabled()
170: */
171: public boolean isPasteActionEnabled() {
172: return true;
173: }
174:
175: /**
176: * @see org.columba.core.gui.focus.FocusOwner#isDeleteActionEnabled()
177: */
178: public boolean isDeleteActionEnabled() {
179: if (view.getSelectedText() == null) {
180: return false;
181: }
182:
183: if (view.getSelectedText().length() > 0) {
184: return true;
185: }
186:
187: return false;
188: }
189:
190: /**
191: * @see org.columba.core.gui.focus.FocusOwner#isSelectAllActionEnabled()
192: */
193: public boolean isSelectAllActionEnabled() {
194: return true;
195: }
196:
197: /**
198: * @see org.columba.core.gui.focus.FocusOwner#isRedoActionEnabled()
199: */
200: public boolean isRedoActionEnabled() {
201: // TODO (@author karlpeder): Implementation of undo/redo missing
202: return false;
203: }
204:
205: /**
206: * @see org.columba.core.gui.focus.FocusOwner#isUndoActionEnabled()
207: */
208: public boolean isUndoActionEnabled() {
209: // TODO (@author karlpeder): Implementation of undo/redo missing
210: return false;
211: }
212:
213: /**
214: * @see org.columba.core.gui.focus.FocusOwner#cut()
215: */
216: public void cut() {
217: view.cut();
218: }
219:
220: /**
221: * @see org.columba.core.gui.focus.FocusOwner#copy()
222: */
223: public void copy() {
224: view.copy();
225: }
226:
227: /**
228: * @see org.columba.core.gui.focus.FocusOwner#paste()
229: */
230: public void paste() {
231: view.paste();
232: }
233:
234: /**
235: * @see org.columba.core.gui.focus.FocusOwner#delete()
236: */
237: public void delete() {
238: view.replaceSelection("");
239: }
240:
241: /**
242: * @see org.columba.core.gui.focus.FocusOwner#redo()
243: */
244: public void redo() {
245: // TODO (@author karlpeder): Implementation of undo/redo missing
246: }
247:
248: /**
249: * @see org.columba.core.gui.focus.FocusOwner#undo()
250: */
251: public void undo() {
252: // TODO (@author karlpeder): Implementation of undo/redo missing
253: }
254:
255: /**
256: * @see org.columba.core.gui.focus.FocusOwner#selectAll()
257: */
258: public void selectAll() {
259: view.selectAll();
260: }
261:
262: /**
263: * @see org.columba.core.gui.focus.FocusOwner#getComponent()
264: */
265: public JComponent getComponent() {
266: return view;
267: }
268:
269: /**
270: * @see org.columba.mail.gui.composer.AbstractEditorController#getViewUIComponent()
271: */
272: public JTextPane getViewUIComponent() {
273: return getView();
274: }
275:
276: /**
277: * @see org.columba.mail.gui.composer.AbstractEditorController#setViewEnabled(boolean)
278: */
279: public void setViewEnabled(boolean enabled) {
280: getView().setEnabled(enabled);
281: }
282:
283: public void setCaretPosition(int position) {
284: getView().setCaretPosition(position);
285: }
286:
287: public void moveCaretPosition(int position) {
288: getView().moveCaretPosition(position);
289: }
290:
291: /**
292: * @see org.columba.mail.gui.composer.AbstractEditorController#getViewFont()
293: */
294: public Font getViewFont() {
295: return getView().getFont();
296: }
297:
298: /**
299: * @see org.columba.mail.gui.composer.AbstractEditorController#setViewFont(java.awt.Font)
300: */
301: public void setViewFont(Font f) {
302: getView().setFont(f);
303: }
304:
305: /**
306: * @see org.columba.mail.gui.composer.AbstractEditorController#getViewText()
307: */
308: public String getViewText() {
309: return getView().getText();
310: }
311:
312: /**
313: * @see org.columba.mail.gui.composer.AbstractEditorController#updateComponents(boolean)
314: */
315: public void updateComponents(boolean b) {
316: if (b) {
317: if (this .getController().getModel().getBodyText() != null) {
318: this .setViewText(this .getController().getModel()
319: .getBodyText());
320: }
321: } else {
322: if (getView().getText() != null) {
323: this .getController().getModel().setBodyText(
324: getView().getText());
325: }
326: }
327: }
328:
329: /**
330: * Gets fired when configuration changes occur.
331: *
332: * @see org.columba.core.gui.config.GeneralOptionsDialog
333: *
334: * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
335: */
336: public void update(Observable arg0, Object arg1) {
337: // fonts
338: overwrite = Boolean.valueOf(
339: fonts.getAttribute("overwrite", "true")).booleanValue();
340:
341: if (overwrite == false) {
342: // use default font settings
343: name = "Default";
344: size = "12";
345:
346: font = new Font(name, Font.PLAIN, Integer.parseInt(size));
347: } else {
348: // overwrite look and feel font settings
349: name = textFontElement.getAttribute("name", "Default");
350: size = textFontElement.getAttribute("size", "12");
351:
352: font = new Font(name, Font.PLAIN, Integer.parseInt(size));
353:
354: setViewFont(font);
355: }
356: }
357: }
|