Skip to content

Commit ceeb1a7

Browse files
authored
Merge pull request #5854 from IllianiCBT/presetChooser_usability
Refactored Preset Dialog Chooser to Include Description
2 parents 10f69d1 + 45ff4af commit ceeb1a7

File tree

1 file changed

+52
-7
lines changed

1 file changed

+52
-7
lines changed

MekHQ/src/mekhq/gui/campaignOptions/SelectPresetDialog.java

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,10 @@
2626

2727
import javax.swing.*;
2828
import javax.swing.GroupLayout.Alignment;
29+
import javax.swing.LayoutStyle.ComponentPlacement;
2930
import java.awt.*;
3031
import java.util.ResourceBundle;
3132

32-
import static megamek.client.ui.WrapLayout.wordWrap;
33-
import static megamek.client.ui.swing.util.FlatLafStyleBuilder.setFontScaling;
3433
import static mekhq.gui.campaignOptions.CampaignOptionsUtilities.createGroupLayout;
3534

3635
/**
@@ -79,6 +78,8 @@ public CampaignPreset getSelectedPreset() {
7978
*/
8079
public SelectPresetDialog(JFrame frame, boolean includePresetSelectOption, boolean includeCustomizePresetOption) {
8180
super(frame, resources.getString("presetDialog.title"), true);
81+
final int DIALOG_WIDTH = UIUtil.scaleForGUI(400);
82+
final int INSERT_SIZE = UIUtil.scaleForGUI(10);
8283
returnState = PRESET_SELECTION_CANCELLED;
8384

8485
setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE);
@@ -101,9 +102,7 @@ public SelectPresetDialog(JFrame frame, boolean includePresetSelectOption, boole
101102

102103
JLabel descriptionLabel = new JLabel(String.format(
103104
"<html><body><div style='width:%spx;'><center>%s</center></div></body></html>",
104-
UIUtil.scaleForGUI(400),
105-
resources.getString("presetDialog.description")));
106-
setFontScaling(descriptionLabel, false, 1);
105+
DIALOG_WIDTH, resources.getString("presetDialog.description")));
107106

108107
final DefaultListModel<CampaignPreset> campaignPresets = new DefaultListModel<>();
109108
campaignPresets.addAll(CampaignPreset.getCampaignPresets());
@@ -122,7 +121,6 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
122121
boolean cellHasFocus) {
123122
if (value instanceof CampaignPreset preset) {
124123
setText(preset.getTitle());
125-
setToolTipText(wordWrap(preset.getDescription()));
126124
}
127125

128126
setHorizontalAlignment(JLabel.CENTER);
@@ -136,6 +134,7 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
136134
layout.setVerticalGroup(
137135
layout.createSequentialGroup()
138136
.addComponent(descriptionLabel)
137+
.addPreferredGap(ComponentPlacement.UNRELATED, INSERT_SIZE, INSERT_SIZE)
139138
.addComponent(comboBox)
140139
);
141140

@@ -148,9 +147,55 @@ public Component getListCellRendererComponent(JList<?> list, Object value,
148147
JPanel outerPanel = new JPanel(new GridBagLayout());
149148
GridBagConstraints gbc = new GridBagConstraints();
150149
gbc.anchor = GridBagConstraints.CENTER;
151-
outerPanel.add(centerPanel,gbc);
150+
151+
// Add padding/margin to outerPanel layout using an empty border
152+
outerPanel.setBorder(BorderFactory.createEmptyBorder(INSERT_SIZE, INSERT_SIZE, INSERT_SIZE, INSERT_SIZE));
153+
outerPanel.add(centerPanel, gbc);
154+
155+
JPanel bottomPanel = new JPanel();
156+
bottomPanel.setBorder(BorderFactory.createEtchedBorder());
157+
JLabel newLabel = new JLabel();
158+
newLabel.setHorizontalAlignment(JLabel.CENTER);
159+
bottomPanel.add(newLabel);
160+
161+
// Add 10px gap between outerPanel and bottomPanel
162+
gbc.gridy = 1;
163+
gbc.insets = new Insets(10, 0, 0, 0);
164+
outerPanel.add(bottomPanel, gbc);
165+
166+
comboBox.addActionListener(e -> {
167+
Object selectedItem = comboBox.getSelectedItem();
168+
if (selectedItem instanceof CampaignPreset preset) {
169+
newLabel.setText(String.format(
170+
"<html><body><div style='width:%spx;'><center>%s</center></div></body></html>",
171+
DIALOG_WIDTH * 0.9,
172+
preset.getDescription()));
173+
} else {
174+
newLabel.setText(String.format(
175+
"<html><body><div style='width:%spx;'><center>%s</center></div></body></html>",
176+
DIALOG_WIDTH * 0.9,
177+
"No description available."));
178+
}
179+
revalidate();
180+
repaint();
181+
});
182+
183+
// Set the initial text in newLabel based on the currently selected item in comboBox
184+
Object initialItem = comboBox.getSelectedItem();
185+
if (initialItem instanceof CampaignPreset preset) {
186+
newLabel.setText(String.format(
187+
"<html><body><div style='width:%spx;'><center>%s</center></div></body></html>",
188+
DIALOG_WIDTH * 0.9,
189+
preset.getDescription()));
190+
} else {
191+
newLabel.setText(String.format(
192+
"<html><body><div style='width:%spx;'><center>%s</center></div></body></html>",
193+
DIALOG_WIDTH * 0.9,
194+
"No description available."));
195+
}
152196

153197
add(outerPanel, BorderLayout.CENTER);
198+
154199
JPanel buttonPanel = new JPanel();
155200

156201
JButton buttonSelect = new CampaignOptionsButton("PresetDialogSelect");

0 commit comments

Comments
 (0)