001/* A Vergil Applet
002
003 Copyright (c) 2009-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 */
028
029package ptolemy.vergil;
030
031import java.io.File;
032import java.io.IOException;
033import java.net.HttpURLConnection;
034import java.net.MalformedURLException;
035import java.net.URL;
036import java.net.URLConnection;
037
038import ptolemy.actor.gui.Configuration;
039import ptolemy.gui.BasicJApplet;
040import ptolemy.kernel.attributes.VersionAttribute;
041import ptolemy.kernel.util.IllegalActionException;
042import ptolemy.util.StringUtilities;
043
044///////////////////////////////////////////////////////////////////
045//// VergilApplet
046/** An applet that bring up a toplevel, standalone Vergil frame.
047
048 @author Christopher Brooks.
049 @version $Id$
050 @since Ptolemy II 8.0
051 @Pt.ProposedRating Red (cxh)
052 @Pt.AcceptedRating Red (cxh)
053 */
054@SuppressWarnings("serial")
055public class VergilApplet extends BasicJApplet {
056    ///////////////////////////////////////////////////////////////////
057    ////                         public methods                    ////
058
059    /** Cleanup after execution of the model.  This method is called
060     *  by the browser or appletviewer to inform this applet that
061     *  it should clean up.
062     */
063    @Override
064    public void destroy() {
065        super.destroy();
066        // Note: we used to call manager.terminate() here to get rid
067        // of a lingering browser problem
068        stop();
069    }
070
071    /** Return a string describing this applet.
072     *  @return A string describing the applet.
073     */
074    @Override
075    public String getAppletInfo() {
076        return "Ptolemy applet that displays Ptolemy II models using Vergil "
077                + VersionAttribute.CURRENT_VERSION
078                + "\nPtolemy II comes from UC Berkeley, Department of EECS.\n"
079                + "See http://ptolemy.eecs.berkeley.edu/ptolemyII"
080                + "\n(Build: $Id$)";
081    }
082
083    /** Describe the applet parameters.
084     *  @return An array describing the applet parameters.
085     */
086    @Override
087    public String[][] getParameterInfo() {
088        String[][] newinfo = { { "commandLineArguments", "",
089                "Command Line Arguments suitable for VergilApplication" }, };
090        return _concatStringArrays(super.getParameterInfo(), newinfo);
091    }
092
093    /** Initialize the applet. This method is called by the browser
094     *  or applet viewer to inform this applet that it has been
095     *  loaded into the system. It is always called before
096     *  the first time that the start() method is called.
097     *  In this class, this invokes {@link VergilApplication#main(String[])}
098     */
099    @Override
100    public void init() {
101        super.init();
102        String commandLineArguments = getParameter("commandLineArguments");
103        String[] vergilArguments = new String[0];
104        if (commandLineArguments != null) {
105            try {
106                vergilArguments = StringUtilities
107                        .tokenizeForExec(commandLineArguments);
108            } catch (IOException ex) {
109                report("Failed to parse \"" + commandLineArguments + "\"", ex);
110            }
111        }
112        int i = 0;
113        for (; i < vergilArguments.length; i++) {
114            if (vergilArguments[i].endsWith(".xml")) {
115                URL docBase = getDocumentBase();
116                try {
117                    URL xmlFile = new URL(docBase, vergilArguments[i]);
118
119                    try {
120                        // Try to open the URL, if it can't be opened, try from the codebase.
121                        URLConnection connection = xmlFile.openConnection();
122                        if (connection instanceof HttpURLConnection) {
123                            HttpURLConnection httpConnection = (HttpURLConnection) connection;
124                            httpConnection.setRequestMethod("HEAD");
125                            if (httpConnection
126                                    .getResponseCode() != HttpURLConnection.HTTP_OK) {
127                                xmlFile = new URL(vergilArguments[i]);
128                            }
129                        } else {
130                            if (xmlFile.getProtocol().equals("file")) {
131                                File urlFile = new File(xmlFile.getPath());
132                                if (!urlFile.exists()) {
133                                    xmlFile = new URL(getCodeBase(),
134                                            vergilArguments[i]);
135                                }
136                            }
137                        }
138                    } catch (Exception ex) {
139                        System.out.println(
140                                "Failed to open " + vergilArguments[i]);
141                        ex.printStackTrace();
142                    }
143                    vergilArguments[i] = xmlFile.toExternalForm();
144                } catch (MalformedURLException ex) {
145                    report("Failed to open \"" + vergilArguments[i] + "\"", ex);
146                }
147            }
148        }
149        VergilApplication.main(vergilArguments);
150    }
151
152    /** Stop execution of the model. This method is called by the
153     *  browser or applet viewer to inform this applet that it should
154     *  stop its execution. It is called when the Web page
155     *  that contains this applet has been replaced by another page,
156     *  and also just before the applet is to be destroyed.
157     *  In this base class, this method calls the stop() method
158     *  of the manager. If there is no manager, do nothing.
159     */
160    @Override
161    public void stop() {
162        super.stop();
163        try {
164            Configuration.closeAllTableaux();
165        } catch (IllegalActionException ex) {
166            ex.printStackTrace();
167        }
168    }
169}