|
2 | 2 |
|
3 | 3 | import java.awt.event.ActionEvent; |
4 | 4 | import java.awt.event.ActionListener; |
| 5 | +import java.util.logging.Level; |
| 6 | +import java.util.logging.Logger; |
| 7 | +import javax.swing.Icon; |
5 | 8 | import javax.swing.plaf.metal.MetalIconFactory; |
| 9 | +import org.netbeans.api.java.platform.JavaPlatform; |
| 10 | +import org.netbeans.api.java.platform.JavaPlatformManager; |
| 11 | +import org.netbeans.api.java.platform.Specification; |
6 | 12 | import org.openide.DialogDisplayer; |
7 | 13 | import org.openide.NotifyDescriptor; |
8 | 14 | import org.openide.awt.NotificationDisplayer; |
9 | 15 | import org.openide.modules.ModuleInstall; |
| 16 | +import org.openide.modules.SpecificationVersion; |
10 | 17 | import org.openide.util.NbBundle; |
11 | 18 |
|
12 | 19 | public class Installer extends ModuleInstall { |
| 20 | + |
| 21 | + /** |
| 22 | + * Class Logger |
| 23 | + */ |
| 24 | + private static final Logger INSTALLER_LOGGER = Logger.getLogger(Installer.class.getName()); |
13 | 25 |
|
14 | | - private static final int MIN_JAVA_VERSION = 8; |
| 26 | + /** |
| 27 | + * A representation of the minimum required version of Java as a |
| 28 | + * {@link SpecificationVersion} compatible "Dewey-decimal version". |
| 29 | + */ |
| 30 | + private static final String MIN_JAVA_VERSION = "1.7.0"; |
15 | 31 |
|
16 | 32 | @Override |
17 | | - public void restored() { |
18 | | - if (detectOldJava()) { |
19 | | - final String title = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title"); |
20 | | - final String message = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message"); |
21 | | - final int messageType = NotifyDescriptor.ERROR_MESSAGE; |
22 | | - |
23 | | - ActionListener actionListener = new ActionListener() { |
24 | | - @Override |
25 | | - public void actionPerformed(ActionEvent e) { |
26 | | - DialogDisplayer.getDefault().notify(new NotifyDescriptor.Message(message, messageType)); |
27 | | - } |
28 | | - }; |
29 | | - NotificationDisplayer.getDefault().notify(title, new MetalIconFactory.FileIcon16(), message, actionListener); |
| 33 | + public void restored() { |
| 34 | + if (isJavaVersionIncompatiable()) { |
| 35 | + registerIncompatabilityNotification(); |
30 | 36 | } |
31 | 37 | } |
32 | 38 |
|
33 | | - private boolean detectOldJava() { |
34 | | - return JavaVersion.getMinor() < MIN_JAVA_VERSION; |
35 | | - } |
| 39 | + /** |
| 40 | + * Displays a notification in the default notification UI containing |
| 41 | + * information on the plugin's incompatibility with the current IDE's version |
| 42 | + * of the JVM |
| 43 | + */ |
| 44 | + private static void registerIncompatabilityNotification() { |
36 | 45 |
|
37 | | - private static class JavaVersion { |
| 46 | + //These are the title, message and icon displayed in the notification area and within popup if user interacts with notification |
| 47 | + final String incompatTitle = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-title"); |
| 48 | + final String incompatMessage = NbBundle.getMessage(Installer.class, "wlc-nbeditorconfig-version-error-message"); |
| 49 | + final Icon incompatIcon = new MetalIconFactory.FileIcon16(); |
| 50 | + |
| 51 | + INSTALLER_LOGGER.log(Level.SEVERE, incompatMessage); |
38 | 52 |
|
39 | | - private JavaVersion() { |
40 | | - } |
41 | | - |
42 | | - public int getMajor() { |
43 | | - try { |
44 | | - return Integer.parseInt(getMappedVersion()[0]); |
45 | | - } catch (Exception ex) { |
46 | | - return 0; |
| 53 | + final ActionListener notificationInteractionListener = new ActionListener() { |
| 54 | + @Override |
| 55 | + public void actionPerformed(ActionEvent e) { |
| 56 | + NotifyDescriptor.Message pluginIncompatiableMessage = new NotifyDescriptor.Message(incompatMessage, NotifyDescriptor.ERROR_MESSAGE); |
| 57 | + DialogDisplayer.getDefault().notify(pluginIncompatiableMessage); |
47 | 58 | } |
48 | | - } |
| 59 | + }; |
49 | 60 |
|
50 | | - public static int getMinor() { |
51 | | - try { |
52 | | - return Integer.parseInt(getMappedVersion()[1]); |
53 | | - } catch (Exception ex) { |
54 | | - return 0; |
55 | | - } |
56 | | - } |
| 61 | + NotificationDisplayer.getDefault().notify(incompatTitle, incompatIcon, incompatMessage, notificationInteractionListener); |
57 | 62 |
|
58 | | - /** |
59 | | - * Example: Patch version is "0_31" in Java 1.8.0_31 |
60 | | - * |
61 | | - * @return |
62 | | - */ |
63 | | - public static String getPatch() { |
64 | | - try { |
65 | | - return getMappedVersion()[2]; |
66 | | - } catch (Exception ex) { |
67 | | - return ""; |
68 | | - } |
69 | | - } |
| 63 | + } |
70 | 64 |
|
71 | | - private static String[] getMappedVersion() throws Exception { |
72 | | - String[] splittedVersion = System.getProperty("java.version").split("\\."); // NOI18N |
73 | | - if (splittedVersion.length >= 3) { |
74 | | - return splittedVersion; |
75 | | - } else { |
76 | | - throw new RuntimeException("Could not determine Java version"); |
77 | | - } |
78 | | - } |
| 65 | + /** |
| 66 | + * Evaluates the compatibility of the current JVM running the IDE against |
| 67 | + * {@link #MIN_JAVA_VERSION}. |
| 68 | + * |
| 69 | + * @return True if the JVM running the IDE is not compatible with this |
| 70 | + * plugin. |
| 71 | + */ |
| 72 | + private boolean isJavaVersionIncompatiable() { |
| 73 | + SpecificationVersion javaVersion = getJavaPlatformVersion(); |
| 74 | + |
| 75 | + INSTALLER_LOGGER.log(Level.FINE, "Found Java version: " + javaVersion); |
| 76 | + INSTALLER_LOGGER.log(Level.FINE, "Expected Java version: " + MIN_JAVA_VERSION); |
| 77 | + |
| 78 | + return javaVersion.compareTo(getMinimumRequiredJavaVersion()) < 0; |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Convenience/readability method for generating a |
| 83 | + * {@link SpecificationVersion} containing the version information equivalent |
| 84 | + * to {@link #MIN_JAVA_VERSION}. |
| 85 | + * |
| 86 | + * @return |
| 87 | + */ |
| 88 | + private static SpecificationVersion getMinimumRequiredJavaVersion() { |
| 89 | + return new SpecificationVersion(MIN_JAVA_VERSION); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Determines the version of the Java environment the IDE is running within |
| 94 | + * utilizing {@link JavaPlatform}. |
| 95 | + * |
| 96 | + * @return A {@link SpecificationVersion} instance containing the version |
| 97 | + * information of the IDE JVM using a "Dewey-decimal format". |
| 98 | + */ |
| 99 | + private static SpecificationVersion getJavaPlatformVersion() { |
| 100 | + JavaPlatformManager idePlatformManager = JavaPlatformManager.getDefault(); |
| 101 | + JavaPlatform idePlatform = idePlatformManager.getDefaultPlatform(); |
| 102 | + Specification idePlatformSpec = idePlatform.getSpecification(); |
| 103 | + |
| 104 | + return idePlatformSpec.getVersion(); |
79 | 105 | } |
80 | 106 | } |
0 commit comments