Skip to content

Commit 28ba052

Browse files
committed
Fix AID star selection persistence across dialog reopen.
Remove all matching combo listeners during programmatic refresh and restore only once, preventing duplicate listeners from overwriting saved star selection during repopulation. Made-with: Cursor
1 parent f9628d0 commit 28ba052

File tree

1 file changed

+40
-10
lines changed

1 file changed

+40
-10
lines changed

src/org/aavso/tools/vstar/ui/dialog/StarGroupSelectionPane.java

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ public StarGroupSelectionPane(JTextField starField, boolean clearStarField) {
9393
starGroupSelector.addActionListener(starGroupSelectorListener);
9494

9595
starSelector = new JComboBox<String>();
96-
populateStarListForSelectedGroup();
9796
starSelector.setBorder(BorderFactory.createTitledBorder(LocaleProps.get("NEW_STAR_FROM_AID_DLG_STAR")));
9897
starSelectorListener = createStarSelectorListener();
98+
populateStarListForSelectedGroup();
9999
starSelector.addActionListener(starSelectorListener);
100100

101101
this.add(starGroupSelector);
@@ -138,21 +138,47 @@ public void actionPerformed(ActionEvent e) {
138138
}
139139

140140
private void withSuppressedStarSelectorListener(Runnable action) {
141-
starSelector.removeActionListener(starSelectorListener);
141+
boolean wasRegistered = removeAllStarSelectorListeners();
142142
try {
143143
action.run();
144144
} finally {
145-
starSelector.addActionListener(starSelectorListener);
145+
if (wasRegistered) {
146+
starSelector.addActionListener(starSelectorListener);
147+
}
146148
}
147149
}
148150

149151
private void withSuppressedGroupSelectorListener(Runnable action) {
150-
starGroupSelector.removeActionListener(starGroupSelectorListener);
152+
boolean wasRegistered = removeAllGroupSelectorListeners();
151153
try {
152154
action.run();
153155
} finally {
154-
starGroupSelector.addActionListener(starGroupSelectorListener);
156+
if (wasRegistered) {
157+
starGroupSelector.addActionListener(starGroupSelectorListener);
158+
}
159+
}
160+
}
161+
162+
private boolean removeAllStarSelectorListeners() {
163+
boolean removedAny = false;
164+
for (ActionListener listener : starSelector.getActionListeners()) {
165+
if (listener == starSelectorListener) {
166+
starSelector.removeActionListener(listener);
167+
removedAny = true;
168+
}
155169
}
170+
return removedAny;
171+
}
172+
173+
private boolean removeAllGroupSelectorListeners() {
174+
boolean removedAny = false;
175+
for (ActionListener listener : starGroupSelector.getActionListeners()) {
176+
if (listener == starGroupSelectorListener) {
177+
starGroupSelector.removeActionListener(listener);
178+
removedAny = true;
179+
}
180+
}
181+
return removedAny;
156182
}
157183

158184
private void syncSelectionFromUI() {
@@ -278,7 +304,9 @@ public void removeGroup(String groupName) {
278304
if (MessageBox.showConfirmDialog("Remove Group", LocaleProps.get("REALLY_DELETE"))) {
279305
starGroups.removeStarGroup(groupName);
280306
starGroupSelector.removeItem(groupName);
281-
selectAndRefreshStarsInGroup((String) starGroupSelector.getItemAt(0));
307+
if (starGroupSelector.getItemCount() > 0) {
308+
selectAndRefreshStarsInGroup((String) starGroupSelector.getItemAt(0));
309+
}
282310
}
283311
}
284312
}
@@ -354,7 +382,7 @@ public void refreshGroups() {
354382

355383
selectedStarName = savedStar;
356384

357-
if (groupToSelect != null && starGroups.doesGroupExist(groupToSelect)) {
385+
if (groupToSelect != null) {
358386
starGroupSelector.setSelectedItem(groupToSelect);
359387
selectedStarGroup = groupToSelect;
360388
populateStarListForSelectedGroup();
@@ -399,9 +427,11 @@ private boolean groupAppearsInCombo(String groupName) {
399427
*/
400428
public void selectAndRefreshStarsInGroup(String groupName) {
401429
if (starGroups.doesGroupExist(groupName)) {
402-
starGroupSelector.setSelectedItem(groupName);
403-
selectedStarGroup = groupName;
404-
populateStarListForSelectedGroup();
430+
withSuppressedGroupSelectorListener(() -> {
431+
starGroupSelector.setSelectedItem(groupName);
432+
selectedStarGroup = groupName;
433+
populateStarListForSelectedGroup();
434+
});
405435
}
406436
}
407437

0 commit comments

Comments
 (0)