geertjanw closed pull request #495: [NETBEANS-648] Cannot select "Source/Binary 
Format : 10" in a maven project
URL: https://github.com/apache/incubator-netbeans/pull/495
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java 
b/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java
index d0d7acaed..ed291d0d3 100644
--- a/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java
+++ b/maven/src/org/netbeans/modules/maven/customizer/CompilePanel.java
@@ -91,11 +91,11 @@
     private Color origComPlatformFore;
 
     /** Creates new form CompilePanel */
-    public CompilePanel(ModelHandle2 handle, Project prj) {
+    public CompilePanel(ModelHandle2 handle, Project prj, 
MavenProjectPropertiesUiSupport uiSupport) {
         initComponents();
         this.handle = handle;
         project = prj;
-        comJavaPlatform.setModel(new PlatformsModel());
+        comJavaPlatform.setModel(uiSupport.getPlatformComboBoxModel());
         comJavaPlatform.setRenderer(new PlatformsRenderer());
 
         origComPlatformFore = comJavaPlatform.getForeground();
@@ -240,7 +240,7 @@ public boolean getDefaultValue() {
         };
 
         // java platform updater
-        new ComboBoxUpdater<Union2<JavaPlatform,String>>(comJavaPlatform, 
lblJavaPlatform) {
+        ComboBoxUpdater<Union2<JavaPlatform,String>> compleComboBoxUpdater = 
new ComboBoxUpdater<Union2<JavaPlatform,String>>(comJavaPlatform, 
lblJavaPlatform) {
             private String modifiedValue;
             private String DEFAULT_PLATFORM_VALUE = "@@DEFAU:T@@";
             private ModelOperation<POMModel> operation = new 
ModelOperation<POMModel>() {
@@ -322,6 +322,10 @@ public void setValue(Union2<JavaPlatform,String> value) {
                 }
             }
         };
+        // the selected item is not set until the compile panel is shown
+        // so, invoke these methods for setting it
+        compleComboBoxUpdater.ancestorAdded(null);
+        compleComboBoxUpdater.ancestorRemoved(null);
     }
 
     private Pair<String,JavaPlatform> getSelPlatform () {
@@ -535,12 +539,18 @@ String getCompilerParam(ModelHandle2 handle, String 
param) {
         return null;
     }
 
-    private class PlatformsModel extends AbstractListModel implements 
ComboBoxModel, PropertyChangeListener {
+    static class PlatformsModel extends AbstractListModel implements 
ComboBoxModel, PropertyChangeListener {
+
+        private static final long serialVersionUID = 1L;
 
         private List<Union2<JavaPlatform,String>> data;
         private Union2<JavaPlatform,String> sel;
+        private final Project project;
+        private final ModelHandle2 handle;
 
-        public PlatformsModel() {
+        public PlatformsModel(Project project, ModelHandle2 handle) {
+            this.project = project;
+            this.handle = handle;
             JavaPlatformManager jpm = JavaPlatformManager.getDefault();
             getPlatforms(jpm);
             jpm.addPropertyChangeListener(WeakListeners.propertyChange(this, 
jpm));
@@ -608,6 +618,11 @@ private void getPlatforms(JavaPlatformManager jpm) {
             data = tmp;
         }
 
+        private Pair<String, JavaPlatform> getSelPlatform() {
+            String platformId = 
project.getLookup().lookup(AuxiliaryProperties.class).
+                    get(Constants.HINT_JDK_PLATFORM, true);
+            return Pair.of(platformId, 
BootClassPathImpl.getActivePlatform(platformId));
+        }
     }
 
     private class PlatformsRenderer extends JLabel implements 
ListCellRenderer, UIResource {
diff --git 
a/maven/src/org/netbeans/modules/maven/customizer/CompilePanelProvider.java 
b/maven/src/org/netbeans/modules/maven/customizer/CompilePanelProvider.java
index 4866a10b4..d7b27bf80 100644
--- a/maven/src/org/netbeans/modules/maven/customizer/CompilePanelProvider.java
+++ b/maven/src/org/netbeans/modules/maven/customizer/CompilePanelProvider.java
@@ -19,7 +19,6 @@
 
 package org.netbeans.modules.maven.customizer;
 import javax.swing.JComponent;
-import org.netbeans.api.project.Project;
 import org.netbeans.modules.maven.api.customizer.ModelHandle2;
 import static org.netbeans.modules.maven.customizer.Bundle.*;
 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
@@ -45,9 +44,8 @@ public Category createCategory(Lookup context) {
     
     @Override
     public JComponent createComponent(Category category, Lookup context) {
-        ModelHandle2 handle = context.lookup(ModelHandle2.class);
-        Project prj = context.lookup(Project.class);
-        return new CompilePanel(handle, prj);
+        MavenProjectPropertiesUiSupport uiSupport = 
context.lookup(MavenProjectPropertiesUiSupport.class);
+        return uiSupport.getCompilePanel();
     }
     
 }
diff --git 
a/maven/src/org/netbeans/modules/maven/customizer/CustomizerProviderImpl.java 
b/maven/src/org/netbeans/modules/maven/customizer/CustomizerProviderImpl.java
index 3118d3a5f..394de3e2f 100644
--- 
a/maven/src/org/netbeans/modules/maven/customizer/CustomizerProviderImpl.java
+++ 
b/maven/src/org/netbeans/modules/maven/customizer/CustomizerProviderImpl.java
@@ -150,7 +150,8 @@ public void showCustomizer( final String 
preselectedCategory, String preselected
                 public void run() {
                     assert EventQueue.isDispatchThread();
                     OptionListener listener = new OptionListener();
-                    Lookup context = Lookups.fixed(new Object[] { project, 
handle, handle2});
+                    MavenProjectPropertiesUiSupport uiSupport = new 
MavenProjectPropertiesUiSupport(handle2, project);
+                    Lookup context = Lookups.fixed(new Object[] { project, 
handle, handle2, uiSupport});
                     Dialog dialog = 
ProjectCustomizer.createCustomizerDialog("Projects/org-netbeans-modules-maven/Customizer",
 //NOI18N
                                                      context,
                                                      preselectedCategory,
diff --git 
a/maven/src/org/netbeans/modules/maven/customizer/MavenProjectPropertiesUiSupport.java
 
b/maven/src/org/netbeans/modules/maven/customizer/MavenProjectPropertiesUiSupport.java
new file mode 100644
index 000000000..9548e163c
--- /dev/null
+++ 
b/maven/src/org/netbeans/modules/maven/customizer/MavenProjectPropertiesUiSupport.java
@@ -0,0 +1,91 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.netbeans.modules.maven.customizer;
+
+import javax.swing.ComboBoxModel;
+import javax.swing.JComponent;
+import org.netbeans.api.java.queries.SourceLevelQuery;
+import org.netbeans.api.project.Project;
+import org.netbeans.modules.maven.api.customizer.ModelHandle2;
+
+final class MavenProjectPropertiesUiSupport {
+
+    private final ModelHandle2 handle;
+    private final Project project;
+
+    private JComponent compilePanel;
+    private ComboBoxModel platformComboBoxModel;
+    private ComboBoxModel sourceLevelComboBoxModel;
+
+    public MavenProjectPropertiesUiSupport(ModelHandle2 handle, Project 
project) {
+        this.handle = handle;
+        this.project = project;
+        init();
+    }
+
+    private void init() {
+        setPlatformComboBoxModel(new CompilePanel.PlatformsModel(project, 
handle));
+        setCompilePanel(new CompilePanel(handle, project, this));
+        String sourceLevel = 
SourceLevelQuery.getSourceLevel(project.getProjectDirectory());
+        setSourceLevelComboBoxModel(new 
SourcesPanel.SourceLevelComboBoxModel(getPlatformComboBoxModel(), sourceLevel));
+    }
+
+    /**
+     * @return the compilePanel
+     */
+    public JComponent getCompilePanel() {
+        return compilePanel;
+    }
+
+    /**
+     * @param compilePanel the compilePanel to set
+     */
+    private void setCompilePanel(JComponent compilePanel) {
+        this.compilePanel = compilePanel;
+    }
+
+    /**
+     * @return the platformComboBoxModel
+     */
+    public ComboBoxModel getPlatformComboBoxModel() {
+        return platformComboBoxModel;
+    }
+
+    /**
+     * @param platformComboBoxModel the platformComboBoxModel to set
+     */
+    private void setPlatformComboBoxModel(ComboBoxModel platformComboBoxModel) 
{
+        this.platformComboBoxModel = platformComboBoxModel;
+    }
+
+    /**
+     * @return the sourceLevelComboBoxModel
+     */
+    public ComboBoxModel getSourceLevelComboBoxModel() {
+        return sourceLevelComboBoxModel;
+    }
+
+    /**
+     * @param sourceLevelComboBoxModel the sourceLevelComboBoxModel to set
+     */
+    private void setSourceLevelComboBoxModel(ComboBoxModel 
sourceLevelComboBoxModel) {
+        this.sourceLevelComboBoxModel = sourceLevelComboBoxModel;
+    }
+
+}
diff --git a/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java 
b/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java
index c189004a7..1dc2ad33f 100644
--- a/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java
+++ b/maven/src/org/netbeans/modules/maven/customizer/SourcesPanel.java
@@ -24,9 +24,16 @@
 import java.awt.event.ActionListener;
 import java.io.File;
 import java.nio.charset.Charset;
+import java.util.ArrayList;
+import java.util.List;
 import java.util.logging.Logger;
-import javax.swing.DefaultComboBoxModel;
+import javax.swing.AbstractListModel;
+import javax.swing.ComboBoxModel;
 import javax.swing.JPanel;
+import javax.swing.event.ListDataEvent;
+import javax.swing.event.ListDataListener;
+import org.netbeans.api.annotations.common.NonNull;
+import org.netbeans.api.java.platform.JavaPlatform;
 import org.netbeans.api.java.queries.SourceLevelQuery;
 import org.netbeans.modules.maven.NbMavenProjectImpl;
 import org.netbeans.modules.maven.api.Constants;
@@ -45,7 +52,9 @@
 import org.netbeans.spi.project.ui.support.ProjectCustomizer;
 import org.openide.filesystems.FileObject;
 import org.openide.filesystems.FileUtil;
+import org.openide.modules.SpecificationVersion;
 import org.openide.util.HelpCtx;
+import org.openide.util.Union2;
 
 /**
  * Customizer panel for setting source level and encoding.
@@ -161,7 +170,7 @@ public void performOperation(POMModel model) {
         }
     };
 
-    public SourcesPanel( ModelHandle2 handle, NbMavenProjectImpl project ) {
+    public SourcesPanel( ModelHandle2 handle, NbMavenProjectImpl project, 
MavenProjectPropertiesUiSupport uiSupport) {
         initComponents();
         this.handle = handle;
         FileObject projectFolder = project.getProjectDirectory();
@@ -171,9 +180,7 @@ public SourcesPanel( ModelHandle2 handle, 
NbMavenProjectImpl project ) {
         // XXX use ComboBoxUpdater to boldface the label when not an inherited 
default
         comSourceLevel.setEditable(false);
         sourceLevel = 
SourceLevelQuery.getSourceLevel(project.getProjectDirectory());
-        comSourceLevel.setModel(new DefaultComboBoxModel(new String[] {
-            "1.3", "1.4", "1.5", "1.6", "1.7", "1.8", "9" //NOI18N
-        }));
+        comSourceLevel.setModel(uiSupport.getSourceLevelComboBoxModel());
         
         comSourceLevel.setSelectedItem(sourceLevel);
         String enc = 
project.getOriginalMavenProject().getProperties().getProperty(Constants.ENCODING_PROP);
@@ -403,5 +410,116 @@ private void initComponents() {
     private javax.swing.JTextField txtSrc;
     private javax.swing.JTextField txtTestSrc;
     // End of variables declaration//GEN-END:variables
-    
+
+    static final class SourceLevelComboBoxModel extends 
AbstractListModel<String> implements ComboBoxModel<String>, ListDataListener {
+
+        private final ComboBoxModel platformComboBoxModel;
+        private String selectedSourceLevel;
+        private String[] sourceLevelCache;
+
+        private static final SpecificationVersion MINIMAL_SOURCE_LEVEL = new 
SpecificationVersion("1.3"); // NOI18N
+        private static final long serialVersionUID = 1L;
+
+        public SourceLevelComboBoxModel(ComboBoxModel platformComboBoxModel, 
String selectedSourceLevel) {
+            this.platformComboBoxModel = platformComboBoxModel;
+            this.platformComboBoxModel.addListDataListener(this);
+            this.selectedSourceLevel = selectedSourceLevel;
+        }
+
+        @Override
+        public int getSize() {
+            String[] sourceLevels = getSourceLevels();
+            return sourceLevels.length;
+        }
+
+        @Override
+        public String getElementAt(int index) {
+            String[] sourceLevels = getSourceLevels();
+            assert index >= 0 && index < sourceLevels.length;
+            return sourceLevels[index];
+        }
+
+        @Override
+        public void setSelectedItem(Object obj) {
+            selectedSourceLevel = (obj == null ? null : (String) obj);
+            fireContentsChanged(this, 0, getSize());
+        }
+
+        @Override
+        public Object getSelectedItem() {
+            return selectedSourceLevel;
+        }
+
+        @Override
+        public void intervalAdded(ListDataEvent e) {
+        }
+
+        @Override
+        public void intervalRemoved(ListDataEvent e) {
+        }
+
+        @Override
+        public void contentsChanged(ListDataEvent e) {
+            resetCache();
+        }
+
+        private void resetCache() {
+            synchronized (this) {
+                sourceLevelCache = null;
+            }
+            fireContentsChanged(this, 0, getSize());
+        }
+
+        private synchronized String[] getSourceLevels() {
+            if (sourceLevelCache == null) {
+                Union2<JavaPlatform, String> union = (Union2<JavaPlatform, 
String>) platformComboBoxModel.getSelectedItem();
+                JavaPlatform platform = union.first();
+                List<String> sourceLevels = new ArrayList<>();
+                if (platform != null) {
+                    SpecificationVersion version = 
platform.getSpecification().getVersion();
+                    SpecificationVersion current = MINIMAL_SOURCE_LEVEL;
+                    while (current.compareTo(version) <= 0) {
+                        sourceLevels.add(current.toString());
+                        current = incJavaSpecVersion(current);
+                    }
+                }
+                sourceLevelCache = sourceLevels.toArray(new 
String[sourceLevels.size()]);
+            }
+            return sourceLevelCache;
+        }
+
+    }
+
+    private static SpecificationVersion incJavaSpecVersion(@NonNull final 
SpecificationVersion version) {
+        int major = major(version);
+        int minor = minor(version);
+        if (major == 1) {
+            if (minor == 8) {
+                major = minor + 1;
+                minor = -1;
+            } else {
+                minor += 1;
+            }
+        } else {
+            major += 1;
+        }
+        return minor == -1
+                ? new SpecificationVersion(Integer.toString(major))
+                : new SpecificationVersion(String.format(
+                        "%d.%d", //NOI18N
+                        major,
+                        minor));
+    }
+
+    private static int minor(@NonNull final SpecificationVersion specVer) {
+        final String s = specVer.toString();
+        final int split = s.indexOf('.');
+        return split < 0 ? -1 : Integer.parseInt(s.substring(split + 1));
+    }
+
+    private static int major(@NonNull final SpecificationVersion specVer) {
+        final String s = specVer.toString();
+        final int split = s.indexOf('.');
+        return Integer.parseInt(split < 0 ? s : s.substring(0, split));
+    }
 }
diff --git 
a/maven/src/org/netbeans/modules/maven/customizer/SourcesPanelProvider.java 
b/maven/src/org/netbeans/modules/maven/customizer/SourcesPanelProvider.java
index a3301cd07..9601492ee 100644
--- a/maven/src/org/netbeans/modules/maven/customizer/SourcesPanelProvider.java
+++ b/maven/src/org/netbeans/modules/maven/customizer/SourcesPanelProvider.java
@@ -50,7 +50,8 @@ public Category createCategory(Lookup context) {
     public JComponent createComponent(Category category, Lookup context) {
         ModelHandle2 handle = context.lookup(ModelHandle2.class);
         NbMavenProjectImpl project = context.lookup(NbMavenProjectImpl.class);
-        return new SourcesPanel(handle, project);
+        MavenProjectPropertiesUiSupport uiSupport = 
context.lookup(MavenProjectPropertiesUiSupport.class);
+        return new SourcesPanel(handle, project, uiSupport);
     }
 
     
@ProjectCustomizer.CompositeCategoryProvider.Registration(projectType="org-netbeans-modules-maven",
 position=1000, category="Formatting", categoryLabel="#LBL_CategoryFormatting")


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@netbeans.apache.org
For additional commands, e-mail: notifications-h...@netbeans.apache.org

For further information about the NetBeans mailing lists, visit:
https://cwiki.apache.org/confluence/display/NETBEANS/Mailing+lists

Reply via email to