KickJava   Java API By Example, From Geeks To Geeks.

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


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

17 package org.columba.mail.gui.composer.html;
18
19 import java.awt.Component JavaDoc;
20 import java.awt.event.ActionEvent JavaDoc;
21 import java.awt.event.ActionListener JavaDoc;
22 import java.util.logging.Logger JavaDoc;
23
24 import javax.swing.BorderFactory JavaDoc;
25 import javax.swing.Box JavaDoc;
26 import javax.swing.DefaultListCellRenderer JavaDoc;
27 import javax.swing.JComboBox JavaDoc;
28 import javax.swing.JList JavaDoc;
29 import javax.swing.JToolBar JavaDoc;
30 import javax.swing.UIManager JavaDoc;
31 import javax.swing.text.html.HTML JavaDoc;
32
33 import org.columba.api.gui.frame.IFrameMediator;
34 import org.columba.api.plugin.IExtension;
35 import org.columba.api.plugin.IExtensionHandler;
36 import org.columba.api.plugin.IExtensionHandlerKeys;
37 import org.columba.api.plugin.PluginException;
38 import org.columba.api.plugin.PluginHandlerNotFoundException;
39 import org.columba.core.gui.action.AbstractColumbaAction;
40 import org.columba.core.gui.action.AbstractSelectableAction;
41 import org.columba.core.gui.base.LabelWithMnemonic;
42 import org.columba.core.gui.base.RoundedBorder;
43 import org.columba.core.gui.toolbar.ToggleToolbarButton;
44 import org.columba.core.logging.Logging;
45 import org.columba.core.plugin.PluginManager;
46 import org.columba.mail.gui.composer.ComposerController;
47 import org.columba.mail.gui.composer.ComposerModelChangedEvent;
48 import org.columba.mail.gui.composer.IComposerModelChangedListener;
49 import org.columba.mail.gui.composer.html.action.FontSizeMenu;
50 import org.columba.mail.gui.composer.html.action.ParagraphMenu;
51 import org.columba.mail.util.MailResourceLoader;
52 import org.frapuccino.htmleditor.api.IFormatChangedListener;
53 import org.frapuccino.htmleditor.event.FormatChangedEvent;
54 import org.frapuccino.htmleditor.event.FormatInfo;
55
56 /**
57  * JPanel with useful HTML related actions.
58  *
59  * @author fdietz
60  */

