1
16package org.tp23.gui.widget;
17
18import java.awt.event.ActionEvent;
19import java.io.File;
20
21import javax.swing.AbstractAction;
22import javax.swing.ImageIcon;
23import javax.swing.JComponent;
24import javax.swing.JFileChooser;
25
26public class SelectFileAction extends AbstractAction {
27 protected JComponent component = null;
28 protected File selectedFile = null;
29 protected String buttonText = null;
30
31 public SelectFileAction(String text, ImageIcon icon, JComponent component) {
32 super(text, icon);
33 this.component = component;
34 }
35 public SelectFileAction(String text, ImageIcon icon, JComponent component,String buttonText) {
36 super(text, icon);
37 this.component = component;
38 }
39 public void actionPerformed(ActionEvent e) {
40 JFileChooser chooser = new JFileChooser();
41 if(buttonText!=null)chooser.setApproveButtonText(buttonText);
42 if(selectedFile!=null && selectedFile.isFile()){
43 if(selectedFile.exists())chooser.setSelectedFile(selectedFile);
44 else chooser.setCurrentDirectory(selectedFile.getParentFile());
45 }
46 else if(selectedFile!=null && selectedFile.isDirectory()){
47 chooser.setCurrentDirectory(selectedFile);
48 }
49 else if(selectedFile!=null && !selectedFile.exists()){
50 chooser.setCurrentDirectory(selectedFile.getParentFile());
51 }
52
53 int returnVal = chooser.showDialog(component,e.getActionCommand());
54 if(returnVal == JFileChooser.APPROVE_OPTION) {
55 selectedFile = chooser.getSelectedFile();
56 } else {
57 selectedFile = null;
58 }
59 }
60}
61