001/* An application providing run control panels for given models.
002
003 Copyright (c) 1999-2014 The Regents of the University of California.
004 All rights reserved.
005 Permission is hereby granted, without written agreement and without
006 license or royalty fees, to use, copy, modify, and distribute this
007 software and its documentation for any purpose, provided that the above
008 copyright notice and the following two paragraphs appear in all copies
009 of this software.
010
011 IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
012 FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
013 ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
014 THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
015 SUCH DAMAGE.
016
017 THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
018 INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
019 MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
020 PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
021 CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
022 ENHANCEMENTS, OR MODIFICATIONS.
023
024 PT_COPYRIGHT_VERSION_2
025 COPYRIGHTENDKEY
026
027 */
028package ptolemy.actor.gui;
029
030import java.io.File;
031import java.net.URL;
032
033import ptolemy.util.MessageHandler;
034import ptolemy.util.StringUtilities;
035
036///////////////////////////////////////////////////////////////////
037//// PtolemyApplication
038
039/**
040 This application opens run control panels for models specified on the
041 command line.  The exact facilities that are available are determined
042 by the configuration file ptolemy/configs/runPanelConfiguration.xml,
043 which is loaded before any command-line arguments are processed.
044 If there are no command-line arguments at all, then the file
045 ptolemy/configs/runBlankConfiguration.xml is read instead.
046
047 @author Edward A. Lee and Steve Neuendorffer
048 @version $Id$
049 @since Ptolemy II 0.4
050 @Pt.ProposedRating Yellow (eal)
051 @Pt.AcceptedRating Red (eal)
052 @see ModelFrame
053 @see RunTableau
054 */
055public class PtolemyApplication extends MoMLApplication {
056    /** Parse the specified command-line arguments, creating models
057     *  and frames to interact with them.
058     *  @param args The command-line arguments.
059     *  See {@link ptolemy.actor.gui.ConfigurationApplication} for a
060     *  description of the command-line arguments.
061     *  @exception Exception If command line arguments have problems.
062     */
063    public PtolemyApplication(String[] args) throws Exception {
064        super(args);
065    }
066
067    /** Parse the specified command-line arguments, creating models
068     *  and frames to interact with them.
069     *  @param arg The command-line argument, typically the name of
070     *  the model to open.
071     *  See {@link ptolemy.actor.gui.ConfigurationApplication} for a
072     *  description of the command-line arguments.
073     *  @exception Exception If command line arguments have problems.
074     */
075    public PtolemyApplication(String arg) throws Exception {
076        // This method is used by the JUnit test framework, see
077        // ptolemy/util/test/junit/AutoGUITests.java
078        super(new String[] { arg });
079    }
080
081    ///////////////////////////////////////////////////////////////////
082    ////                         public methods                    ////
083
084    /** Create a new instance of this application, passing it the
085     *  command-line arguments.
086     *  @param args The command-line arguments.
087     */
088    public static void main(String[] args) {
089        try {
090            new PtolemyApplication(args);
091        } catch (Exception ex) {
092            MessageHandler.error("Command failed", ex);
093            StringUtilities.exit(1);
094        }
095
096        // If the -test arg was set, then exit after 2 seconds.
097        if (_test) {
098            try {
099                Thread.sleep(2000);
100            } catch (InterruptedException e) {
101            }
102
103            StringUtilities.exit(0);
104        }
105    }
106
107    ///////////////////////////////////////////////////////////////////
108    ////                         protected methods                 ////
109
110    /** Return a default Configuration, which in this case is given by
111     *  the MoML file ptolemy/configs/runPanelConfiguration.xml.
112     *  That configuration supports executing, but not editing,
113     *  Ptolemy models.
114     *  @return A default configuration.
115     *  @exception Exception If the configuration cannot be opened.
116     */
117    @Override
118    protected Configuration _createDefaultConfiguration() throws Exception {
119        URL specificationURL = specToURL(
120                "ptolemy/configs/runPanelConfiguration.xml");
121
122        Configuration configuration = readConfiguration(specificationURL);
123
124        // This has the side effect of merging properties from ptII.properties.
125        super._createDefaultConfiguration();
126
127        // Read the user preferences, if any.
128        PtolemyPreferences.setDefaultPreferences(configuration);
129
130        return configuration;
131    }
132
133    /** Return a default Configuration to use when there are no command-line
134     *  arguments, which in this case is the same as the default configuration
135     *  given by _createDefaultConfiguration, but with the additional
136     *  contents of the file ptolemy/configs/runWelcomeWindow.xml.
137     *  @return A configuration for when there no command-line arguments.
138     *  @exception Exception If the configuration cannot be opened.
139     */
140    @Override
141    protected Configuration _createEmptyConfiguration() throws Exception {
142        Configuration configuration = _createDefaultConfiguration();
143        URL inURL = specToURL("ptolemy/configs/runWelcomeWindow.xml");
144        _parser.reset();
145        _parser.setContext(configuration);
146        _parser.parse(inURL, inURL);
147
148        //URL idURL = specToURL("ptolemy/configs/full/intro.htm");
149        File configurationDirectory = ConfigurationApplication
150                .configurationDirectoryFullOrFirst();
151        File configurationIntro = new File(configurationDirectory, "intro.htm");
152        URL idURL = configurationIntro.toURL();
153        Effigy doc = (Effigy) configuration.getEntity("directory.doc");
154        doc.identifier.setExpression(idURL.toExternalForm());
155        return configuration;
156    }
157
158    /** Parse the command-line arguments. This overrides the base class
159     *  only to set the usage information.
160     *  @exception Exception If an argument is not understood or triggers
161     *   an error.
162     */
163    @Override
164    protected void _parseArgs(String[] args) throws Exception {
165        _commandTemplate = "ptolemy [ options ] [file ...]";
166        super._parseArgs(args);
167    }
168}