Skip to content

Commit

Permalink
Clean Up Preferences: register each preference only once #797 (#807)
Browse files Browse the repository at this point in the history
Fixes number of selected cleanups. Since
CleanUpTabPage.registerSlavePreference calls registerPreference(master)
again.

#797

Co-authored-by: Jörg Kubitz <jkubitz-eclipse@gmx.de>
  • Loading branch information
jukzi and EcljpseB0T authored Sep 21, 2023
1 parent e572977 commit e3c8d0c
Showing 1 changed file with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
*******************************************************************************/
package org.eclipse.jdt.internal.ui.preferences.cleanup;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
Expand All @@ -43,7 +43,7 @@ public abstract class CleanUpTabPage extends ModifyDialogTabPage implements ICle

private int fCount;
private int fSelectedCount;
private List<ButtonPreference> fCheckboxes= new ArrayList<>();
private Set<ButtonPreference> fCheckboxes= new HashSet<>();

public CleanUpTabPage() {
super();
Expand Down Expand Up @@ -86,7 +86,12 @@ public int getSelectedCleanUpCount() {
}

private void setSelectedCleanUpCount(int selectedCount) {
Assert.isLegal(selectedCount >= 0 && selectedCount <= fCount);
if (selectedCount >= 0 && selectedCount <= fCount) {

}
else {
Assert.isLegal(selectedCount >= 0 && selectedCount <= fCount);
}
fSelectedCount= selectedCount;
}

Expand All @@ -108,18 +113,20 @@ protected void initializePage() {
fInitialValues= Map.copyOf(fValues);
}

@SuppressWarnings("deprecation") // java.util.Observer
protected void registerPreference(final CheckboxPreference preference) {
fCount++;
fCheckboxes.add(preference);
preference.addObserver((o, arg) -> {
if (fCheckboxes.add(preference)) {
fCount++;
preference.addObserver((o, arg) -> {
if (preference.getChecked()) {
setSelectedCleanUpCount(fSelectedCount + 1);
} else {
setSelectedCleanUpCount(fSelectedCount - 1);
}
});
if (preference.getChecked()) {
setSelectedCleanUpCount(fSelectedCount + 1);
} else {
setSelectedCleanUpCount(fSelectedCount - 1);
}
});
if (preference.getChecked()) {
setSelectedCleanUpCount(fSelectedCount + 1);
}
}

Expand Down Expand Up @@ -154,6 +161,7 @@ public void resetValues() {

/* Register a preference that is an option for a cleanup. Checking it does not change the number of clean ups.
*/
@SuppressWarnings("deprecation") // java.util.Observer
protected void registerOptionPreference(final CheckboxPreference main, final CheckboxPreference... options) {
registerPreference(main);
fCheckboxes.addAll(Arrays.asList(options));
Expand Down Expand Up @@ -182,6 +190,7 @@ protected void registerSlavePreference(final CheckboxPreference master, final Ch
* @param subSlaves indirect slaves, i.e. a slave is a master of its subSlave).
* First index into array is the subSlave's master's index. subSlaves can also be <code>null</code>.
*/
@SuppressWarnings("deprecation") // java.util.Observer
protected void registerSlavePreference(final CheckboxPreference master, final CheckboxPreference[] slaves, final CheckboxPreference[][] subSlaves) {
internalRegisterSlavePreference(master, slaves);

Expand Down Expand Up @@ -232,6 +241,7 @@ protected void registerSlavePreference(final CheckboxPreference master, final Ch
}
}

@SuppressWarnings("deprecation") // java.util.Observer
private void internalRegisterSlavePreference(final CheckboxPreference master, final ButtonPreference[] slaves) {
fCheckboxes.addAll(Arrays.asList(slaves));
master.addObserver( (o, arg) -> {
Expand Down

0 comments on commit e3c8d0c

Please sign in to comment.