package versusSNP.util.action;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFileChooser;
import javax.swing.JTextField;
import versusSNP.Parameter;
import versusSNP.gui.UICaption;
import versusSNP.gui.widgets.MyFileChooser;
import versusSNP.gui.widgets.MyFileFilter;
public class BrowseAction implements ActionListener {
private JTextField txtField;
private MyFileFilter[] filters;
private boolean isDirectory;
private int operationType = JFileChooser.OPEN_DIALOG;
public BrowseAction(JTextField txtField) {
this.txtField = txtField;
this.filters = new MyFileFilter[]{};
}
public BrowseAction(JTextField txtField, int operationType) {
this(txtField);
this.operationType = operationType;
}
public BrowseAction(JTextField txtField, boolean isDirectory) {
this(txtField);
this.isDirectory = isDirectory;
}
public BrowseAction(JTextField txtField, MyFileFilter[] filters) {
this.txtField = txtField;
this.filters = filters;
}
public BrowseAction(JTextField txtField, MyFileFilter[] filters, int operationType) {
this(txtField, filters);
this.operationType = operationType;
}
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals(UICaption.dialog_caption_browse)) {
MyFileChooser chooser = new MyFileChooser(Parameter.path_previous_dir);
if (filters.length != 0) {
for (MyFileFilter filter : filters)
chooser.addChoosableFileFilter(filter);
chooser.setFileFilter(filters[0]);
}
if (isDirectory) chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
if (operationType == JFileChooser.SAVE_DIALOG) {
if (chooser.showSaveDialog(null)==JFileChooser.APPROVE_OPTION) {
if (txtField != null)
txtField.setText(chooser.getSelectedFile().getAbsolutePath());
}
} else {
if (chooser.showOpenDialog(null)==JFileChooser.APPROVE_OPTION) {
if (txtField != null)
txtField.setText(chooser.getSelectedFile().getAbsolutePath());
}
}
}
}
public void setTxtField(JTextField txtField) {
this.txtField = txtField;
}
}