Skip to content

Commit

Permalink
Also show main window if a message dialog is shown
Browse files Browse the repository at this point in the history
  • Loading branch information
bwRavencl committed Nov 1, 2023
1 parent 667e5c8 commit e19b412
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 10 deletions.
11 changes: 9 additions & 2 deletions src/main/java/de/bwravencl/controllerbuddy/gui/GuiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -181,25 +181,28 @@ private static void setFrameLocationRespectingBounds(
}

public static void showMessageDialog(
@SuppressWarnings("exports") final Component parentComponent, final Object message)
final Main main, @SuppressWarnings("exports") final Component parentComponent, final Object message)
throws HeadlessException {
showMessageDialog(
main,
parentComponent,
message,
UIManager.getString("OptionPane.messageDialogTitle", parentComponent.getLocale()),
JOptionPane.INFORMATION_MESSAGE);
}

public static void showMessageDialog(
final Main main,
@SuppressWarnings("exports") final Component parentComponent,
final Object message,
final String title,
final int messageType)
throws HeadlessException {
showMessageDialog(parentComponent, message, title, messageType, null);
showMessageDialog(main, parentComponent, message, title, messageType, null);
}

public static void showMessageDialog(
final Main main,
@SuppressWarnings("exports") final Component parentComponent,
final Object message,
final String title,
Expand All @@ -210,6 +213,10 @@ public static void showMessageDialog(
return;
}

if (main != null) {
main.show();
}

JOptionPane.showMessageDialog(parentComponent, message, title, messageType, icon);
}

