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.ActionEvent;
19import java.awt.event.ActionListener;
20
21import javax.swing.JLabel;
22import javax.swing.JPanel;
23
24import org.tp23.antinstaller.input.CheckboxInput;
25import org.tp23.antinstaller.input.InputField;
26import org.tp23.antinstaller.input.OutputField;
27import org.tp23.gui.GBCF;
28
29public class CheckboxInputRenderer
30    extends SwingOutputFieldRenderer {
31
32    protected CheckboxInput inputField;
33
34    protected JLabel fieldLabel = new AILabel();
35    protected AICheckBox checkBox = new AICheckBox();
36
37    public CheckboxInputRenderer() {
38    }
39    public void initComponent(JPanel parent){
40        try {
41            jbInit();
42        }
43        catch(Exception e) {
44            e.printStackTrace();
45        }
46    }
47
48
49
50    public void setOutputField(OutputField inputField) {
51        this.inputField = (CheckboxInput)inputField;
52    }
53    public void updateInputField(){
54        boolean selected = checkBox.isSelected();
55        if(selected)inputField.setValue("true");
56        else inputField.setValue("false");
57    }
58    public void updateDefaultValue(){
59        if(!inputField.isEditted()){
60            String newDefault = inputField.getDefaultValue();
61            checkBox.setSelected(InputField.isTrue(newDefault));
62        }
63    }
64
65    private void jbInit() throws Exception {
66        fieldLabel.setText(inputField.getDisplayText());
67        checkBox.setSelected(OutputField.isTrue(inputField.getDefaultValue()));
68        checkBox.setEnabled(!OutputField.isTrue(inputField.getForce()));
69        checkBox.addActionListener(new ActionListener() {
70            public void actionPerformed(ActionEvent e) {
71                updateInputField();
72                inputField.setEditted(true);
73            }
74        });
75    }
76    public int addSelf(JPanel content, GBCF cf,  int row, boolean overflow) {
77        content.add(fieldLabel, cf.getCell(row, 0));
78        content.add(checkBox, cf.getCell(row, 1));
79        if(overflow){
80            checkBox.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
81        }
82        return ++row;
83    }
84
85    /**
86     * renderError
87     */
88    public void renderError() {
89    }
90}
91