001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.renderer.swing;
017:
018: import java.awt.Color;
019: import java.awt.event.FocusAdapter;
020: import java.awt.event.FocusEvent;
021: import java.awt.event.KeyAdapter;
022: import java.awt.event.KeyEvent;
023:
024: import javax.swing.JPanel;
025:
026: import org.tp23.antinstaller.input.DateInput;
027: import org.tp23.antinstaller.input.OutputField;
028: import org.tp23.antinstaller.renderer.AIResourceBundle;
029: import org.tp23.antinstaller.renderer.MessageRenderer;
030: import org.tp23.gui.GBCF;
031:
032: public class DateInputRenderer extends SwingOutputFieldRenderer {
033:
034: private static final AIResourceBundle res = new AIResourceBundle();
035:
036: protected DateInput inputField;
037:
038: protected AILabel fieldLabel = new AILabel();
039: protected AITextfield jTextField = new AITextfield();
040: protected Color origFore;
041:
042: public DateInputRenderer() {
043: origFore = jTextField.getForeground();
044: }
045:
046: public void initComponent(JPanel parent) {
047: try {
048: jbInit();
049: } catch (Exception e) {
050: e.printStackTrace();
051: }
052: }
053:
054: public void setOutputField(OutputField inputField) {
055: this .inputField = (DateInput) inputField;
056: this .inputField.setValue(this .inputField.getDefaultValue());
057: }
058:
059: public void updateInputField() {
060: inputField.setValue(jTextField.getText());
061: }
062:
063: public void updateDefaultValue() {
064: if (!inputField.isEditted())
065: jTextField.setText(inputField.getDefaultValue());
066: }
067:
068: private void jbInit() throws Exception {
069: fieldLabel.setText(inputField.getDisplayText());
070: jTextField.setText(inputField.getDefaultValue());
071: jTextField.addKeyListener(new KeyAdapter() {
072: public void keyTyped(KeyEvent e) {
073: if (e.getKeyChar() != '\t') {
074: inputField.setEditted(true);
075: }
076: }
077: });
078: jTextField.addFocusListener(new FocusAdapter() {
079: public void focusLost(FocusEvent fe) {
080: jTextField.setForeground(origFore);
081: }
082: });
083: }
084:
085: public int addSelf(JPanel content, GBCF cf, int row,
086: boolean overflow) {
087: content.add(fieldLabel, cf.getCell(row, 0));
088: content.add(jTextField, cf.getCell(row, 1));
089: if (overflow) {
090: jTextField.setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
091: }
092: return ++row;
093: }
094:
095: /**
096: * renderError
097: */
098: public void renderError() {
099: MessageRenderer mr = ctx.getMessageRenderer();
100: mr.printMessage(res.getString("not.correct.format") + "\n\n "
101: + inputField.getDateFormat());
102: //fixed BUG1295944 mr.printMessage("The field is not of the correct format\n\n "+inputField.getDateFormat());
103: this.jTextField.requestFocus();
104: this.jTextField.setForeground(Color.red);
105: }
106:
107: }
|