Expand Down
48 changes: 40 additions & 8 deletions src/main/java/de/bwravencl/controllerbuddy/gui/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -868,12 +868,14 @@ public void windowOpened(final WindowEvent e) {

if (isWindows || isLinux) {
GuiUtils.showMessageDialog(
this,
frame,
strings.getString("COULD_NOT_INITIALIZE_GLFW_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
JOptionPane.ERROR_MESSAGE);
} else {
GuiUtils.showMessageDialog(
this,
frame,
strings.getString("COULD_NOT_INITIALIZE_GLFW_DIALOG_TEXT_MAC"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -908,6 +910,7 @@ public void windowOpened(final WindowEvent e) {
log.log(Level.WARNING, "An error occurred while updating the SDL game controller mappings");

GuiUtils.showMessageDialog(
this,
frame,
strings.getString("ERROR_UPDATING_GAME_CONTROLLER_DB_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -972,12 +975,14 @@ public void windowOpened(final WindowEvent e) {
if (noControllerConnected) {
if (isWindows || isLinux) {
GuiUtils.showMessageDialog(
this,
frame,
strings.getString("NO_CONTROLLER_CONNECTED_DIALOG_TEXT"),
strings.getString("INFORMATION_DIALOG_TITLE"),
JOptionPane.INFORMATION_MESSAGE);
} else {
GuiUtils.showMessageDialog(
this,
frame,
strings.getString("NO_CONTROLLER_CONNECTED_DIALOG_TEXT_MAC"),
strings.getString("INFORMATION_DIALOG_TITLE"),
Expand All @@ -996,6 +1001,17 @@ public void windowOpened(final WindowEvent e) {
}
}

boolean show() {
if (frame == null || frame.isVisible()) {
return false;
}

frame.setVisible(true);
updateShowMenuItem();

return true;
}

private static void addGlueToSettingsPanel(final JPanel settingsPanel) {
final var constraints = new GridBagConstraints(
0,
Expand Down Expand Up @@ -1161,7 +1177,11 @@ public static void main(final String[] args) {
scrollPane.setPreferredSize(new Dimension(600, 400));
panel.add(scrollPane, BorderLayout.CENTER);
GuiUtils.showMessageDialog(
main.frame, panel, strings.getString("ERROR_DIALOG_TITLE"), JOptionPane.ERROR_MESSAGE);
main,
main.frame,
panel,
strings.getString("ERROR_DIALOG_TITLE"),
JOptionPane.ERROR_MESSAGE);

terminate(1);
});
Expand Down Expand Up @@ -1288,6 +1308,7 @@ private static void printCommandLineMessage(final String message) {

final var imageIcon = new ImageIcon(getResourceLocation(Main.ICON_RESOURCE_PATHS[2]));
GuiUtils.showMessageDialog(
null,
null,
textArea,
strings.getString("APPLICATION_NAME"),
Expand Down Expand Up @@ -1464,6 +1485,7 @@ public void exportVisualization(final File file) {
} catch (final DOMException | ParserConfigurationException | TransformerException | IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
GuiUtils.showMessageDialog(
main,
frame,
strings.getString("COULD_NOT_EXPORT_VISUALIZATION_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -1619,6 +1641,7 @@ private void handleRemainingCommandLine(final CommandLine commandLine) {
startServer();
} else {
GuiUtils.showMessageDialog(
this,
frame,
MessageFormat.format(
strings.getString("INVALID_VALUE_FOR_COMMAND_LINE_OPTION_AUTOSTART_DIALOG_TEXT"),
Expand Down Expand Up @@ -1661,6 +1684,7 @@ private void initOpenVrOverlay() {
log.log(Level.WARNING, t.getMessage(), t);

EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("OPENVR_OVERLAY_INITIALIZATION_ERROR_DIALOG_TEXT"),
Main.strings.getString("WARNING_DIALOG_TITLE"),
Expand Down Expand Up @@ -1849,6 +1873,7 @@ private void loadProfile(

if (!skipMessageDialogs) {
GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(
strings.getString("PROFILE_VERSION_MISMATCH_DIALOG_TEXT"),
Expand All @@ -1864,6 +1889,7 @@ private void loadProfile(

if (!skipMessageDialogs) {
GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(
strings.getString("PROFILE_VERSION_MISMATCH_DIALOG_TEXT"),
Expand All @@ -1877,6 +1903,7 @@ private void loadProfile(

if (!skipMessageDialogs) {
GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(
strings.getString("PROFILE_VERSION_MISMATCH_DIALOG_TEXT"),
Expand All @@ -1897,6 +1924,7 @@ private void loadProfile(

if (!skipMessageDialogs) {
GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(
strings.getString("UNKNOWN_ACTION_TYPES_DIALOG_TEXT"),
Expand Down Expand Up @@ -1937,6 +1965,7 @@ private void loadProfile(

if (!skipMessageDialogs) {
GuiUtils.showMessageDialog(
main,
frame,
strings.getString("COULD_NOT_LOAD_PROFILE_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -1971,6 +2000,7 @@ public void newActivation(final String[] args) {
}
} else {
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.frame,
strings.getString("ALREADY_RUNNING_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -2377,6 +2407,7 @@ private void saveProfile(File file, final boolean saveAsLastProfile) {
} catch (final IOException e) {
log.log(Level.SEVERE, e.getMessage(), e);
GuiUtils.showMessageDialog(
main,
frame,
strings.getString("COULD_NOT_SAVE_PROFILE_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -2714,6 +2745,7 @@ private boolean updateGameControllerMappingsFromFile(final String path) {
log.log(Level.WARNING, "Could not read external game controller mappings file: " + path);

GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(
strings.getString("COULD_NOT_READ_GAME_CONTROLLER_MAPPINGS_FILE_DIALOG_TEXT"), path),
Expand All @@ -2727,6 +2759,7 @@ private boolean updateGameControllerMappingsFromFile(final String path) {
log.log(Level.WARNING, "An error occurred while updating the SDL game controller mappings");

GuiUtils.showMessageDialog(
main,
frame,
strings.getString("ERROR_UPDATING_GAME_CONTROLLER_DB_DIALOG_TEXT"),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -3848,6 +3881,7 @@ public void actionPerformed(final ActionEvent e) {
vJoyDirectoryLabel.setText(vjoyPath);
} else {
GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(strings.getString("INVALID_VJOY_DIRECTORY_DIALOG_TEXT"), defaultVJoyPath),
strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -3977,17 +4011,13 @@ private OpenAction() {

@Override
protected void doAction() {
final var frameVisible = frame.isVisible();
if (!frameVisible) {
frame.setVisible(true);
updateShowMenuItem();
}
final var wasInvisible = show();

if (profileFileChooser.showOpenDialog(frame) == JFileChooser.APPROVE_OPTION) {
loadProfile(profileFileChooser.getSelectedFile(), false, true);
}

if (!frameVisible) {
if (wasInvisible) {
frame.setVisible(false);
updateShowMenuItem();
}
Expand Down Expand Up @@ -4251,6 +4281,7 @@ public void actionPerformed(final ActionEvent e) {
final var imageIcon = new ImageIcon(getResourceLocation(Main.ICON_RESOURCE_PATHS[2]));

GuiUtils.showMessageDialog(
main,
frame,
MessageFormat.format(strings.getString("ABOUT_DIALOG_TEXT"), Version.VERSION),
(String) getValue(NAME),
Expand Down Expand Up @@ -4298,7 +4329,8 @@ public void actionPerformed(final ActionEvent e) {
textArea.setEditable(false);
final var scrollPane = new JScrollPane(textArea);
scrollPane.setPreferredSize(new Dimension(600, 400));
GuiUtils.showMessageDialog(frame, scrollPane, (String) getValue(NAME), JOptionPane.DEFAULT_OPTION);
GuiUtils.showMessageDialog(
main, frame, scrollPane, (String) getValue(NAME), JOptionPane.DEFAULT_OPTION);
} catch (final IOException e1) {
throw new RuntimeException(e1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ boolean readInput() throws IOException {
"Protocol version mismatch: client " + ServerRunMode.PROTOCOL_VERSION
+ " vs server " + serverProtocolVersion);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("PROTOCOL_VERSION_MISMATCH_DIALOG_TEXT"),
Expand Down Expand Up @@ -162,6 +163,7 @@ boolean readInput() throws IOException {
if (retry != -1 && !Thread.currentThread().isInterrupted()) {
log.log(Level.INFO, "Could not connect after " + N_CONNECTION_RETRIES + " retries");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("COULD_NOT_CONNECT_DIALOG_TEXT"),
Expand Down Expand Up @@ -288,6 +290,7 @@ boolean readInput() throws IOException {
} catch (final SocketTimeoutException e) {
log.log(Level.FINE, e.getMessage(), e);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("CONNECTION_LOST_DIALOG_TEXT"),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -346,6 +349,7 @@ public void run() {

log.log(Level.INFO, "Could not resolve host: " + host);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(Main.strings.getString("INVALID_HOST_ADDRESS_DIALOG_TEXT"), host),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,7 @@ private boolean enoughButtons(final int nButtons) {
if (Main.isWindows) {
log.log(Level.WARNING, "vJoy device has not enough buttons");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("TOO_FEW_VJOY_BUTTONS_DIALOG_TEXT"),
Expand All @@ -448,6 +449,7 @@ private boolean enoughButtons(final int nButtons) {
} else if (Main.isLinux) {
log.log(Level.WARNING, "uinput device has not enough buttons");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("TOO_FEW_UINPUT_BUTTONS_DIALOG_TEXT"),
Expand All @@ -470,6 +472,7 @@ final void handleIOException(final IOException ioException) {

log.log(Level.SEVERE, ioException.getMessage(), ioException);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("GENERAL_INPUT_OUTPUT_ERROR_DIALOG_TEXT"),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand All @@ -491,6 +494,7 @@ final boolean init() {
if (!vJoy.vJoyEnabled().booleanValue()) {
log.log(Level.WARNING, "vJoy driver is not enabled");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("VJOY_DRIVER_NOT_ENABLED_DIALOG_TEXT"),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand All @@ -505,6 +509,7 @@ final boolean init() {
Level.WARNING,
"vJoy DLL version " + dllVersion + " does not match driver version " + drvVersion);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("VJOY_VERSION_MISMATCH_DIALOG_TEXT"),
Expand All @@ -520,6 +525,7 @@ final boolean init() {
if (vJoy.GetVJDStatus(vJoyDevice) != VjoyInterface.VJD_STAT_FREE) {
log.log(Level.WARNING, "vJoy device is not available");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("INVALID_VJOY_DEVICE_STATUS_DIALOG_TEXT"),
Expand Down Expand Up @@ -582,6 +588,7 @@ final boolean init() {
final var missingAxesString = String.join(", ", missingAxes);
log.log(Level.WARNING, "vJoy device is missing the following axes: " + missingAxesString);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("MISSING_AXES_DIALOG_TEXT"),
Expand All @@ -595,6 +602,7 @@ final boolean init() {
if (!vJoy.AcquireVJD(vJoyDevice).booleanValue()) {
log.log(Level.WARNING, "Could not acquire vJoy device");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("COULD_NOT_ACQUIRE_VJOY_DEVICE_DIALOG_TEXT"),
Expand All @@ -607,6 +615,7 @@ final boolean init() {
if (!vJoy.ResetVJD(vJoyDevice).booleanValue()) {
log.log(Level.WARNING, "Could not reset vJoy device");
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
MessageFormat.format(
Main.strings.getString("COULD_NOT_RESET_VJOY_DEVICE_DIALOG_TEXT"),
Expand Down Expand Up @@ -639,6 +648,7 @@ final boolean init() {
} catch (final UnsatisfiedLinkError e) {
log.log(Level.SEVERE, e.getMessage(), e);
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("COULD_NOT_LOAD_VJOY_LIBRARY_DIALOG_TEXT"),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand Down Expand Up @@ -700,6 +710,7 @@ final boolean init() {
log.log(Level.WARNING, t.getMessage(), t);

EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("COULD_NOT_OPEN_UINPUT_DEVICE_DIALOG_TEXT"),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ final void controllerDisconnected() {

log.log(Level.WARNING, Main.assembleControllerLoggingMessage("Could not read from", input.getController()));
EventQueue.invokeLater(() -> GuiUtils.showMessageDialog(
main,
main.getFrame(),
Main.strings.getString("COULD_NOT_READ_FROM_CONTROLLER_DIALOG_TEXT"),
Main.strings.getString("ERROR_DIALOG_TITLE"),
Expand Down
Loading

0 comments on commit e19b412

Please sign in to comment.