001: // $Header $
002: /*
003: * Licensed to the Apache Software Foundation (ASF) under one or more
004: * contributor license agreements. See the NOTICE file distributed with
005: * this work for additional information regarding copyright ownership.
006: * The ASF licenses this file to You under the Apache License, Version 2.0
007: * (the "License"); you may not use this file except in compliance with
008: * the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: *
018: */
019:
020: package org.apache.jmeter.gui.util;
021:
022: import java.awt.Color;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.util.Iterator;
026: import java.util.LinkedList;
027: import java.util.List;
028:
029: import javax.swing.BorderFactory;
030: import javax.swing.Box;
031: import javax.swing.JButton;
032: import javax.swing.JFileChooser;
033: import javax.swing.JTextField;
034: import javax.swing.event.ChangeEvent;
035: import javax.swing.event.ChangeListener;
036:
037: import org.apache.jmeter.util.JMeterUtils;
038:
039: /**
040: * @author Michael Stover Created April 18, 2002
041: * @version $Revision: 493793 $ Last updated: $Date: 2007-01-07 18:19:27 +0000 (Sun, 07 Jan 2007) $
042: */
043: public class DirectoryPanel extends HorizontalPanel implements
044: ActionListener {
045: protected JTextField filename = new JTextField(20);
046:
047: protected JButton browse = new JButton(JMeterUtils
048: .getResString("browse"));
049:
050: List listeners = new LinkedList();
051:
052: String title;
053:
054: String filetype;
055:
056: Color background;
057:
058: /**
059: * Constructor for the FilePanel object.
060: */
061: public DirectoryPanel() {
062: title = "";
063: init();
064: }
065:
066: public DirectoryPanel(String title) {
067: this .title = title;
068: init();
069: }
070:
071: public DirectoryPanel(String title, String filetype, Color bk) {
072: this (title, filetype);
073: this .background = bk;
074: init();
075: }
076:
077: public DirectoryPanel(String title, String filetype) {
078: this (title);
079: this .filetype = filetype;
080: }
081:
082: /**
083: * Constructor for the FilePanel object.
084: */
085: public DirectoryPanel(ChangeListener l, String title) {
086: this .title = title;
087: init();
088: listeners.add(l);
089: }
090:
091: public void addChangeListener(ChangeListener l) {
092: listeners.add(l);
093: }
094:
095: private void init() {
096: setBackground(this .background);
097: setBorder(BorderFactory.createTitledBorder(title));
098: add(Box.createHorizontalStrut(5));
099: add(filename);
100: add(Box.createHorizontalStrut(5));
101: filename.addActionListener(this );
102: add(browse);
103: browse.setActionCommand("browse");
104: browse.addActionListener(this );
105: }
106:
107: /**
108: * If the gui needs to enable/disable the FilePanel, call the method.
109: *
110: * @param enable
111: */
112: public void enableFile(boolean enable) {
113: browse.setEnabled(enable);
114: filename.setEnabled(enable);
115: }
116:
117: /**
118: * Gets the filename attribute of the FilePanel object.
119: *
120: * @return the filename value
121: */
122: public String getFilename() {
123: return filename.getText();
124: }
125:
126: /**
127: * Sets the filename attribute of the FilePanel object.
128: *
129: * @param f
130: * the new filename value
131: */
132: public void setFilename(String f) {
133: filename.setText(f);
134: }
135:
136: private void fireFileChanged() {
137: Iterator iter = listeners.iterator();
138: while (iter.hasNext()) {
139: ((ChangeListener) iter.next())
140: .stateChanged(new ChangeEvent(this ));
141: }
142: }
143:
144: public void actionPerformed(ActionEvent e) {
145: if (e.getActionCommand().equals("browse")) {
146: JFileChooser chooser = DirectoryDialoger.promptToOpenFile();
147: if (chooser.getSelectedFile() != null) {
148: filename.setText(chooser.getSelectedFile().getPath());
149: fireFileChanged();
150: }
151: } else {
152: fireFileChanged();
153: }
154: }
155: }
|