JTreeFileBrowser

v1.1


JTreeFileBrowser is a java swing component for browsing a file system under a tree view. This is similar to a windows file explorer view.

This component has :
The action model (ActionModel) can be updated and the toolbar content is updated dynamically.

The main tree is a set of files and directories. The appearance can be updated with a delegate (FileView). This delegate can change the background, foreground color and the icon of a file or a directory. A drag'n drop for moving a file can be disabled calling setEnabledFileMoving, this is similar when renaming a file or a directory name by double-clicking calling setEnabledFileRenaming. The user can listen for a selection calling addFileSelectionListener.

By default the component works with one selection at a time, the setMultipleSelection method can be called for multiple selections usage.

A file filter must be added before the visibility of the component inside a FileFilterModel. This filter will update the way each file is displayed.

A main action is a java.swing.Action located at the bottom of the component. This action is provided once inside the JTreeFileBrowser constructor.

Here a complete sample :

import java.awt.Color;
import java.io.File;

import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;

import com.japisoft.treefilebrowser.FileFilter;
import com.japisoft.treefilebrowser.FileSelectionListener;
import com.japisoft.treefilebrowser.FileView;
import com.japisoft.treefilebrowser.JTreeFileBrowser;

/**
 * Here a sample usage of the JTreeFileBrowser. In this sample we color with a red background
 * and a custom icon each file terminating by .txt.
 * @author (c) 2004 JAPISoft / http://www.japisoft.com
 * @version 1.0
 */
public class Demo1 implements FileSelectionListener, FileView, FileFilter {

    static JTreeFileBrowser fb;

    // A File had been selected inside the JTreeFileBrowser
    // This is for the FileSelectionListener interface
    public void fileSelected(File[] file) {
    }

    // Override the default background for the files terminating by txt
    // This is for the FileView interface
    public Color getBackground(File file) {
    if (file.getName().endsWith("txt"))
        return Color.red;
    return null;
    }

    // Override the default foreground for the files terminating by txt
    // This is for the FileView interface
    public Color getForeground(File file) {
    if (file.getName().endsWith("txt"))
        return Color.white;
    return null;
    }

    static Icon icon = new ImageIcon(Demo1.class.getResource("delete2.png"));

    // Override the default icon for the files terminating by txt
    // This is for the FileView interface
    public Icon getIcon(File file) {
    if (file.getName().endsWith("txt"))
        return icon;
    return null;
    }

    // Here the file filter for the file terminating by .txt
    // This is for the FileFilter interface
    public boolean accept(File file) {
    return file.getName().endsWith( "txt" );
    }

    // Here the file filter description for the files terminating by .txt
    // This is for the FileFilter interface
    public String getDescription() {
    return "Filter for text file (*.txt)";
    }

    // Here the file filter icon for the files terminating by .txt
    // This is for the FileFilter interface
    public Icon getIcon() {
    return null;
    }

    /**  Here a custom file filter for the files terminating by .xml
     */
    static class Filter2 implements FileFilter {
    public boolean accept(File file) {
        return file.getName().endsWith( "xml" );
    }
    public String getDescription() {
        return "(*.xml)";
    }
    public Icon getIcon() {
        return icon;
    }
    }

    public static void main(String[] args) {
    JFrame fr = new JFrame();
    fb = new JTreeFileBrowser();

    File currentDir = new File( System.getProperty( "user.dir" ) );
    // Here a sample for updateing the default selection
    fb.setSelectedFile( currentDir );

    fr.getContentPane().add(fb);
    fr.setSize(300, 300);
    Demo1 d1 = new Demo1();

    // Add a listener each time a file is selected
    fb.addFileSelectionListener( d1 );

    // Create a custom view for the file
    fb.setFileView( d1 );

    // Add two file filters

    fb.getFileFilterModel().addFileFilter( d1 );
    fb.getFileFilterModel().addFileFilter( new Filter2() );
    fr.setVisible(true);
    }

}