Skip to content

Commit

Permalink
Merge pull request #3788 from gcoopercos/fixJDialogOwnerHq
Browse files Browse the repository at this point in the history
Adjust campaign creation dialogs to have correct jdialod owner for re…
  • Loading branch information
HammerGS authored Jan 9, 2024
2 parents aacf400 + 6479b8d commit e85114e
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
24 changes: 21 additions & 3 deletions MekHQ/src/mekhq/gui/baseComponents/AbstractMHQButtonDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
* This is the Base Dialog for a dialog with buttons in MegaMek. It extends Base Dialog, and adds a
* button panel with base Ok and Cancel buttons. It also includes an enum tracker for the result of
* the dialog.
*
* <p>
* Inheriting classes must call initialize() in their constructors and override createCenterPane()
*
* <p>
* The resources associated with this dialog need to contain at least the following keys:
* - "Ok.text" - text for the ok button
* - "Ok.toolTipText" - toolTipText for the ok button
Expand All @@ -39,6 +39,7 @@
*/
public abstract class AbstractMHQButtonDialog extends AbstractButtonDialog {
//region Constructors

/**
* This creates a modal AbstractMHQButtonDialog using the default MHQ resource bundle. This is
* the normal constructor to use for an AbstractMHQButtonDialog.
Expand All @@ -47,6 +48,10 @@ protected AbstractMHQButtonDialog(final JFrame frame, final String name, final S
this(frame, true, name, title);
}

protected AbstractMHQButtonDialog(final JDialog dialog, final JFrame frame, final String name, final String title) {
this(dialog, frame, true, name, title);
}

/**
* This creates an AbstractMHQButtonDialog using the default MHQ resource bundle. It allows one
* to create non-modal button dialogs, which is not recommended by default.
Expand All @@ -57,6 +62,12 @@ protected AbstractMHQButtonDialog(final JFrame frame, final boolean modal, final
MekHQ.getMHQOptions().getLocale()), name, title);
}

protected AbstractMHQButtonDialog(final JDialog dialog, final JFrame frame, final boolean modal, final String name,
final String title) {
this(dialog, frame, modal, ResourceBundle.getBundle("mekhq.resources.GUI",
MekHQ.getMHQOptions().getLocale()), name, title);
}

/**
* This creates an AbstractMHQButtonDialog using the specified resource bundle. This is not
* recommended by default.
Expand All @@ -65,12 +76,19 @@ protected AbstractMHQButtonDialog(final JFrame frame, final boolean modal, final
final String name, final String title) {
super(frame, modal, resources, name, title);
}

protected AbstractMHQButtonDialog(final JDialog dialog, final JFrame frame, final boolean modal, final ResourceBundle resources,
final String name, final String title) {
super(dialog, frame, modal, resources, name, title);
}
//endregion Constructors
//endregion Constructors

/**
* This override forces the preferences for this class to be tracked in MekHQ instead of MegaMek.
*
* @throws Exception if there's an issue initializing the preferences. Normally this means
* a component has <strong>not</strong> had its name value set.
* a component has <strong>not</strong> had its name value set.
*/
@Override
protected void setPreferences() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@ protected AbstractMHQValidationButtonDialog(final JFrame frame, final boolean mo
final String title) {
super(frame, modal, resources, name, title);
}

/**
* This creates an AbstractMHQValidationButtonDialog using the specified resource bundle. This
* is not recommended by default. Allows a JDialog to be specified as parent.
*/
protected AbstractMHQValidationButtonDialog(final JDialog dialog, final JFrame frame, final boolean modal,
final ResourceBundle resources, final String name,
final String title) {
super(dialog, frame, modal, resources, name, title);
}
//endregion Constructors

/**
Expand Down
12 changes: 12 additions & 0 deletions MekHQ/src/mekhq/gui/dialog/CampaignOptionsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,18 @@ public CampaignOptionsDialog(final JFrame frame, final Campaign campaign, final
this.startup = startup;
initialize();
}

/**
* Allows dialog to be constructed with an owner being another dialog
*/
public CampaignOptionsDialog(final JDialog owner, final JFrame frame, final Campaign campaign, final boolean startup) {
super(owner, frame, true, ResourceBundle.getBundle("mekhq.resources.CampaignOptionsDialog",
MekHQ.getMHQOptions().getLocale()),
"CampaignOptionsDialog", "CampaignOptionsDialog.title");
this.campaign = campaign;
this.startup = startup;
initialize();
}
//endregion Constructors

//region Getters/Setters
Expand Down
12 changes: 10 additions & 2 deletions MekHQ/src/mekhq/gui/dialog/CampaignPresetSelectionDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,16 @@ public class CampaignPresetSelectionDialog extends AbstractMHQButtonDialog {
//endregion Variable Declarations

//region Constructors
public CampaignPresetSelectionDialog(final JFrame parent) {
super(parent, "CampaignPresetSelectionDialog", "CampaignPresetSelectionDialog.title");
public CampaignPresetSelectionDialog(final JFrame parentFrame) {
super(parentFrame, "CampaignPresetSelectionDialog", "CampaignPresetSelectionDialog.title");
initialize();
}

/**
* Allows dialog to be constructed using a dialog as owner
*/
public CampaignPresetSelectionDialog(final JDialog parentDialog, final JFrame parentFrame) {
super(parentDialog, parentFrame, "CampaignPresetSelectionDialog", "CampaignPresetSelectionDialog.title");
initialize();
}
//endregion Constructors
Expand Down
12 changes: 8 additions & 4 deletions MekHQ/src/mekhq/gui/dialog/DataLoadingDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public DataLoadingDialog(final JFrame frame, final MekHQ application,
super(frame, "DataLoadingDialog", "DataLoadingDialog.title");
this.application = application;
this.campaignFile = campaignFile;
this.task = new Task();
this.task = new Task(this);
getTask().addPropertyChangeListener(this);
initialize();
getTask().execute();
Expand Down Expand Up @@ -190,6 +190,10 @@ public void propertyChange(final PropertyChangeEvent evt) {
* Main task. This is executed in a background thread.
*/
private class Task extends SwingWorker<Campaign, Campaign> {
JDialog dialog;
public Task(JDialog dialog) {
this.dialog = dialog;
}
/**
* This uses the following stages of loading:
* 0 : Basics
Expand Down Expand Up @@ -258,7 +262,7 @@ public Campaign doInBackground() throws Exception {
campaign = new Campaign();

// Campaign Preset
final CampaignPresetSelectionDialog presetSelectionDialog = new CampaignPresetSelectionDialog(getFrame());
final CampaignPresetSelectionDialog presetSelectionDialog = new CampaignPresetSelectionDialog(dialog, getFrame());
if (presetSelectionDialog.showDialog().isCancelled()) {
return null;
}
Expand All @@ -267,7 +271,7 @@ public Campaign doInBackground() throws Exception {
// Date
final LocalDate date = ((preset == null) || (preset.getDate() == null))
? campaign.getLocalDate() : preset.getDate();
final DateChooser dc = new DateChooser(getFrame(), date);
final DateChooser dc = new DateChooser(dialog, date);
dc.setLocationRelativeTo(getFrame());
// user can either choose a date or cancel by closing
if (dc.showDateChooser() != DateChooser.OK_OPTION) {
Expand All @@ -285,7 +289,7 @@ public Campaign doInBackground() throws Exception {
setVisible(false);

// Campaign Options
CampaignOptionsDialog optionsDialog = new CampaignOptionsDialog(getFrame(), campaign, true);
CampaignOptionsDialog optionsDialog = new CampaignOptionsDialog(dialog, getFrame(), campaign, true);
optionsDialog.setLocationRelativeTo(getFrame());
optionsDialog.applyPreset(preset);
if (optionsDialog.showDialog().isCancelled()) {
Expand Down
22 changes: 21 additions & 1 deletion MekHQ/src/mekhq/gui/dialog/DateChooser.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,21 @@ public class DateChooser extends JDialog implements ActionListener, FocusListene
private JFormattedTextField dateField;

/**
* Constructor for DateChooser
* Constructor for DateChooser which has parent dialog
*
* @param parentDialog
* JDialog istance. Dialog that owns this
* @param d
* LocalDate instance that will be the initial date for
* this dialog
*/
public DateChooser(JDialog parentDialog, LocalDate d) {
super(parentDialog, "Date Chooser", true);
init(parentDialog, d);
}

/**
* Constructor for DateChooser which does not have a parent dialog
*
* @param owner
* JFrame instance, owner of DateChooser dialog
Expand All @@ -83,6 +97,12 @@ public class DateChooser extends JDialog implements ActionListener, FocusListene
*/
public DateChooser(JFrame owner, LocalDate d) {
super(owner, "Date Chooser", true);
init(owner, d);
}

private void init(Component owner, LocalDate d) {


date = d;
workingDate = date;

Expand Down

0 comments on commit e85114e

Please sign in to comment.