001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jmeter.testbeans.gui;
018:
019: import java.awt.BorderLayout;
020: import java.awt.Component;
021: import java.awt.Graphics;
022: import java.awt.Rectangle;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.beans.PropertyChangeListener;
026: import java.beans.PropertyEditor;
027: import java.beans.PropertyEditorSupport;
028: import java.io.File;
029:
030: import javax.swing.JButton;
031: import javax.swing.JFileChooser;
032: import javax.swing.JPanel;
033:
034: import org.apache.jmeter.gui.util.FileDialoger;
035:
036: /**
037: * A property editor for File properties.
038: * <p>
039: * Note that it never gives out File objects, but always Strings. This is
040: * because JMeter is now too dumb to handle File objects (there's no
041: * FileProperty).
042: *
043: * @version $Revision: 503290 $ updated on $Date: 2007-02-03 19:31:31 +0000 (Sat, 03 Feb 2007) $
044: */
045: public class FileEditor implements PropertyEditor, ActionListener {
046:
047: /**
048: * The editor's panel.
049: */
050: private JPanel panel;
051:
052: /**
053: * The editor handling the text field inside:
054: */
055: private PropertyEditor editor;
056:
057: public FileEditor() {
058: // Create a button to trigger the file chooser:
059: JButton button = new JButton("Browse...");
060: button.addActionListener(this );
061:
062: // Get a WrapperEditor to provide the field or combo -- we'll delegate
063: // most methods to it:
064: editor = new WrapperEditor(this , new SimpleFileEditor(),
065: new ComboStringEditor(), true, true, true, null);
066:
067: // Create a panel containing the combo and the button:
068: panel = new JPanel(new BorderLayout(5, 0));
069: panel.add(editor.getCustomEditor(), BorderLayout.CENTER);
070: panel.add(button, BorderLayout.EAST);
071: }
072:
073: /*
074: * (non-Javadoc)
075: *
076: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
077: */
078: public void actionPerformed(ActionEvent e) {
079: JFileChooser chooser = FileDialoger.promptToOpenFile();
080:
081: File file = chooser.getSelectedFile();
082:
083: setValue(file.getPath());
084: }
085:
086: /**
087: * @param listener
088: */
089: public void addPropertyChangeListener(
090: PropertyChangeListener listener) {
091: editor.addPropertyChangeListener(listener);
092: }
093:
094: /**
095: * @return the text
096: */
097: public String getAsText() {
098: return editor.getAsText();
099: }
100:
101: /**
102: * @return custom editor panel
103: */
104: public Component getCustomEditor() {
105: return panel;
106: }
107:
108: /**
109: * @return the Java initialisation string
110: */
111: public String getJavaInitializationString() {
112: return editor.getJavaInitializationString();
113: }
114:
115: /**
116: * @return the editor tags
117: */
118: public String[] getTags() {
119: return editor.getTags();
120: }
121:
122: /**
123: * @return the value
124: */
125: public Object getValue() {
126: return editor.getValue();
127: }
128:
129: /**
130: * @return true if the editor is paintable
131: */
132: public boolean isPaintable() {
133: return editor.isPaintable();
134: }
135:
136: /**
137: * @param gfx
138: * @param box
139: */
140: public void paintValue(Graphics gfx, Rectangle box) {
141: editor.paintValue(gfx, box);
142: }
143:
144: /**
145: * @param listener
146: */
147: public void removePropertyChangeListener(
148: PropertyChangeListener listener) {
149: editor.removePropertyChangeListener(listener);
150: }
151:
152: /**
153: * @param text
154: * @throws java.lang.IllegalArgumentException
155: */
156: public void setAsText(String text) throws IllegalArgumentException {
157: editor.setAsText(text);
158: }
159:
160: /**
161: * @param value
162: */
163: public void setValue(Object value) {
164: editor.setValue(value);
165: }
166:
167: /**
168: * @return true if supports a custom editor
169: */
170: public boolean supportsCustomEditor() {
171: return editor.supportsCustomEditor();
172: }
173:
174: private static class SimpleFileEditor extends PropertyEditorSupport {
175: /*
176: * (non-Javadoc)
177: *
178: * @see java.beans.PropertyEditor#getAsText()
179: */
180: public String getAsText() {
181: return ((File) super .getValue()).getPath();
182: }
183:
184: /*
185: * (non-Javadoc)
186: *
187: * @see java.beans.PropertyEditor#setAsText(java.lang.String)
188: */
189: public void setAsText(String text)
190: throws IllegalArgumentException {
191: super .setValue(new File(text));
192: }
193:
194: /*
195: * Oh, I forgot: JMeter doesn't support File properties yet. Need to
196: * work on this as a String :-(
197: */
198: public Object getValue() {
199: return getAsText(); // should be super.getValue();
200: }
201:
202: /**
203: * Tsk, tsk... I need to handle Strings when setting too.
204: */
205: public void setValue(Object file) {
206: setAsText((String) file);
207: }
208: }
209: }
|