Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Global properties should now properly forward listeners.

  • Loading branch information...
commit e06f6b27a12782e4a228a9e83b0592ca07a76fbd 1 parent d0adbd0
@kelemen authored
View
85 src/org/netbeans/gradle/project/properties/GlobalGradleSettings.java
@@ -6,18 +6,20 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
+import java.util.concurrent.locks.Lock;
+import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.prefs.PreferenceChangeEvent;
import java.util.prefs.PreferenceChangeListener;
import java.util.prefs.Preferences;
-import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.netbeans.api.java.platform.JavaPlatform;
import org.netbeans.api.java.platform.JavaPlatformManager;
import org.netbeans.gradle.project.StringUtils;
import org.openide.filesystems.FileObject;
import org.openide.filesystems.FileUtil;
+import org.openide.util.ChangeSupport;
import org.openide.util.NbPreferences;
public final class GlobalGradleSettings {
@@ -283,6 +285,9 @@ public String toString(FileObject value) {
private static class GlobalProperty<ValueType> implements StringBasedProperty<ValueType> {
private final String settingsName;
private final ValueConverter<ValueType> converter;
+ private final Lock changesLock;
+ private final ChangeSupport changes;
+ private final PreferenceChangeListener changeForwarder;
public GlobalProperty(String settingsName, ValueConverter<ValueType> converter) {
if (settingsName == null) throw new NullPointerException("settingsName");
@@ -290,6 +295,16 @@ public GlobalProperty(String settingsName, ValueConverter<ValueType> converter)
this.settingsName = settingsName;
this.converter = converter;
+ this.changesLock = new ReentrantLock();
+ this.changes = new ChangeSupport(this);
+ this.changeForwarder = new PreferenceChangeListener() {
+ @Override
+ public void preferenceChange(PreferenceChangeEvent evt) {
+ if (GlobalProperty.this.settingsName.equals(evt.getKey())) {
+ changes.fireChange();
+ }
+ }
+ };
}
private static Preferences getPreferences() {
@@ -323,18 +338,30 @@ public boolean isDefault() {
@Override
public void addChangeListener(ChangeListener listener) {
- getPreferences().addPreferenceChangeListener(
- new ChangeListenerWrapper(settingsName, listener));
+ changesLock.lock();
+ try {
+ boolean hasListeners = changes.hasListeners();
+ changes.addChangeListener(listener);
+ if (!hasListeners) {
+ getPreferences().addPreferenceChangeListener(changeForwarder);
+ }
+ } finally {
+ changesLock.unlock();
+ }
}
@Override
public void removeChangeListener(ChangeListener listener) {
- // We assume that listeners are looked up based on the equals
- // method. This is not documented to be necessary and for example:
- // AWTEventMulticaster does rely on reference equality. Still, we
- // hope for the best as there is nothing else we can do.
- getPreferences().removePreferenceChangeListener(
- new ChangeListenerWrapper(settingsName, listener));
+ changesLock.lock();
+ try {
+ changes.removeChangeListener(listener);
+ boolean hasListeners = changes.hasListeners();
+ if (!hasListeners) {
+ getPreferences().removePreferenceChangeListener(changeForwarder);
+ }
+ } finally {
+ changesLock.unlock();
+ }
}
@Override
@@ -353,46 +380,6 @@ public String getValueAsString() {
}
}
- private static class ChangeListenerWrapper implements PreferenceChangeListener {
- private final String preferenceName;
- private final ChangeListener wrapped;
-
- public ChangeListenerWrapper(String preferenceName, ChangeListener wrapped) {
- if (preferenceName == null) throw new NullPointerException("preferenceName");
- if (wrapped == null) throw new NullPointerException("wrapped");
-
- this.preferenceName = preferenceName;
- this.wrapped = wrapped;
- }
-
- @Override
- public void preferenceChange(PreferenceChangeEvent evt) {
- if (preferenceName.equals(evt.getKey())) {
- wrapped.stateChanged(new ChangeEvent(evt.getSource()));
- }
- }
-
- @Override
- public int hashCode() {
- return 61 * wrapped.hashCode() + 3;
- }
-
- @Override
- public boolean equals(Object obj) {
- if (obj == this) {
- return true;
- }
- if (obj == null) {
- return false;
- }
- if (getClass() != obj.getClass()) {
- return false;
- }
- final ChangeListenerWrapper other = (ChangeListenerWrapper)obj;
- return this.wrapped.equals(other.wrapped);
- }
- }
-
private GlobalGradleSettings() {
throw new AssertionError();
}
View
92 src/org/netbeans/gradle/project/properties/ProjectPropertiesPanel.java
@@ -4,7 +4,6 @@
import java.awt.Dialog;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
-import java.io.File;
import java.nio.charset.Charset;
import java.nio.charset.IllegalCharsetNameException;
import java.nio.charset.UnsupportedCharsetException;
@@ -109,14 +108,6 @@ public void run() {
}
private void initFromProperties(ProjectProperties properties) {
- File gradleHome = properties.getGradleHome().getValue();
- jGradleHomeEdit.setText(gradleHome != null ? gradleHome.getPath() : "");
- jGradleHomeInherit.setSelected(properties.getGradleHome().isDefault());
-
- JavaPlatform currentScriptPlatform = properties.getScriptPlatform().getValue();
- jScriptPlatformCombo.setSelectedItem(new PlatformComboItem(currentScriptPlatform));
- jScriptPlatformInherit.setSelected(properties.getScriptPlatform().isDefault());
-
JavaPlatform currentPlatform = properties.getPlatform().getValue();
jPlatformCombo.setSelectedItem(new PlatformComboItem(currentPlatform));
jPlatformComboInherit.setSelected(properties.getPlatform().isDefault());
@@ -234,7 +225,6 @@ private void fillPlatformCombo() {
}
}
- jScriptPlatformCombo.setModel(new DefaultComboBoxModel(comboItems.toArray(new PlatformComboItem[0])));
jPlatformCombo.setModel(new DefaultComboBoxModel(comboItems.toArray(new PlatformComboItem[0])));
}
@@ -270,8 +260,6 @@ private void setEnableDisable() {
return;
}
- jScriptPlatformCombo.setEnabled(!jScriptPlatformInherit.isSelected());
- jGradleHomeEdit.setEnabled(!jGradleHomeInherit.isSelected());
jPlatformCombo.setEnabled(!jPlatformComboInherit.isSelected());
jSourceEncoding.setEnabled(!jSourceEncodingInherit.isSelected());
jSourceLevelCombo.setEnabled(!jSourceLevelComboInherit.isSelected());
@@ -313,15 +301,6 @@ private void saveShownProfile() {
return;
}
- String gradleHomeStr = jGradleHomeEdit.getText().trim();
- File gradleHome = gradleHomeStr.isEmpty() ? null : new File(gradleHomeStr);
- properties.getGradleHome().setValueFromSource(asConst(gradleHome, jGradleHomeInherit.isSelected()));
-
- PlatformComboItem selectedScriptPlatform = (PlatformComboItem)jPlatformCombo.getSelectedItem();
- if (selectedScriptPlatform != null) {
- properties.getScriptPlatform().setValueFromSource(asConst(selectedScriptPlatform.getPlatform(), jScriptPlatformInherit.isSelected()));
- }
-
PlatformComboItem selected = (PlatformComboItem)jPlatformCombo.getSelectedItem();
if (selected != null) {
properties.getPlatform().setValueFromSource(asConst(selected.getPlatform(), jPlatformComboInherit.isSelected()));
@@ -540,12 +519,6 @@ private void initComponents() {
jAddProfileButton = new javax.swing.JButton();
jRemoveProfileButton = new javax.swing.JButton();
jManageBuiltInTasks = new javax.swing.JButton();
- jGradleHomeCaption = new javax.swing.JLabel();
- jGradleHomeEdit = new javax.swing.JTextField();
- jGradleHomeInherit = new javax.swing.JCheckBox();
- jScriptPlatformCaption = new javax.swing.JLabel();
- jScriptPlatformCombo = new javax.swing.JComboBox();
- jScriptPlatformInherit = new javax.swing.JCheckBox();
org.openide.awt.Mnemonics.setLocalizedText(jSourceEncodingCaption, org.openide.util.NbBundle.getMessage(ProjectPropertiesPanel.class, "ProjectPropertiesPanel.jSourceEncodingCaption.text")); // NOI18N
@@ -593,16 +566,6 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
}
});
- org.openide.awt.Mnemonics.setLocalizedText(jGradleHomeCaption, org.openide.util.NbBundle.getMessage(ProjectPropertiesPanel.class, "ProjectPropertiesPanel.jGradleHomeCaption.text")); // NOI18N
-
- jGradleHomeEdit.setText(org.openide.util.NbBundle.getMessage(ProjectPropertiesPanel.class, "ProjectPropertiesPanel.jGradleHomeEdit.text")); // NOI18N
-
- org.openide.awt.Mnemonics.setLocalizedText(jGradleHomeInherit, org.openide.util.NbBundle.getMessage(ProjectPropertiesPanel.class, "ProjectPropertiesPanel.jGradleHomeInherit.text")); // NOI18N
-
- org.openide.awt.Mnemonics.setLocalizedText(jScriptPlatformCaption, org.openide.util.NbBundle.getMessage(ProjectPropertiesPanel.class, "ProjectPropertiesPanel.jScriptPlatformCaption.text")); // NOI18N
-
- org.openide.awt.Mnemonics.setLocalizedText(jScriptPlatformInherit, org.openide.util.NbBundle.getMessage(ProjectPropertiesPanel.class, "ProjectPropertiesPanel.jScriptPlatformInherit.text")); // NOI18N
-
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
@@ -610,6 +573,16 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
+ .addComponent(jSourceLevelCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jPlatformCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
+ .addComponent(jSourceEncoding, javax.swing.GroupLayout.Alignment.LEADING))
+ .addGap(6, 6, 6)
+ .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
+ .addComponent(jSourceEncodingInherit, javax.swing.GroupLayout.Alignment.TRAILING)
+ .addComponent(jPlatformComboInherit, javax.swing.GroupLayout.Alignment.TRAILING)
+ .addComponent(jSourceLevelComboInherit, javax.swing.GroupLayout.Alignment.TRAILING)))
.addGroup(layout.createSequentialGroup()
.addComponent(jProfileCaption)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
@@ -626,24 +599,8 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createSequentialGroup()
.addComponent(jManageTasksButton)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addComponent(jManageBuiltInTasks))
- .addComponent(jGradleHomeCaption)
- .addComponent(jScriptPlatformCaption))
- .addGap(0, 0, Short.MAX_VALUE))
- .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
- .addComponent(jScriptPlatformCombo, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jGradleHomeEdit)
- .addComponent(jSourceLevelCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jPlatformCombo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jSourceEncoding, javax.swing.GroupLayout.Alignment.LEADING))
- .addGap(6, 6, 6)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jSourceEncodingInherit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jGradleHomeInherit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jScriptPlatformInherit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jPlatformComboInherit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addComponent(jSourceLevelComboInherit, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
+ .addComponent(jManageBuiltInTasks)))
+ .addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
@@ -655,25 +612,13 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addComponent(jProfileCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jAddProfileButton)
.addComponent(jRemoveProfileButton))
- .addGap(18, 18, 18)
+ .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 13, Short.MAX_VALUE)
.addComponent(jSourceEncodingCaption)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSourceEncoding, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSourceEncodingInherit))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jGradleHomeCaption, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jGradleHomeEdit, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jGradleHomeInherit))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addComponent(jScriptPlatformCaption)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
- .addComponent(jScriptPlatformCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jScriptPlatformInherit))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jPlatformCaption)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
@@ -685,11 +630,10 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jSourceLevelCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jSourceLevelComboInherit))
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
+ .addGap(9, 9, 9)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jManageTasksButton)
- .addComponent(jManageBuiltInTasks))
- .addGap(0, 8, Short.MAX_VALUE))
+ .addComponent(jManageBuiltInTasks)))
);
}// </editor-fold>//GEN-END:initComponents
@@ -836,9 +780,6 @@ public void run() {
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton jAddProfileButton;
- private javax.swing.JLabel jGradleHomeCaption;
- private javax.swing.JTextField jGradleHomeEdit;
- private javax.swing.JCheckBox jGradleHomeInherit;
private javax.swing.JButton jManageBuiltInTasks;
private javax.swing.JButton jManageTasksButton;
private javax.swing.JLabel jPlatformCaption;
@@ -847,9 +788,6 @@ public void run() {
private javax.swing.JLabel jProfileCaption;
private javax.swing.JComboBox jProfileCombo;
private javax.swing.JButton jRemoveProfileButton;
- private javax.swing.JLabel jScriptPlatformCaption;
- private javax.swing.JComboBox jScriptPlatformCombo;
- private javax.swing.JCheckBox jScriptPlatformInherit;
private javax.swing.JTextField jSourceEncoding;
private javax.swing.JLabel jSourceEncodingCaption;
private javax.swing.JCheckBox jSourceEncodingInherit;
Please sign in to comment.
Something went wrong with that request. Please try again.