001: /*
002: * soapUI, copyright (C) 2004-2007 eviware.com
003: *
004: * soapUI is free software; you can redistribute it and/or modify it under the
005: * terms of version 2.1 of the GNU Lesser General Public License as published by
006: * the Free Software Foundation.
007: *
008: * soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
009: * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
010: * See the GNU Lesser General Public License for more details at gnu.org.
011: */
012:
013: package com.eviware.x.impl.swing;
014:
015: import java.awt.event.ActionEvent;
016: import java.io.File;
017:
018: import javax.swing.AbstractAction;
019: import javax.swing.JButton;
020: import javax.swing.JFileChooser;
021: import javax.swing.JPanel;
022: import javax.swing.JTextField;
023: import javax.swing.text.Document;
024:
025: import org.apache.log4j.Logger;
026:
027: import com.eviware.soapui.settings.ProjectSettings;
028: import com.eviware.soapui.support.DocumentListenerAdapter;
029: import com.eviware.soapui.support.UISupport;
030: import com.eviware.x.form.XFormTextField;
031: import com.eviware.x.form.XForm.FieldType;
032: import com.jgoodies.forms.builder.ButtonBarBuilder;
033:
034: public class FileFormField extends AbstractSwingXFormField<JPanel>
035: implements XFormTextField {
036: private final static Logger log = Logger
037: .getLogger(FileFormField.class);
038:
039: private JTextField textField;
040: private final FieldType type;
041: private JButton selectDirectoryButton;
042: private String projectRoot;
043:
044: private boolean updating;
045: private String oldValue;
046:
047: public FileFormField(String tooltip, FieldType type) {
048: super (new JPanel());
049: this .type = type;
050:
051: ButtonBarBuilder builder = new ButtonBarBuilder(getComponent());
052: textField = new JTextField(30);
053: textField.setToolTipText(tooltip);
054: builder.addGriddedGrowing(textField);
055: builder.addRelatedGap();
056: selectDirectoryButton = new JButton(new SelectDirectoryAction());
057: builder.addFixed(selectDirectoryButton);
058:
059: textField.getDocument().addDocumentListener(
060: new DocumentListenerAdapter() {
061:
062: @Override
063: public void update(Document document) {
064: String text = textField.getText();
065:
066: if (!updating)
067: fireValueChanged(text, oldValue);
068:
069: oldValue = text;
070: }
071: });
072: }
073:
074: public void setValue(String value) {
075: updating = true;
076: oldValue = null;
077: updateValue(value);
078: updating = false;
079: }
080:
081: private void updateValue(String value) {
082: if (value != null && projectRoot != null
083: && value.startsWith(projectRoot)) {
084: value = value.substring(projectRoot.length() + 1);
085: }
086:
087: textField.setText(value);
088: }
089:
090: public String getValue() {
091: String text = textField.getText().trim();
092:
093: if (projectRoot != null && text.length() > 0) {
094: String tempName = projectRoot + File.separatorChar + text;
095: if (new File(tempName).exists()) {
096: text = tempName;
097: }
098: }
099:
100: return text;
101: }
102:
103: public void setEnabled(boolean enabled) {
104: textField.setEnabled(enabled);
105: selectDirectoryButton.setEnabled(enabled);
106: }
107:
108: public class SelectDirectoryAction extends AbstractAction {
109: private JFileChooser fileChooser;
110:
111: public SelectDirectoryAction() {
112: super ("Browse...");
113: }
114:
115: public void actionPerformed(ActionEvent e) {
116: if (fileChooser == null) {
117: fileChooser = new JFileChooser();
118: if (type == FieldType.FOLDER
119: || type == FieldType.PROJECT_FOLDER)
120: fileChooser
121: .setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
122: }
123:
124: String value = FileFormField.this .getValue();
125: if (value.length() > 0) {
126: fileChooser.setSelectedFile(new File(value));
127: } else if (projectRoot != null) {
128: fileChooser.setCurrentDirectory(new File(projectRoot));
129: }
130:
131: int returnVal = fileChooser.showOpenDialog(UISupport
132: .getMainFrame());
133: if (returnVal == JFileChooser.APPROVE_OPTION) {
134: updateValue(fileChooser.getSelectedFile()
135: .getAbsolutePath());
136: }
137: }
138: }
139:
140: public void setProperty(String name, Object value) {
141: super .setProperty(name, value);
142:
143: if (name.equals(ProjectSettings.PROJECT_ROOT)) {
144: projectRoot = (String) value;
145: log.debug("Set projectRoot to [" + projectRoot + "]");
146: }
147: }
148:
149: public void setWidth(int columns) {
150: textField.setColumns(columns);
151: }
152: }
|