JResourceBrowser

v1.1
http://www.swingall.com
(c) 2004 JAPISoft


JResourceBrowser is a swing component similar to the javax.swing.JFileChooser but letting the user navigating into a set of directories bound to various protocols or containers. For an usage sample, listing an ftp directory, creating a new file and editing it inside your application. JResourceBrowser is provided by default with a set of "managers" dedicated to ZIP and JAR, FTP and WebDAV. The framework can receive new managers, actions or UI delegates for rendering files.

The user can go into a directory by double - clicking on it or by choosing a path to the root or by entering a full path. The parent directory is symbolized by the ".." value.

JResourceBrowser

JResourceBrowser uses with the following libraries :

- lib/jrb.jar : The main component
- lib/ftp/*.jar : A set of jar required for working with FTP
- lib/webdav/*.jar : A set of jar required for working with WebDAV.
  1. The main component
  2. State
  3. UI
  4. Actions
  5. Sample for ZIP
  6. Sample for FTP
  7. Sample for the File System

The main component

The main UI component is the ResourceBrowserPanel, this component works with a delegate called ResourceManager for listing or acting on a directory (remote or not).

The ResourceBrowserPanel has several selection modes :
Available ResourceManager :
For using a dedicated resource manager this is required to choose one inside the constructor of the ResourceBrowserPanel :

As sample new ResourceBrowserPanel( new FTPResourceManager() ) will work with a set of directories from an FTP server/account.

Note that you can use any ResourceManager outside the UI component, A BasicResourceItem is available for defining a new
resource path.

As sample : We write here a new file inside the /home/test directory with an FTP server.

// We create a new path
BasicResourceItem myPath = new BasicResourceItem( "/home/test/test.txt", true, null );
FTPResourceManager ftpSite = new FTPResourceManager();
// We start the FTP Connection
ftpSite.start( "http://www. ... ", "user1", "password1" );
// We update the /home/test/test.txt content
ftpSite.setContent( myPath, new byte[] { .... } );
// We close the connection
ftpSite.stop();

State

For initializing a remote server account (host, name, password) or saving the user choice, JResourceBrowser works with a state
object. This object implements the java.io.Serializable interface and so can be stored or restored using a java.io.ObjectOutputStream or
java.io.ObjectInputStream. A State is a set of key,value, for sample there's a key for the host server or another one for the account user
name... Here a sample of usage

        State state = new State();
        state.putValue( State.HOST_KEY, "MyHost" );
        state.putValue( State.USER_KEY, "MyUserName" );
        state.putValue( State.PASSWORD_KEY, "MyPassword" );

Here we choose a "MyHost" server host while initializing our ResourceBrowserPanel. The state can be provided with the setState method or
by the constructor of the ResourceBrowserPanel.

UI

The user can customize the file rendering implementing a FileResourceView. This is a delegate for the browser part providing an icon and a name bound to a file. This delegate can be used calling setFileResourceView inside the ResourceBrowserPanel.

Here a sample :

We don't want an icon for the file terminating by .class.

class FileUIDelegate implements FileResourceView {
       
        ImageIcon icon = new ImageIcon( ClassLoader.getSystemClassLoader().getResource( "images/document.png" ) );
       
        public Icon getIcon(String fileName) {
           
            // We don't want icons for java class files
            if ( fileName.endsWith( ".class" ) )
                return null;
           
            return icon;
        }

        // We maintain the same name
        public String getName(String fileName) {
            return fileName;
        }
}


Actions

JResourceBrowser contains an actions model. Thus it is possible to add/remove some actions. An action must implement the javax.swing.Action interface. The action model is available calling getActionModel from the ResourceBrowserPanel.

Note that an action is disabled when no connection has been established and is enabled when the connection is activated. Also the action model
cannot be changed once the component is visible.

Default actions :
Here a sample for adding a new action displaying a file content :

ResourceBrowserPanel panel = ...

panel.getActionModel().addAction( new DisplayAction() );


    /** Here a new action for displaying a file content */
    class DisplayAction extends AbstractAction {
        public DisplayAction() {
            putValue( Action.NAME, "Display" );
        }

        public void actionPerformed( ActionEvent e ) {

            // We get the file content
            byte[] content = panel.getSelectedContentFile( false );
            if ( content != null ) {

                try {
                    String text = new String( content, "UTF8" );
                    JDialog dialog2 = new JDialog( dialog );
                    JTextArea area = new JTextArea( text );
                    dialog2.getContentPane().add( new JScrollPane( area ) );
                    dialog2.setSize( 400, 400 );
                    dialog2.setVisible( true );
                } catch( UnsupportedEncodingException exc ) {}

            }

        }
    }

Sample for ZIP