61 public class HtmlToolbar extends JToolBar JavaDoc implements ActionListener JavaDoc,
62         IFormatChangedListener, IComposerModelChangedListener {
63
64     /** JDK 1.4+ logging framework logger, used for logging. */
65     private static final Logger JavaDoc LOG = Logger
66             .getLogger("org.columba.mail.gui.composer.html");
67
68     private ComposerController controller;
69
70     private JComboBox JavaDoc paragraphComboBox;
71
72     private JComboBox JavaDoc sizeComboBox;
73
74     /**
75      * Flag indicating whether we are programatically changing the paragraph
76      * combobox, and therefore shall do nothing in actionPerformed.
77      */

78     private boolean ignoreFormatAction = false;
79
80     private IExtensionHandler handler = null;
81
82     /**
83      *
84      */

85     public HtmlToolbar(ComposerController controller) {
86         super();
87         this.controller = controller;
88
89         setBorder(BorderFactory.createCompoundBorder(new RoundedBorder(
90                 UIManager.getColor("controlShadow")), BorderFactory
91                 .createEmptyBorder(4, 4, 4, 4)));
92
93         setRollover(true);
94
95         setFloatable(false);
96
97         try {
98             handler = PluginManager.getInstance().getExtensionHandler(
99                     IExtensionHandlerKeys.ORG_COLUMBA_CORE_ACTION);
100         } catch (PluginHandlerNotFoundException e) {
101             e.printStackTrace();
102         }
103
104         try {
105             initComponents();
106         } catch (Exception JavaDoc e) {
107             e.printStackTrace();
108         }
109
110         ((HtmlEditorController2) controller.getHtmlEditorController())
111                 .addFormatChangedListener(this);
112
113         // register for text selection changes
114
// controller.getEditorController().addObserver(this);
115

116         // register for changes to the editor
117
// controller.addContainerListenerForEditor(this);
118

119         // register for changes to editor type (text / html)
120
controller.getModel().addModelChangedListener(this);
121
122         // XmlElement optionsElement = MailConfig.getInstance().get(
123
// "composer_options").getElement("/options");
124
// XmlElement htmlElement = optionsElement.getElement("html");
125
//
126
// if (htmlElement == null) {
127
// htmlElement = optionsElement.addSubElement("html");
128
// }
129
//
130
// htmlElement.addObserver(this);
131
}
132
133     protected void initComponents() throws Exception JavaDoc {
134         // CellConstraints cc = new CellConstraints();
135

136         // we generate most buttons using the actions already instanciated
137

138         paragraphComboBox = new JComboBox JavaDoc(ParagraphMenu.STYLE_TAGS);
139         paragraphComboBox.setRenderer(new ParagraphTagRenderer());
140         paragraphComboBox.setActionCommand("PARA");
141         paragraphComboBox.addActionListener(this);
142         paragraphComboBox.setFocusable(false);
143
144         LabelWithMnemonic sizeLabel = new LabelWithMnemonic(MailResourceLoader
145                 .getString("dialog", "composer", "size"));
146         sizeComboBox = new JComboBox JavaDoc(FontSizeMenu.SIZES);
147         sizeComboBox.setActionCommand("SIZE");
148         sizeComboBox.addActionListener(this);
149         sizeComboBox.setSelectedIndex(2);
150         sizeComboBox.setFocusable(false);
151
152         // set initial enabled state of combo boxes
153
// XmlElement optionsElement = MailConfig.getInstance().get(
154
// "composer_options").getElement("/options");
155
// XmlElement htmlElement = optionsElement.getElement("html");
156
// String s = htmlElement.getAttribute("enable", "false");
157
// boolean enableHtml = Boolean.valueOf(s).booleanValue();
158

159         // paragraphComboBox.setEnabled(enableHtml);
160

161         // TODO (@author javaprog):sizeComboBox can be enabled as
162
// paragraphComboBox when implemented
163
sizeComboBox.setEnabled(false);
164
165         ToggleToolbarButton boldFormatButton = new ToggleToolbarButton(
166                 (AbstractSelectableAction) getAction("BoldFormatAction",
167                         getFrameController()));
168         ToggleToolbarButton italicFormatButton = new ToggleToolbarButton(
169                 (AbstractSelectableAction) getAction("ItalicFormatAction",
170                         getFrameController()));
171         ToggleToolbarButton underlineFormatButton = new ToggleToolbarButton(
172                 (AbstractSelectableAction) getAction("UnderlineFormatAction",
173                         getFrameController()));
174         ToggleToolbarButton strikeoutFormatButton = new ToggleToolbarButton(
175                 (AbstractSelectableAction) getAction("StrikeoutFormatAction",
176                         getFrameController()));
177         ToggleToolbarButton leftJustifyButton = new ToggleToolbarButton(
178                 (AbstractSelectableAction) getAction("LeftJustifyAction",
179                         getFrameController()));
180         ToggleToolbarButton centerJustifyButton = new ToggleToolbarButton(
181                 (AbstractSelectableAction) getAction("CenterJustifyAction",
182                         getFrameController()));
183         ToggleToolbarButton rightJustifyButton = new ToggleToolbarButton(
184                 (AbstractSelectableAction) getAction("RightJustifyAction",
185                         getFrameController()));
186
187         // builder.add(paraLabel, cc.xy(1, 7));
188

189         add(paragraphComboBox);
190         addSeparator();
191         add(sizeLabel);
192         add(sizeComboBox);
193         addSeparator();
194
195         add(boldFormatButton);
196         add(italicFormatButton);
197         add(underlineFormatButton);
198         add(strikeoutFormatButton);
199         addSeparator();
200         add(leftJustifyButton);
201         add(centerJustifyButton);
202         add(rightJustifyButton);
203
204         add(Box.createHorizontalGlue());
205         // FormLayout layout = new FormLayout(
206
// "default, 3dlu, default, 3dlu, default, 3dlu, "
207
// + "default, 3dlu, default, 3dlu, default, 3dlu, "
208
// + "default, 6dlu, default, 3dlu, default, 3dlu, "
209
// + "default, 3dlu", "fill:default");
210
// PanelBuilder b = new PanelBuilder(this, layout);
211
//
212
// CellConstraints c = new CellConstraints();
213
//
214
// b.add(paragraphComboBox, cc.xy(1, 1));
215
// b.add(sizeLabel, cc.xy(3, 1));
216
// b.add(sizeComboBox, cc.xy(5, 1));
217
// b.add(boldFormatButton, cc.xy(7, 1));
218
// b.add(italicFormatButton, cc.xy(9, 1));
219
// b.add(underlineFormatButton, cc.xy(11, 1));
220
// b.add(strikeoutFormatButton, cc.xy(13, 1));
221
// b.add(leftJustifyButton, cc.xy(15, 1));
222
// b.add(centerJustifyButton, cc.xy(17, 1));
223
// b.add(rightJustifyButton, cc.xy(19, 1));
224

225         // builder.add(panel, cc.xy(1, 7));
226
}
227
228     /**
229      * @return
230      */

231     public ComposerController getFrameController() {
232         return controller;
233     }
234
235     /**
236      * Method is called when text selection has changed.
237      * <p>
238      * Set state of togglebutton / -menu to pressed / not pressed when
239      * selections change.
240      *
241      * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
242      */

243     // public void update(Observable arg0, Object arg1) {
244
// if (arg0 instanceof HtmlEditorController2) {
245
// // Handling of paragraph combo box
246
// // select the item in the combo box corresponding to present format
247
// FormatInfo info = (FormatInfo) arg1;
248
//
249
// if (info.isHeading1()) {
250
// selectInParagraphComboBox(HTML.Tag.H1);
251
// } else if (info.isHeading2()) {
252
// selectInParagraphComboBox(HTML.Tag.H2);
253
// } else if (info.isHeading3()) {
254
// selectInParagraphComboBox(HTML.Tag.H3);
255
// } else if (info.isPreformattet()) {
256
// selectInParagraphComboBox(HTML.Tag.PRE);
257
// } else if (info.isAddress()) {
258
// selectInParagraphComboBox(HTML.Tag.ADDRESS);
259
// } else {
260
// // select the "Normal" entry as default
261
// selectInParagraphComboBox(HTML.Tag.P);
262
// }
263
//
264
// // Font size combo box
265
// // TODO (@author fdietz): Add handling for font size combo box
266
// } else if (arg0 instanceof XmlElement) {
267
// // possibly change btw. html and text
268
// XmlElement e = (XmlElement) arg0;
269
//
270
// if (e.getName().equals("html")) {
271
// // paragraphComboBox should only be enabled in html mode
272
// paragraphComboBox.setEnabled(Boolean.valueOf(
273
// e.getAttribute("enable", "false")).booleanValue());
274
//
275
// // TODO (@author fdietz): Add handling for font size combo box
276
// }
277
// }
278
// }
279
/**
280      * Private utility to select an item in the paragraph combo box, given the
281      * corresponding html tag. If such a sub menu does not exist - nothing
282      * happens
283      */

284     private void selectInParagraphComboBox(HTML.Tag JavaDoc tag) {
285         // need to change selection
286
// Set ignore flag
287
ignoreFormatAction = true;
288
289         paragraphComboBox.setSelectedItem(tag);
290
291         // clear ignore flag
292
ignoreFormatAction = false;
293     }
294
295     /*
296      * (non-Javadoc)
297      *
298      * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
299      */

300     public void actionPerformed(ActionEvent JavaDoc arg0) {
301         String JavaDoc action = arg0.getActionCommand();
302
303         if (action.equals("PARA")) {
304             // selection in the paragraph combo box
305
if (!ignoreFormatAction) {
306                 // only do something if ignore flag is not set
307
HtmlEditorController2 ctrl = (HtmlEditorController2) getFrameController()
308                         .getCurrentEditor();
309
310                 // set paragraph formatting according to the selection
311
int selectedIndex = paragraphComboBox.getSelectedIndex();
312                 HTML.Tag JavaDoc tag = ParagraphMenu.STYLE_TAGS[selectedIndex];
313
314                 LOG.fine("Setting paragraph format to: " + tag.toString());
315
316                 ctrl.setParagraphFormat(tag);
317             }
318         } else if (action.equals("SIZE")) {
319             // int selectedIndex = sizeComboBox.getSelectedIndex();
320

321             // TODO (@author fdietz):: implement action for font size combo box!
322
}
323     }
324
325     // /**
326
// * This event could mean that a the editor controller has changed.
327
// Therefore
328
// * this object is re-registered as observer to keep getting information
329
// * about format changes.
330
// *
331
// * @see
332
// java.awt.event.ContainerListener#componentAdded(java.awt.event.ContainerEvent)
333
// */
334
// public void componentAdded(ContainerEvent e) {
335
// LOG.info("Re-registering as observer on editor controller");
336
// controller.getEditorController().addObserver(this);
337
// }
338
//
339
// public void componentRemoved(ContainerEvent e) {
340
// }
341

342     /**
343      * Cell renderer responsible for displaying localized strings in the combo
344      * box.
345      */

346     protected static class ParagraphTagRenderer extends DefaultListCellRenderer JavaDoc {
347         public Component JavaDoc getListCellRendererComponent(JList JavaDoc list, Object JavaDoc value,
348                 int index, boolean selected, boolean hasFocus) {
349             return super.getListCellRendererComponent(list, MailResourceLoader
350                     .getString("menu", "composer", "menu_format_paragraph_"
351                             + value.toString()), index, selected, hasFocus);
352         }
353     }
354
355     private AbstractColumbaAction getAction(String JavaDoc id, IFrameMediator controller) {
356         if (id == null)
357             throw new IllegalArgumentException JavaDoc("id == null");
358         if (controller == null)
359             throw new IllegalArgumentException JavaDoc("controller == null");
360
361         IExtension extension = handler.getExtension(id);
362
363         AbstractColumbaAction a = null;
364
365         try {
366             if (extension != null)
367                 a = (AbstractColumbaAction) extension
368                         .instanciateExtension(new Object JavaDoc[] { controller });
369         } catch (PluginException e) {
370             LOG.severe(e.getMessage());
371             if (Logging.DEBUG)
372                 e.printStackTrace();
373
374         }
375
376         return a;
377
378     }
379
380     // Handling of paragraph combo box
381
// select the item in the combo box corresponding to present format
382
public void formatChanged(FormatChangedEvent event) {
383
384         FormatInfo info = event.getInfo();
385
386         if (info.isHeading1()) {
387             selectInParagraphComboBox(HTML.Tag.H1);
388         } else if (info.isHeading2()) {
389             selectInParagraphComboBox(HTML.Tag.H2);
390         } else if (info.isHeading3()) {
391             selectInParagraphComboBox(HTML.Tag.H3);
392         } else if (info.isPreformattet()) {
393             selectInParagraphComboBox(HTML.Tag.PRE);
394         } else if (info.isAddress()) {
395             selectInParagraphComboBox(HTML.Tag.ADDRESS);
396         } else {
397             // select the "Normal" entry as default
398
selectInParagraphComboBox(HTML.Tag.P);
399         }
400     }
401
402     // enable controls if we are not in html edit mode
403
public void htmlModeChanged(ComposerModelChangedEvent event) {
404         paragraphComboBox.setEnabled(event.isHtmlEnabled());
405         sizeComboBox.setEnabled(event.isHtmlEnabled());
406     }
407
408     public void modelChanged(ComposerModelChangedEvent event) {
409     }
410 }
411
Popular Tags