001: package org.conform.wings.editor;
002:
003: import org.wings.*;
004: import org.conform.format.Format;
005: import org.conform.format.FormatFactory;
006:
007: import javax.swing.*;
008: import javax.swing.event.ListDataListener;
009: import javax.swing.event.ListDataEvent;
010: import java.util.*;
011: import java.awt.event.ActionListener;
012: import java.awt.event.ActionEvent;
013:
014: /**
015: * Behaves like a SComboBox but displays as columns of SRadioButtons.
016: * Instead of a CellRenderer it accepts a Format only. In that respect this component is
017: * less flexible than its pendant. It will display the options as text only.
018: *
019: * @author hengels
020: * @version $Revision: 772 $
021: */
022: public class RadioButtonPanel extends SPanel implements
023: ListDataListener, ActionListener {
024: int columns = 1;
025: boolean update;
026:
027: protected ComboBoxModel model;
028: protected Format format = FormatFactory.NO_FORMAT;
029: SButtonGroup buttonGroup = new SButtonGroup();
030: Map radioByValue = new HashMap();
031:
032: public RadioButtonPanel() {
033: super (new SGridLayout());
034: buttonGroup.addActionListener(this );
035: }
036:
037: public ComboBoxModel getModel() {
038: return model;
039: }
040:
041: public void setModel(ComboBoxModel model) {
042: if (isDifferent(this .model, model)) {
043: if (this .model != null)
044: this .model.removeListDataListener(this );
045:
046: this .model = model;
047:
048: if (this .model != null)
049: this .model.addListDataListener(this );
050:
051: adaptButtons();
052: updateSelection();
053: reload();
054: }
055: }
056:
057: public SButtonGroup getButtonGroup() {
058: return buttonGroup;
059: }
060:
061: public void updateSelection() {
062: try {
063: update = true;
064: SRadioButton button = (SRadioButton) radioByValue.get(model
065: .getSelectedItem());
066: if (button != null)
067: button.setSelected(true);
068: } finally {
069: update = false;
070: }
071: }
072:
073: public Format getFormat() {
074: return format != FormatFactory.NO_FORMAT ? format : null;
075: }
076:
077: public void setFormat(Format format) {
078: if (isDifferent(this .format, format)) {
079: this .format = format;
080: if (format == null)
081: format = FormatFactory.NO_FORMAT;
082:
083: adaptFormat();
084: }
085: }
086:
087: public int getColumns() {
088: return columns;
089: }
090:
091: public void setColumns(int columns) {
092: this .columns = columns;
093: SGridLayout layout = (SGridLayout) getLayout();
094: layout.setColumns(columns);
095: }
096:
097: public void setEnabled(boolean enabled) {
098: super .setEnabled(enabled);
099: for (Iterator iterator = radioByValue.values().iterator(); iterator
100: .hasNext();) {
101: SRadioButton radio = (SRadioButton) iterator.next();
102: radio.setEnabled(enabled);
103: }
104: }
105:
106: private void adaptButtons() {
107: removeAll();
108: buttonGroup.removeAll();
109: radioByValue.clear();
110:
111: if (model == null)
112: return;
113:
114: for (int i = 0; i < model.getSize(); i++) {
115: Object value = model.getElementAt(i);
116: SRadioButton radio = new SRadioButton(format.format(value));
117: radio.setHorizontalAlignment(SConstants.LEFT_ALIGN);
118: radio.putClientProperty("value", value);
119: radioByValue.put(value, radio);
120: buttonGroup.add(radio);
121: add(radio);
122: }
123: }
124:
125: private void adaptFormat() {
126: if (model == null)
127: return;
128:
129: for (int i = 0; i < model.getSize(); i++) {
130: Object value = model.getElementAt(i);
131: SRadioButton radio = (SRadioButton) radioByValue.get(value);
132: radio.setText(format.format(value));
133: }
134: }
135:
136: public void intervalAdded(ListDataEvent e) {
137: adaptButtons();
138: }
139:
140: public void intervalRemoved(ListDataEvent e) {
141: adaptButtons();
142: }
143:
144: public void contentsChanged(ListDataEvent e) {
145: adaptButtons();
146: }
147:
148: public void actionPerformed(ActionEvent e) {
149: if (update)
150: return;
151:
152: SAbstractButton radio = buttonGroup.getSelection();
153: if (radio == null)
154: return;
155:
156: Object value = radio.getClientProperty("value");
157: model.setSelectedItem(value);
158: }
159: }
|