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:
021: import javax.swing.JComboBox;
022: import javax.swing.JLabel;
023: import javax.swing.JPanel;
024:
025: import org.tp23.antinstaller.input.LargeSelectInput;
026: import org.tp23.antinstaller.input.OutputField;
027: import org.tp23.antinstaller.renderer.AIResourceBundle;
028: import org.tp23.gui.GBCF;
029:
030: public class LargeSelectInputRenderer extends SwingOutputFieldRenderer {
031:
032: private static final AIResourceBundle res = new AIResourceBundle();
033:
034: protected LargeSelectInput inputField;
035:
036: protected JLabel fieldLabel = new AILabel();
037: protected JComboBox optionCombo = new JComboBox();
038:
039: //private int numOfEntries = 2;
040:
041: public LargeSelectInputRenderer() {
042: }
043:
044: public void initComponent(JPanel parent) {
045: try {
046: jbInit();
047: } catch (Exception e) {
048: e.printStackTrace();
049: }
050: }
051:
052: public void setOutputField(OutputField inputField) {
053: this .inputField = (LargeSelectInput) inputField;
054: //this.numOfEntries=this.inputField.getOptions().length;
055: }
056:
057: public void updateInputField() {
058:
059: int selectedIdx = optionCombo.getSelectedIndex();
060: if (selectedIdx != -1) {
061: inputField
062: .setValue(inputField.getOptions()[selectedIdx].value);
063:
064: } else {
065: inputField.setValue(inputField.getDefaultValue());
066: }
067: }
068:
069: public void updateDefaultValue() {
070: if (!inputField.isEditted()) {
071:
072: String newDefault = inputField.getDefaultValue();
073:
074: for (int i = 0; i < optionCombo.getItemCount(); i++) {
075: if (newDefault.equals(inputField.getOptions()[i].value)) {
076: optionCombo.setSelectedIndex(i);
077: break;
078: }
079: }
080: }
081: }
082:
083: private void jbInit() throws Exception {
084: fieldLabel.setText(inputField.getDisplayText());
085: LargeSelectInput.Option[] options = inputField.getOptions();
086:
087: for (int i = 0; i < options.length; i++) {
088: optionCombo.addItem(options[i].getText());
089: if (options[i].value.equals(inputField.getDefaultValue())) {
090: optionCombo.setSelectedIndex(i);
091: }
092: }
093: optionCombo.addItemListener(new ItemListener() {
094: public void itemStateChanged(ItemEvent e) {
095: inputField.setEditted(true);
096: }
097: });
098: }
099:
100: public int addSelf(JPanel content, GBCF cf, int row,
101: boolean overflow) {
102: content.add(fieldLabel, cf.getCell(row, 0));
103: content.add(optionCombo, cf.getCell(row, 1));
104: if (overflow) {
105: optionCombo
106: .setPreferredSize(SizeConstants.OVERFLOW_FIELD_SIZE);
107: }
108: return ++row;
109: }
110:
111: /**
112: * renderError
113: */
114: public void renderError() {
115: ctx.getMessageRenderer().printMessage(
116: res.getString("not.valid.selection"));
117: optionCombo.requestFocus();
118: }
119: }
|