001    /*
002     * $Id: JVM.java 3265 2009-02-22 02:01:04Z 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     */
021    package org.jdesktop.swingx.util;
022    
023    import javax.swing.UIManager;
024    import javax.swing.UIManager.LookAndFeelInfo;
025    
026    /**
027     * Deals with the different version of the Java Virtual Machine. <br>
028     */
029    public class JVM {
030    
031      public final static int JDK1_0 = 10;
032      public final static int JDK1_1 = 11;
033      public final static int JDK1_2 = 12;
034      public final static int JDK1_3 = 13;
035      public final static int JDK1_4 = 14;
036      public final static int JDK1_5 = 15;
037      public final static int JDK1_6 = 16;
038      public final static int JDK1_6N = 1610;
039      public final static int JDK1_7 = 17;
040    
041      private static JVM current;
042      static {
043        current = new JVM();
044      }
045    
046      /**
047       * @return the current JVM object
048       */
049      public static JVM current() {
050        return current;
051      }
052    
053      private int jdkVersion;
054    
055      /**
056       * Creates a new JVM data from the <code>java.version</code>
057       * System property
058       *  
059       */
060      public JVM() {
061        this(System.getProperty("java.version"));
062      }
063    
064      /**
065       * Constructor for the OS object
066       */
067      public JVM(String p_JavaVersion) {
068        if (p_JavaVersion.startsWith("1.7.")) {
069          jdkVersion = JDK1_7;
070        } else if (p_JavaVersion.startsWith("1.6.")) {
071          for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
072              if ("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel".equals(info.getClassName())) {
073                  jdkVersion = JDK1_6N;
074                  break;
075              }
076          }
077          
078          jdkVersion = jdkVersion == 0 ? JDK1_6 : jdkVersion;
079        } else if (p_JavaVersion.startsWith("1.5.")) {
080          jdkVersion = JDK1_5;
081        } else if (p_JavaVersion.startsWith("1.4.")) {
082          jdkVersion = JDK1_4;
083        } else if (p_JavaVersion.startsWith("1.3.")) {
084          jdkVersion = JDK1_3;
085        } else if (p_JavaVersion.startsWith("1.2.")) {
086          jdkVersion = JDK1_2;
087        } else if (p_JavaVersion.startsWith("1.1.")) {
088          jdkVersion = JDK1_1;
089        } else if (p_JavaVersion.startsWith("1.0.")) {
090          jdkVersion = JDK1_0;
091        } else {
092          // unknown version, assume 1.3
093          jdkVersion = JDK1_3;
094        }
095      }
096    
097      public boolean isOrLater(int p_Version) {
098        return jdkVersion >= p_Version;
099      }
100    
101      public boolean isOneDotOne() {
102        return jdkVersion == JDK1_1;
103      }
104    
105      public boolean isOneDotTwo() {
106        return jdkVersion == JDK1_2;
107      }
108    
109      public boolean isOneDotThree() {
110        return jdkVersion == JDK1_3;
111      }
112    
113      public boolean isOneDotFour() {
114        return jdkVersion == JDK1_4;
115      }
116    
117      public boolean isOneDotFive() {
118        return jdkVersion == JDK1_5;
119      }
120    
121      public boolean isOneDotSix() {
122        return jdkVersion == JDK1_6;
123      }
124    
125        /**
126         * Determines if the version of JDK1_6 has Nimbus Look and Feel installed.
127         * 
128         * @return {@code true} if Nimbus is available and the version is 1.6;
129         *         {@code false} otherwise
130         */
131      public boolean isOneDotSixUpdateN() {
132          return jdkVersion == JDK1_6N;
133      }
134      
135      public boolean isOneDotSeven() {
136          return jdkVersion == JDK1_7;
137      }
138    
139    }