001/*
002 * Copyright (c) 2006-2010 The Regents of the University of California.
003 * All rights reserved.
004 *
005 * '$Author: welker $'
006 * '$Date: 2010-05-06 05:21:26 +0000 (Thu, 06 May 2010) $' 
007 * '$Revision: 24234 $'
008 * 
009 * Permission is hereby granted, without written agreement and without
010 * license or royalty fees, to use, copy, modify, and distribute this
011 * software and its documentation for any purpose, provided that the above
012 * copyright notice and the following two paragraphs appear in all copies
013 * of this software.
014 *
015 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
016 * FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
017 * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
018 * THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
019 * SUCH DAMAGE.
020 *
021 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
022 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
023 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
024 * PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
025 * CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE, SUPPORT, UPDATES,
026 * ENHANCEMENTS, OR MODIFICATIONS.
027 *
028 */
029
030package org.kepler.gui;
031
032import java.awt.Color;
033import java.awt.Dimension;
034import java.util.MissingResourceException;
035
036import javax.swing.LookAndFeel;
037import javax.swing.UIManager;
038
039import org.kepler.configuration.ConfigurationProperty;
040import org.kepler.util.StaticResources;
041
042
043//////////////////////////////////////////////////////////////////////////
044//// StaticResources
045
046/**
047 * 
048 * Static resources for accessing ResourceBundles etc.
049 * <p>
050 * FIXME: this class imports awt classes, so it should not be in kernel.util.
051 * 
052 * @author Matthew Brooke
053 * @version $Id: StaticGUIResources.java 24234 2010-05-06 05:21:26Z welker $
054 * @since Ptolemy II 7.2
055 * @Pt.ProposedRating
056 * @Pt.AcceptedRating
057 */
058
059public class StaticGUIResources extends StaticResources {
060    
061    // private constructor - non-instantiable
062    private StaticGUIResources() {
063    }
064
065    ///////////////////////////////////////////////////////////////////
066    ////                       public methods                      ////
067    ///////////////////////////////////////////////////////////////////
068    
069    /**
070     * Search the uiSettings resourcebundle for the 3 color components specified
071     * by the redComponent, greenComponent and blueComponent properties. Return
072     * a java.awt.Color object representing the color specified. If any of the 3
073     * properties are not found, null is returned
074     * 
075     * @param redComponent
076     *            String the properties key String for the red component
077     * 
078     * @param greenComponent
079     *            String the properties key String for the green component
080     * 
081     * @param blueComponent
082     *            String the properties key String for the blue component
083     * 
084     * @return a java.awt.Color object representing the color specified. If any
085     *         of the 3 properties are not found, null is returned
086     */
087    public static Color getColor(String redComponent, String greenComponent,
088            String blueComponent) {
089        int red = 0;
090        int green = 0;
091        int blue = 0;
092        try {
093            red = _getInt(redComponent, getUISettingsProperty());
094            green = _getInt(greenComponent, getUISettingsProperty());
095            blue = _getInt(blueComponent, getUISettingsProperty());
096        } catch (Exception ex) {
097            if (_isDebugging) {
098                System.out
099                        .println("StaticResources could not find Color component(s) for the keys:\n "
100                                + redComponent
101                                + ", "
102                                + greenComponent
103                                + " and/or "
104                                + blueComponent
105                                + "\n; returning NULL!");
106            }
107            return null;
108        }
109        return new Color(red, green, blue);
110    }
111
112    /**
113     * Search the uiSettings resourcebundle for the width and height specified
114     * by the widthKey and heightKey properties. Return a java.awt.Dimension
115     * object with the width and height specified . If either or both of the
116     * properties are not found, a Dimension object is returned with the width
117     * and height specified by the defaultWidth and defaultHeight parameters.
118     * This method should never return null
119     * 
120     * @param widthKey
121     *            the properties key String for the width setting
122     * 
123     * @param heightKey
124     *            the properties key String for the height setting
125     * 
126     * @param defaultWidth
127     *            int - the default width to be used if the property cannot be
128     *            found
129     * 
130     * @param defaultHeight
131     *            int - the default height to be used if the property cannot be
132     *            found
133     * 
134     * @return Dimension object with the width and height specified by the
135     *         widthKey and heightID properties in the uiSettings
136     *         resourcebundle. If either or both of the properties are not
137     *         found, a Dimension object is returned with the width and height
138     *         specified by the defaultWidth and defaultHeight parameters. This
139     *         method should never return null
140     */
141    public static Dimension getDimension(String widthKey, String heightKey,
142            int defaultWidth, int defaultHeight) {
143
144        int width = 0;
145        int height = 0;
146        try {
147            width = _getInt(widthKey, getUISettingsProperty());
148            height = _getInt(heightKey, getUISettingsProperty());
149        } catch (Exception ex) {
150            if (_isDebugging) {
151                System.out
152                        .println("StaticResources could not find Dimension(s) for the keys "
153                                + widthKey
154                                + " and/or "
155                                + heightKey
156                                + "\n; returning default dimensions: "
157                                + defaultWidth + " x " + defaultHeight);
158            }
159            return new Dimension(defaultWidth, defaultHeight);
160        }
161        return new Dimension(width, height);
162    }
163    
164    /**
165     * Get the platform on which this application is running.
166     * 
167     * @return one of the following positive int values representing the
168     *         platform: StaticGUIResource.WINDOWS StaticGUIResource.MAC_OSX
169     *         StaticGUIResource.LINUX or -1 if the platform is unknown
170     */
171    public static int getPlatform() {
172        return _platform;
173    }
174
175   
176    public static short getSVGRenderingMethod() {
177
178        if (svgRenderingMethod == SVG_RENDERING_NOT_SET) {
179
180            System.out
181                    .println("*** Attempting to get ResourceBundle for SVG defaults ***");
182            ConfigurationProperty defaultsBundle = null;
183            try {
184                defaultsBundle = getUISettingsProperty();
185            } catch (Exception ex) {
186                if (_isDebugging) {
187                    System.out.println("Exception getting defaultsBundle: "
188                            + ex + "\nDefaulting to DIVA rendering");
189                }
190                svgRenderingMethod = SVG_DIVA_RENDERING;
191                return svgRenderingMethod;
192            }
193
194            if (defaultsBundle == null) {
195                if (_isDebugging) {
196                    System.out
197                            .println("defaultsBundle==null; Defaulting to DIVA rendering");
198                }
199                svgRenderingMethod = SVG_DIVA_RENDERING;
200                return svgRenderingMethod;
201            }
202
203            String isBatikStr = null;
204            try {
205                isBatikStr = getSettingsString("SVG_RENDERING_IS_BATIK", null);
206            } catch (MissingResourceException mre) {
207                if (_isDebugging) {
208                    System.out.println("MissingResourceException getting "
209                            + "SVG_RENDERING_IS_BATIK"
210                            + "\nDefaulting to DIVA rendering");
211                }
212                svgRenderingMethod = SVG_DIVA_RENDERING;
213                return svgRenderingMethod;
214            }
215
216            if (isBatikStr != null
217                    && isBatikStr.trim().equalsIgnoreCase("true")) {
218                svgRenderingMethod = SVG_BATIK_RENDERING;
219                System.out
220                        .println("*** svgRenderingMethod = SVG_BATIK_RENDERING ***");
221            } else {
222                svgRenderingMethod = SVG_DIVA_RENDERING;
223                System.out
224                        .println("*** svgRenderingMethod = SVG_DIVA_RENDERING ***");
225            }
226        }
227        return svgRenderingMethod;
228    }
229    
230    /**
231     * Set the look & feel - first check if a user-specified L&F exists in the file
232     * whose path is obtained from StaticResources.UI_SETTINGS_BUNDLE. If not,
233     * use the default platform L&F.
234     */
235    public static void setLookAndFeel() {
236
237        // override ptii look & feel
238        String propsLNF = null;
239        String lnfClassName = null;
240        try {
241            //ResourceBundle uiSettingsBundle = ResourceBundle
242            //        .getBundle(StaticResources.UI_SETTINGS_BUNDLE);
243            lnfClassName = UIManager.getSystemLookAndFeelClassName();
244
245            if (lnfClassName.indexOf("windows") > -1
246                    || lnfClassName.indexOf("Windows") > -1) {
247                _platform = WINDOWS;
248                propsLNF = getSettingsString("WINDOWS_LNF", "\n\r");
249            } else if (lnfClassName.indexOf("apple") > -1
250                    || lnfClassName.indexOf("Aqua") > -1) {
251                _platform = MAC_OSX;
252                propsLNF = getSettingsString("MACOSX_LNF", "\n");
253            } else {
254                _platform = LINUX;
255                propsLNF = getSettingsString("LINUX_LNF", "\n");
256            }
257            Class classDefinition = Class.forName(propsLNF);
258            UIManager.setLookAndFeel((LookAndFeel) classDefinition
259                    .newInstance());
260            return;
261        } catch (Exception e) {
262            // Ignore exceptions, which only result in the wrong look and feel.
263        }
264        // gets here only if a custom L&F was not found,
265        // or was found but not successfully assigned
266        try {
267            UIManager.setLookAndFeel(lnfClassName);
268        } catch (Exception ex) {
269            // Ignore exceptions, which only result in the wrong look and feel.
270        }
271    }
272
273    ///////////////////////////////////////////////////////////////////
274    ////                      public variables                     ////
275    ///////////////////////////////////////////////////////////////////
276    
277    public static final short SVG_RENDERING_NOT_SET = 0;
278    public static final short SVG_DIVA_RENDERING = 1;
279    public static final short SVG_BATIK_RENDERING = 2;
280
281    public static final int WINDOWS = 1;
282    public static final int MAC_OSX = 2;
283    public static final int LINUX = 3;
284    
285    ///////////////////////////////////////////////////////////////////
286    ////                        private variables                  ////
287    ///////////////////////////////////////////////////////////////////
288
289    private static short svgRenderingMethod = SVG_RENDERING_NOT_SET;
290    
291    private static int _platform = -1;
292}