001/* Wrapper to start up Ptolemy from a Menu choice.
002
003 Copyright (c) 2001-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.jnlp;
029
030import ptolemy.util.StringUtilities;
031
032///////////////////////////////////////////////////////////////////
033//// MenuApplication
034
035/** A wrapper that starts up Vergil from a menu.
036
037 <p>MenuApplication is primarily for use with the
038 Java Network Launching Protocol
039 (JNLP) aka Web Start, but it can be used with any system that invokes
040 applications via a menu choice such as the InstallAnywhere lax tool.
041
042 <p>If MenuApplication detects that it was invoked from a menu, then
043 it sets the current directory to the value of user's home directory
044 so that when the user opens a file chooser to safe a file then
045 the initial default directory is the user's home directory
046 instead of the application directory
047
048 <p>The security manager is set to null for two reasons:
049 <ol>
050 <li> Get rid of the following message when we open the file browser:
051 <br> "There is no disk in the drive. Please insert a disk into drive A"
052 with the standard Abort/Retry/Ignore buttons.
053 See:
054 <a href="http://forum.java.sun.com/thread.jsp?forum=38&amp;thread=71610">http://forum.java.sun.com/thread.jsp?forum=38&amp;thread=71610</a>
055
056 <li> Speed things up, see
057 <a href="http://forums.java.sun.com/thread.jsp?forum=38&amp;thread=134393">http://forums.java.sun.com/thread.jsp?forum=38&amp;thread=134393</a>
058 </ol>
059
060
061 <p>Note that in Web Start 1.0.1, it is necessary to sign the application
062 if it is to have access to the local disk etc.  The way that this is
063 handled is that the .jnlp file that defines the application
064 is copied to the .jar file that defines the main() method for
065 the application and the .jar file is signed.  Unfortunately, this means
066 that two Web Start applications cannot share one jar file, so
067 we create wrappers that call the appropriate main method.
068
069 <p>For more information about JNLP, see $PTII/mk/jnlp.in.
070
071 <p>Each JNLP Application should extend MenuApplication and simply
072 have its main() call this main().  The makefile will need
073 to be extended to create a jar file that includes
074 <pre>
075 MenuApplication.class
076 <i>Foo</i>Application.class
077 </pre>
078
079 @see ptolemy.vergil.VergilApplication
080
081 @author Christopher Hylands
082 @version $Id$
083 @since Ptolemy II 2.0
084 @Pt.ProposedRating Red (cxh)
085 @Pt.AcceptedRating Red (cxh)
086 */
087public class MenuApplication {
088    /** Main method that sets user.dir as necessary.
089     * @param args Arguments to be passed on to VergilApplication.main()
090     */
091    public static void main(final String[] args) {
092        // If we were started from a menu choice instead of a command
093        // line, then the current working directory is likely
094        // somewhere odd, so set the current working directory (the
095        // user.dir property) to the home directory of the user (the
096        // user.home property)
097        // Note that Java has a very poor notion of the current
098        // directory and that changing user.dir will not necessarily
099        // change the current directory for all aspects of Java.
100        // In particular, the File class does not seems to always respect
101        // the value of user.dir.  In general, changing user.dir
102        // is frowned upon, but we do what we can here.
103        if (_invokedFromAMenu()) {
104            try {
105                System.setProperty("user.dir",
106                        StringUtilities.getProperty("user.home"));
107            } catch (SecurityException ex) {
108                System.out.println(
109                        "Warning: MenuApplication: Could not get user.home property "
110                                + "or set user.dir property. (-sandbox always causes this)");
111            } catch (Exception ex) {
112                // Don't crash here, just print a message and move on
113                ex.printStackTrace();
114            }
115        }
116
117        // Note that VergilApplication.main() invokes the VergilApplication
118        // constructor in the Swing Event thread.
119        ptolemy.vergil.VergilApplication.main(args);
120    }
121
122    ///////////////////////////////////////////////////////////////////
123    ////                         private methods                   ////
124    // Return true if this command was invoked from a menu.
125    private static boolean _invokedFromAMenu() {
126        // Check for Web Start
127        if (StringUtilities.getProperty("javawebstart.version").length() > 0) {
128            return true;
129        }
130
131        if (StringUtilities.getProperty("lax.user.dir").length() > 0) {
132            // If we are running under ZeroG's InstallAnywhere, then this
133            // property will be present.
134            return true;
135        }
136
137        return false;
138    }
139}