create changelog entry
[debian/openrocket] / core / src / net / sf / openrocket / startup / VersionHelper.java
1 package net.sf.openrocket.startup;
2
3 import java.awt.GraphicsEnvironment;
4 import java.util.Locale;
5
6 import javax.swing.JOptionPane;
7
8 import net.sf.openrocket.logging.LogHelper;
9
10 public class VersionHelper {
11         
12         private static final LogHelper log = Application.getLogger();
13         
14         private static final int REQUIRED_MAJOR_VERSION = 1;
15         private static final int REQUIRED_MINOR_VERSION = 6;
16         
17         // OpenJDK 1.6.0_0-b16 is known to work, 1.6.0_0-b12 does not
18         private static final String BAD_OPENJDK_VERSION = "^1.6.0_0-b([0-9]|1[1-5])$";
19         
20         
21         /**
22          * Check that the JRE version is high enough.
23          */
24         static void checkVersion() {
25                 
26                 String[] version = System.getProperty("java.specification.version", "").split("\\.");
27                 
28                 String jreName = System.getProperty("java.vm.name", "(unknown)");
29                 String jreVersion = System.getProperty("java.runtime.version", "(unknown)");
30                 String jreVendor = System.getProperty("java.vendor", "(unknown)");
31                 
32                 log.info("Running JRE " + jreName + " version " + jreVersion + " by " + jreVendor);
33                 
34                 int major, minor;
35                 
36                 try {
37                         major = Integer.parseInt(version[0]);
38                         minor = Integer.parseInt(version[1]);
39                         
40                         if (major < REQUIRED_MAJOR_VERSION ||
41                                         (major == REQUIRED_MAJOR_VERSION && minor < REQUIRED_MINOR_VERSION)) {
42                                 error(new String[] { "Java SE version 6 is required to run OpenRocket.",
43                                                 "You are currently running " + jreName + " version " +
44                                                                 jreVersion + " by " + jreVendor });
45                         }
46                         
47                 } catch (RuntimeException e) {
48                         
49                         confirm(new String[] { "The Java version in use could not be detected.",
50                                         "OpenRocket requires at least Java SE 6.",
51                                         "Continue anyway?" });
52                         
53                 }
54                 
55         }
56         
57         /**
58          * Check whether OpenJDK is being used, and if it is warn the user about
59          * problems and confirm whether to continue.
60          */
61         static void checkOpenJDK() {
62                 
63                 if (System.getProperty("java.runtime.name", "").toLowerCase(Locale.ENGLISH).indexOf("icedtea") >= 0 ||
64                                 System.getProperty("java.vm.name", "").toLowerCase(Locale.ENGLISH).indexOf("openjdk") >= 0) {
65                         
66                         String jreName = System.getProperty("java.vm.name", "(unknown)");
67                         String jreVersion = System.getProperty("java.runtime.version", "(unknown)");
68                         String jreVendor = System.getProperty("java.vendor", "(unknown)");
69                         
70                         if (jreVersion.matches(BAD_OPENJDK_VERSION)) {
71                                 
72                                 confirm(new String[] { "Old versions of OpenJDK are known to have problems " +
73                                                 "running OpenRocket.",
74                                                 " ",
75                                                 "You are currently running " + jreName + " version " +
76                                                                 jreVersion + " by " + jreVendor,
77                                                 "Do you want to continue?" });
78                         }
79                 }
80         }
81         
82         
83         
84         ///////////  Helper methods  //////////
85         
86         /**
87          * Presents an error message to the user and exits the application.
88          * 
89          * @param message       an array of messages to present.
90          */
91         private static void error(String[] message) {
92                 
93                 System.err.println();
94                 System.err.println("Error starting OpenRocket:");
95                 System.err.println();
96                 for (int i = 0; i < message.length; i++) {
97                         System.err.println(message[i]);
98                 }
99                 System.err.println();
100                 
101                 
102                 if (!GraphicsEnvironment.isHeadless()) {
103                         
104                         JOptionPane.showMessageDialog(null, message, "Error starting OpenRocket",
105                                         JOptionPane.ERROR_MESSAGE);
106                         
107                 }
108                 
109                 System.exit(1);
110         }
111         
112         
113         /**
114          * Presents the user with a message dialog and asks whether to continue.
115          * If the user does not select "Yes" the the application exits.
116          * 
117          * @param message       the message Strings to show.
118          */
119         private static void confirm(String[] message) {
120                 
121                 if (!GraphicsEnvironment.isHeadless()) {
122                         
123                         if (JOptionPane.showConfirmDialog(null, message, "Error starting OpenRocket",
124                                         JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION) {
125                                 System.exit(1);
126                         }
127                 }
128         }
129 }