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.Enumeration;
21import java.util.ResourceBundle;
22
23import javax.swing.ButtonGroup;
24import javax.swing.JLabel;
25import javax.swing.JPanel;
26import javax.swing.JRadioButton;
27
28import org.tp23.antinstaller.input.OutputField;
29import org.tp23.antinstaller.input.SelectInput;
30import org.tp23.gui.GBCF;
31
32public class SelectInputRenderer
33    extends SwingOutputFieldRenderer {
34    
35    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
36
37    protected SelectInput inputField;
38
39    protected JLabel fieldLabel = new AILabel();
40    protected ButtonGroup optionGroup = new ButtonGroup();
41
42    public SelectInputRenderer() {
43    }
44    public void initComponent(JPanel parent){
45        try {
46            jbInit();
47        }
48        catch(Exception e) {
49            ctx.log(e.getMessage());
50            if(ctx.getInstaller().isVerbose()) {
51                ctx.log(e);
52            }
53        }
54    }
55
56
57
58    public void setOutputField(OutputField inputField) {
59        this.inputField = (SelectInput)inputField;
60    }
61    public void updateInputField(){
62        Enumeration enumumeration = optionGroup.getElements();
63        int i = 0;
64        for(; enumumeration.hasMoreElements(); i++){
65            JRadioButton o = (JRadioButton)enumumeration.nextElement();
66            if(o.isSelected()) {
67                inputField.setValue(inputField.getOptions()[i].value);
68                break;
69            }
70        }
71        if(i > inputField.getOptions().length) {
72            inputField.setValue(inputField.getDefaultValue());
73        }
74    }
75    public void updateDefaultValue() {
76        if(!inputField.isEditted()) {
77            String newDefault = inputField.getDefaultValue();
78            Enumeration enumeration = optionGroup.getElements();
79            for(int i = 0; enumeration.hasMoreElements(); i++){
80                if(newDefault.equals(inputField.getOptions()[i].value)) {
81                    JRadioButton jrb = (JRadioButton) enumeration.nextElement();
82                    jrb.setSelected(true);
83                    // @todo should break?
84                } else {
85                    enumeration.nextElement();
86                }
87            }
88        }
89    }
90
91    private void jbInit() throws Exception {
92        fieldLabel.setText(inputField.getDisplayText());
93        SelectInput.Option[] options = inputField.getOptions();
94
95        for (int i = 0; i < options.length; i++) {
96            JRadioButton jrb = new AIRadioButton(options[i].getText());
97            optionGroup.add(jrb);
98            if(options[i].value.equals(inputField.getDefaultValue())){
99                jrb.setSelected(true);
00            }
01            jrb.addItemListener(new ItemListener(){
02                public void itemStateChanged(ItemEvent e) {
03                    inputField.setEditted(true);
04                }
05            });
06        }
07    }
08    public int addSelf(JPanel content,GBCF cf,  int row,boolean overflow) {
09        content.add(fieldLabel,cf.getCell(row, 0));
10        Enumeration enumeration = optionGroup.getElements();
11        // there should be at least two
12        enumeration.hasMoreElements();
13        AIRadioButton jrb = (AIRadioButton)enumeration.nextElement();
14        content.add(jrb,cf.getCell(row++, 1));
15        if(overflow) {
16            jrb.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
17        }
18        JPanel empty = new JPanel();
19        while(enumeration.hasMoreElements()){
20            jrb = (AIRadioButton)enumeration.nextElement();
21            content.add(empty,cf.getCell(row, 0));
22            content.add(jrb,cf.getCell(row++, 1));
23            if(overflow) {
24                jrb.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
25            }
26        }
27        return row;
28    }
29
30    /**
31     * renderError
32     */
33    public void renderError() {
34        ctx.getMessageRenderer().printMessage(res.getString("notValidSelection"));
35        // fixed BUG:1295944  ctx.getMessageRenderer().printMessage("Not a valid selection");
36        //optionGroup.requestFocus();
37    }
38}
39