XMLShare.java



package xmlshare;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import org.xml.sax.InputSource;

/** XML File Sharing Tool ver.0.01 */
public class XMLShare extends JFrame {

  /** port number */
    public static final int PORT = 8080;       

  /** manages public files */
    public ContentManager manager;

  /** file server */
    public SimpleServer server;

  /** file down loader */
    public DownLoader loader;       

  /** parse list.xml */
    public ListParser parser;       

  /** input host name  */
    public HostNameField field;

  /** select file  */
    public FileSelecter selecter;

  /** constructor */
    public XMLShare() {

        manager = new ContentManager();
        server = new SimpleServer();
        loader = new DownLoader();       
        parser = new ListParser();
        field = new HostNameField( new HostNameAdapter() );
        getContentPane().setLayout( new BorderLayout() );
        getContentPane().add( field, "North" );
        pack();
        show();
        server.loop();
    }

  /** inner class for process host name */
    class HostNameAdapter implements ActionListener {

         public void actionPerformed( ActionEvent evt ) {

             parser.parse(
                new InputSource( "http://" + field.getHostName()
                               + ":" + PORT + "/list.xml" ) );
             String[] names = parser.getListedName();
             if( selecter != null ) {
                 getContentPane().remove( selecter );
                 selecter = null;
             }
             selecter = new FileSelecter( names,
                                   new DownLoadAdapter() );
             getContentPane().add( selecter, "Center" );
             pack();
             show();
         }
    }

  /** inner class for process file down load */
    class DownLoadAdapter implements ActionListener {

         public void actionPerformed( ActionEvent evt ) {

             String host = field.getHostName();
             String file = selecter.getSelectedFileName();
             loader.download( host, file );
         }
    }

  /** main (start here) */
    public static void main( String[] argv ) {
        try {
            XMLShare test = new XMLShare();
            System.out.println( "XML Sharing Tool started..." );
        }
        catch( Exception e ) {
            System.err.println( e.toString() );
        }
    }
}