package composants;
import gestFile.FilePreview;
import gestFile.ImagePreview;
import gestFile.PreviewAll;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.filechooser.FileFilter;
import utilitaires.Extensions;
public class JSelectFile extends JPanel {
public static JSelectFile p = new JSelectFile();
private JTextField path;
private JButton parcourir;
private JFileChooser fc;
public enum FILTRE {
IMAGE, FILE_MS_OFFICE, FILE_PROGRAMMING_LANGAGE, ALL;
}
// ----------------------------------------------------------------------
private JSelectFile() {
super();
// setLayout(new FlowLayout(FlowLayout.LEFT));
parcourir = new JButton("Parcourir");
path = new JTextField(20);
path.setPreferredSize(new Dimension(20, 20));
parcourir.setPreferredSize(new Dimension(50, 20));
parcourir.setBorder(BorderFactory.createEmptyBorder());
path.setBorder(BorderFactory.createEmptyBorder());
fc = new JFileChooser();
path.setText(new File("").getAbsolutePath());
parcourir.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int returnVal = fc.showOpenDialog(JSelectFile.this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
path.setText(file.getPath());
}
}
});
JPanel panel = new JPanel();
FlowLayout fl = new FlowLayout(FlowLayout.LEFT);
fl.setHgap(0);
panel.setLayout(fl);
panel.add(path);
panel.add(parcourir);
Dimension d = new Dimension(
(int) (path.getPreferredSize().getWidth() + parcourir
.getPreferredSize().getWidth()) + 10, (int) path
.getPreferredSize().getHeight());
// panel.setPreferredSize(d);
// panel.setBorder(BorderFactory.createLineBorder(Color.gray));
add(panel);
}
public static JSelectFile getInstance() {
return p;
}
public void setPath(String path) {
this.path.setText(path);
}
public String getPath() {
return this.path.getText();
}
public void setFiltre(FileFilter filtre) {
this.fc.addChoosableFileFilter(filtre);
}
public void setFiltre(FILTRE filtre) {
switch (filtre) {
case IMAGE:
this.fc.addChoosableFileFilter(new gestFile.ImageFilter());
break;
case FILE_MS_OFFICE:
this.fc.addChoosableFileFilter(Extensions.getMicorsoftExtension());
break;
case FILE_PROGRAMMING_LANGAGE:
this.fc.addChoosableFileFilter(Extensions
.getProgrammingLangageExtension());
break;
}
}
public void setFiltreWithPreview(FILTRE filtre) {
switch (filtre) {
case IMAGE:
this.fc.addChoosableFileFilter(new gestFile.ImageFilter());
fc.setAccessory(new ImagePreview(fc));
break;
case FILE_MS_OFFICE:
this.fc.addChoosableFileFilter(Extensions.getMicorsoftExtension());
fc.setAccessory(new FilePreview(fc));
break;
case FILE_PROGRAMMING_LANGAGE:
this.fc.addChoosableFileFilter(Extensions
.getProgrammingLangageExtension());
fc.setAccessory(new FilePreview(fc));
break;
default:
fc.setAccessory(new PreviewAll(fc));
break;
}
}
}