1 18 19 package org.apache.jmeter.gui.util; 20 21 import java.awt.event.ActionEvent ; 22 import java.awt.event.ActionListener ; 23 import java.util.Iterator ; 24 import java.util.LinkedList ; 25 import java.util.List ; 26 27 import javax.swing.BorderFactory ; 28 import javax.swing.Box ; 29 import javax.swing.JButton ; 30 import javax.swing.JFileChooser ; 31 import javax.swing.JLabel ; 32 import javax.swing.JTextField ; 33 import javax.swing.event.ChangeEvent ; 34 import javax.swing.event.ChangeListener ; 35 36 import org.apache.jmeter.util.JMeterUtils; 37 38 43 public class FilePanel extends HorizontalPanel implements ActionListener 44 { 45 JTextField filename = new JTextField (20); 46 JLabel label = 47 new JLabel (JMeterUtils.getResString("file_visualizer_filename")); 48 JButton browse = new JButton (JMeterUtils.getResString("browse")); 49 List listeners = new LinkedList (); 50 String title; 51 String filetype; 52 53 56 public FilePanel() 57 { 58 title = ""; 59 init(); 60 } 61 62 public FilePanel(String title) 63 { 64 this.title = title; 65 init(); 66 } 67 68 public FilePanel(String title, String filetype) 69 { 70 this(title); 71 this.filetype = filetype; 72 } 73 74 77 public FilePanel(ChangeListener l, String title) 78 { 79 this.title = title; 80 init(); 81 listeners.add(l); 82 } 83 84 public void addChangeListener(ChangeListener 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 108 public String getFilename() 109 { 110 return filename.getText(); 111 } 112 113 118 public void setFilename(String f) 119 { 120 filename.setText(f); 121 } 122 123 private void fireFileChanged() 124 { 125 Iterator iter = listeners.iterator(); 126 while (iter.hasNext()) 127 { 128 ((ChangeListener ) iter.next()).stateChanged(new ChangeEvent (this)); 129 } 130 } 131 132 public void actionPerformed(ActionEvent e) 133 { 134 if (e.getActionCommand().equals("browse")) 135 { 136 JFileChooser chooser = 137 FileDialoger.promptToOpenFile(new String [] { 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 |