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.BorderLayout;
022: import java.awt.Dimension;
023: import java.awt.event.ActionEvent;
024: import java.awt.event.ActionListener;
025: import java.io.File;
026: import java.util.Iterator;
027: import java.util.LinkedList;
028: import java.util.List;
029:
030: import javax.swing.BorderFactory;
031: import javax.swing.JButton;
032: import javax.swing.JFileChooser;
033: import javax.swing.JLabel;
034: import javax.swing.JPanel;
035: import javax.swing.JScrollPane;
036: import javax.swing.JTable;
037: import javax.swing.ListSelectionModel;
038: import javax.swing.event.ChangeEvent;
039: import javax.swing.event.ChangeListener;
040:
041: import org.apache.jmeter.gui.GuiPackage;
042: import org.apache.jmeter.gui.JMeterFileFilter;
043: import org.apache.jmeter.util.JMeterUtils;
044: import org.apache.jorphan.gui.ObjectTableModel;
045: import org.apache.jorphan.reflect.Functor;
046:
047: /**
048: * @author Peter Lin
049: * @version $Revision: 587897 $ Last updated: $Date: 2007-10-24 15:30:19 +0100 (Wed, 24 Oct 2007) $
050: */
051: public class FileListPanel extends JPanel implements ActionListener {
052: private JTable files = null;
053:
054: private transient ObjectTableModel tableModel = null;
055:
056: private static final String ACTION_BROWSE = "browse"; // $NON-NLS-1$
057:
058: private JButton browse = new JButton(JMeterUtils
059: .getResString(ACTION_BROWSE));
060:
061: private JButton clear = new JButton(JMeterUtils
062: .getResString("clear")); // $NON-NLS-1$
063:
064: private JButton delete = new JButton(JMeterUtils
065: .getResString("delete")); // $NON-NLS-1$
066:
067: private List listeners = new LinkedList();
068:
069: private String title;
070:
071: private String filetype;
072:
073: /**
074: * Constructor for the FilePanel object.
075: */
076: public FileListPanel() {
077: title = ""; // $NON-NLS-1$
078: init();
079: }
080:
081: public FileListPanel(String title) {
082: this .title = title;
083: init();
084: }
085:
086: public FileListPanel(String title, String filetype) {
087: this .title = title;
088: this .filetype = filetype;
089: init();
090: }
091:
092: /**
093: * Constructor for the FilePanel object.
094: */
095: public FileListPanel(ChangeListener l, String title) {
096: this .title = title;
097: init();
098: listeners.add(l);
099: }
100:
101: public void addChangeListener(ChangeListener l) {
102: listeners.add(l);
103: }
104:
105: private void init() {
106: this .setLayout(new BorderLayout(0, 5));
107: setBorder(BorderFactory.createEmptyBorder(5, 0, 5, 5));
108: JLabel jtitle = new JLabel(title);
109:
110: HorizontalPanel buttons = new HorizontalPanel();
111: buttons.add(jtitle);
112: buttons.add(browse);
113: buttons.add(delete);
114: buttons.add(clear);
115: add(buttons, BorderLayout.NORTH);
116:
117: this .initializeTableModel();
118: files = new JTable(tableModel);
119: files
120: .setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
121: files.revalidate();
122:
123: JScrollPane scrollpane = new JScrollPane(files);
124: scrollpane.setPreferredSize(new Dimension(100, 80));
125: add(scrollpane, BorderLayout.CENTER);
126:
127: browse.setActionCommand(ACTION_BROWSE); // $NON-NLS-1$
128: browse.addActionListener(this );
129: clear.addActionListener(this );
130: delete.addActionListener(this );
131: //this.setPreferredSize(new Dimension(400,150));
132: }
133:
134: /**
135: * If the gui needs to enable/disable the FilePanel, call the method.
136: *
137: * @param enable
138: */
139: public void enableFile(boolean enable) {
140: browse.setEnabled(enable);
141: files.setEnabled(false);
142: }
143:
144: /**
145: * Add a single file to the table
146: * @param f
147: */
148: public void addFilename(String f) {
149: tableModel.addRow(f);
150: }
151:
152: /**
153: * clear the files from the table
154: */
155: public void clearFiles() {
156: tableModel.clearData();
157: }
158:
159: public void setFiles(String[] files) {
160: this .clearFiles();
161: for (int idx = 0; idx < files.length; idx++) {
162: addFilename(files[idx]);
163: }
164: }
165:
166: public String[] getFiles() {
167: String[] _files = new String[tableModel.getRowCount()];
168: for (int idx = 0; idx < _files.length; idx++) {
169: _files[idx] = (String) tableModel.getValueAt(idx, 0);
170: }
171: return _files;
172: }
173:
174: protected void deleteFile() {
175: // If a table cell is being edited, we must cancel the editing before
176: // deleting the row
177:
178: int rowSelected = files.getSelectedRow();
179: if (rowSelected >= 0) {
180: tableModel.removeRow(rowSelected);
181: tableModel.fireTableDataChanged();
182:
183: }
184: }
185:
186: private void fireFileChanged() {
187: Iterator iter = listeners.iterator();
188: while (iter.hasNext()) {
189: ((ChangeListener) iter.next())
190: .stateChanged(new ChangeEvent(this ));
191: }
192: }
193:
194: protected void initializeTableModel() {
195: tableModel = new ObjectTableModel(new String[] { "Library" },
196: new Functor[0], new Functor[0], // i.e. bypass the Functors
197: new Class[] { String.class });
198: }
199:
200: public void actionPerformed(ActionEvent e) {
201: if (e.getSource() == clear) {
202: this .clearFiles();
203: } else if (e.getActionCommand().equals(ACTION_BROWSE)) {
204: JFileChooser chooser = new JFileChooser();
205: String start = JMeterUtils.getPropDefault("user.dir", ""); // $NON-NLS-1$ // $NON-NLS-2$
206: chooser.setCurrentDirectory(new File(start));
207: chooser.setFileFilter(new JMeterFileFilter(
208: new String[] { filetype }));
209: chooser
210: .setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
211: chooser.setMultiSelectionEnabled(true);
212: chooser.showOpenDialog(GuiPackage.getInstance()
213: .getMainFrame());
214: File[] cfiles = chooser.getSelectedFiles();
215: if (chooser != null && cfiles != null) {
216: for (int idx = 0; idx < cfiles.length; idx++) {
217: this.addFilename(cfiles[idx].getPath());
218: }
219: fireFileChanged();
220: }
221: } else if (e.getSource() == delete) {
222: this.deleteFile();
223: } else {
224: fireFileChanged();
225: }
226: }
227: }
|