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.Dimension;
020: import java.awt.Frame;
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.io.File;
024:
025: import javax.swing.JFileChooser;
026: import javax.swing.JFrame;
027: import javax.swing.JPanel;
028:
029: import org.tp23.antinstaller.input.DirectoryInput;
030: import org.tp23.antinstaller.input.OutputField;
031: import org.tp23.antinstaller.renderer.AIResourceBundle;
032: import org.tp23.gui.GBCF;
033: import org.tp23.gui.tree.CustomDirectoryChooser;
034: import org.tp23.gui.widget.DefaultingDirectoryChooser;
035: import org.tp23.gui.widget.DirectoryChooser;
036:
037: public class DirectoryInputRenderer extends SwingOutputFieldRenderer {
038:
039: private static final AIResourceBundle res = new AIResourceBundle();
040:
041: private static final String EMPTY_STRING = "";
042: protected DirectoryInput inputField;
043: private boolean createMode;
044: private DirectoryChooser chooser = null;
045:
046: protected AILabel fieldLabel = new AILabel();
047: protected AIShortTextField jTextField = new AIShortTextField();
048: protected AIButton browseButton = new AIButton();
049: protected JPanel browsePanel = new JPanel();
050: private JPanel parent;
051:
052: public DirectoryInputRenderer() {
053: }
054:
055: public void initComponent(JPanel parent) {
056: this .parent = parent;
057: try {
058: jbInit();
059: } catch (Exception e) {
060: e.printStackTrace();
061: }
062: }
063:
064: public void setOutputField(OutputField inputField) {
065: this .inputField = (DirectoryInput) inputField;
066: this .inputField.setValue(this .inputField.getDefaultValue(true));
067: this .createMode = OutputField.isTrue(this .inputField
068: .getCreate());
069:
070: }
071:
072: public void updateInputField() {
073: if (!inputField.getDefaultValue(true).equals(
074: jTextField.getText())) {
075: inputField.setEditted(true);
076: }
077: inputField.setValue(jTextField.getText());
078: }
079:
080: public void updateDefaultValue() {
081: if (!inputField.isEditted())
082: jTextField.setText(inputField.getDefaultValue(true));
083: }
084:
085: private void jbInit() throws Exception {
086: BorderLayout bl = new BorderLayout();
087: //bl.setHgap(3);
088: browsePanel.setLayout(bl);
089: fieldLabel.setText(inputField.getDisplayText());
090: jTextField.setText(inputField.getDefaultValue(true));
091: browsePanel.add(jTextField, BorderLayout.CENTER);
092: browsePanel.add(browseButton, BorderLayout.EAST);
093: browseButton.addActionListener(new ActionListener() {
094: public void actionPerformed(ActionEvent e) {
095: File selectedFile = null;
096: //PR 1468823 - if input is cleared, dialogue won't be displayed
097: String dirPath = jTextField.getText();
098: if (dirPath == null) {
099: dirPath = EMPTY_STRING;
100: }
101: dirPath = dirPath.trim();
102:
103: if (dirPath.length() == 0) {
104: dirPath = inputField.getDefaultValue(true);
105: jTextField.setText(dirPath);
106: }
107:
108: if (chooser == null) {
109: if (createMode) {
110: chooser = new CustomDirectoryChooser(parent
111: .getRootPane(), new File(dirPath), true);
112: } else {
113: chooser = new DefaultingDirectoryChooser(
114: createMode);
115: }
116: chooser.setFileHidingEnabled(false);
117: }
118: chooser.setDefaultDirectory(new File(dirPath));
119:
120: int returnVal = chooser.showDialog(parent, e
121: .getActionCommand());
122: //File currentdir = null;
123: if (returnVal == JFileChooser.APPROVE_OPTION) {
124: selectedFile = chooser.getSelectedFile();
125: //currentdir = chooser.getCurrentDirectory();
126: }
127: if (selectedFile != null) {
128: jTextField.setText(selectedFile.getAbsolutePath());
129: inputField.setValue(selectedFile.getAbsolutePath());
130: inputField.setEditted(true);
131: }
132: }
133: });
134: browseButton.setText(res.getString("button.select.dir"));
135: browseButton.setPreferredSize(new Dimension(150,
136: SizeConstants.FIELD_HEIGHT));
137:
138: jTextField.addActionListener(new ActionListener() {
139: public void actionPerformed(ActionEvent e) {
140: updateInputField();
141: }
142: });
143:
144: }
145:
146: public int addSelf(JPanel content, GBCF cf, int row,
147: boolean overflow) {
148: content.add(fieldLabel, cf.getCell(row, 0));
149: content.add(browsePanel, cf.getCell(row, 1));
150: if (overflow) {
151: jTextField
152: .setOverflow(SizeConstants.OVERFLOW_SHORT_FIELD_SIZE);
153: }
154: return ++row;
155: }
156:
157: /**
158: * renderError
159: */
160: public void renderError() {
161: jTextField.requestFocus();
162: }
163: }
|