From f4357931ba22908d22302ee4facd8425d6f6f9d0 Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Fri, 22 Dec 2023 15:46:04 -0600 Subject: [PATCH 1/4] Standardized error messaging in Sim Control Panel. --- .../simcontrol/SimControlApplication.java | 68 +++++++++++-------- 1 file changed, 38 insertions(+), 30 deletions(-) diff --git a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java index f56f4c7a1..f46f296c7 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -341,28 +341,17 @@ public void connect() { elements = selectedStr.split("\\s+"); } - Document doc = statusMsgPane.getDocument(); - StyleContext sc = new StyleContext(); - Style redStyle = sc.addStyle("Red", null); - setColorStyleAttr(redStyle, Color.red, Color.black); - - if (elements == null || elements.length < 2) { - try { - doc.insertString(doc.getLength(), "Can't connect! Please provide valid host name and port number separated by : or whitespace!\n", redStyle); - } catch (BadLocationException ble) { - System.out.println("Can't connect! Please provide valid host name and port number separated by : or whitespace!"); - } + if (elements == null || elements.length < 2) { + String errMsg = "Can't connect! Please provide valid host name and port number separated by : or whitespace!"; + printErrorMessage(errMsg); return; } host = elements[0].trim(); try { port = Integer.parseInt(elements[1].trim()); } catch (NumberFormatException nfe) { - try { - doc.insertString(doc.getLength(), elements[1] + " is not a valid port number!\n", redStyle); - } catch (BadLocationException ble) { - System.out.println(elements[1] + " is not a valid port number!"); - } + String errMsg = elements[1] + " is not a valid port number!"; + printErrorMessage(errMsg); return; } } @@ -370,9 +359,8 @@ public void connect() { getInitializationPacket(); if (commandSimcom == null) { - JOptionPane.showMessageDialog(getMainFrame(), - "Sorry, can't connect. Please make sure the availability of both server and port!", - "Connection error", JOptionPane.ERROR_MESSAGE); + String errMsg = "Sorry, can't connect. Please make sure the availability of both server and port!"; + printErrorMessage(errMsg); return; } else { Object[] keys = actionMap.allKeys(); @@ -411,6 +399,7 @@ public void getInitializationPacket() { String simRunDir = null; String[] results = null; try { + String errMsg = "Error: SimControlApplication:getInitializationPacket()"; try { if (host != null && port != -1) { commandSimcom = new VariableServerConnection(host, port); @@ -419,16 +408,16 @@ public void getInitializationPacket() { } } catch (UnknownHostException host_exception) { /** The IP address of the host could not be determined. */ - System.out.println("Error: SimControlApplication:getInitializationPacket()"); - System.out.println(" Unknown host \""+host+"\""); - System.out.println(" Please use a valid host name (e.g. localhost)"); + errMsg += "\n Unknown host \""+host+"\""; + errMsg += "\n Please use a valid host name (e.g. localhost)"; + printErrorMessage(errMsg); } catch (IOException ioe) { /** Port number is unavailable, or there is no connection, etc. */ - System.out.println("Error: SimControlApplication:getInitializationPacket()"); - System.out.println(" Invalid TCP/IP port number \""+port+"\""); - System.out.println(" Please check the server and enter a proper port number!"); - System.out.println(" IOException ..." + ioe); - System.out.println(" If there is no connection, please make sure SIM is up running properly!"); + errMsg += "\n Invalid TCP/IP port number \""+port+"\""; + errMsg += "\n Please check the server and enter a proper port number!"; + errMsg += "\n IOException ..." + ioe; + errMsg += "\n If there is no connection, please make sure SIM is up running properly!"; + printErrorMessage(errMsg); } if (commandSimcom == null) { @@ -618,9 +607,8 @@ protected void ready() { getAction(theKey).setEnabled(false); } } - JOptionPane.showMessageDialog(getMainFrame(), - "No server connection. Please connect!", - "No server connection", JOptionPane.ERROR_MESSAGE); + String errMsg = "No server connection. Please connect!"; + printErrorMessage(errMsg); return; } @@ -656,6 +644,26 @@ protected void startup() { show(view); } + /** + * Prints an error message to the status message pane. In the event there is an error with it, a JOptionPane will pop up. + * @param err + */ + protected void printErrorMessage(String err) { + Document doc = statusMsgPane.getDocument(); + StyleContext sc = new StyleContext(); + Style redStyle = sc.addStyle("Red", null); + setColorStyleAttr(redStyle, Color.red, Color.black); + + try { + doc.insertString(doc.getLength(), err + "\n", redStyle); + } catch (BadLocationException ble) { + JOptionPane.showMessageDialog(getMainFrame(), + "Status Message Pane had an issue when printing: " + err, + "Status Message Pane Error", + JOptionPane.ERROR_MESSAGE); + } + } + /** * Main method for this application. * @param args command line arguments From 1422dac26f19333483cf5ba22db2d156d647df88 Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Tue, 9 Jan 2024 13:53:26 -0600 Subject: [PATCH 2/4] Allowed for a popup when in Lite mode --- .../src/main/java/trick/simcontrol/SimControlApplication.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java index f46f296c7..8fda0d3f4 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -656,6 +656,9 @@ protected void printErrorMessage(String err) { try { doc.insertString(doc.getLength(), err + "\n", redStyle); + if (liteButton.isSelected() || getMainFrame().getSize().height <= 400) { + JOptionPane.showMessageDialog(getMainFrame(), err, "Sim Control Panel Error", JOptionPane.ERROR_MESSAGE); + } } catch (BadLocationException ble) { JOptionPane.showMessageDialog(getMainFrame(), "Status Message Pane had an issue when printing: " + err, From b90bba5c8bc044d1b356d76fbed547a6622d42e8 Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Thu, 11 Jan 2024 10:13:39 -0600 Subject: [PATCH 3/4] Fixed the NullPointerException --- .../trick/simcontrol/SimControlApplication.java | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java index 8fda0d3f4..48a502468 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -649,13 +649,14 @@ protected void startup() { * @param err */ protected void printErrorMessage(String err) { - Document doc = statusMsgPane.getDocument(); - StyleContext sc = new StyleContext(); - Style redStyle = sc.addStyle("Red", null); - setColorStyleAttr(redStyle, Color.red, Color.black); - try { + Document doc = statusMsgPane.getDocument(); + StyleContext sc = new StyleContext(); + Style redStyle = sc.addStyle("Red", null); + + setColorStyleAttr(redStyle, Color.red, Color.black); doc.insertString(doc.getLength(), err + "\n", redStyle); + if (liteButton.isSelected() || getMainFrame().getSize().height <= 400) { JOptionPane.showMessageDialog(getMainFrame(), err, "Sim Control Panel Error", JOptionPane.ERROR_MESSAGE); } @@ -664,6 +665,8 @@ protected void printErrorMessage(String err) { "Status Message Pane had an issue when printing: " + err, "Status Message Pane Error", JOptionPane.ERROR_MESSAGE); + } catch (NullPointerException npe) { + System.err.println( "Sim Control Error at Initialization: \n" + err); } } From 8c4edf3873208d90db5db650df6b751caf4757ba Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Thu, 11 Jan 2024 11:01:14 -0600 Subject: [PATCH 4/4] Cleaning up and documenting implementation. --- .../trick/simcontrol/SimControlApplication.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java index 48a502468..2faad6ed0 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -181,6 +181,9 @@ public class SimControlApplication extends TrickApplication implements PropertyC final private static String LOCALHOST = "localhost"; + final private Dimension FULL_SIZE = new Dimension(680, 640); + final private Dimension LITE_SIZE = new Dimension(340, 360); + //======================================== // Actions //======================================== @@ -314,9 +317,9 @@ public void loadChkpnt() { @Action public void lite() { if (liteButton.isSelected()) { - getMainFrame().setSize(340, 360); + getMainFrame().setSize(LITE_SIZE); } else { - getMainFrame().setSize(680, 640); + getMainFrame().setSize(FULL_SIZE); } } @@ -650,14 +653,20 @@ protected void startup() { */ protected void printErrorMessage(String err) { try { + // Get the document attached to the Status Message Pane Document doc = statusMsgPane.getDocument(); + + // Set the font color to red and the background to black StyleContext sc = new StyleContext(); Style redStyle = sc.addStyle("Red", null); - setColorStyleAttr(redStyle, Color.red, Color.black); + + // Add the error message to the bottom of the message pane doc.insertString(doc.getLength(), err + "\n", redStyle); - if (liteButton.isSelected() || getMainFrame().getSize().height <= 400) { + // If Lite mode is engaged, or the window is small enough + // to obscure the message pane, create a popup for the error as well. + if (liteButton.isSelected() || getMainFrame().getSize().height <= LITE_SIZE.height + 50) { JOptionPane.showMessageDialog(getMainFrame(), err, "Sim Control Panel Error", JOptionPane.ERROR_MESSAGE); } } catch (BadLocationException ble) {