Here a sample for browsing inside a ZIP or JAR file. Note that the user must select a file
when starting.

        JFileChooser chooser = new JFileChooser();
        chooser.setFileFilter(
                new FileFilter() {
                    public boolean accept(File f) {
                        return f.isDirectory() ||
                            f.getName().endsWith( ".zip" ) ||
                                f.getName().endsWith( ".jar" );
                    }
                   
                    public String getDescription() {
                        return "ZIP (*.zip), JAR (*.jar)";
                    }
                   
                } );

        chooser.setDialogTitle( "Choose a ZIP or a JAR file" );

        if ( chooser.showOpenDialog( null ) == JFileChooser.APPROVE_OPTION ) {
            File f = chooser.getSelectedFile();
            JDialog dialog = new JDialog();
            dialog.setModal( true );
            dialog.setTitle( "Browse " + f );

            ZIPBrowser browser;
           
            dialog.getContentPane().add( browser = new ZIPBrowser( f ) );
            dialog.setSize( 500, 550 );
           
            // We create a custom view for files
            browser.setFileResourceView( new FileUIDelegate() );
           
            dialog.setVisible( true );

            // It will close the browsed file as it is in a modal state
            dialog.dispose();
        }

    }

    /** We create a delegate for rendering each file. Note that this is not
     * required and this is only for demonstration */
    static class FileUIDelegate implements FileResourceView {
       
        ImageIcon icon = new ImageIcon( ClassLoader.getSystemClassLoader().getResource( "images/document.png" ) );
       
        public Icon getIcon(String fileName) {
           
            // We don't want icons for java class files
            if ( fileName.endsWith( ".class" ) )
                return null;
           
            return icon;
        }

        // We maintain the same name
        public String getName(String fileName) {
            return fileName;
        }

    }

Sample for FTP

Here a sample for browsing an FTP server and displaying a selected file content.

public class FTPBrowserDemo {

    FTPBrowser browser;
    JDialog dialog;
   
    public FTPBrowserDemo() {
        dialog = new JDialog();
        dialog.setModal( true );
        dialog.setTitle( "FTP Editor " );

        // We initialize a default FTP server and user account
        State state = new State();
        state.putValue( State.HOST_KEY, "MyHost" );
        state.putValue( State.USER_KEY, "MyUserName" );
        state.putValue( State.PASSWORD_KEY, "MyPassword" );
       
        dialog.getContentPane().add( browser = new FTPBrowser( state ) );

        // We add a new action inside the FTPBrowser for displaying a file content
        browser.getActionModel().addAction( new DisplayAction() );
       
        dialog.setSize( 500, 550 );
        dialog.setVisible( true );

        // It will close the browsed file as it is in a modal state
        dialog.dispose();       
    }

    /** Here a new action for displaying a file content */
    class DisplayAction extends AbstractAction {
        public DisplayAction() {
            putValue( Action.NAME, "Display" );
        }

        public void actionPerformed( ActionEvent e ) {

            byte[] content = browser.getSelectedContentFile( false );
            if ( content != null ) {

                try {
                    String text = new String( content, "UTF8" );
                    JDialog dialog2 = new JDialog( dialog );
                    JTextArea area = new JTextArea( text );
                    dialog2.getContentPane().add( new JScrollPane( area ) );
                    dialog2.setSize( 400, 400 );
                    dialog2.setVisible( true );
                } catch( UnsupportedEncodingException exc ) {}

            }

        }
    }


Sample for the Local File System

In this sample, we  browse a local file system and we can display a file content

   public class FileBrowserDemo {

    FileBrowser browser;
    JDialog dialog;
   
    public FileBrowserDemo() {
        dialog = new JDialog();
        dialog.setModal( true );
        dialog.setTitle( "File Editor" );

        dialog.getContentPane().add( browser = new FileBrowser() );
        browser.getActionModel().addAction( new DisplayAction() );

        dialog.setSize( 500, 550 );
        dialog.setVisible( true );

        // It will close the browsed file as it is in a modal state
        dialog.dispose();       
    }

    /** Here a new action for displaying a file content */
    class DisplayAction extends AbstractAction {
        public DisplayAction() {
            putValue( Action.NAME, "Display" );
        }

        public void actionPerformed( ActionEvent e ) {
            byte[] content = browser.getSelectedContentFile( false );
            if ( content != null ) {
                try {
                    String text = new String( content, "UTF8" );
                    JDialog dialog2 = new JDialog( dialog );
                    JTextArea area = new JTextArea( text );
                    dialog2.getContentPane().add( new JScrollPane( area ) );
                    dialog2.setSize( 400, 400 );
                    dialog2.setVisible( true );
                } catch( UnsupportedEncodingException exc ) {}
            }
        }
    }
  
}