@@ -41,6 +41,7 @@ public class StarGroupSelectionPane extends JPanel {
4141
4242 private JComboBox <String > starGroupSelector ;
4343 private JComboBox <String > starSelector ;
44+ private ActionListener starGroupSelectorListener ;
4445 private ActionListener starSelectorListener ;
4546
4647 private StarGroups starGroups ;
@@ -51,6 +52,7 @@ public class StarGroupSelectionPane extends JPanel {
5152 private String selectedAUID ;
5253
5354 private boolean clearStarField ;
55+ private JTextField starField ;
5456
5557 /**
5658 * Constructor
@@ -79,14 +81,16 @@ public StarGroupSelectionPane(JTextField starField, boolean clearStarField) {
7981 selectedAUID = null ;
8082
8183 this .clearStarField = clearStarField ;
84+ this .starField = starField ;
8285
8386 starGroups = StarGroups .getInstance ();
8487 Set <String > starGroupMapKeys = starGroups .getGroupNames ();
8588
8689 starGroupSelector = new JComboBox <String >(starGroupMapKeys .toArray (new String [0 ]));
8790 selectedStarGroup = (String ) starGroupSelector .getItemAt (0 );
8891 starGroupSelector .setBorder (BorderFactory .createTitledBorder (LocaleProps .get ("NEW_STAR_FROM_AID_DLG_GROUP" )));
89- starGroupSelector .addActionListener (createStarGroupSelectorListener ());
92+ starGroupSelectorListener = createStarGroupSelectorListener ();
93+ starGroupSelector .addActionListener (starGroupSelectorListener );
9094
9195 starSelector = new JComboBox <String >();
9296 populateStarListForSelectedGroup ();
@@ -104,10 +108,16 @@ private ActionListener createStarGroupSelectorListener() {
104108 public void actionPerformed (ActionEvent e ) {
105109 // Populate the star selector list according
106110 // to the selected group.
107- selectedStarGroup = (String ) starGroupSelector .getSelectedItem ();
108- starSelector .removeActionListener (starSelectorListener );
111+ String group = (String ) starGroupSelector .getSelectedItem ();
112+ // Avoid clearing the star list / selectedStarName when the combo fires
113+ // with no selection (can happen around dialog show/layout); that would
114+ // wipe the last star and break persistence across invocations.
115+ if (group == null ) {
116+ return ;
117+ }
118+ selectedStarGroup = group ;
109119 populateStarListForSelectedGroup ();
110- starSelector . addActionListener ( starSelectorListener );
120+ updateStarFieldForSelection ( );
111121 }
112122 };
113123 }
@@ -117,32 +127,132 @@ private ActionListener createStarSelectorListener() {
117127 return new ActionListener () {
118128 public void actionPerformed (ActionEvent e ) {
119129 String starName = (String ) starSelector .getSelectedItem ();
120- if (starName != null && !NO_STARS .equals (starName )) {
121- // Select a new star & AUID.
122- selectedStarName = starName ;
123- selectedAUID = starGroups .getAUID (selectedStarGroup , selectedStarName );
130+ if (starName == null || NO_STARS .equals (starName ) || selectedStarGroup == null ) {
131+ return ;
124132 }
133+ selectedStarName = starName ;
134+ selectedAUID = starGroups .getAUID (selectedStarGroup , selectedStarName );
135+ updateStarFieldForSelection ();
125136 }
126137 };
127138 }
128139
140+ private void withSuppressedStarSelectorListener (Runnable action ) {
141+ starSelector .removeActionListener (starSelectorListener );
142+ try {
143+ action .run ();
144+ } finally {
145+ starSelector .addActionListener (starSelectorListener );
146+ }
147+ }
148+
149+ private void withSuppressedGroupSelectorListener (Runnable action ) {
150+ starGroupSelector .removeActionListener (starGroupSelectorListener );
151+ try {
152+ action .run ();
153+ } finally {
154+ starGroupSelector .addActionListener (starGroupSelectorListener );
155+ }
156+ }
157+
158+ private void syncSelectionFromUI () {
159+ Object group = starGroupSelector .getSelectedItem ();
160+ if (group instanceof String && starGroups .doesGroupExist ((String ) group )) {
161+ selectedStarGroup = (String ) group ;
162+ }
163+
164+ Object star = starSelector .getSelectedItem ();
165+ if (star instanceof String && !NO_STARS .equals (star ) && selectedStarGroup != null
166+ && starGroups .doesStarExistInGroup (selectedStarGroup , (String ) star )) {
167+ selectedStarName = (String ) star ;
168+ selectedAUID = starGroups .getAUID (selectedStarGroup , selectedStarName );
169+ }
170+ }
171+
172+ private void updateStarFieldForSelection () {
173+ if (starField == null ) {
174+ return ;
175+ }
176+ if (clearStarField ) {
177+ starField .setText ("" );
178+ } else if (selectedStarName != null ) {
179+ starField .setText (selectedStarName );
180+ }
181+ }
182+
129183 /**
130184 * Populate the star list combo-box given the currently selected star group.
185+ * If the previously selected star is still in this group (e.g. after
186+ * {@link #refreshGroups()} or prefs-driven star-group updates),
187+ * keep that selection instead of resetting to the first list entry.
131188 */
132189 public void populateStarListForSelectedGroup () {
133- starSelector .removeAllItems ();
190+ withSuppressedStarSelectorListener (() -> {
191+ starSelector .removeAllItems ();
134192
135- if (selectedStarGroup != null && !starGroups .getStarNamesInGroup (selectedStarGroup ).isEmpty ()) {
193+ if (selectedStarGroup != null && !starGroups .getStarNamesInGroup (selectedStarGroup ).isEmpty ()) {
136194
137- for (String starName : starGroups .getStarNamesInGroup (selectedStarGroup )) {
138- starSelector .addItem (starName );
195+ for (String starName : starGroups .getStarNamesInGroup (selectedStarGroup )) {
196+ starSelector .addItem (starName );
197+ }
198+
199+ // Prefer the model's string instance so setSelectedIndex/Item matches reliably.
200+ String nameToSelect = findStarNameToSelect (selectedStarGroup , selectedStarName );
201+ if (nameToSelect == null && starSelector .getItemCount () > 0 ) {
202+ nameToSelect = (String ) starSelector .getItemAt (0 );
203+ }
204+ if (nameToSelect != null ) {
205+ int idx = indexOfStarItem (nameToSelect );
206+ if (idx >= 0 ) {
207+ starSelector .setSelectedIndex (idx );
208+ nameToSelect = (String ) starSelector .getItemAt (idx );
209+ } else {
210+ starSelector .setSelectedIndex (0 );
211+ nameToSelect = (String ) starSelector .getItemAt (0 );
212+ }
213+ }
214+ selectedStarName = nameToSelect ;
215+ if (selectedStarName != null ) {
216+ selectedAUID = starGroups .getAUID (selectedStarGroup , selectedStarName );
217+ } else {
218+ selectedAUID = null ;
219+ }
220+ } else {
221+ starSelector .addItem (NO_STARS );
222+ selectedStarName = null ;
223+ selectedAUID = null ;
139224 }
225+ });
226+ }
140227
141- selectedStarName = (String ) starSelector .getItemAt (0 );
142- selectedAUID = starGroups .getAUID (selectedStarGroup , selectedStarName );
143- } else {
144- starSelector .addItem (NO_STARS );
228+ /**
229+ * Pick a star name to show: keep {@code desiredName} if it matches a star in the
230+ * group (exact or trim), else null so the caller can fall back to the first star.
231+ */
232+ private String findStarNameToSelect (String groupName , String desiredName ) {
233+ if (desiredName == null || !starGroups .doesStarExistInGroup (groupName , desiredName )) {
234+ String trimmed = desiredName == null ? null : desiredName .trim ();
235+ if (trimmed != null && starGroups .doesStarExistInGroup (groupName , trimmed )) {
236+ return trimmed ;
237+ }
238+ for (String key : starGroups .getStarNamesInGroup (groupName )) {
239+ if (key != null && trimmed != null && key .equalsIgnoreCase (trimmed )) {
240+ return key ;
241+ }
242+ }
243+ return null ;
244+ }
245+ return desiredName ;
246+ }
247+
248+ private int indexOfStarItem (String starName ) {
249+ for (int i = 0 ; i < starSelector .getItemCount (); i ++) {
250+ Object o = starSelector .getItemAt (i );
251+ if (starName .equals (o )) {
252+ return i ;
253+ }
145254 }
255+ return -1 ;
146256 }
147257
148258 /**
@@ -208,37 +318,78 @@ public void removeStar(String groupName, String starName) {
208318 public void resetGroups () {
209319 starGroups .resetGroupsToDefault ();
210320
211- starGroupSelector .removeAllItems ();
321+ withSuppressedGroupSelectorListener (() -> {
322+ starGroupSelector .removeAllItems ();
212323
213- for (String groupName : starGroups .getGroupNames ()) {
214- starGroupSelector .addItem (groupName );
215- }
324+ for (String groupName : starGroups .getGroupNames ()) {
325+ starGroupSelector .addItem (groupName );
326+ }
216327
217- selectAndRefreshStarsInGroup (starGroups .getDefaultStarListTitle ());
328+ selectAndRefreshStarsInGroup (starGroups .getDefaultStarListTitle ());
329+ });
218330 }
219331
220332 /**
221333 * Refresh the groups in the star group selector list. Only groups with stars
222334 * will be "refreshed".
223335 */
224336 public void refreshGroups () {
225- boolean prevClearStarField = clearStarField ;
337+ syncSelectionFromUI ();
338+ // Rebuilding the combo fires ActionListeners; without removing them first,
339+ // selectedStarGroup is overwritten (often to the first group) before we
340+ // can restore the user's last choice across dialog invocations.
341+ final String savedGroup = getSelectedStarGroupName ();
342+ final String savedStar = getSelectedStarName ();
343+
344+ withSuppressedGroupSelectorListener (() -> {
345+ starGroupSelector .removeAllItems ();
346+
347+ for (String groupName : starGroups .getGroupNames ()) {
348+ if (!starGroups .getStarNamesInGroup (groupName ).isEmpty ()) {
349+ starGroupSelector .addItem (groupName );
350+ }
351+ }
226352
227- starGroupSelector . removeAllItems ( );
353+ String groupToSelect = resolveGroupToSelect ( savedGroup );
228354
229- for (String groupName : starGroups .getGroupNames ()) {
230- if (!starGroups .getStarNamesInGroup (groupName ).isEmpty ()) {
231- starGroupSelector .addItem (groupName );
355+ selectedStarName = savedStar ;
356+
357+ if (groupToSelect != null && starGroups .doesGroupExist (groupToSelect )) {
358+ starGroupSelector .setSelectedItem (groupToSelect );
359+ selectedStarGroup = groupToSelect ;
360+ populateStarListForSelectedGroup ();
232361 }
362+ });
363+ }
364+
365+ private String resolveGroupToSelect (String savedGroup ) {
366+ if (savedGroup != null && starGroups .doesGroupExist (savedGroup )
367+ && !starGroups .getStarNamesInGroup (savedGroup ).isEmpty ()
368+ && groupAppearsInCombo (savedGroup )) {
369+ return savedGroup ;
233370 }
234371
235- if (selectedStarGroup != null && starGroups .doesGroupExist (selectedStarGroup )) {
236- selectAndRefreshStarsInGroup (selectedStarGroup );
237- } else {
238- selectAndRefreshStarsInGroup (starGroups .getDefaultStarListTitle ());
372+ String def = starGroups .getDefaultStarListTitle ();
373+ if (groupAppearsInCombo (def )) {
374+ return def ;
239375 }
376+ if (starGroupSelector .getItemCount () > 0 ) {
377+ return (String ) starGroupSelector .getItemAt (0 );
378+ }
379+
380+ return null ;
381+ }
240382
241- clearStarField = prevClearStarField ;
383+ private boolean groupAppearsInCombo (String groupName ) {
384+ if (groupName == null ) {
385+ return false ;
386+ }
387+ for (int i = 0 ; i < starGroupSelector .getItemCount (); i ++) {
388+ if (groupName .equals (starGroupSelector .getItemAt (i ))) {
389+ return true ;
390+ }
391+ }
392+ return false ;
242393 }
243394
244395 /**
0 commit comments