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: import javax.swing.JTextField;
026:
027: import org.tp23.antinstaller.input.OutputField;
028: import org.tp23.antinstaller.input.ValidatedTextInput;
029: import org.tp23.antinstaller.renderer.AIResourceBundle;
030: import org.tp23.antinstaller.renderer.MessageRenderer;
031: import org.tp23.gui.GBCF;
032:
033: public class ValidatedTextInputRenderer extends
034: SwingOutputFieldRenderer {
035:
036: private static final AIResourceBundle res = new AIResourceBundle();
037:
038: protected ValidatedTextInput inputField;
039: protected AILabel fieldLabel = new AILabel();
040: protected JTextField jTextField = new AITextfield();
041: protected Color origFore;
042:
043: public ValidatedTextInputRenderer() {
044: origFore = jTextField.getForeground();
045: }
046:
047: public void initComponent(JPanel parent) {
048: try {
049: jbInit();
050: } catch (Exception e) {
051: ctx.log(e.getMessage());
052: if (ctx.getInstaller().isVerbose()) {
053: ctx.log(e);
054: }
055:
056: }
057: }
058:
059: public void setOutputField(OutputField inputField) {
060: this .inputField = (ValidatedTextInput) inputField;
061: this .inputField.setValue(this .inputField.getDefaultValue());
062: }
063:
064: public void updateInputField() {
065: inputField.setValue(jTextField.getText());
066: }
067:
068: public void updateDefaultValue() {
069: if (!inputField.isEditted()) {
070: jTextField.setText(inputField.getDefaultValue());
071: }
072: }
073:
074: private void jbInit() throws Exception {
075: fieldLabel.setText(inputField.getDisplayText());
076: jTextField.setText(inputField.getDefaultValue());
077:
078: jTextField.addFocusListener(new FocusAdapter() {
079: public void focusLost(FocusEvent fe) {
080: jTextField.setForeground(origFore);
081: }
082: });
083: jTextField.addKeyListener(new KeyAdapter() {
084: public void keyTyped(KeyEvent e) {
085: if (e.getKeyChar() != '\t') {
086: inputField.setEditted(true);
087: }
088: }
089: });
090: }
091:
092: public int addSelf(JPanel content, GBCF cf, int row,
093: boolean overflow) {
094: content.add(fieldLabel, cf.getCell(row, 0));
095: content.add(jTextField, cf.getCell(row, 1));
096: if (overflow) {
097: ((AITextfield) jTextField)
098: .setOverflow(SizeConstants.OVERFLOW_FIELD_SIZE);
099: }
100: return ++row;
101: }
102:
103: /**
104: * renderError
105: */
106: public void renderError() {
107: MessageRenderer mr = ctx.getMessageRenderer();
108: mr.printMessage(res.getString("not.correct.format")
109: + "\n\n e.g. " + inputField.getDefaultValue());
110: this.jTextField.requestFocus();
111: this.jTextField.setForeground(Color.red);
112: }
113:
114: }
|