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:
017: package org.columba.mail.gui.composer.html.action;
018:
019: import java.awt.event.ActionEvent;
020: import java.awt.event.ActionListener;
021: import java.util.Enumeration;
022:
023: import javax.swing.ButtonGroup;
024: import javax.swing.JRadioButtonMenuItem;
025: import javax.swing.text.html.HTML;
026:
027: import org.columba.api.gui.frame.IFrameMediator;
028: import org.columba.core.gui.menu.IMenu;
029: import org.columba.mail.gui.composer.ComposerController;
030: import org.columba.mail.gui.composer.ComposerModelChangedEvent;
031: import org.columba.mail.gui.composer.IComposerModelChangedListener;
032: import org.columba.mail.gui.composer.html.HtmlEditorController2;
033: import org.columba.mail.util.MailResourceLoader;
034: import org.frapuccino.htmleditor.api.IFormatChangedListener;
035: import org.frapuccino.htmleditor.event.FormatChangedEvent;
036: import org.frapuccino.htmleditor.event.FormatInfo;
037:
038: /**
039: * Submenu for formatting text.
040: * <p>
041: * Possible values are: - normal - preformatted - heading 1 - heading 2 -
042: * heading 3 - address
043: *
044: * Note: This is the place to add further formats like lists, etc.
045: *
046: * Note: The HtmlEditorView and -Controller must of course also support new
047: * formats when adding them!
048: *
049: * @author fdietz, Karl Peder Olesen (karlpeder)
050: */
051: public class ParagraphMenu extends IMenu implements ActionListener,
052: IFormatChangedListener, IComposerModelChangedListener {
053:
054: /** Html tags corresponding to supported paragraph styles */
055: public static final HTML.Tag[] STYLE_TAGS = { HTML.Tag.P,
056: HTML.Tag.PRE, HTML.Tag.H1, HTML.Tag.H2, HTML.Tag.H3,
057: HTML.Tag.ADDRESS };
058:
059: protected ButtonGroup group;
060:
061: /**
062: * @param controller
063: * @param caption
064: */
065: public ParagraphMenu(IFrameMediator controller) {
066: super (controller, MailResourceLoader.getString("menu",
067: "composer", "menu_format_paragraph"),
068: "menu_format_paragraph");
069:
070: initMenu();
071: }
072:
073: public void modelChanged(ComposerModelChangedEvent event) {
074: }
075:
076: public void htmlModeChanged(ComposerModelChangedEvent event) {
077: setEnabled(event.isHtmlEnabled());
078: }
079:
080: public void formatChanged(FormatChangedEvent event) {
081: // select the menu item corresponding to present format
082: FormatInfo info = event.getInfo();
083:
084: if (info.isHeading1()) {
085: selectMenuItem(HTML.Tag.H1);
086: } else if (info.isHeading2()) {
087: selectMenuItem(HTML.Tag.H2);
088: } else if (info.isHeading3()) {
089: selectMenuItem(HTML.Tag.H3);
090: } else if (info.isPreformattet()) {
091: selectMenuItem(HTML.Tag.PRE);
092: } else if (info.isAddress()) {
093: selectMenuItem(HTML.Tag.ADDRESS);
094: } else {
095: // select the "Normal" entry as default
096: selectMenuItem(HTML.Tag.P);
097: }
098: }
099:
100: /**
101: * Initializes the sub menu by creating a menu item for each available
102: * paragraph style. All menu items are grouped in a ButtonGroup (as radio
103: * buttons).
104: */
105: protected void initMenu() {
106: group = new ButtonGroup();
107:
108: for (int i = 0; i < STYLE_TAGS.length; i++) {
109: JRadioButtonMenuItem m = new ParagraphFormatMenuItem(
110: STYLE_TAGS[i]);
111: m.addActionListener(this );
112: add(m);
113:
114: group.add(m);
115: }
116: }
117:
118: /**
119: * Private utility to select a given sub menu, given the corresponding html
120: * tag. If such a sub menu does not exist - nothing happens
121: */
122: private void selectMenuItem(HTML.Tag tag) {
123: Enumeration e = group.getElements();
124: while (e.hasMoreElements()) {
125: ParagraphFormatMenuItem item = (ParagraphFormatMenuItem) e
126: .nextElement();
127:
128: if (item.getAssociatedTag().equals(tag)) {
129: item.setSelected(true);
130: } else
131: item.setSelected(false);
132: }
133: }
134:
135: public void actionPerformed(ActionEvent e) {
136: HtmlEditorController2 ctrl = (HtmlEditorController2) ((ComposerController) controller)
137: .getCurrentEditor();
138:
139: // set paragraph formatting according to the given action
140: ParagraphFormatMenuItem source = (ParagraphFormatMenuItem) e
141: .getSource();
142: ctrl.setParagraphFormat(source.getAssociatedTag());
143: }
144:
145: /**
146: * A specialized radio button menu item class used to render paragraph
147: * format actions.
148: */
149: protected static class ParagraphFormatMenuItem extends
150: JRadioButtonMenuItem {
151: protected HTML.Tag tag;
152:
153: public ParagraphFormatMenuItem(HTML.Tag tag) {
154: super (MailResourceLoader.getString("menu", "composer",
155: "menu_format_paragraph_" + tag.toString()));
156: this .tag = tag;
157: }
158:
159: public HTML.Tag getAssociatedTag() {
160: return tag;
161: }
162: }
163: }
|