001: /*
002: #IFNDEF ALT_LICENSE
003: ThinWire(R) RIA Ajax Framework
004: Copyright (C) 2003-2007 Custom Credit Systems
005:
006: This library is free software; you can redistribute it and/or modify it under
007: the terms of the GNU Lesser General Public License as published by the Free
008: Software Foundation; either version 2.1 of the License, or (at your option) any
009: later version.
010:
011: This library is distributed in the hope that it will be useful, but WITHOUT ANY
012: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
013: PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
014:
015: You should have received a copy of the GNU Lesser General Public License along
016: with this library; if not, write to the Free Software Foundation, Inc., 59
017: Temple Place, Suite 330, Boston, MA 02111-1307 USA
018:
019: Users who would rather have a commercial license, warranty or support should
020: contact the following company who invented, built and supports the technology:
021:
022: Custom Credit Systems, Richardson, TX 75081, USA.
023: email: info@thinwire.com ph: +1 (888) 644-6405
024: http://www.thinwire.com
025: #ENDIF
026: [ v1.2_RC2 ]
027: */
028: package thinwire.render.web;
029:
030: import java.util.ArrayList;
031: import java.util.HashMap;
032: import java.util.Iterator;
033: import java.util.List;
034: import java.util.Map;
035:
036: import thinwire.ui.Application;
037: import thinwire.ui.Component;
038: import thinwire.ui.RadioButton;
039: import thinwire.ui.event.PropertyChangeEvent;
040:
041: /**
042: * @author Joshua J. Gertzen
043: */
044: final class RadioButtonRenderer extends TextComponentRenderer {
045: private static final String RADIOBUTTON_CLASS = "tw_RadioButton";
046: private static final String SET_CHECKED = "setChecked";
047: private static final String SET_GROUP = "setGroup";
048:
049: private Application.Local<Map<Integer, List<RadioButtonRenderer>>> groups = new Application.Local<Map<Integer, List<RadioButtonRenderer>>>() {
050: public Map<Integer, List<RadioButtonRenderer>> initialValue() {
051: return new HashMap<Integer, List<RadioButtonRenderer>>(5);
052: }
053: };
054:
055: private int groupId;
056:
057: void render(WindowRenderer wr, Component c,
058: ComponentRenderer container) {
059: init(RADIOBUTTON_CLASS, wr, c, container);
060: RadioButton rb = (RadioButton) c;
061: addClientSideProperty(RadioButton.PROPERTY_CHECKED);
062: addInitProperty(RadioButton.PROPERTY_CHECKED, rb.isChecked());
063: setGroup(System.identityHashCode(rb.getGroup()));
064: addInitProperty(RadioButton.PROPERTY_GROUP, groupId);
065: super .render(wr, c, container);
066: }
067:
068: private List<RadioButtonRenderer> getRenderGroup(Integer groupId) {
069: Map<Integer, List<RadioButtonRenderer>> map = groups.get();
070: List<RadioButtonRenderer> group = map.get(groupId);
071: if (group == null)
072: map.put(groupId,
073: group = new ArrayList<RadioButtonRenderer>(3));
074: return group;
075: }
076:
077: private void setGroup(int groupId) {
078: if (this .groupId != 0) {
079: for (Iterator<RadioButtonRenderer> it = getRenderGroup(
080: this .groupId).iterator(); it.hasNext();) {
081: if (it.next() == this ) {
082: it.remove();
083: break;
084: }
085: }
086: }
087:
088: this .groupId = groupId;
089: if (groupId != 0)
090: getRenderGroup(groupId).add(this );
091: }
092:
093: public void propertyChange(PropertyChangeEvent pce) {
094: String name = pce.getPropertyName();
095: if (isPropertyChangeIgnored(name))
096: return;
097:
098: if (name.equals(RadioButton.PROPERTY_CHECKED)) {
099: postClientEvent(SET_CHECKED, pce.getNewValue());
100: } else if (name.equals(RadioButton.PROPERTY_GROUP)) {
101: setGroup(System.identityHashCode(pce.getNewValue()));
102: postClientEvent(SET_GROUP, groupId);
103: } else {
104: super .propertyChange(pce);
105: }
106: }
107:
108: public void componentChange(WebComponentEvent event) {
109: String name = event.getName();
110: String value = (String) event.getValue();
111: RadioButton rb = (RadioButton) comp;
112:
113: if (name.equals(RadioButton.PROPERTY_CHECKED)) {
114: RadioButton.Group group = rb.getGroup();
115:
116: if (group != null) {
117: List<RadioButtonRenderer> lst = getRenderGroup(System
118: .identityHashCode(group));
119:
120: for (RadioButtonRenderer r : lst) {
121: r.setPropertyChangeIgnored(
122: RadioButton.PROPERTY_CHECKED, true);
123: }
124:
125: rb.setChecked(Boolean.valueOf(value).booleanValue());
126:
127: for (RadioButtonRenderer r : lst) {
128: r.setPropertyChangeIgnored(
129: RadioButton.PROPERTY_CHECKED, false);
130: }
131: } else {
132: setPropertyChangeIgnored(RadioButton.PROPERTY_CHECKED,
133: true);
134: rb.setChecked(Boolean.valueOf(value).booleanValue());
135: setPropertyChangeIgnored(RadioButton.PROPERTY_CHECKED,
136: false);
137: }
138: } else {
139: super .componentChange(event);
140: }
141: }
142:
143: void destroy() {
144: setGroup(0);
145: super.destroy();
146: }
147: }
|