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;
20import java.awt.event.KeyAdapter;
21import java.awt.event.KeyEvent;
22
23import javax.swing.JPanel;
24
25import org.tp23.antinstaller.input.OutputField;
26import org.tp23.antinstaller.input.UnvalidatedTextInput;
27import org.tp23.gui.GBCF;
28
29public class UnvalidatedTextInputRenderer
30    extends SwingOutputFieldRenderer {
31
32    protected UnvalidatedTextInput inputField;
33
34    protected AILabel fieldLabel = new AILabel();
35    protected AITextfield jTextField = new AITextfield();
36
37    public UnvalidatedTextInputRenderer() {
38    }
39
40    public void initComponent(JPanel parent){
41        try {
42            jbInit();
43        }
44        catch(Exception e) {
45            ctx.log(e.getMessage());
46            if(ctx.getInstaller().isVerbose()) {
47                ctx.log(e);
48            }
49
50        }
51    }
52
53    public void setOutputField(OutputField inputField) {
54        this.inputField=(UnvalidatedTextInput)inputField;
55        this.inputField.setValue(this.inputField.getDefaultValue());
56    }
57    public void updateInputField(){
58        inputField.setValue(jTextField.getText());
59    }
60    public void updateDefaultValue(){
61        if(!inputField.isEditted())jTextField.setText(inputField.getDefaultValue());
62    }
63
64    private void jbInit() throws Exception {
65        fieldLabel.setText(inputField.getDisplayText());
66        jTextField.setText(inputField.getDefaultValue());
67        jTextField.addActionListener(new ActionListener() {
68            public void actionPerformed(ActionEvent e) {
69                updateInputField();
70            }
71        });
72        jTextField.addKeyListener(new KeyAdapter() {
73            public void keyTyped(KeyEvent e) {
74                if (e.getKeyChar() != '\t') {
75                    inputField.setEditted(true);
76                }
77            }
78        });
79
80    }
81    public int addSelf(JPanel content,GBCF cf,  int row,boolean overflow) {
82        content.add(fieldLabel,cf.getCell(row,0));
83        content.add(jTextField,cf.getCell(row,1));
84        if(overflow){
85            jTextField.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
86        }
87        return ++row;
88    }
89
90    /**
91     * renderError
92     */
93    public void renderError() {
94    }
95}
96