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:
025: import net.ar.webonswing.*;
026: import net.ar.webonswing.helpers.*;
027: import net.ar.webonswing.remote.*;
028: import net.ar.webonswing.render.markup.*;
029: import net.ar.webonswing.render.templates.*;
030: import net.ar.webonswing.render.templates.html.*;
031: import net.ar.webonswing.wrapping.*;
032:
033: public class RadioButtonUIContributor extends
034: AbstractSwingComponentUIContributor {
035: public void doRenderingContribution(
036: RenderingContributionContainer theContribManager) {
037: JRadioButton aRadioButton = (JRadioButton) getJComponent();
038:
039: DefaultButtonModel theButtonModel = (DefaultButtonModel) aRadioButton
040: .getModel();
041: ButtonGroup theGroup = theButtonModel.getGroup();
042: AbstractButton theFirstButton = aRadioButton;
043: if (theGroup != null)
044: theFirstButton = (AbstractButton) theButtonModel.getGroup()
045: .getElements().nextElement();
046:
047: String name = WosFramework.getInstance().getHierarchyWrapper()
048: .getComponentWrapper(theFirstButton).getName();
049:
050: Tag theTag = new Tag("input");
051: theTag.setNeedsClosure(false);
052: theTag.addAttribute(new TagAttribute("type", "radio"));
053: theTag.addAttribute(new TagAttribute("name", name));
054: theTag.addAttribute(new TagAttribute("value", theComponent
055: .getName()));
056: theTag.addAttribute(new TagAttribute("id", theComponent
057: .getName()));
058:
059: TagContent theTextContent = aRadioButton.getText() == null ? new TagContent(
060: "")
061: : new TagContent(aRadioButton.getText());
062:
063: if (aRadioButton.getListeners(ActionListener.class).length > 0)
064: theTag.addAttribute(new TagAttribute("onclick",
065: "ed.dispatch(new ActionEvent(this.value, '"
066: + RemoteHelper
067: .escapeSingleQuotes(aRadioButton
068: .getActionCommand())
069: + "'));"));
070:
071: if (aRadioButton.isSelected())
072: theTag.addAttribute(new TagAttribute("CHECKED", ""));
073:
074: Template theTemplate = WosSwingHelper.getTemplateForComponent(
075: "JRadioButton", aRadioButton);
076:
077: Tag theTextTag = new Tag("label");
078: theTextTag.addAttribute("for", theComponent.getName());
079: theTextTag.getTheMarkupContainer().addElement(theTextContent);
080:
081: theTemplate.addElement(new IdTagTemplateElement("theButton",
082: theTag));
083: theTemplate.addElement(new IdTagTemplateElement("theText",
084: theTextTag));
085:
086: theContribManager.doContribution(theComponent, theTemplate,
087: theTag, RemoteHelper.getListenersAdds(theComponent));
088: }
089:
090: public void dispatchEvents(List anEvents) {
091: boolean actionFound = false;
092:
093: for (Iterator i = anEvents.iterator(); i.hasNext()
094: && !actionFound;) {
095: RemoteEvent theEvent = (RemoteEvent) i.next();
096:
097: VisualComponent theSourceComponent = (ComponentWrapper) theEvent
098: .getSource();
099:
100: JRadioButton theRadioButton = (JRadioButton) getJComponent();
101: DefaultButtonModel theButtonModel = (DefaultButtonModel) theRadioButton
102: .getModel();
103: ButtonGroup theGroup = theButtonModel.getGroup();
104:
105: if (theGroup != null) {
106: if (actionFound = theEvent.getType().equals(
107: "actionPerformed")) {
108: if (theGroup.getSelection() != theButtonModel)
109: theRadioButton.doClick();
110: }
111:
112: if (theEvent.getType().equals("update")) {
113: JRadioButton theSelectedRadioButton;
114:
115: VisualComponent theFoundComponent = theSourceComponent
116: .getTopParent()
117: .findComponent(
118: (String) theEvent.getParameters()[0]);
119: theSelectedRadioButton = (JRadioButton) ((ComponentWrapper) theFoundComponent)
120: .getWrappedComponent();
121:
122: if (theGroup != null
123: && theSelectedRadioButton != null
124: && theGroup.getSelection() != theSelectedRadioButton
125: .getModel())
126: theSelectedRadioButton.doClick();
127: }
128: }
129: }
130: }
131: }
|