001    /*
002     * $Id: OS.java 1705 2007-01-23 18:32:03Z l2fprod $
003     *
004     * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005     * Santa Clara, California 95054, U.S.A. All rights reserved.
006     *
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    package org.jdesktop.swingx.util;
022    
023    import java.awt.Toolkit;
024    
025    import javax.swing.UIManager;
026    
027    /**
028     * Provides methods related to the runtime environment.
029     */
030    public class OS {
031    
032      private static final boolean osIsMacOsX;
033      private static final boolean osIsWindows;
034      private static final boolean osIsWindowsXP;
035      private static final boolean osIsWindows2003;
036      private static final boolean osIsWindowsVista;
037      private static final boolean osIsLinux;
038    
039      static {
040        String os = System.getProperty("os.name").toLowerCase();
041    
042        osIsMacOsX = "mac os x".equals(os);
043        osIsWindows = os != null && os.indexOf("windows") != -1;
044        osIsWindowsXP = "windows xp".equals(os);
045        osIsWindows2003 = "windows 2003".equals(os);
046        osIsWindowsVista = "windows vista".equals(os);
047        osIsLinux = os != null && os.indexOf("linux") != -1;
048      }
049    
050      /**
051       * @return true if this VM is running on Mac OS X
052       */
053      public static boolean isMacOSX() {
054        return osIsMacOsX;
055      }
056    
057      /**
058       * @return true if this VM is running on Windows
059       */
060      public static boolean isWindows() {
061        return osIsWindows;
062      }
063    
064      /**
065       * @return true if this VM is running on Windows XP
066       */
067      public static boolean isWindowsXP() {
068        return osIsWindowsXP;
069      }
070    
071      /**
072       * @return true if this VM is running on Windows 2003
073       */
074      public static boolean isWindows2003() {
075        return osIsWindows2003;
076      }
077    
078      /**
079       * @return true if this VM is running on Windows Vista
080       */
081      public static boolean isWindowsVista() {
082        return osIsWindowsVista;
083      }
084      
085      /**
086       * @return true if this VM is running on a Linux distribution
087       */
088      public static boolean isLinux() {
089        return osIsLinux;
090      }
091      
092      /**
093       * @return true if the VM is running Windows and the Java
094       *         application is rendered using XP Visual Styles.
095       */
096      public static boolean isUsingWindowsVisualStyles() {
097        if (!isWindows()) {
098          return false;
099        }
100    
101        boolean xpthemeActive = Boolean.TRUE.equals(Toolkit.getDefaultToolkit()
102            .getDesktopProperty("win.xpstyle.themeActive"));
103        if (!xpthemeActive) {
104          return false;
105        } else {
106          try {
107            return System.getProperty("swing.noxp") == null;
108          } catch (RuntimeException e) {
109            return true;
110          }
111        }
112      }
113    
114      /**
115       * Returns the name of the current Windows visual style.
116       * <ul>
117       * <li>it looks for a property name "win.xpstyle.name" in UIManager and if not found
118       * <li>it queries the win.xpstyle.colorName desktop property ({@link Toolkit#getDesktopProperty(java.lang.String)})
119       * </ul>
120       * 
121       * @return the name of the current Windows visual style if any. 
122       */
123      public static String getWindowsVisualStyle() {
124        String style = UIManager.getString("win.xpstyle.name");
125        if (style == null) {
126          // guess the name of the current XPStyle
127          // (win.xpstyle.colorName property found in awt_DesktopProperties.cpp in
128          // JDK source)
129          style = (String)Toolkit.getDefaultToolkit().getDesktopProperty(
130            "win.xpstyle.colorName");
131        }
132        return style;
133      }
134      
135    }