001/* Ptolemy GUI Utilities
002
003 Copyright (c) 2008-2013 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.gui;
029
030import javax.swing.UIManager;
031
032import ptolemy.util.StringUtilities;
033
034///////////////////////////////////////////////////////////////////
035//// PtGUIUtilities
036
037/**
038 GUI Utilities.
039
040 @author Christopher Brooks
041 @version $Id$
042 @since Ptolemy II 8.0
043 @Pt.ProposedRating Red (cxh)
044 @Pt.AcceptedRating Red (cxh)
045 */
046public class PtGUIUtilities {
047    /** Instances of this class cannot be created.
048     */
049    private PtGUIUtilities() {
050    }
051
052    ///////////////////////////////////////////////////////////////////
053    ////                         public methods                    ////
054
055    /** Return true if we are running under MacOS look and feel or
056     *  if the ptolemy.ptII.MacOS property is defined.
057     *  To define ptolemy.ptII.MacOS, invoke Vergil with
058     *  java -Dptolemy.ptII.MacOS=true -classpath $PTII ptolemy.vergil.VergilApplication
059     *  Or, under Cygwin:
060     *  <pre>
061     *  export JAVAFLAGS=-Dptolemy.ptII.MacOS=true
062     *  $PTII/bin/vergil
063     *  </pre>
064     *  If the ptolemy.ptII.MacOS property is set to true, this method
065     *  prints the message "ptolemy.ptII.MacOS = true property detected".
066     *
067     *  @return True if the look and feel starts with "Mac OS" or the
068     *  ptolemy.ptII.MacOS property is set to true.
069     */
070    public static boolean macOSLookAndFeel() {
071        // Dan Higgins writes:
072        // "Apple suggests in their tech note 2042 (http://developer.apple.com/
073        // documentation/Java/Conceptual/Java131Development/x_platform/
074        // chapter_5_section_5.html) that the statement
075        //
076        // System.getProperty("mrj.version")
077        //
078        // should be used to detect a mac, with any non-null result
079        // indicating that the machine is a mac. This approach is
080        // independent of the string value which may change."
081        // However, calling getProperty will likely fail in applets
082        // or within the sandbox, so we use this method instead.
083        try {
084            String macOSProperty = StringUtilities
085                    .getProperty("ptolemy.ptII.MacOS");
086            if (macOSProperty.equals("true")) {
087                System.out.println(
088                        "ptolemy.ptII.MacOS = " + "true property detected");
089                return true;
090            } else if (macOSProperty.equals("false")) {
091                System.out.println(
092                        "ptolemy.ptII.MacOS = " + "false property detected");
093                return false;
094            }
095        } catch (SecurityException ex) {
096            if (!_printedSecurityExceptionMessage) {
097                _printedSecurityExceptionMessage = true;
098                System.out.println("Warning: Failed to get the "
099                        + "ptolemy.ptII.MacOS property "
100                        + "(-sandbox always causes this)");
101            }
102        }
103
104        return UIManager.getLookAndFeel().getName().startsWith("Mac OS");
105    }
106
107    /** Return true if java.awt.FileDialog should be used instead of
108     *  javax.swing.JFileChooser.  Certain platforms such as Mac OS X
109     *  have a much better implementation of java.awt.FileDialog
110     *  than they do of javax.swing.JFileChooser.
111     *
112     *  <p>If the ptolemy.ptII.useFileDialog property is set to
113     *  "true", then return true.  If the ptolemy.ptII.useFileDialog
114     *  property is set to any other value, return false.  If the
115     *  ptolemy.ptII.useFileDialog property is not set, then return
116     *  the value of #macOSLookAndFeel().</p>
117     *
118     *  <p>To define ptolemy.ptII.useFileDialog, invoke Vergil with
119     *  java -Dptolemy.ptII.useFileDialog=true -classpath $PTII ptolemy.vergil.VergilApplication
120     *  Or, under Cygwin or bash:</p>
121     *  <pre>
122     *  export JAVAFLAGS=-Dptolemy.ptII.useFileDialog=true
123     *  $PTII/bin/vergil
124     *  </pre>
125     *  @return true if java.awt.FileDialog should be used.
126     */
127    public static boolean useFileDialog() {
128        String useFileDialog = StringUtilities
129                .getProperty("ptolemy.ptII.useFileDialog");
130        if (useFileDialog.length() > 0) {
131            if (useFileDialog.equals("true")) {
132                return true;
133            } else {
134                return false;
135            }
136        }
137        // Avoid JVM crashes in Kepler on Mac OS X 10.8 with Java 1.6 when
138        // trying to use Save dialog's directory drop-down chooser menu by
139        // not using java.awt.FileDialog.
140        // See http://bugzilla.ecoinformatics.org/show_bug.cgi?id=5725
141        if (System.getProperty("os.name").equals("Mac OS X")
142                && System.getProperty("os.version").startsWith("10.8")
143                && System.getProperty("java.specification.version")
144                        .equals("1.6")) {
145            return false;
146        }
147
148        return PtGUIUtilities.macOSLookAndFeel();
149    }
150
151    // True if we have printed the securityExceptionMessage;
152    private static boolean _printedSecurityExceptionMessage = false;
153
154}