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: */
018:
019: package org.apache.jmeter.gui.util;
020:
021: import java.awt.event.ActionEvent;
022: import java.awt.event.ActionListener;
023: import java.util.Iterator;
024: import java.util.LinkedList;
025: import java.util.List;
026:
027: import javax.swing.BorderFactory;
028: import javax.swing.Box;
029: import javax.swing.JButton;
030: import javax.swing.JFileChooser;
031: import javax.swing.JLabel;
032: import javax.swing.JTextField;
033: import javax.swing.event.ChangeEvent;
034: import javax.swing.event.ChangeListener;
035:
036: import org.apache.jmeter.util.JMeterUtils;
037:
038: /**
039: * author Michael Stover Created April 18, 2002
040: */
041: public class FilePanel extends HorizontalPanel implements
042: ActionListener {
043: JTextField filename = new JTextField(20);
044:
045: JLabel label = new JLabel(JMeterUtils
046: .getResString("file_visualizer_filename")); //$NON-NLS-1$
047:
048: JButton browse = new JButton(JMeterUtils.getResString("browse")); //$NON-NLS-1$
049:
050: private static final String ACTION_BROWSE = "browse"; //$NON-NLS-1$
051:
052: List listeners = new LinkedList();
053:
054: String title;
055:
056: String filetype;
057:
058: /**
059: * Constructor for the FilePanel object.
060: */
061: public FilePanel() {
062: title = ""; //$NON-NLS-1$
063: init();
064: }
065:
066: public FilePanel(String title) {
067: this .title = title;
068: init();
069: }
070:
071: public FilePanel(String title, String filetype) {
072: this (title);
073: this .filetype = filetype;
074: }
075:
076: /**
077: * Constructor for the FilePanel object.
078: */
079: public FilePanel(ChangeListener l, String title) {
080: this .title = title;
081: init();
082: listeners.add(l);
083: }
084:
085: public void addChangeListener(ChangeListener l) {
086: listeners.add(l);
087: }
088:
089: private void init() {
090: setBorder(BorderFactory.createTitledBorder(title));
091: add(label);
092: add(Box.createHorizontalStrut(5));
093: add(filename);
094: add(Box.createHorizontalStrut(5));
095: filename.addActionListener(this );
096: add(browse);
097: browse.setActionCommand(ACTION_BROWSE);
098: browse.addActionListener(this );
099:
100: }
101:
102: public void clearGui() {
103: filename.setText(""); // $NON-NLS-1$
104: }
105:
106: /**
107: * If the gui needs to enable/disable the FilePanel, call the method.
108: *
109: * @param enable
110: */
111: public void enableFile(boolean enable) {
112: browse.setEnabled(enable);
113: filename.setEnabled(enable);
114: }
115:
116: /**
117: * Gets the filename attribute of the FilePanel object.
118: *
119: * @return the filename value
120: */
121: public String getFilename() {
122: return filename.getText();
123: }
124:
125: /**
126: * Sets the filename attribute of the FilePanel object.
127: *
128: * @param f
129: * the new filename value
130: */
131: public void setFilename(String f) {
132: filename.setText(f);
133: }
134:
135: private void fireFileChanged() {
136: Iterator iter = listeners.iterator();
137: while (iter.hasNext()) {
138: ((ChangeListener) iter.next())
139: .stateChanged(new ChangeEvent(this ));
140: }
141: }
142:
143: public void actionPerformed(ActionEvent e) {
144: if (e.getActionCommand().equals(ACTION_BROWSE)) {
145: JFileChooser chooser;
146: if (filetype == null) {
147: chooser = FileDialoger.promptToOpenFile();
148: } else {
149: chooser = FileDialoger
150: .promptToOpenFile(new String[] { filetype });
151: }
152: if (chooser != null && chooser.getSelectedFile() != null) {
153: filename.setText(chooser.getSelectedFile().getPath());
154: fireFileChanged();
155: }
156: } else {
157: fireFileChanged();
158: }
159: }
160: }
|