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.Color;
19import java.awt.event.FocusAdapter;
20import java.awt.event.FocusEvent;
21import java.awt.event.KeyAdapter;
22import java.awt.event.KeyEvent;
23import java.util.ResourceBundle;
24
25import javax.swing.JPanel;
26import javax.swing.JTextField;
27
28import org.tp23.antinstaller.input.OutputField;
29import org.tp23.antinstaller.input.ValidatedTextInput;
30import org.tp23.antinstaller.renderer.MessageRenderer;
31import org.tp23.gui.GBCF;
32
33public class ValidatedTextInputRenderer extends SwingOutputFieldRenderer {
34
35    private static final ResourceBundle res = ResourceBundle.getBundle("org.tp23.antinstaller.renderer.Res");
36
37    protected ValidatedTextInput inputField;
38    protected AILabel fieldLabel = new AILabel();
39    protected JTextField jTextField = new AITextfield();
40    protected Color origFore;
41
42    public ValidatedTextInputRenderer() {
43        origFore = jTextField.getForeground();
44    }
45
46    public void initComponent(JPanel parent) {
47        try {
48            jbInit();
49        } catch (Exception e) {
50            ctx.log(e.getMessage());
51            if (ctx.getInstaller().isVerbose()) {
52                ctx.log(e);
53            }
54
55        }
56    }
57
58    public void setOutputField(OutputField inputField) {
59        this.inputField = (ValidatedTextInput) inputField;
60        this.inputField.setValue(this.inputField.getDefaultValue());
61    }
62
63    public void updateInputField() {
64        inputField.setValue(jTextField.getText());
65    }
66
67    public void updateDefaultValue() {
68        if (!inputField.isEditted()) {
69            jTextField.setText(inputField.getDefaultValue());
70        }
71    }
72
73    private void jbInit() throws Exception {
74        fieldLabel.setText(inputField.getDisplayText());
75        jTextField.setText(inputField.getDefaultValue());
76
77        jTextField.addFocusListener(new FocusAdapter() {
78            public void focusLost(FocusEvent fe) {
79                jTextField.setForeground(origFore);
80            }
81        });
82        jTextField.addKeyListener(new KeyAdapter() {
83            public void keyTyped(KeyEvent e) {
84                if (e.getKeyChar() != '\t') {
85                    inputField.setEditted(true);
86                }
87            }
88        });
89    }
90
91    public int addSelf(JPanel content, GBCF cf, int row, boolean overflow) {
92        content.add(fieldLabel, cf.getCell(row, 0));
93        content.add(jTextField, cf.getCell(row, 1));
94        if (overflow) {
95            ((AITextfield) jTextField).setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
96        }
97        return ++row;
98    }
99
00    /**
01     * renderError
02     */
03    public void renderError() {
04        MessageRenderer mr = ctx.getMessageRenderer();
05        mr.printMessage(res.getString("notCorrectFormat") + "\n\n e.g. "
06                + inputField.getDefaultValue());
07        this.jTextField.requestFocus();
08        this.jTextField.setForeground(Color.red);
09    }
10
11}
12