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.helpers.*;
027: import net.ar.webonswing.managers.templates.*;
028: import net.ar.webonswing.remote.*;
029: import net.ar.webonswing.render.markup.*;
030: import net.ar.webonswing.render.templates.*;
031: import net.ar.webonswing.render.templates.html.*;
032: import net.ar.webonswing.wrapping.*;
033:
034: public class CheckBoxMenuItemUIContributor extends
035: AbstractSwingComponentUIContributor {
036: public void doRenderingContribution(
037: RenderingContributionContainer theContribManager) {
038: JCheckBoxMenuItem theCheckBoxMenuItem = (JCheckBoxMenuItem) getJComponent();
039:
040: paintStateButton((ComponentWrapper) theComponent,
041: theContribManager, theCheckBoxMenuItem, "CheckState",
042: "JCheckBoxMenuItem", ItemListener.class);
043: }
044:
045: public static void paintStateButton(ComponentWrapper aComponent,
046: RenderingContributionContainer theContribManager,
047: JMenuItem theMenuItem, String theStateGroup,
048: String theJsObject, Class theListenerClass) {
049: removeChangeListener(theMenuItem);
050:
051: String theMenuItemName = aComponent.getName();
052:
053: Template theTemplate = WosSwingHelper.getTemplateForComponent(
054: "JCheckBoxMenuItem", (JComponent) aComponent
055: .getWrappedComponent());
056:
057: Tag theTextTag = new Tag("span").addElementToContainer(
058: new TagContent(theMenuItem.getText())).addAttribute(
059: "id", theMenuItemName);
060:
061: String thePrefix = "";
062: if (theMenuItem.isSelected())
063: thePrefix = theStateGroup + ".";
064:
065: HtmlTemplate theMenuDisabledTemplate = HtmlTemplateManager
066: .getSubTemplate(theTemplate, thePrefix
067: + "aMenuDisabled");
068: theMenuDisabledTemplate.mergeFoundTag(new Tag("div")
069: .addAttribute("id", theMenuItemName + ".disabled"));
070: theMenuDisabledTemplate.addIdTagTemplateElement("aText",
071: new Tag("span").addElementToContainer(new TagContent(
072: theMenuItem.getText())));
073:
074: HtmlTemplate theMenuEnabledTemplate = HtmlTemplateManager
075: .getSubTemplate(theTemplate, thePrefix + "aMenuEnabled");
076: Tag theDivTag = new Tag("div").addAttribute("id",
077: theMenuItemName + ".enabled").addAttribute("style",
078: "display:none");
079: if (theMenuItem.getListeners(theListenerClass).length > 0) {
080: String theJsEvent = theListenerClass
081: .equals(ItemListener.class) ? "ItemEvent"
082: : "ActionEvent";
083: theDivTag.addAttribute(new TagAttribute("onclick",
084: "ed.dispatch(new " + theJsEvent + "('"
085: + theMenuItemName + "'));"));
086:
087: }
088: theMenuEnabledTemplate.mergeFoundTag(theDivTag);
089: theMenuEnabledTemplate.addIdTagTemplateElement("aText",
090: theTextTag);
091:
092: if (!theMenuItem.isSelected())
093: MenuItemUIContributor.setIcon(theMenuItem,
094: theMenuDisabledTemplate, theMenuEnabledTemplate);
095:
096: String theConstructionScript = "new " + theJsObject + "('"
097: + theMenuItemName + "')";
098:
099: theContribManager.doContribution(aComponent, new TextContent(
100: theMenuDisabledTemplate.renderToText()
101: + theMenuEnabledTemplate.renderToText()),
102: theTextTag, new String[] {
103: RemoteHelper.getListenersAdds(aComponent),
104: theConstructionScript });
105: }
106:
107: public void dispatchEvents(List anEvents) {
108: for (Iterator i = anEvents.iterator(); i.hasNext();) {
109: RemoteEvent theEvent = (RemoteEvent) i.next();
110: JMenuItem theMenuItem = (JMenuItem) ((ComponentWrapper) theEvent
111: .getSource()).getWrappedComponent();
112:
113: if (theEvent.getType().equals("itemStateChange"))
114: theMenuItem.doClick();
115: }
116: }
117:
118: private static void removeChangeListener(JMenuItem theMenuItem) {
119: ChangeListener[] theChangeListeners = (ChangeListener[]) theMenuItem
120: .getListeners(ChangeListener.class);
121:
122: if (theChangeListeners.length > 0)
123: theMenuItem.removeChangeListener(theChangeListeners[0]);
124: }
125: }
|