KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > mail > gui > composer > html > action > ParagraphMenu


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.action;
18
19 import java.awt.event.ActionEvent JavaDoc;
20 import java.awt.event.ActionListener JavaDoc;
21 import java.util.Enumeration JavaDoc;
22
23 import javax.swing.ButtonGroup JavaDoc;
24 import javax.swing.JRadioButtonMenuItem JavaDoc;
25 import javax.swing.text.html.HTML JavaDoc;
26
27 import org.columba.api.gui.frame.IFrameMediator;
28 import org.columba.core.gui.menu.IMenu;
29 import org.columba.mail.gui.composer.ComposerController;
30 import org.columba.mail.gui.composer.ComposerModelChangedEvent;
31 import org.columba.mail.gui.composer.IComposerModelChangedListener;
32 import org.columba.mail.gui.composer.html.HtmlEditorController2;
33 import org.columba.mail.util.MailResourceLoader;
34 import org.frapuccino.htmleditor.api.IFormatChangedListener;
35 import org.frapuccino.htmleditor.event.FormatChangedEvent;
36 import org.frapuccino.htmleditor.event.FormatInfo;
37
38 /**
39  * Submenu for formatting text.
40  * <p>
41  * Possible values are: - normal - preformatted - heading 1 - heading 2 -
42  * heading 3 - address
43  *
44  * Note: This is the place to add further formats like lists, etc.
45  *
46  * Note: The HtmlEditorView and -Controller must of course also support new
47  * formats when adding them!
48  *
49  * @author fdietz, Karl Peder Olesen (karlpeder)
50  */

51 public class ParagraphMenu extends IMenu implements ActionListener JavaDoc,
52         IFormatChangedListener, IComposerModelChangedListener {
53
54     /** Html tags corresponding to supported paragraph styles */
55     public static final HTML.Tag JavaDoc[] STYLE_TAGS = { HTML.Tag.P, HTML.Tag.PRE,
56             HTML.Tag.H1, HTML.Tag.H2, HTML.Tag.H3, HTML.Tag.ADDRESS };
57
58     protected ButtonGroup JavaDoc group;
59
60     /**
61      * @param controller
62      * @param caption
63      */

64     public ParagraphMenu(IFrameMediator controller) {
65         super(controller, MailResourceLoader.getString("menu", "composer",
66                 "menu_format_paragraph"), "menu_format_paragraph");
67
68         initMenu();
69     }
70
71     public void modelChanged(ComposerModelChangedEvent event) {
72     }
73
74     public void htmlModeChanged(ComposerModelChangedEvent event) {
75         setEnabled(event.isHtmlEnabled());
76     }
77
78     public void formatChanged(FormatChangedEvent event) {
79         // select the menu item corresponding to present format
80
FormatInfo info = event.getInfo();
81
82         if (info.isHeading1()) {
83             selectMenuItem(HTML.Tag.H1);
84         } else if (info.isHeading2()) {
85             selectMenuItem(HTML.Tag.H2);
86         } else if (info.isHeading3()) {
87             selectMenuItem(HTML.Tag.H3);
88         } else if (info.isPreformattet()) {
89             selectMenuItem(HTML.Tag.PRE);
90         } else if (info.isAddress()) {
91             selectMenuItem(HTML.Tag.ADDRESS);
92         } else {
93             // select the "Normal" entry as default
94
selectMenuItem(HTML.Tag.P);
95         }
96     }
97
98     /**
99      * Initializes the sub menu by creating a menu item for each available
100      * paragraph style. All menu items are grouped in a ButtonGroup (as radio
101      * buttons).
102      */

103     protected void initMenu() {
104         group = new ButtonGroup JavaDoc();
105
106         for (int i = 0; i < STYLE_TAGS.length; i++) {
107             JRadioButtonMenuItem JavaDoc m = new ParagraphFormatMenuItem(STYLE_TAGS[i]);
108             m.addActionListener(this);
109             add(m);
110
111             group.add(m);
112         }
113     }
114
115     /**
116      * Private utility to select a given sub menu, given the corresponding html
117      * tag. If such a sub menu does not exist - nothing happens
118      */

119     private void selectMenuItem(HTML.Tag JavaDoc tag) {
120         Enumeration JavaDoc e = group.getElements();
121         while (e.hasMoreElements()) {
122             ParagraphFormatMenuItem item = (ParagraphFormatMenuItem) e
123                     .nextElement();
124
125             if (item.getAssociatedTag().equals(tag)) {
126                 item.setSelected(true);
127             } else
128                 item.setSelected(false);
129         }
130     }
131
132     public void actionPerformed(ActionEvent JavaDoc e) {
133         HtmlEditorController2 ctrl = (HtmlEditorController2) ((ComposerController) controller)
134                 .getCurrentEditor();
135
136         // set paragraph formatting according to the given action
137
ParagraphFormatMenuItem source = (ParagraphFormatMenuItem) e
138                 .getSource();
139         ctrl.setParagraphFormat(source.getAssociatedTag());
140     }
141
142     /**
143      * A specialized radio button menu item class used to render paragraph
144      * format actions.
145      */

146     protected static class ParagraphFormatMenuItem extends JRadioButtonMenuItem JavaDoc {
147         protected HTML.Tag JavaDoc tag;
148
149         public ParagraphFormatMenuItem(HTML.Tag JavaDoc tag) {
150             super(MailResourceLoader.getString("menu", "composer",
151                     "menu_format_paragraph_" + tag.toString()));
152             this.tag = tag;
153         }
154
155         public HTML.Tag JavaDoc getAssociatedTag() {
156             return tag;
157         }
158     }
159 }
160
Popular Tags