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.util.*;
021:
022: import javax.swing.*;
023:
024: import net.ar.webonswing.helpers.*;
025: import net.ar.webonswing.remote.*;
026: import net.ar.webonswing.render.markup.*;
027: import net.ar.webonswing.render.templates.*;
028: import net.ar.webonswing.render.templates.html.*;
029: import net.ar.webonswing.wrapping.*;
030:
031: public class CheckBoxUIContributor extends
032: AbstractSwingComponentUIContributor {
033: public void doRenderingContribution(
034: RenderingContributionContainer theContribManager) {
035: JCheckBox aCheckBox = (JCheckBox) getJComponent();
036:
037: Tag theInputTag = new Tag("input").addAttribute("type",
038: "checkbox").setNeedsClosure(false);
039:
040: if (aCheckBox.isSelected())
041: theInputTag.addAttribute(new TagAttribute("CHECKED", ""));
042:
043: TagContent theTextContent = new TagContent("");
044: if (aCheckBox.getText() != null)
045: theTextContent = new TagContent(aCheckBox.getText());
046:
047: Tag theHiddenTag = new Tag("input").addAttribute("type",
048: "hidden").setNeedsClosure(false);
049:
050: theInputTag.addAttribute(new TagAttribute("onclick",
051: "ed.dispatch(new ItemEvent('" + theComponent.getName()
052: + "'));"));
053: theHiddenTag.addAttribute("name", theComponent.getName())
054: .addAttribute("value",
055: aCheckBox.isSelected() ? "on" : "off");
056:
057: Tag theTextTag = new Tag("label");
058: theTextTag.addAttribute("for", theComponent.getName());
059: theTextTag.getTheMarkupContainer().addElement(theTextContent);
060:
061: Template theTemplate = WosSwingHelper.getTemplateForComponent(
062: "JCheckBox", aCheckBox);
063:
064: theTemplate.addElement(new IdTagTemplateElement(
065: "theHiddenField", theHiddenTag, theHiddenTag));
066: theTemplate.addElement(new IdTagTemplateElement("theButton",
067: theInputTag, theInputTag));
068: theTemplate.addElement(new IdTagTemplateElement("theText",
069: theTextTag, theTextTag));
070:
071: theContribManager.doContribution(theComponent, theTemplate,
072: theInputTag, getInitScript(theComponent));
073: }
074:
075: public String getInitScript(VisualComponent aComponent) {
076: StringBuffer theScript = new StringBuffer();
077:
078: if (!theComponent.getName().equals("unnamed")) {
079: theScript
080: .append("getComponent('"
081: + theComponent.getName()
082: + "').addListener(new ChangeCheckStateListener());");
083: theScript.append(RemoteHelper.getListenersAdds(aComponent));
084: }
085:
086: return theScript.toString();
087: }
088:
089: public void dispatchEvents(List anEvents) {
090: for (Iterator i = anEvents.iterator(); i.hasNext();) {
091: RemoteEvent theEvent = (RemoteEvent) i.next();
092: JCheckBox theCheckBox = (JCheckBox) ((ComponentWrapper) theEvent
093: .getSource()).getWrappedComponent();
094:
095: if (theEvent.getType().equals("update"))
096: if ((theCheckBox.getSelectedObjects() != null) != theEvent
097: .getParameters()[0].equals("on"))
098: theCheckBox.doClick();
099: }
100: }
101: }
|