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