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