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.BorderLayout;
019: import java.awt.Color;
020: import java.awt.Dimension;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.awt.event.FocusAdapter;
024: import java.awt.event.FocusEvent;
025: import java.io.File;
026:
027: import javax.swing.JFileChooser;
028: import javax.swing.JPanel;
029:
030: import org.tp23.antinstaller.input.FileInput;
031: import org.tp23.antinstaller.input.OutputField;
032: import org.tp23.antinstaller.renderer.AIResourceBundle;
033: import org.tp23.antinstaller.renderer.MessageRenderer;
034: import org.tp23.gui.GBCF;
035:
036: public class FileInputRenderer extends SwingOutputFieldRenderer {
037:
038: private static final AIResourceBundle res = new AIResourceBundle();
039:
040: protected FileInput inputField;
041:
042: protected AILabel fieldLabel = new AILabel();
043: protected AIShortTextField jTextField = new AIShortTextField();
044: protected AIButton browseButton = new AIButton();
045: protected JPanel browsePanel = new JPanel();
046: private Color origFore = jTextField.getForeground();
047: private JPanel parent;
048:
049: public FileInputRenderer() {
050: }
051:
052: public void initComponent(JPanel parent) {
053: this .parent = parent;
054: try {
055: jbInit();
056: } catch (Exception e) {
057: e.printStackTrace();
058: }
059: }
060:
061: public void setOutputField(OutputField inputField) {
062: this .inputField = (FileInput) inputField;
063: this .inputField.setValue(this .inputField.getDefaultValue(true));
064: }
065:
066: public void updateInputField() {
067: if (!inputField.getDefaultValue(true).equals(
068: jTextField.getText())) {
069: inputField.setEditted(true);
070: }
071: inputField.setValue(jTextField.getText());
072: }
073:
074: public void updateDefaultValue() {
075: if (!inputField.isEditted())
076: jTextField.setText(inputField.getDefaultValue(true));
077: }
078:
079: private void jbInit() throws Exception {
080: BorderLayout bl = new BorderLayout();
081: //bl.setHgap(3);
082: browsePanel.setLayout(bl);
083: fieldLabel.setText(inputField.getDisplayText());
084: jTextField.setText(inputField.getDefaultValue(true));
085: browsePanel.add(jTextField, BorderLayout.CENTER);
086: browsePanel.add(browseButton, BorderLayout.EAST);
087: browseButton.addActionListener(new ActionListener() {
088: public void actionPerformed(ActionEvent e) {
089: File selectedFile = null;
090:
091: JFileChooser chooser = new JFileChooser();
092: chooser.setFileHidingEnabled(false);
093: chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
094: if (jTextField.getText() != null) {
095: chooser.setCurrentDirectory(new File(jTextField
096: .getText()).getParentFile());
097: }
098: int returnVal = chooser.showDialog(parent, e
099: .getActionCommand());
100: if (returnVal == JFileChooser.APPROVE_OPTION) {
101: selectedFile = chooser.getSelectedFile();
102: }
103: if (selectedFile != null) {
104: jTextField.setText(selectedFile.getAbsolutePath());
105: inputField.setValue(selectedFile.getAbsolutePath());
106: inputField.setEditted(true);
107: }
108: }
109: });
110: browseButton.setText(res.getString("button.select.file"));
111: browseButton.setPreferredSize(new Dimension(150,
112: SizeConstants.FIELD_HEIGHT));
113:
114: jTextField.addActionListener(new ActionListener() {
115: public void actionPerformed(ActionEvent e) {
116: updateInputField();
117: }
118: });
119: jTextField.addFocusListener(new FocusAdapter() {
120: public void focusLost(FocusEvent fe) {
121: jTextField.setForeground(origFore);
122: }
123: });
124:
125: }
126:
127: public int addSelf(JPanel content, GBCF cf, int row,
128: boolean overflow) {
129: content.add(fieldLabel, cf.getCell(row, 0));
130: content.add(browsePanel, cf.getCell(row, 1));
131: if (overflow) {
132: jTextField
133: .setOverflow(SizeConstants.OVERFLOW_SHORT_FIELD_SIZE);
134: }
135: return ++row;
136: }
137:
138: /**
139: * renderError
140: */
141: public void renderError() {
142: MessageRenderer mr = ctx.getMessageRenderer();
143: mr.printMessage(res.getString("file.not.exist"));
144: this.jTextField.requestFocus();
145: this.jTextField.setForeground(Color.red);
146: }
147: }
|