Skip to content

Commit 8d6afbe

Browse files
authored
Merge pull request #6505 from IllianiCBT/advancedMedicalDialogBug
Refactored Advanced Medical Edit Injury Dialog To Improve Error Handling
2 parents 65fa418 + c2ac112 commit 8d6afbe

File tree

1 file changed

+87
-83
lines changed

1 file changed

+87
-83
lines changed

MekHQ/src/mekhq/gui/dialog/EditInjuryEntryDialog.java

+87-83
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ public class EditInjuryEntryDialog extends JDialog {
6464

6565
private Injury injury;
6666

67-
private JButton btnClose;
68-
private JButton btnOK;
69-
private JTextArea txtDays;
67+
private JButton btnClose;
68+
private JButton btnOK;
69+
private JTextArea txtDays;
7070
private JComboBox<BodyLocationChoice> ddLocation;
71-
private JComboBox<InjuryTypeChoice> ddType;
72-
private JTextArea txtFluff;
73-
private JTextArea txtHits;
74-
private JComboBox<String> ddPermanent;
75-
private JComboBox<String> ddWorkedOn;
76-
private JComboBox<String> ddExtended;
77-
private JPanel panBtn;
78-
private JPanel panMain;
79-
80-
private BodyLocationChoice[] locations;
81-
private InjuryTypeChoice[] types;
71+
private JComboBox<InjuryTypeChoice> ddType;
72+
private JTextArea txtFluff;
73+
private JTextArea txtHits;
74+
private JComboBox<String> ddPermanent;
75+
private JComboBox<String> ddWorkedOn;
76+
private JComboBox<String> ddExtended;
77+
private JPanel panBtn;
78+
private JPanel panMain;
79+
80+
private BodyLocationChoice[] locations;
81+
private InjuryTypeChoice[] types;
8282
private FilterableComboBoxModel<InjuryTypeChoice> ddTypeModel;
8383

8484
public EditInjuryEntryDialog(final JFrame frame, final boolean modal, final Injury injury) {
@@ -109,7 +109,7 @@ private void initComponents() {
109109

110110
types = InjuryType.getAllTypes().stream().map(InjuryTypeChoice::new).toArray(InjuryTypeChoice[]::new);
111111

112-
ddType = new JComboBox<>(types);
112+
ddType = new JComboBox<>(types);
113113
ddTypeModel = new FilterableComboBoxModel<>(ddType.getModel());
114114
ddTypeModel.setFilter(it -> {
115115
BodyLocation loc = ((BodyLocationChoice) Objects.requireNonNull(ddLocation.getSelectedItem())).loc;
@@ -124,17 +124,17 @@ private void initComponents() {
124124
}
125125
}
126126

127-
txtDays = new JTextArea();
127+
txtDays = new JTextArea();
128128
txtFluff = new JTextArea();
129-
txtHits = new JTextArea();
129+
txtHits = new JTextArea();
130130
String[] tf = { "True", "False" };
131131
ddPermanent = new JComboBox<>(tf);
132-
ddWorkedOn = new JComboBox<>(tf);
133-
ddExtended = new JComboBox<>(tf);
134-
btnOK = new JButton();
135-
btnClose = new JButton();
136-
panBtn = new JPanel();
137-
panMain = new JPanel();
132+
ddWorkedOn = new JComboBox<>(tf);
133+
ddExtended = new JComboBox<>(tf);
134+
btnOK = new JButton();
135+
btnClose = new JButton();
136+
panBtn = new JPanel();
137+
panMain = new JPanel();
138138

139139
final ResourceBundle resourceMap = ResourceBundle.getBundle("mekhq.resources.EditInjuryEntryDialog",
140140
MekHQ.getMHQOptions().getLocale());
@@ -154,14 +154,14 @@ private void initComponents() {
154154
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
155155
txtDays.setPreferredSize(new Dimension(250, 75));
156156
txtDays.setMinimumSize(new Dimension(250, 75));
157-
gridBagConstraints = new GridBagConstraints();
158-
gridBagConstraints.gridx = 0;
159-
gridBagConstraints.gridy = 0;
157+
gridBagConstraints = new GridBagConstraints();
158+
gridBagConstraints.gridx = 0;
159+
gridBagConstraints.gridy = 0;
160160
gridBagConstraints.weightx = 1.0;
161161
gridBagConstraints.weighty = 1.0;
162-
gridBagConstraints.fill = GridBagConstraints.BOTH;
163-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
164-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
162+
gridBagConstraints.fill = GridBagConstraints.BOTH;
163+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
164+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
165165
panMain.add(txtDays, gridBagConstraints);
166166

167167
ddLocation.setName("ddLocation");
@@ -173,21 +173,21 @@ private void initComponents() {
173173
ddLocation.addActionListener(evt -> {
174174
ddTypeModel.updateFilter();
175175

176-
BodyLocation loc = ((BodyLocationChoice) Objects.requireNonNull(ddLocation.getSelectedItem())).loc;
177-
InjuryType type = ((InjuryTypeChoice) Objects.requireNonNull(ddType.getSelectedItem())).type;
176+
BodyLocation loc = ((BodyLocationChoice) Objects.requireNonNull(ddLocation.getSelectedItem())).loc;
177+
InjuryType type = ((InjuryTypeChoice) Objects.requireNonNull(ddType.getSelectedItem())).type;
178178
if (!type.isValidInLocation(loc)) {
179179
ddType.setSelectedItem(ddTypeModel.getElementAt(0));
180180
}
181181
});
182182

183-
gridBagConstraints = new GridBagConstraints();
184-
gridBagConstraints.gridx = 1;
185-
gridBagConstraints.gridy = 0;
183+
gridBagConstraints = new GridBagConstraints();
184+
gridBagConstraints.gridx = 1;
185+
gridBagConstraints.gridy = 0;
186186
gridBagConstraints.weightx = 1.0;
187187
gridBagConstraints.weighty = 1.0;
188-
gridBagConstraints.fill = GridBagConstraints.BOTH;
189-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
190-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
188+
gridBagConstraints.fill = GridBagConstraints.BOTH;
189+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
190+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
191191
panMain.add(ddLocation, gridBagConstraints);
192192

193193
ddType.setName("ddType");
@@ -196,14 +196,14 @@ private void initComponents() {
196196
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
197197
ddType.setPreferredSize(new Dimension(250, 75));
198198
ddType.setMinimumSize(new Dimension(250, 75));
199-
gridBagConstraints = new GridBagConstraints();
200-
gridBagConstraints.gridx = 2;
201-
gridBagConstraints.gridy = 0;
199+
gridBagConstraints = new GridBagConstraints();
200+
gridBagConstraints.gridx = 2;
201+
gridBagConstraints.gridy = 0;
202202
gridBagConstraints.weightx = 1.0;
203203
gridBagConstraints.weighty = 1.0;
204-
gridBagConstraints.fill = GridBagConstraints.BOTH;
205-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
206-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
204+
gridBagConstraints.fill = GridBagConstraints.BOTH;
205+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
206+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
207207
panMain.add(ddType, gridBagConstraints);
208208

209209
txtFluff.setText(injury.getFluff());
@@ -215,14 +215,14 @@ private void initComponents() {
215215
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
216216
txtFluff.setPreferredSize(new Dimension(250, 75));
217217
txtFluff.setMinimumSize(new Dimension(250, 75));
218-
gridBagConstraints = new GridBagConstraints();
219-
gridBagConstraints.gridx = 3;
220-
gridBagConstraints.gridy = 0;
218+
gridBagConstraints = new GridBagConstraints();
219+
gridBagConstraints.gridx = 3;
220+
gridBagConstraints.gridy = 0;
221221
gridBagConstraints.weightx = 1.0;
222222
gridBagConstraints.weighty = 1.0;
223-
gridBagConstraints.fill = GridBagConstraints.BOTH;
224-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
225-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
223+
gridBagConstraints.fill = GridBagConstraints.BOTH;
224+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
225+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
226226
panMain.add(txtFluff, gridBagConstraints);
227227

228228
txtHits.setText(Integer.toString(injury.getHits()));
@@ -234,14 +234,14 @@ private void initComponents() {
234234
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
235235
txtHits.setPreferredSize(new Dimension(250, 75));
236236
txtHits.setMinimumSize(new Dimension(250, 75));
237-
gridBagConstraints = new GridBagConstraints();
238-
gridBagConstraints.gridx = 0;
239-
gridBagConstraints.gridy = 1;
237+
gridBagConstraints = new GridBagConstraints();
238+
gridBagConstraints.gridx = 0;
239+
gridBagConstraints.gridy = 1;
240240
gridBagConstraints.weightx = 1.0;
241241
gridBagConstraints.weighty = 1.0;
242-
gridBagConstraints.fill = GridBagConstraints.BOTH;
243-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
244-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
242+
gridBagConstraints.fill = GridBagConstraints.BOTH;
243+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
244+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
245245
panMain.add(txtHits, gridBagConstraints);
246246

247247
ddPermanent.setSelectedIndex(injury.isPermanent() ? 0 : 1);
@@ -251,14 +251,14 @@ private void initComponents() {
251251
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
252252
ddPermanent.setPreferredSize(new Dimension(250, 75));
253253
ddPermanent.setMinimumSize(new Dimension(250, 75));
254-
gridBagConstraints = new GridBagConstraints();
255-
gridBagConstraints.gridx = 1;
256-
gridBagConstraints.gridy = 1;
254+
gridBagConstraints = new GridBagConstraints();
255+
gridBagConstraints.gridx = 1;
256+
gridBagConstraints.gridy = 1;
257257
gridBagConstraints.weightx = 1.0;
258258
gridBagConstraints.weighty = 1.0;
259-
gridBagConstraints.fill = GridBagConstraints.BOTH;
260-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
261-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
259+
gridBagConstraints.fill = GridBagConstraints.BOTH;
260+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
261+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
262262
panMain.add(ddPermanent, gridBagConstraints);
263263

264264
ddWorkedOn.setSelectedIndex(injury.isWorkedOn() ? 0 : 1);
@@ -268,14 +268,14 @@ private void initComponents() {
268268
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
269269
ddWorkedOn.setPreferredSize(new Dimension(250, 75));
270270
ddWorkedOn.setMinimumSize(new Dimension(250, 75));
271-
gridBagConstraints = new GridBagConstraints();
272-
gridBagConstraints.gridx = 2;
273-
gridBagConstraints.gridy = 1;
271+
gridBagConstraints = new GridBagConstraints();
272+
gridBagConstraints.gridx = 2;
273+
gridBagConstraints.gridy = 1;
274274
gridBagConstraints.weightx = 1.0;
275275
gridBagConstraints.weighty = 1.0;
276-
gridBagConstraints.fill = GridBagConstraints.BOTH;
277-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
278-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
276+
gridBagConstraints.fill = GridBagConstraints.BOTH;
277+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
278+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
279279
panMain.add(ddWorkedOn, gridBagConstraints);
280280

281281
ddExtended.setSelectedIndex(injury.getExtended() ? 0 : 1);
@@ -285,14 +285,14 @@ private void initComponents() {
285285
BorderFactory.createEmptyBorder(5, 5, 5, 5)));
286286
ddExtended.setPreferredSize(new Dimension(250, 75));
287287
ddExtended.setMinimumSize(new Dimension(250, 75));
288-
gridBagConstraints = new GridBagConstraints();
289-
gridBagConstraints.gridx = 3;
290-
gridBagConstraints.gridy = 1;
288+
gridBagConstraints = new GridBagConstraints();
289+
gridBagConstraints.gridx = 3;
290+
gridBagConstraints.gridy = 1;
291291
gridBagConstraints.weightx = 1.0;
292292
gridBagConstraints.weighty = 1.0;
293-
gridBagConstraints.fill = GridBagConstraints.BOTH;
294-
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
295-
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
293+
gridBagConstraints.fill = GridBagConstraints.BOTH;
294+
gridBagConstraints.anchor = GridBagConstraints.NORTHWEST;
295+
gridBagConstraints.insets = new Insets(5, 5, 5, 5);
296296
panMain.add(ddExtended, gridBagConstraints);
297297

298298
btnOK.setText(resourceMap.getString("btnOkay.text"));
@@ -328,16 +328,20 @@ private void setUserPreferences() {
328328
}
329329

330330
private void btnOKActionPerformed(ActionEvent evt) {
331-
injury.setTime(Integer.parseInt(txtDays.getText()));
332-
injury.setHits(Integer.parseInt(txtHits.getText()));
333-
injury.setFluff(txtFluff.getText());
334-
injury.setLocation(((BodyLocationChoice) Objects.requireNonNull(ddLocation.getSelectedItem())).loc);
335-
injury.setType(((InjuryTypeChoice) Objects.requireNonNull(ddType.getSelectedItem())).type);
336-
injury.setPermanent(ddPermanent.getSelectedIndex() == 0);
337-
injury.setWorkedOn(ddWorkedOn.getSelectedIndex() == 0);
338-
injury.setExtended(ddExtended.getSelectedIndex() == 0);
339-
injury.setUUID(UUID.randomUUID());
340-
this.setVisible(false);
331+
try {
332+
injury.setTime(Integer.parseInt(txtDays.getText()));
333+
injury.setHits(Integer.parseInt(txtHits.getText()));
334+
injury.setFluff(txtFluff.getText());
335+
injury.setLocation(((BodyLocationChoice) Objects.requireNonNull(ddLocation.getSelectedItem())).loc);
336+
injury.setType(((InjuryTypeChoice) Objects.requireNonNull(ddType.getSelectedItem())).type);
337+
injury.setPermanent(ddPermanent.getSelectedIndex() == 0);
338+
injury.setWorkedOn(ddWorkedOn.getSelectedIndex() == 0);
339+
injury.setExtended(ddExtended.getSelectedIndex() == 0);
340+
injury.setUUID(UUID.randomUUID());
341+
this.dispose();
342+
} catch (Exception ex) {
343+
logger.info("Failed to update injury entry", ex);
344+
}
341345
}
342346

343347
private void btnCloseActionPerformed(ActionEvent evt) {

0 commit comments

Comments
 (0)