001: /*
002: * Copyright 2005 Paul Hinds
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: package org.tp23.antinstaller.renderer.swing;
017:
018: import java.awt.event.ItemEvent;
019: import java.awt.event.ItemListener;
020: import java.util.Enumeration;
021:
022: import javax.swing.ButtonGroup;
023: import javax.swing.JLabel;
024: import javax.swing.JPanel;
025: import javax.swing.JRadioButton;
026:
027: import org.tp23.antinstaller.input.OutputField;
028: import org.tp23.antinstaller.input.SelectInput;
029: import org.tp23.antinstaller.renderer.AIResourceBundle;
030: import org.tp23.gui.GBCF;
031:
032: public class SelectInputRenderer extends SwingOutputFieldRenderer {
033:
034: private static final AIResourceBundle res = new AIResourceBundle();
035:
036: protected SelectInput inputField;
037:
038: protected JLabel fieldLabel = new AILabel();
039: protected ButtonGroup optionGroup = new ButtonGroup();
040:
041: public SelectInputRenderer() {
042: }
043:
044: public void initComponent(JPanel parent) {
045: try {
046: jbInit();
047: } catch (Exception e) {
048: ctx.log(e.getMessage());
049: if (ctx.getInstaller().isVerbose()) {
050: ctx.log(e);
051: }
052: }
053: }
054:
055: public void setOutputField(OutputField inputField) {
056: this .inputField = (SelectInput) inputField;
057: }
058:
059: public void updateInputField() {
060: Enumeration enumumeration = optionGroup.getElements();
061: int i = 0;
062: for (; enumumeration.hasMoreElements(); i++) {
063: JRadioButton o = (JRadioButton) enumumeration.nextElement();
064: if (o.isSelected()) {
065: inputField.setValue(inputField.getOptions()[i].value);
066: break;
067: }
068: }
069: if (i > inputField.getOptions().length) {
070: inputField.setValue(inputField.getDefaultValue());
071: }
072: }
073:
074: public void updateDefaultValue() {
075: if (!inputField.isEditted()) {
076: String newDefault = inputField.getDefaultValue();
077: Enumeration enumeration = optionGroup.getElements();
078: for (int i = 0; enumeration.hasMoreElements(); i++) {
079: if (newDefault.equals(inputField.getOptions()[i].value)) {
080: JRadioButton jrb = (JRadioButton) enumeration
081: .nextElement();
082: jrb.setSelected(true);
083: // @todo should break?
084: } else {
085: enumeration.nextElement();
086: }
087: }
088: }
089: }
090:
091: private void jbInit() throws Exception {
092: fieldLabel.setText(inputField.getDisplayText());
093: SelectInput.Option[] options = inputField.getOptions();
094:
095: for (int i = 0; i < options.length; i++) {
096: JRadioButton jrb = new AIRadioButton(options[i].getText());
097: optionGroup.add(jrb);
098: if (options[i].value.equals(inputField.getDefaultValue())) {
099: jrb.setSelected(true);
100: }
101: jrb.addItemListener(new ItemListener() {
102: public void itemStateChanged(ItemEvent e) {
103: inputField.setEditted(true);
104: }
105: });
106: }
107: }
108:
109: public int addSelf(JPanel content, GBCF cf, int row,
110: boolean overflow) {
111: content.add(fieldLabel, cf.getCell(row, 0));
112: Enumeration enumeration = optionGroup.getElements();
113: // there should be at least two
114: enumeration.hasMoreElements();
115: AIRadioButton jrb = (AIRadioButton) enumeration.nextElement();
116: content.add(jrb, cf.getCell(row++, 1));
117: if (overflow) {
118: jrb.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
119: }
120: JPanel empty = new AIPanel();
121: while (enumeration.hasMoreElements()) {
122: jrb = (AIRadioButton) enumeration.nextElement();
123: content.add(empty, cf.getCell(row, 0));
124: content.add(jrb, cf.getCell(row++, 1));
125: if (overflow) {
126: jrb.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
127: }
128: }
129: return row;
130: }
131:
132: /**
133: * renderError
134: */
135: public void renderError() {
136: ctx.getMessageRenderer().printMessage(
137: res.getString("not.valid.selection"));
138: // fixed BUG:1295944 ctx.getMessageRenderer().printMessage("Not a valid selection");
139: //optionGroup.requestFocus();
140: }
141: }
|