From 28bece60650df6dd3ae181459966c7c496ddb46e Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Mon, 16 Sep 2024 11:44:39 -0400 Subject: [PATCH 1/3] Increased accessibility for a few members and functions --- .../simcontrol/SimControlApplication.java | 30 +++++++++---------- 1 file changed, 14 insertions(+), 16 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 b6b0ae9c..d1835abe 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -103,11 +103,24 @@ public class SimControlApplication extends TrickApplication implements PropertyC //======================================== // Public data //======================================== + final public static Dimension FULL_SIZE = new Dimension(680, 640); + final public static Dimension LITE_SIZE = new Dimension(340, 360); //======================================== // Protected data //======================================== + protected static String host; + protected static int port = -1; + + /** whether or not to print a send_hs file to the status message panel */ + protected static boolean isRestartOptionOn; + + /** whether automatically exit when sim is done/killed. */ + protected static boolean isAutoExitOn; + + /** The action controller that performs actions for such as clicking button, selection a menu item and etc. */ + protected SimControlActionController actionController; //======================================== @@ -119,9 +132,6 @@ public class SimControlApplication extends TrickApplication implements PropertyC private int overrun_present ; private int message_present ; private int message_port ; - - /** whether automatically exit when sim is done/killed. */ - private static boolean isAutoExitOn; // The panel that displays the current sim state description as well as progress. private JXTitledPanel runtimeStatePanel; @@ -154,11 +164,6 @@ public class SimControlApplication extends TrickApplication implements PropertyC private JXLabel statusLabel; - /* - * The action controller that performs actions for such as clicking button, selection a menu item and etc. - */ - private SimControlActionController actionController; - // The animation image player panel private AnimationPlayer logoImagePanel; @@ -171,9 +176,6 @@ public class SimControlApplication extends TrickApplication implements PropertyC private SocketChannel healthStatusSocketChannel ; private JComboBox runningSimList; - private static String host; - private static int port = -1; - private static boolean isRestartOptionOn; //True if an error was encountered during the attempt to connect to Variable Server during intialize() private boolean errOnInitConnect = false; //Time out when attempting to establish connection with Variable Server in milliseconds @@ -188,9 +190,6 @@ 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 //======================================== @@ -565,7 +564,6 @@ public class SimControlApplication extends TrickApplication implements PropertyC } } - //======================================== // Methods //======================================== @@ -1263,7 +1261,7 @@ public class SimControlApplication extends TrickApplication implements PropertyC /** * Updates the GUI as needed if SIM states are changed. */ - private void updateGUI() { + protected void updateGUI() { String newStatusDesc = SimState.SIM_MODE_DESCRIPTION[simState.getMode()]; recTime.setText(simState.getTwoFractionFormatted(simState.getExecOutTime())); From 66081ea7a697fb2034040c7cf9b33a7db6e256cc Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Mon, 16 Sep 2024 11:46:53 -0400 Subject: [PATCH 2/3] Reorganized Connect function to shorten it and increase readability --- .../simcontrol/SimControlApplication.java | 89 +++++++++++-------- 1 file changed, 50 insertions(+), 39 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 d1835abe..0f4741d3 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -347,7 +347,52 @@ public class SimControlApplication extends TrickApplication implements PropertyC */ @Action public void connect() { - // get host and port for selected sim + parseConnectionFromSimList(); // get host and port from running sim list + getInitializationPacket(); // init variable server connection + if (commandSimcom == null) { + String errMsg = "Sorry, can't connect. Please make sure the availability of both server and port!"; + printErrorMessage(errMsg); + return; + } + setEnabledAllActions(true); // enable all actions + scheduleGetSimState(); // set up the sim status variables + startStatusMonitors(); // start monitors for sim and health status + } + + + /** + * Helper method for starting monitors for sim status as well as health status. + */ + protected void startStatusMonitors() { + MonitorSimStatusTask monitorSimStatusTask = new MonitorSimStatusTask(this); + monitorSimStatusTask.addPropertyChangeListener(this); + getContext().getTaskService().execute(monitorSimStatusTask); + + // For receiving hs messages. + getContext().getTaskService().execute(new MonitorHealthStatusTask(this)); + } + + /** + * Sets all actions as either enabled or disabled + * @param isEnabled the state to set each action as + */ + protected void setEnabledAllActions(boolean isEnabled) { + if(actionMap == null) + return; + + Object[] keys = actionMap.allKeys(); + // If there is a server connection established, enable all actions. + for (int i = 0; i < keys.length; i++) { + String theKey = (String)keys[i]; + getAction(theKey).setEnabled(isEnabled); + } + } + + /** + * Parse the host and port from the runningSimList object + */ + protected void parseConnectionFromSimList() { + if (runningSimList != null && runningSimList.getSelectedItem() != null) { String selectedStr = runningSimList.getSelectedItem().toString(); // remove the run info if it is shown @@ -365,49 +410,15 @@ public class SimControlApplication extends TrickApplication implements PropertyC 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; - } + } else { host = elements[0].trim(); - try { - port = Integer.parseInt(elements[1].trim()); - } catch (NumberFormatException nfe) { + try { port = Integer.parseInt(elements[1].trim()); } + catch (NumberFormatException nfe) { String errMsg = elements[1] + " is not a valid port number!"; printErrorMessage(errMsg); - return; - } + } } - - getInitializationPacket(); - - if (commandSimcom == null) { - String errMsg = "Sorry, can't connect. Please make sure the availability of both server and port!"; - printErrorMessage(errMsg); - return; - } else { - Object[] keys = actionMap.allKeys(); - // If there is a server connection established, enable all actions. - for (int i = 0; i < keys.length; i++) { - String theKey = (String)keys[i]; - getAction(theKey).setEnabled(true); - } } - - scheduleGetSimState(); - - startStatusMonitors(); - } - - - /** - * Helper method for starting monitors for sim status as well as health status. - */ - private void startStatusMonitors() { - MonitorSimStatusTask monitorSimStatusTask = new MonitorSimStatusTask(this); - monitorSimStatusTask.addPropertyChangeListener(this); - getContext().getTaskService().execute(monitorSimStatusTask); - - // For receiving hs messages. - getContext().getTaskService().execute(new MonitorHealthStatusTask(this)); } //======================================== From f34be562e9869173e3068095995b7bf6cb0ec8f9 Mon Sep 17 00:00:00 2001 From: Marcus Rockwell Date: Mon, 16 Sep 2024 11:49:33 -0400 Subject: [PATCH 3/3] Added input validation for enabling/disabling GUIs and actions --- .../simcontrol/SimControlApplication.java | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 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 0f4741d3..94fe771d 100644 --- a/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java +++ b/trick_source/java/src/main/java/trick/simcontrol/SimControlApplication.java @@ -9,6 +9,7 @@ package trick.simcontrol; //======================================== import java.awt.BorderLayout; import java.awt.Color; +import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; @@ -393,7 +394,7 @@ public class SimControlApplication extends TrickApplication implements PropertyC */ protected void parseConnectionFromSimList() { - if (runningSimList != null && runningSimList.getSelectedItem() != null) { + if (runningSimList != null && runningSimList.getSelectedItem() != null) { String selectedStr = runningSimList.getSelectedItem().toString(); // remove the run info if it is shown int leftPre = selectedStr.indexOf("("); @@ -411,16 +412,16 @@ public class SimControlApplication extends TrickApplication implements PropertyC String errMsg = "Can't connect! Please provide valid host name and port number separated by : or whitespace!"; printErrorMessage(errMsg); } else { - host = elements[0].trim(); + host = elements[0].trim(); try { port = Integer.parseInt(elements[1].trim()); } catch (NumberFormatException nfe) { - String errMsg = elements[1] + " is not a valid port number!"; - printErrorMessage(errMsg); + String errMsg = elements[1] + " is not a valid port number!"; + printErrorMessage(errMsg); } - } + } } } - + //======================================== // Set/Get methods //======================================== @@ -465,7 +466,7 @@ public class SimControlApplication extends TrickApplication implements PropertyC (new RetrieveHostPortTask()).execute(); return; } - + actionController.setVariableServerConnection(commandSimcom); simState = new SimState(); @@ -849,9 +850,8 @@ public class SimControlApplication extends TrickApplication implements PropertyC statusSimcom.put(status_vars) ; statusSimcom.put("trick.var_cycle(0.25)\n"); - - getAction("connect").setEnabled(false); - runningSimList.setEnabled(false); + setActionsEnabled("connect", false); + setGUIEnabled(runningSimList, false); } catch (NumberFormatException nfe) { @@ -866,6 +866,14 @@ public class SimControlApplication extends TrickApplication implements PropertyC } } + private void setGUIEnabled(Component gui, boolean flag) { + if(gui != null) gui.setEnabled(flag); + } + + private void setGUIsEnabled(Component[] guis, boolean flag) { + for(Component gui : guis) setGUIEnabled(gui, flag); + } + /** * Convenient method for setting the state of specified actions. *