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 Peter Lin
040: * @version $Revision: 493793 $ Last updated: $Date: 2007-01-07 18:19:27 +0000 (Sun, 07 Jan 2007) $
041: */
042: public class ReportFilePanel extends HorizontalPanel implements
043: ActionListener {
044: JTextField filename = new JTextField(20);
045:
046: JLabel label = new JLabel(JMeterUtils
047: .getResString("file_visualizer_filename"));
048:
049: JButton browse = new JButton(JMeterUtils.getResString("browse"));
050:
051: List listeners = new LinkedList();
052:
053: String title;
054:
055: String filetype;
056:
057: /**
058: * Constructor for the FilePanel object.
059: */
060: public ReportFilePanel() {
061: title = "";
062: init();
063: }
064:
065: public ReportFilePanel(String title) {
066: this .title = title;
067: init();
068: }
069:
070: public ReportFilePanel(String title, String filetype) {
071: this (title);
072: this .filetype = filetype;
073: }
074:
075: /**
076: * Constructor for the FilePanel object.
077: */
078: public ReportFilePanel(ChangeListener l, String title) {
079: this .title = title;
080: init();
081: listeners.add(l);
082: }
083:
084: public void addChangeListener(ChangeListener l) {
085: listeners.add(l);
086: }
087:
088: private void init() {
089: setBorder(BorderFactory.createTitledBorder(title));
090: add(label);
091: add(Box.createHorizontalStrut(5));
092: add(filename);
093: add(Box.createHorizontalStrut(5));
094: filename.addActionListener(this );
095: add(browse);
096: browse.setActionCommand("browse");
097: browse.addActionListener(this );
098:
099: }
100:
101: /**
102: * If the gui needs to enable/disable the FilePanel, call the method.
103: *
104: * @param enable
105: */
106: public void enableFile(boolean enable) {
107: browse.setEnabled(enable);
108: filename.setEnabled(enable);
109: }
110:
111: /**
112: * Gets the filename attribute of the FilePanel object.
113: *
114: * @return the filename value
115: */
116: public String getFilename() {
117: return filename.getText();
118: }
119:
120: /**
121: * Sets the filename attribute of the FilePanel object.
122: *
123: * @param f
124: * the new filename value
125: */
126: public void setFilename(String f) {
127: filename.setText(f);
128: }
129:
130: private void fireFileChanged() {
131: Iterator iter = listeners.iterator();
132: while (iter.hasNext()) {
133: ((ChangeListener) iter.next())
134: .stateChanged(new ChangeEvent(this ));
135: }
136: }
137:
138: public void actionPerformed(ActionEvent e) {
139: if (e.getActionCommand().equals("browse")) {
140: JFileChooser chooser = ReportFileDialoger
141: .promptToOpenFile(new String[] { filetype });
142: if (chooser != null && chooser.getSelectedFile() != null) {
143: filename.setText(chooser.getSelectedFile().getPath());
144: fireFileChanged();
145: }
146: } else {
147: fireFileChanged();
148: }
149: }
150: }
|