Skip to content

Commit 04e475b

Browse files
authored
Merge pull request #6224 from IllianiCBT/systemPlanetPicker
Fixed Origin System-Planet Selection to Ensure No Null Values in Campaign Options IIC
2 parents 3abcd06 + 192af7a commit 04e475b

File tree

1 file changed

+33
-10
lines changed

1 file changed

+33
-10
lines changed

MekHQ/src/mekhq/gui/campaignOptions/contents/BiographyTab.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -573,18 +573,15 @@ private JPanel createRandomOriginOptionsPanel() {
573573
chkRandomizeOrigin = new CampaignOptionsCheckBox("RandomizeOrigin");
574574
chkRandomizeDependentsOrigin = new CampaignOptionsCheckBox("RandomizeDependentsOrigin");
575575
chkRandomizeAroundSpecifiedPlanet = new CampaignOptionsCheckBox("RandomizeAroundSpecifiedPlanet");
576+
chkSpecifiedSystemFactionSpecific.addActionListener(evt -> refreshSystemsAndPlanets());
576577

577578
chkSpecifiedSystemFactionSpecific = new CampaignOptionsCheckBox("SpecifiedSystemFactionSpecific");
578-
chkSpecifiedSystemFactionSpecific.addActionListener(evt -> {
579-
final PlanetarySystem planetarySystem = comboSpecifiedSystem.getSelectedItem();
580-
if ((planetarySystem == null)
581-
|| !planetarySystem.getFactionSet(campaign.getLocalDate()).contains(campaign.getFaction())) {
582-
restoreComboSpecifiedSystem();
583-
}
584-
});
579+
chkSpecifiedSystemFactionSpecific.addActionListener(evt -> refreshSystemsAndPlanets());
585580

586581

587582
lblSpecifiedSystem = new CampaignOptionsLabel("SpecifiedSystem");
583+
comboSpecifiedSystem.setModel(new DefaultComboBoxModel<>(getPlanetarySystems(
584+
chkSpecifiedSystemFactionSpecific.isSelected() ? campaign.getFaction() : null)));
588585
comboSpecifiedSystem.setRenderer(new DefaultListCellRenderer() {
589586
@Override
590587
public Component getListCellRendererComponent(final JList<?> list, final Object value,
@@ -607,6 +604,10 @@ public Component getListCellRendererComponent(final JList<?> list, final Object
607604
});
608605

609606
lblSpecifiedPlanet = new CampaignOptionsLabel("SpecifiedPlanet");
607+
final PlanetarySystem planetarySystem = comboSpecifiedSystem.getSelectedItem();
608+
if (planetarySystem != null) {
609+
comboSpecifiedPlanet.setModel(new DefaultComboBoxModel<>(planetarySystem.getPlanets().toArray(new Planet[] { })));
610+
}
610611
comboSpecifiedPlanet.setRenderer(new DefaultListCellRenderer() {
611612
@Override
612613
public Component getListCellRendererComponent(final JList<?> list, final Object value,
@@ -688,6 +689,30 @@ public Component getListCellRendererComponent(final JList<?> list, final Object
688689
return panel;
689690
}
690691

692+
/**
693+
* Refreshes the planetary systems and planets displayed in the associated combo boxes.
694+
*
695+
* <p>This method first stores the currently selected planetary system and planet. It then
696+
* restores the list of available planetary systems by repopulating the `comboSpecifiedSystem`.
697+
* Finally, it reselects the previously selected planetary system and planet in their respective
698+
* combo boxes.</p>
699+
*
700+
* <p>The method ensures that the user selection persists even after the combo boxes are refreshed.
701+
* Any exceptions during the selection process are caught and ignored. As if we can't restore
702+
* the selection, that's fine, we just use the fallback index of 0.</p>
703+
*/
704+
private void refreshSystemsAndPlanets() {
705+
final PlanetarySystem planetarySystem = comboSpecifiedSystem.getSelectedItem();
706+
final Planet planet = comboSpecifiedPlanet.getSelectedItem();
707+
708+
restoreComboSpecifiedSystem();
709+
710+
try {
711+
comboSpecifiedSystem.setSelectedItem(planetarySystem);
712+
comboSpecifiedPlanet.setSelectedItem(planet);
713+
} catch (Exception ignored) {}
714+
}
715+
691716
/**
692717
* Resets the planet combo box to show only the planets matching the currently selected planetary system.
693718
*/
@@ -1304,6 +1329,7 @@ public void loadValuesFromCampaignOptions(@Nullable CampaignOptions presetCampai
13041329
chkRandomizeOrigin.setSelected(originOptions.isRandomizeOrigin());
13051330
chkRandomizeDependentsOrigin.setSelected(originOptions.isRandomizeDependentOrigin());
13061331
chkRandomizeAroundSpecifiedPlanet.setSelected(originOptions.isRandomizeAroundSpecifiedPlanet());
1332+
comboSpecifiedSystem.setSelectedItem(originOptions.getSpecifiedPlanet().getParentSystem());
13071333
comboSpecifiedPlanet.setSelectedItem(originOptions.getSpecifiedPlanet());
13081334
spnOriginSearchRadius.setValue(originOptions.getOriginSearchRadius());
13091335
spnOriginDistanceScale.setValue(originOptions.getOriginDistanceScale());
@@ -1395,9 +1421,6 @@ public void applyCampaignOptionsToCampaign(@Nullable CampaignOptions presetCampa
13951421
originOptions.setRandomizeAroundSpecifiedPlanet(chkRandomizeAroundSpecifiedPlanet.isSelected());
13961422

13971423
Planet selectedPlanet = comboSpecifiedPlanet.getSelectedItem();
1398-
if (selectedPlanet == null && comboSpecifiedPlanet.getItemCount() > 0) {
1399-
selectedPlanet = comboSpecifiedPlanet.getItemAt(0);
1400-
}
14011424
originOptions.setSpecifiedPlanet(selectedPlanet);
14021425

14031426
originOptions.setOriginSearchRadius((int) spnOriginSearchRadius.getValue());

0 commit comments

Comments
 (0)