001: //WebOnSwing - Web Application Framework
002: //Copyright (C) 2003 Fernando Damian Petrola
003: //
004: //This library is free software; you can redistribute it and/or
005: //modify it under the terms of the GNU Lesser General Public
006: //License as published by the Free Software Foundation; either
007: //version 2.1 of the License, or (at your option) any later version.
008: //
009: //This library is distributed in the hope that it will be useful,
010: //but WITHOUT ANY WARRANTY; without even the implied warranty of
011: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: //Lesser General Public License for more details.
013: //
014: //You should have received a copy of the GNU Lesser General Public
015: //License along with this library; if not, write to the Free Software
016: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017:
018: package net.ar.webonswing.ui;
019:
020: import java.awt.event.*;
021: import java.util.*;
022:
023: import javax.swing.*;
024: import javax.swing.event.*;
025:
026: import net.ar.webonswing.*;
027: import net.ar.webonswing.helpers.*;
028: import net.ar.webonswing.managers.templates.*;
029: import net.ar.webonswing.remote.*;
030: import net.ar.webonswing.render.markup.*;
031: import net.ar.webonswing.render.templates.*;
032: import net.ar.webonswing.render.templates.html.*;
033: import net.ar.webonswing.wrapping.*;
034:
035: public class MenuItemUIContributor extends
036: AbstractSwingComponentUIContributor {
037: public void doRenderingContribution(
038: RenderingContributionContainer theContribManager) {
039: JMenuItem theMenuItem = (JMenuItem) getJComponent();
040:
041: removeChangeListener(theMenuItem);
042:
043: String theMenuItemName = theComponent.getName();
044:
045: Template theTemplate = WosSwingHelper.getTemplateForComponent(
046: "JMenuItem", theMenuItem);
047:
048: HtmlTemplate theMenuDisabledTemplate = HtmlTemplateManager
049: .getSubTemplate(theTemplate, "aMenuDisabled");
050: theMenuDisabledTemplate.mergeFoundTag(new Tag("div")
051: .addAttribute("id", theMenuItemName + ".disabled"));
052: theMenuDisabledTemplate.addIdTagTemplateElement("aText",
053: new Tag("span").addElementToContainer(new TagContent(
054: theMenuItem.getText())));
055:
056: HtmlTemplate theMenuEnabledTemplate = HtmlTemplateManager
057: .getSubTemplate(theTemplate, "aMenuEnabled");
058: Tag theDivTag = new Tag("div").addAttribute("id",
059: theMenuItemName + ".enabled").addAttribute("style",
060: "display:none");
061: if (theMenuItem.getListeners(ActionListener.class).length > 0)
062: theDivTag.addAttribute(new TagAttribute("onclick",
063: "ed.dispatch(new ActionEvent('" + theMenuItemName
064: + "', ''));"));
065:
066: theMenuEnabledTemplate.mergeFoundTag(theDivTag);
067:
068: Tag theTextTag = new Tag("span").addElementToContainer(
069: new TagContent(theMenuItem.getText())).addAttribute(
070: "id", theMenuItemName);
071: theMenuEnabledTemplate.addIdTagTemplateElement("aText",
072: theTextTag);
073:
074: setIcon(theMenuItem, theMenuDisabledTemplate,
075: theMenuEnabledTemplate);
076:
077: theContribManager.doContribution(theComponent, new TextContent(
078: theMenuDisabledTemplate.renderToText()
079: + theMenuEnabledTemplate.renderToText()),
080: theTextTag, new String[] {
081: RemoteHelper.getListenersAdds(theComponent),
082: "new JMenuItem('" + theMenuItemName + "')" });
083: }
084:
085: public static void setIcon(JMenuItem theMenuItem,
086: HtmlTemplate theMenuDisabledTemplate,
087: HtmlTemplate theMenuEnabledTemplate) {
088: Icon theIcon = theMenuItem.getIcon();
089: Tag theIconTag = null;
090:
091: if (theIcon != null)
092: theIconTag = new Tag("img").addAttribute("src",
093: WosFramework.getInstance().getContextPath() + "/"
094: + ((ImageIcon) theIcon).getDescription());
095:
096: HtmlTemplate theIconElement = HtmlTemplateManager
097: .getSubTemplate(theMenuDisabledTemplate, "theIcon");
098: if (theIconElement != null)
099: theIconElement.setFoundTag(theIconTag);
100:
101: theIconElement = HtmlTemplateManager.getSubTemplate(
102: theMenuEnabledTemplate, "theIcon");
103: if (theIconElement != null)
104: theIconElement.setFoundTag(theIconTag);
105: }
106:
107: private void removeChangeListener(JMenuItem theMenuItem) {
108: ChangeListener[] theChangeListeners = (ChangeListener[]) theMenuItem
109: .getListeners(ChangeListener.class);
110: if (theChangeListeners.length > 0)
111: theMenuItem.removeChangeListener(theChangeListeners[0]);
112: }
113:
114: public void dispatchEvents(List anEvents) {
115: for (Iterator i = anEvents.iterator(); i.hasNext();) {
116: RemoteEvent theEvent = (RemoteEvent) i.next();
117: JMenuItem theMenuItem = (JMenuItem) ((ComponentWrapper) theEvent
118: .getSource()).getWrappedComponent();
119:
120: if (theEvent.getType().equals("actionPerformed"))
121: theMenuItem.doClick();
122: }
123: }
124: }
|