001: /*
002: * Copyright 2006 Luca Garulli (luca.garulli@assetdata.it)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.romaframework.aspect.view.echo2.component;
018:
019: import java.util.ArrayList;
020: import java.util.List;
021: import java.util.Map;
022:
023: import nextapp.echo2.app.Column;
024: import nextapp.echo2.app.Component;
025: import nextapp.echo2.app.RadioButton;
026: import nextapp.echo2.app.Row;
027: import nextapp.echo2.app.button.ButtonGroup;
028:
029: import org.romaframework.aspect.view.ViewConstants;
030: import org.romaframework.aspect.view.echo2.GUIEventHandler;
031: import org.romaframework.aspect.view.echo2.form.FormUtil;
032: import org.romaframework.aspect.view.echo2.look.LookAndFeelManager;
033: import org.romaframework.aspect.view.form.ContentComponent;
034: import org.romaframework.core.flow.ObjectContext;
035:
036: public class DynaRadio extends AbstractContentComponent {
037:
038: protected Component frame;
039: protected ButtonGroup group;
040: protected List<DynaRadioButton> buttons;
041: protected Class clazz;
042: protected String fieldName;
043:
044: public DynaRadio(ContentComponent iParent) {
045: super (iParent);
046: group = new ButtonGroup();
047: buttons = new ArrayList<DynaRadioButton>();
048: }
049:
050: public void setOrientation(String iOrientation) {
051: if (getComponentCount() > 0)
052: remove(0);
053:
054: if (iOrientation == ViewConstants.ORIENTATION_HORIZONTAL)
055: frame = new Row();
056: else
057: frame = new Column();
058: add(frame);
059:
060: ObjectContext.getInstance().getComponent(
061: LookAndFeelManager.class).assignStyle(frame, clazz,
062: fieldName);
063: }
064:
065: public void addElement(Object iValue) {
066: // CREATE A BUTTON WITH LABEL TO-STRING
067: DynaRadioButton button = new DynaRadioButton(iValue);
068: FormUtil.setMetadata(button, FormUtil.getComponentForm(this ),
069: FormUtil.getComponentSchema(this ));
070:
071: button.addActionListener(GUIEventHandler.getInstance());
072: button.setText(iValue.toString());
073: button.setGroup(group);
074:
075: ObjectContext.getInstance().getComponent(
076: LookAndFeelManager.class).assignStyle(button, clazz,
077: fieldName);
078:
079: group.addButton(button);
080: buttons.add(button);
081: frame.add(button);
082: }
083:
084: public void addElements(Object value) {
085: Object[] coll = null;
086:
087: if (value instanceof List)
088: coll = ((Map) value).values().toArray();
089: else if (value instanceof Map)
090: coll = ((Map) value).values().toArray();
091: else if (value instanceof Object[])
092: coll = (Object[]) value;
093:
094: if (coll != null) {
095: if (frame == null)
096: setOrientation(ViewConstants.ORIENTATION_HORIZONTAL);
097:
098: // Clear RadioButton
099: frame.removeAll();
100:
101: // FILL VALUES
102: for (Object o : coll)
103: addElement(o);
104: }
105: }
106:
107: public void render(ContentComponent iFormToRender) {
108: clazz = FormUtil.getComponentForm(this ).getSchemaInstance()
109: .getSchemaClass().getClazz();
110: fieldName = FormUtil.getComponentSchema(this ).getName();
111:
112: setOrientation(ViewConstants.ORIENTATION_HORIZONTAL);
113: }
114:
115: /**
116: * Return the selected button index in Radio Button Group
117: *
118: * @return the index of button selected, or -1 if none
119: */
120: public int getSelectedIndex() {
121: RadioButton[] buttons = group.getButtons();
122: for (int i = 0; i < buttons.length; ++i) {
123: if (buttons[i].isSelected()) {
124: group.updateSelection(buttons[i]);
125: return i;
126: }
127: }
128: return -1;
129: }
130:
131: /**
132: * Return the element value.
133: *
134: * @param iIndex
135: * @return
136: */
137: public Object getElement(int iIndex) {
138: if (iIndex > buttons.size() - 1)
139: return null;
140: return buttons.get(iIndex).getValue();
141: }
142:
143: public void setSelected(int iIndex) {
144: if (iIndex < buttons.size()) {
145: buttons.get(iIndex).setSelected(true);
146: group.updateSelection(buttons.get(iIndex));
147: }
148: }
149:
150: public ButtonGroup getGroup() {
151: return group;
152: }
153: }
|