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