KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > gui > util > FilePanel


1 // $Header: /home/cvs/jakarta-jmeter/src/core/org/apache/jmeter/gui/util/FilePanel.java,v 1.11 2004/02/13 02:21:38 sebb Exp $
2
/*
3  * Copyright 2002-2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.gui.util;
20
21 import java.awt.event.ActionEvent JavaDoc;
22 import java.awt.event.ActionListener JavaDoc;
23 import java.util.Iterator JavaDoc;
24 import java.util.LinkedList JavaDoc;
25 import java.util.List JavaDoc;
26
27 import javax.swing.BorderFactory JavaDoc;
28 import javax.swing.Box JavaDoc;
29 import javax.swing.JButton JavaDoc;
30 import javax.swing.JFileChooser JavaDoc;
31 import javax.swing.JLabel JavaDoc;
32 import javax.swing.JTextField JavaDoc;
33 import javax.swing.event.ChangeEvent JavaDoc;
34 import javax.swing.event.ChangeListener JavaDoc;
35
36 import org.apache.jmeter.util.JMeterUtils;
37
38 /**
39  * @author Michael Stover
40  * Created April 18, 2002
41  * @version $Revision: 1.11 $ Last updated: $Date: 2004/02/13 02:21:38 $
42  */

43 public class FilePanel extends HorizontalPanel implements ActionListener JavaDoc
44 {
45     JTextField JavaDoc filename = new JTextField JavaDoc(20);
46     JLabel JavaDoc label =
47         new JLabel JavaDoc(JMeterUtils.getResString("file_visualizer_filename"));
48     JButton JavaDoc browse = new JButton JavaDoc(JMeterUtils.getResString("browse"));
49     List JavaDoc listeners = new LinkedList JavaDoc();
50     String JavaDoc title;
51     String JavaDoc filetype;
52
53     /**
54      * Constructor for the FilePanel object.
55      */

56     public FilePanel()
57     {
58         title = "";
59         init();
60     }
61
62     public FilePanel(String JavaDoc title)
63     {
64         this.title = title;
65         init();
66     }
67
68     public FilePanel(String JavaDoc title, String JavaDoc filetype)
69     {
70         this(title);
71         this.filetype = filetype;
72     }
73
74     /**
75      * Constructor for the FilePanel object.
76      */

77     public FilePanel(ChangeListener JavaDoc l, String JavaDoc title)
78     {
79         this.title = title;
80         init();
81         listeners.add(l);
82     }
83
84     public void addChangeListener(ChangeListener JavaDoc l)
85     {
86         listeners.add(l);
87     }
88
89     private void init()
90     {
91         setBorder(BorderFactory.createTitledBorder(title));
92         add(label);
93         add(Box.createHorizontalStrut(5));
94         add(filename);
95         add(Box.createHorizontalStrut(5));
96         filename.addActionListener(this);
97         add(browse);
98         browse.setActionCommand("browse");
99         browse.addActionListener(this);
100
101     }
102
103     /**
104      * Gets the filename attribute of the FilePanel object.
105      *
106      * @return the filename value
107      */

108     public String JavaDoc getFilename()
109     {
110         return filename.getText();
111     }
112
113     /**
114      * Sets the filename attribute of the FilePanel object.
115      *
116      * @param f the new filename value
117      */

118     public void setFilename(String JavaDoc f)
119     {
120         filename.setText(f);
121     }
122
123     private void fireFileChanged()
124     {
125         Iterator JavaDoc iter = listeners.iterator();
126         while (iter.hasNext())
127         {
128             ((ChangeListener JavaDoc) iter.next()).stateChanged(new ChangeEvent JavaDoc(this));
129         }
130     }
131
132     public void actionPerformed(ActionEvent JavaDoc e)
133     {
134         if (e.getActionCommand().equals("browse"))
135         {
136             JFileChooser JavaDoc chooser =
137                 FileDialoger.promptToOpenFile(new String JavaDoc[] { filetype });
138             if (chooser != null && chooser.getSelectedFile() != null)
139             {
140                 filename.setText(chooser.getSelectedFile().getPath());
141                 fireFileChanged();
142             }
143         }
144         else
145         {
146             fireFileChanged();
147         }
148     }
149 }
150
Popular Tags