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 ToggleButtonUIContributor extends
032: AbstractSwingComponentUIContributor {
033: public void doRenderingContribution(
034: RenderingContributionContainer theContribManager) {
035: JToggleButton aToggleButton = (JToggleButton) getJComponent();
036:
037: Tag theInputTag = new Tag("input").addAttribute("type",
038: "button")
039: .addAttribute("value", aToggleButton.getText())
040: .addAttribute("class", "buttonUp").setNeedsClosure(
041: false);
042: theInputTag.addAttribute(new TagStyleAttribute().addElement(
043: "width", aToggleButton.getWidth()).addElement("height",
044: aToggleButton.getHeight()));
045:
046: if (aToggleButton.isSelected())
047: theInputTag.addAttribute(new TagAttribute("CHECKED", ""));
048:
049: Tag theHiddenTag = new Tag("input").addAttribute("type",
050: "hidden").setNeedsClosure(false);
051:
052: theInputTag
053: .addAttribute(new TagAttribute(
054: "onclick",
055: "ed.dispatch(new ItemEvent('"
056: + theComponent.getName()
057: + "'));this.className= this.className == 'buttonUp' ? 'buttonDown' : 'buttonUp';"));
058: theHiddenTag.addAttribute("name", theComponent.getName())
059: .addAttribute("value",
060: aToggleButton.isSelected() ? "on" : "off");
061:
062: Tag theTextTag = new Tag("span");
063:
064: Template theTemplate = WosSwingHelper.getTemplateForComponent(
065: "JCheckBox", aToggleButton);
066:
067: theTemplate.addElement(new IdTagTemplateElement(
068: "theHiddenField", theHiddenTag, theHiddenTag));
069: theTemplate.addElement(new IdTagTemplateElement("theButton",
070: theInputTag, theInputTag));
071: theTemplate.addElement(new IdTagTemplateElement("theText",
072: theTextTag, theTextTag));
073:
074: theContribManager.doContribution(theComponent, theTemplate,
075: theInputTag, getInitScript(theComponent));
076: }
077:
078: public String getInitScript(VisualComponent aComponent) {
079: StringBuffer theScript = new StringBuffer();
080:
081: if (!theComponent.getName().equals("unnamed")) {
082: theScript
083: .append("getComponent('"
084: + theComponent.getName()
085: + "').addListener(new ChangeCheckStateListener());");
086: theScript.append(RemoteHelper.getListenersAdds(aComponent));
087: }
088:
089: return theScript.toString();
090: }
091:
092: public void dispatchEvents(List anEvents) {
093: for (Iterator i = anEvents.iterator(); i.hasNext();) {
094: RemoteEvent theEvent = (RemoteEvent) i.next();
095: JCheckBox theCheckBox = (JCheckBox) ((ComponentWrapper) theEvent
096: .getSource()).getWrappedComponent();
097:
098: if (theEvent.getType().equals("update"))
099: if ((theCheckBox.getSelectedObjects() != null) != theEvent
100: .getParameters()[0].equals("on"))
101: theCheckBox.doClick();
102: }
103: }
104: }
|