001/* 002 * $Id: OS.java 4088 2011-11-17 19:53:49Z kschaefe $ 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 */ 021package org.jdesktop.swingx.util; 022 023import java.awt.Toolkit; 024 025import javax.swing.UIManager; 026 027/** 028 * Provides methods related to the runtime environment. 029 */ 030@SuppressWarnings("nls") 031public class OS { 032 033 private static final boolean osIsMacOsX; 034 private static final boolean osIsWindows; 035 private static final boolean osIsWindowsXP; 036 private static final boolean osIsWindows2003; 037 private static final boolean osIsWindowsVista; 038 private static final boolean osIsLinux; 039 040 static { 041 String os = System.getProperty("os.name"); 042 if (os != null) 043 os = os.toLowerCase(); 044 045 osIsMacOsX = "mac os x".equals(os); 046 osIsWindows = os != null && os.indexOf("windows") != -1; 047 osIsWindowsXP = "windows xp".equals(os); 048 osIsWindows2003 = "windows 2003".equals(os); 049 osIsWindowsVista = "windows vista".equals(os); 050 osIsLinux = os != null && os.indexOf("linux") != -1; 051 } 052 053 /** 054 * @return true if this VM is running on Mac OS X 055 */ 056 public static boolean isMacOSX() { 057 return osIsMacOsX; 058 } 059 060 /** 061 * @return true if this VM is running on Windows 062 */ 063 public static boolean isWindows() { 064 return osIsWindows; 065 } 066 067 /** 068 * @return true if this VM is running on Windows XP 069 */ 070 public static boolean isWindowsXP() { 071 return osIsWindowsXP; 072 } 073 074 /** 075 * @return true if this VM is running on Windows 2003 076 */ 077 public static boolean isWindows2003() { 078 return osIsWindows2003; 079 } 080 081 /** 082 * @return true if this VM is running on Windows Vista 083 */ 084 public static boolean isWindowsVista() { 085 return osIsWindowsVista; 086 } 087 088 /** 089 * @return true if this VM is running on a Linux distribution 090 */ 091 public static boolean isLinux() { 092 return osIsLinux; 093 } 094 095 /** 096 * @return true if the VM is running Windows and the Java 097 * application is rendered using XP Visual Styles. 098 */ 099 public static boolean isUsingWindowsVisualStyles() { 100 if (!isWindows()) { 101 return false; 102 } 103 104 boolean xpthemeActive = Boolean.TRUE.equals(Toolkit.getDefaultToolkit() 105 .getDesktopProperty("win.xpstyle.themeActive")); 106 if (!xpthemeActive) { 107 return false; 108 } else { 109 try { 110 return System.getProperty("swing.noxp") == null; 111 } catch (RuntimeException e) { 112 return true; 113 } 114 } 115 } 116 117 /** 118 * Returns the name of the current Windows visual style. 119 * <ul> 120 * <li>it looks for a property name "win.xpstyle.name" in UIManager and if not found 121 * <li>it queries the win.xpstyle.colorName desktop property ({@link Toolkit#getDesktopProperty(java.lang.String)}) 122 * </ul> 123 * 124 * @return the name of the current Windows visual style if any. 125 */ 126 public static String getWindowsVisualStyle() { 127 String style = UIManager.getString("win.xpstyle.name"); 128 if (style == null) { 129 // guess the name of the current XPStyle 130 // (win.xpstyle.colorName property found in awt_DesktopProperties.cpp in 131 // JDK source) 132 style = (String)Toolkit.getDefaultToolkit().getDesktopProperty( 133 "win.xpstyle.colorName"); 134 } 135 return style; 136 } 137 138}