package ca.janeg.cb;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipException;
import java.util.zip.ZipFile;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;

/**
 *  A simple Java class browser.<p>
 *  Takes a .jar or .zip archive, extracts the class names and
 *  displays them in a JTree by package or alphabetically.<p>
 *  Selecting a class displays it's superclasses, fields,
 *  constructors and methods in an adjacent JTextPane.
 *
 *@author     Jane Griscti jane@janeg.ca
 *@created    January 26, 2002
 */
public class ClassBrowser extends JFrame {
    private JSplitPane mainPanel;
    private CBTreePanel treePanel;
    private CBTextPane textPane    = new CBTextPane();


    /**
     *  Constructs a new ClassBrowser object
     *
     *  @param  cbcg  a CBClassGroup object
     */
    public ClassBrowser( final CBClassGroup cbcg ) {
        super( "ClassBrowser" );
        setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

        treePanel = new CBTreePanel( this, cbcg );

        JScrollPane tsp  = new JScrollPane( textPane );
        tsp.setPreferredSize( new Dimension( 500, 300 ) );
        tsp.setMinimumSize( tsp.getPreferredSize() );

        mainPanel = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT,
                                    treePanel, tsp );

        getContentPane().add( mainPanel );
        createMenuBar();

        pack();
        setVisible( true );
    }


    /**  Builds the menu bar. */
    private void createMenuBar() {

        JMenu menu           = new JMenu( "View" );
        menu.setMnemonic( 'v' );

        JMenuItem pkgItem    = new JMenuItem( "by Packages" );
        JMenuItem classItem  = new JMenuItem( "by Class" );

        pkgItem.addActionListener(
            new ActionListener() {
                public void actionPerformed( ActionEvent evt ) {
                    treePanel.switchToPkgTree();
                }
            }
        );

        classItem.addActionListener(
            new ActionListener() {
                public void actionPerformed( ActionEvent evt ) {
                    treePanel.switchToClassTree();
                }
            }
        );

        pkgItem.setMnemonic( 'p' );
        classItem.setMnemonic( 'c' );

        menu.add( pkgItem );
        menu.add( classItem );

        JMenuItem exitItem = new JMenuItem( "Exit" );
        exitItem.addActionListener (
            new ActionListener() {
                public void actionPerformed( ActionEvent evt ) {
                    dispose();
                    System.exit(0);
                }
            }
        );

        exitItem.setMnemonic( 'x' );

        JMenuBar menuBar     = new JMenuBar();
        menuBar.add( menu );
        menuBar.add( exitItem );
        setJMenuBar( menuBar );
    }


    void displayClassInfo( final String className ) {
        textPane.displayClassInfo( className );
    }

    private static void exit(){
        System.exit(1);
    }


    /**
     *  The main program for the ClassBrowser class
     *
     *@param  args  The command line arguments
     */
    public static void main( String[] args ) {
        if( args.length == 0 ) {
            System.out.println( "Usage: java ClassBrowser filepath" );
            System.out.println( "  where, filepath is the full path to the archive file" );
            System.out.println( "         containing the class or source files." );
            System.out.println( "  e.g. c:/j2sdk1.4.0_01/src.zip" );
            exit();
        }

        CBClassGroup cbcg  = null;

        try {
            cbcg = new CBClassGroup( new ZipFile( new File( args[0] ) ) );
        } catch( ZipException e ) {
            System.out.println( args[0] + " is not a valid .jar or .zip file." );
            exit();
        }
        catch( IOException e ) {
            System.out.println( args[0] + " is not a valid file path." );
            exit();
        }

        ClassBrowser cb    = new ClassBrowser( cbcg );
    }
}