Back to index

wims  3.65+svn20090927
PlotApplication.java
Go to the documentation of this file.
00001 /* A standalone plot application.
00002 
00003  Copyright (c) 1998-2008 The Regents of the University of California.
00004  All rights reserved.
00005  Permission is hereby granted, without written agreement and without
00006  license or royalty fees, to use, copy, modify, and distribute this
00007  software and its documentation for any purpose, provided that the above
00008  copyright notice and the following two paragraphs appear in all copies
00009  of this software.
00010 
00011  IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
00012  FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
00013  ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
00014  THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
00015  SUCH DAMAGE.
00016 
00017  THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
00018  INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
00020  PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
00021  CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
00022  ENHANCEMENTS, OR MODIFICATIONS.
00023 
00024  PT_COPYRIGHT_VERSION_2
00025  COPYRIGHTENDKEY
00026  */
00027 package ptolemy.plot;
00028 
00029 import java.awt.event.WindowAdapter;
00030 import java.awt.event.WindowEvent;
00031 import java.io.File;
00032 import java.io.FileInputStream;
00033 import java.io.FileNotFoundException;
00034 import java.io.IOException;
00035 import java.io.InputStream;
00036 import java.net.MalformedURLException;
00037 import java.net.URL;
00038 
00039 import javax.swing.JOptionPane;
00040 import javax.swing.SwingUtilities;
00041 
00042 import ptolemy.util.StringUtilities;
00043 
00046 
00112 public class PlotApplication extends PlotFrame {
00117     public PlotApplication() throws Exception {
00118         this(new String[0]);
00119     }
00120 
00125     public PlotApplication(String[] args) throws Exception {
00126         this(new Plot(), args);
00127     }
00128 
00136     public PlotApplication(PlotBox plot, String[] args) throws Exception {
00137         // invoke the base class constructor and pass in the argument a Plot
00138         // object. This makes sure that the plot field is an instance of
00139         // Plot class.
00140         super("PlotApplication", plot);
00141 
00142         // Handle window closing by exiting the application.
00143         addWindowListener(new WindowAdapter() {
00144             public void windowClosing(WindowEvent e) {
00145                 // Strangely, calling _close() here sends javac into
00146                 // an infinite loop (in jdk 1.1.4).
00147                 //              _close();
00148                 System.exit(0);
00149             }
00150         });
00151 
00152         _parseArgs(args);
00153 
00154         if ((args == null) || (args.length == 0)) {
00155             samplePlot();
00156         }
00157 
00158         setVisible(true);
00159     }
00160 
00166     public PlotApplication(PlotBox plot) {
00167         // Invoke the base class constructor and pass in the argument a Plot
00168         // object. This makes sure that the plot field is an instance of
00169         // Plot class.
00170         super("PlotApplication", plot);
00171 
00172         // Handle window closing by exiting the application.
00173         addWindowListener(new WindowAdapter() {
00174             public void windowClosing(WindowEvent e) {
00175                 // Strangely, calling _close() here sends javac into
00176                 // an infinite loop (in jdk 1.1.4).
00177                 //              _close();
00178                 System.exit(0);
00179             }
00180         });
00181 
00182         setVisible(true);
00183     }
00184 
00187 
00190     public static void main(final String[] args) {
00191         try {
00192             // Run this in the Swing Event Thread.
00193             Runnable doActions = new Runnable() {
00194                 public void run() {
00195                     try {
00196                         new PlotApplication(new Plot(), args);
00197                     } catch (Exception ex) {
00198                         System.err.println(ex.toString());
00199                         ex.printStackTrace();
00200                     }
00201                 }
00202             };
00203 
00204             SwingUtilities.invokeAndWait(doActions);
00205         } catch (Exception ex) {
00206             System.err.println(ex.toString());
00207             ex.printStackTrace();
00208         }
00209 
00210         // If the -test arg was set, then exit after 2 seconds.
00211         if (_test) {
00212             try {
00213                 Thread.sleep(2000);
00214             } catch (InterruptedException e) {
00215             }
00216 
00217             System.exit(0);
00218         }
00219     }
00220 
00223     protected void _about() {
00224         JOptionPane
00225                 .showMessageDialog(
00226                         this,
00227                         "PlotApplication class\n"
00228                                 + "By: Edward A. Lee "
00229                                 + "and Christopher Brooks\n"
00230                                 + "Version "
00231                                 + PlotBox.PTPLOT_RELEASE
00232                                 + ", Build: $Id: PlotApplication.java,v 1.69.4.1 2008/04/04 20:01:01 cxh Exp $\n\n"
00233                                 + "For more information, see\n"
00234                                 + "http://ptolemy.eecs.berkeley.edu/java/ptplot\n\n"
00235                                 + "Copyright (c) 1997-2008, "
00236                                 + "The Regents of the University of California.",
00237                         "About Ptolemy Plot", JOptionPane.INFORMATION_MESSAGE);
00238     }
00239 
00242     protected void _close() {
00243         StringUtilities.exit(0);
00244     }
00245 
00248     protected void _help() {
00249         JOptionPane.showMessageDialog(this,
00250                 "PlotApplication is a standalone plot " + " application.\n"
00251                         + "  File formats understood: Ptplot ASCII.\n"
00252                         + "  Left mouse button: Zooming.\n\n" + _usage(),
00253                 "About Ptolemy Plot", JOptionPane.INFORMATION_MESSAGE);
00254     }
00255 
00264     protected int _parseArgs(String[] args) throws CmdLineArgException,
00265             FileNotFoundException, IOException {
00266         int i = 0;
00267         int argumentsRead;
00268         String arg;
00269         String title = "Ptolemy plot";
00270 
00271         int width = 500; // Default width of the graph
00272         int height = 300; // Default height of the graph
00273 
00274         while ((args != null) && (i < args.length)) {
00275             arg = args[i++];
00276 
00277             if (arg.equals("-height")) {
00278                 if (i > (args.length - 1)) {
00279                     throw new CmdLineArgException(_usage());
00280                 }
00281 
00282                 height = Integer.valueOf(args[i++]).intValue();
00283                 continue;
00284             } else if (arg.equals("-help")) {
00285                 System.out.println(_usage());
00286                 StringUtilities.exit(0);
00287                 continue;
00288             } else if (arg.equals("-test")) {
00289                 _test = true;
00290                 continue;
00291             } else if (arg.equals("-version")) {
00292                 System.out
00293                         .println("Version "
00294                                 + PlotBox.PTPLOT_RELEASE
00295                                 + ", Build $Id: PlotApplication.java,v 1.69.4.1 2008/04/04 20:01:01 cxh Exp $");
00296                 StringUtilities.exit(0);
00297                 continue;
00298             } else if (arg.equals("-width")) {
00299                 if (i > (args.length - 1)) {
00300                     throw new CmdLineArgException(
00301                             "-width requires an integer argument");
00302                 }
00303 
00304                 width = Integer.valueOf(args[i++]).intValue();
00305                 continue;
00306             } else if (arg.equals("")) {
00307                 // Ignore blank argument.
00308             } else if (arg.equals("-")) {
00309                 // read from standard input
00310                 URL base = new URL("file", null, "standard input");
00311                 _read(base, System.in);
00312             } else if (!arg.startsWith("-")) {
00313                 // Have a filename.  First attempt to open it as a URL.
00314                 InputStream instream;
00315                 URL base;
00316 
00317                 try {
00318                     // First argument is null because we are only
00319                     // processing absolute URLs this way.  Relative
00320                     // URLs are opened as ordinary files.
00321                     URL inurl = new URL(null, arg);
00322                     base = inurl;
00323                     instream = inurl.openStream();
00324                 } catch (MalformedURLException ex) {
00325                     File file = new File(arg);
00326                     instream = new FileInputStream(file);
00327                     _file = new File(file.getAbsolutePath());
00328                     title = _file.getName();
00329                     _directory = _file.getParentFile();
00330                     base = new URL("file", null, _directory.getAbsolutePath());
00331                 }
00332 
00333                 _read(base, instream);
00334             } else {
00335                 // Unrecognized option.
00336                 throw new CmdLineArgException("Unrecognized option: " + arg);
00337             }
00338         }
00339 
00340         setSize(width, height);
00341         setTitle(title);
00342 
00343         argumentsRead = i;
00344 
00345         return argumentsRead;
00346     }
00347 
00351     protected String _usage() {
00352         // We use a table here to keep things neat.
00353         // If we have:
00354         //  {"-bd",  "<color>", "Border",  "White", "(Unsupported)"},
00355         // -bd       - The argument
00356         // <color>   - The description of the value of the argument
00357         // Border    - The Xgraph file directive (not supported at this time).
00358         // White     - The default (not supported at this time)
00359         // "(Unsupported)" - The string that is printed to indicate if
00360         //                   a option is unsupported.
00361         String[][] commandOptions = { { "-height", "<pixels>" },
00362                 { "-width", "<pixels>" }, };
00363 
00364         String[] commandFlags = { "-help", "-test", "-version", "-", };
00365         StringBuffer result = new StringBuffer(
00366                 "Usage: ptplot [ options ] [file ...]\n\n"
00367                         + "Options that take values:\n");
00368 
00369         int i;
00370 
00371         for (i = 0; i < commandOptions.length; i++) {
00372             result.append(" " + commandOptions[i][0] + " "
00373                     + commandOptions[i][1] + "\n");
00374         }
00375 
00376         result.append("\nBoolean flags:\n");
00377 
00378         for (i = 0; i < commandFlags.length; i++) {
00379             result.append(" " + commandFlags[i]);
00380         }
00381 
00382         return result.toString();
00383     }
00384 
00387 
00389     protected static boolean _test = false;
00390 }