1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.tp23.antinstaller.renderer.swing;
17
18import java.awt.event.ItemEvent;
19import java.awt.event.ItemListener;
20import java.util.ResourceBundle;
21
22import javax.swing.JComboBox;
23import javax.swing.JLabel;
24import javax.swing.JPanel;
25
26import org.tp23.antinstaller.input.LargeSelectInput;
27import org.tp23.antinstaller.input.OutputField;
28import org.tp23.gui.GBCF;
29
30public class LargeSelectInputRenderer
31    extends SwingOutputFieldRenderer {
32
33    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
34
35    protected LargeSelectInput inputField;
36
37    protected JLabel fieldLabel = new AILabel();
38    protected JComboBox optionCombo = new JComboBox();
39
40    //private int numOfEntries = 2;
41
42    public LargeSelectInputRenderer() {
43    }
44    public void initComponent(JPanel parent){
45        try {
46            jbInit();
47        }
48        catch(Exception e) {
49            e.printStackTrace();
50        }
51    }
52
53
54
55    public void setOutputField(OutputField inputField) {
56        this.inputField=(LargeSelectInput)inputField;
57        //this.numOfEntries=this.inputField.getOptions().length;
58    }
59    public void updateInputField(){
60
61        int selectedIdx = optionCombo.getSelectedIndex();
62        if (selectedIdx != -1) {
63            inputField.setValue(inputField.getOptions()[selectedIdx].value);
64
65        }else{
66            inputField.setValue(inputField.getDefaultValue());
67        }
68    }
69    public void updateDefaultValue(){
70        if(!inputField.isEditted()){
71
72            String newDefault = inputField.getDefaultValue();
73
74            for(int i=0;i<optionCombo.getItemCount();i++){
75                if(newDefault.equals(inputField.getOptions()[i].value)){
76                    optionCombo.setSelectedIndex(i);
77                    break;
78                }
79            }
80        }
81    }
82
83    private void jbInit() throws Exception {
84        fieldLabel.setText(inputField.getDisplayText());
85        LargeSelectInput.Option[] options = inputField.getOptions();
86
87
88        for (int i = 0; i < options.length; i++) {
89            optionCombo.addItem(options[i].getText());
90            if(options[i].value.equals(inputField.getDefaultValue())){
91                optionCombo.setSelectedIndex(i);
92            }
93        }
94        optionCombo.addItemListener(new ItemListener(){
95            public void itemStateChanged(ItemEvent e) {
96                inputField.setEditted(true);
97            }
98        });
99    }
00    public int addSelf(JPanel content,GBCF cf,  int row,boolean overflow) {
01        content.add(fieldLabel,cf.getCell(row,0));
02        content.add(optionCombo,cf.getCell(row,1));
03        if(overflow){
04            optionCombo.setPreferredSize(SizeConstants.OVERFLOW_FIELD_SIZE);
05        }
06        return ++row;
07    }
08
09    /**
10     * renderError
11     */
12    public void renderError() {
13        ctx.getMessageRenderer().printMessage(res.getString("notValidSelection"));
14        optionCombo.requestFocus();
15    }
16}
17