import java.io.File;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter.*;

class Browser extends application{
    
    public JFileChooser FChooser;
    
    public Browser(boolean DirectoriesOnly) {
        FChooser = new JFileChooser();
        
        if (DirectoriesOnly){
            FChooser.addActionListener(new DirectoryChooserHandler());
            FChooser.setFileSelectionMode(FChooser.DIRECTORIES_ONLY);
        }
        else{
            FChooser.addActionListener(new FileChooserHandler());
            ImageFilter filter = new ImageFilter();
            FChooser.setFileFilter(filter);
            FChooser.setAcceptAllFileFilterUsed(false);
        }
        addComponent(FChooser, 0, 0, 1, 1);
        
        setSize(500, 350);
        setVisible(true);
    }
    
    private class ImageFilter extends javax.swing.filechooser.FileFilter {
        public boolean accept(File file) {
            String filename = file.getName();
            if (file.isDirectory())
                return true;
            else
                return (filename.endsWith(".jpg") || filename.endsWith(".gif") || filename.endsWith(".png"));
        }
        
        public String getDescription() {
            return "Compatible images";
        }
    }
    
    private class FileChooserHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
             if (e.getActionCommand() == "ApproveSelection")
                 admin.QEditor.FileChooserHandler(FChooser.getSelectedFile().toString());
            admin.QEditor.FileChooser.dispose();
        }
    }
        
    private class DirectoryChooserHandler implements ActionListener{
        public void actionPerformed(ActionEvent e){
             if (e.getActionCommand() == "ApproveSelection")
                 admin.rt.DirectoryChooserHandler(FChooser.getSelectedFile().toString());
             admin.rt.DirectoryChooser.dispose();
        }
    }    
}