View Javadoc

1   /*
2    * Copyright (c) 2005 The University of Reading
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    * 1. Redistributions of source code must retain the above copyright
9    *    notice, this list of conditions and the following disclaimer.
10   * 2. Redistributions in binary form must reproduce the above copyright
11   *    notice, this list of conditions and the following disclaimer in the
12   *    documentation and/or other materials provided with the distribution.
13   * 3. Neither the name of the University of Reading, nor the names of the
14   *    authors or contributors may be used to endorse or promote products
15   *    derived from this software without specific prior written permission.
16   * 
17   * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18   * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20   * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23   * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24   * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25   * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26   * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27   */
28  
29  package uk.ac.rdg.resc.jstyx.client.browser;
30  
31  import javax.swing.*;
32  import java.awt.event.WindowAdapter;
33  import java.awt.event.WindowEvent;
34  
35  import uk.ac.rdg.resc.jstyx.client.StyxConnection;
36  import uk.ac.rdg.resc.jstyx.StyxUtils;
37  
38  /***
39   * Main class for a graphical StyxBrowser; simply puts a JTreeTable in a JFrame
40   * @todo Make into an applet?
41   *
42   * @author Jon Blower
43   * $Revision: 604 $
44   * $Date: 2006-03-21 14:58:42 +0000 (Tue, 21 Mar 2006) $
45   * $Log$
46   * Revision 1.11  2006/03/21 14:58:42  jonblower
47   * Implemented clear-text password-based authentication and did some simple tests
48   *
49   * Revision 1.10  2006/03/21 09:06:14  jonblower
50   * Still implementing authentication
51   *
52   * Revision 1.9  2005/03/18 13:55:59  jonblower
53   * Improved freeing of ByteBuffers, and bug fixes
54   *
55   * Revision 1.8  2005/03/15 15:51:41  jonblower
56   * Removed hard limit on maximum message size
57   *
58   * Revision 1.6  2005/03/11 15:06:21  jonblower
59   * Replaced deprecated frame.show() with frame.setVisible(true)
60   *
61   * Revision 1.5  2005/03/11 13:58:54  jonblower
62   * Merged MINA-Test_20059309 into main line of development
63   *
64   * Revision 1.4.2.1  2005/03/10 11:47:36  jonblower
65   * Changed to create the StyxConnection in the StyxBrowser class so it can be closed cleanly
66   *
67   * Revision 1.4  2005/02/28 16:16:26  jonblower
68   * Specified anonymous user when logging on without a user name
69   *
70   * Revision 1.3  2005/02/26 09:59:34  jonblower
71   * Now reads connection details from command line
72   *
73   * Revision 1.1  2005/02/18 17:52:40  jonblower
74   * Added client.browser package
75   *
76   */
77  
78  public class StyxBrowser
79  {
80      public static void main(String[] args) throws Throwable
81      {
82          if (args.length < 2 || args.length > 4)
83          {
84              System.err.println("Usage: java StyxBrowser <hostname> <port> [user] [password]");
85              return;
86          }
87          int port;
88          try
89          {
90              port = Integer.parseInt(args[1]);
91          }
92          catch(NumberFormatException nfe)
93          {
94              System.err.println("Invalid port number");
95              return;
96          }
97          if (port < 0 || port > StyxUtils.MAXUSHORT)
98          {
99              System.err.println("Invalid port number (must be between 0 and " +
100                 StyxUtils.MAXUSHORT + ")");
101             return;
102         }
103         String user = args.length > 2 ? args[2] : "";
104         String password = args.length > 3 ? args[3] : "";
105         
106         new StyxBrowser(args[0], port, user, password);
107     }
108     
109     public StyxBrowser(String host, int port, String user, String password)
110         throws Throwable
111     {
112         String s = user.trim().equals("") ? StyxUtils.ANONYMOUS_USER : user;
113         JFrame frame = new JFrame(s + "@" + host + ":" + port);
114         final StyxConnection conn = new StyxConnection(host, port, user, password);
115         conn.connect();
116         JTreeTable treeTable = new JTreeTable(new StyxFileSystemModel(conn));
117         
118         frame.addWindowListener(new WindowAdapter()
119         {
120             public void windowClosing(WindowEvent we)
121             {
122                 conn.close();
123                 System.exit(0);
124             }
125         });
126         
127         frame.getContentPane().add(new JScrollPane(treeTable));
128         frame.pack();
129         frame.setVisible(true);
130     }
131 }
132