3737
3838/**
3939 * A Controller for HackSimulators. Executes scripts written in a special scripting language
40- * that controlls the features of the simulators.
40+ * that controls the features of the simulators.
4141 * Constructed with a GUI that enables the execution control of the script.
4242 */
4343public class HackController
@@ -56,7 +56,7 @@ public class HackController
5656 /**
5757 * The speed function for fast forward mode.
5858 */
59- public static final int [] FASTFORWARD_SPEED_FUNCTION = {500 , 1000 , 2000 , 4000 , 15000 };
59+ private static final int [] FASTFORWARD_SPEED_FUNCTION = {500 , 1000 , 2000 , 4000 , 15000 };
6060
6161 // ANIMATION MODES:
6262
@@ -127,6 +127,10 @@ public class HackController
127127 // A helper string with spaces
128128 private static final String SPACES = " " ;
129129 private static final String DIRECTORY = "directory" ;
130+ private static final String SPEED = "speed" ;
131+ private static final String ANIMATION_MODE = "animation_mode" ;
132+ private static final String NUMERIC_FORMAT = "numeric_format" ;
133+ private final Preferences preferences ;
130134
131135 // The controller's GUI
132136 protected ControllerGUI gui ;
@@ -229,6 +233,7 @@ public class HackController
229233 * The script will be executed and the final result will be printed.
230234 */
231235 public HackController (HackSimulator simulator , String scriptFileName ) {
236+ this .preferences = Preferences .userNodeForPackage (simulator .getClass ());
232237 File file = new File (scriptFileName );
233238 if (!file .exists ())
234239 displayMessage (scriptFileName + " doesn't exist" , true );
@@ -241,10 +246,8 @@ public HackController(HackSimulator simulator, String scriptFileName) {
241246 try {
242247 loadNewScript (file , false );
243248 saveWorkingDir (file );
244- } catch (ScriptException se ) {
249+ } catch (ScriptException | ControllerException se ) {
245250 displayMessage (se .getMessage (), true );
246- } catch (ControllerException ce ) {
247- displayMessage (ce .getMessage (), true );
248251 }
249252
250253 fastForwardRunning = true ;
@@ -260,6 +263,7 @@ public HackController(HackSimulator simulator, String scriptFileName) {
260263 public HackController (ControllerGUI gui , HackSimulator simulator , String defaultScriptName )
261264 throws ScriptException , ControllerException {
262265
266+ this .preferences = Preferences .userNodeForPackage (simulator .getClass ());
263267 this .gui = gui ;
264268 this .simulator = simulator ;
265269 singleStepTask = new SingleStepTask ();
@@ -276,26 +280,27 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
276280 for (int i = 0 ; i < NUMBER_OF_SPEED_UNITS ; i ++)
277281 delays [i ] = (int )(MAX_MS - SPEED_FUNCTION [i ] * (float )(MAX_MS - MIN_MS ));
278282
279- currentSpeedUnit = INITIAL_SPEED_UNIT ;
280- animationMode = simulator .getInitialAnimationMode ();
283+ currentSpeedUnit = preferences . getInt ( SPEED , INITIAL_SPEED_UNIT ) ;
284+ animationMode = preferences . getInt ( ANIMATION_MODE , simulator .getInitialAnimationMode () );
281285 simulator .setAnimationMode (animationMode );
282- simulator .setAnimationSpeed (INITIAL_SPEED_UNIT );
283- simulator .setNumericFormat (simulator .getInitialNumericFormat ());
286+ simulator .setAnimationSpeed (currentSpeedUnit );
287+ final int numericFormat = preferences .getInt (NUMERIC_FORMAT , simulator .getInitialNumericFormat ());
288+ simulator .setNumericFormat (numericFormat );
284289 timer = new Timer (delays [currentSpeedUnit - 1 ], this );
285290
286291 // adds the simulator component to the controller component
287292 gui .setSimulator (simulator .getGUI ());
288293 gui .setTitle (simulator .getName () + getVersionString ());
289294
290295 // load and set working dir
291- File file = loadWorkingDir ( );
296+ File file = new File ( preferences . get ( DIRECTORY , "." ) );
292297 simulator .setWorkingDir (file );
293298 gui .setWorkingDir (file );
294299
295300 gui .addControllerListener (this );
296301 gui .setSpeed (currentSpeedUnit );
297302 gui .setAnimationMode (animationMode );
298- gui .setNumericFormat (simulator . getInitialNumericFormat () );
303+ gui .setNumericFormat (numericFormat );
299304 gui .setAdditionalDisplay (simulator .getInitialAdditionalDisplay ());
300305 gui .setVariables (simulator .getVariables ());
301306
@@ -443,14 +448,8 @@ else if (breakpoint.isReached()) {
443448 if (breakpointReached )
444449 tempBreakpoints .clear ();
445450
446- } catch (ControllerException ce ) {
451+ } catch (ControllerException | ProgramException | CommandException | VariableException ce ) {
447452 stopWithError (ce );
448- } catch (ProgramException pe ) {
449- stopWithError (pe );
450- } catch (CommandException ce ) {
451- stopWithError (ce );
452- } catch (VariableException ve ) {
453- stopWithError (ve );
454453 }
455454
456455 singleStepLocked = false ;
@@ -736,7 +735,7 @@ private void outputAndCompare(String line) throws ControllerException {
736735 }
737736
738737 // loads the given script file and restarts the GUI.
739- protected void loadNewScript (File file , boolean displayMessage )
738+ private void loadNewScript (File file , boolean displayMessage )
740739 throws ControllerException , ScriptException {
741740 currentScriptFile = file ;
742741 script = new Script (file .getPath ());
@@ -793,6 +792,8 @@ private void setSpeed(int newSpeedUnit) {
793792 currentSpeedUnit = newSpeedUnit ;
794793 timer .setDelay (delays [currentSpeedUnit - 1 ]);
795794 simulator .setAnimationSpeed (newSpeedUnit );
795+ preferences .putInt (SPEED , newSpeedUnit );
796+ savePreferences ();
796797 }
797798
798799 // Sets the animation mode with the given one.
@@ -806,12 +807,16 @@ private void setAnimationMode(int newAnimationMode) {
806807
807808 gui .setAnimationMode (newAnimationMode );
808809 animationMode = newAnimationMode ;
810+ preferences .putInt (ANIMATION_MODE , newAnimationMode );
811+ savePreferences ();
809812 }
810813
811814 // Sets the numeric format with the given code.
812815 private void setNumericFormat (int formatCode ) {
813816 simulator .setNumericFormat (formatCode );
814817 gui .setNumericFormat (formatCode );
818+ preferences .putInt (NUMERIC_FORMAT , formatCode );
819+ savePreferences ();
815820 }
816821
817822 // Sets the additional display with the given code.
@@ -865,15 +870,8 @@ private void displayMessage(String message, boolean error) {
865870 }
866871 }
867872
868- // Returns the working dir that is saved in the data file, or "" if data file doesn't exist.
869- protected File loadWorkingDir () {
870- final Preferences preferences = Preferences .userNodeForPackage (simulator .getClass ());
871- final String dir = preferences .get (DIRECTORY , "." );
872- return new File (dir );
873- }
874-
875873 // Saves the given working dir into the data file and gui's.
876- protected void saveWorkingDir (File file ) {
874+ private void saveWorkingDir (File file ) {
877875 final File parent = file .getParentFile ();
878876
879877 if (gui != null )
@@ -883,8 +881,11 @@ protected void saveWorkingDir(File file) {
883881
884882 final File dir = file .isDirectory () ? file : parent ;
885883
886- final Preferences preferences = Preferences .userNodeForPackage (simulator .getClass ());
887884 preferences .put (DIRECTORY , dir .toString ());
885+ savePreferences ();
886+ }
887+
888+ private void savePreferences () {
888889 try {
889890 preferences .sync ();
890891 } catch (BackingStoreException ignored ) {
@@ -904,8 +905,7 @@ protected void reloadDefaultScript() {
904905 try {
905906 loadNewScript (defaultScriptFile , false );
906907 rewind ();
907- } catch (ScriptException ignored ) {
908- } catch (ControllerException ignored ) {
908+ } catch (ScriptException | ControllerException ignored ) {
909909 }
910910 }
911911 }
@@ -1061,10 +1061,7 @@ public void actionPerformed(ControllerEvent event) {
10611061 doUnknownAction (event .getAction (), event .getData ());
10621062 break ;
10631063 }
1064- } catch (ScriptException e ) {
1065- displayMessage (e .getMessage (), true );
1066- stopMode ();
1067- } catch (ControllerException e ) {
1064+ } catch (ScriptException | ControllerException e ) {
10681065 displayMessage (e .getMessage (), true );
10691066 stopMode ();
10701067 }
@@ -1077,7 +1074,7 @@ protected void doUnknownAction(byte action, Object data) throws ControllerExcept
10771074 }
10781075
10791076 // Performs the single step task
1080- class SingleStepTask implements Runnable {
1077+ private class SingleStepTask implements Runnable {
10811078
10821079 public void run () {
10831080 singleStep ();
@@ -1100,7 +1097,7 @@ public void run() {
11001097 }
11011098
11021099 // Performs the fast forward task
1103- class FastForwardTask implements Runnable {
1100+ private class FastForwardTask implements Runnable {
11041101 public synchronized void run () {
11051102 try {
11061103 System .runFinalization ();
@@ -1132,7 +1129,7 @@ public synchronized void run() {
11321129 }
11331130
11341131 // Sets the animation mode
1135- class SetAnimationModeTask implements Runnable {
1132+ private class SetAnimationModeTask implements Runnable {
11361133
11371134 private int animationMode ;
11381135
@@ -1146,7 +1143,7 @@ public void run() {
11461143 }
11471144
11481145 // Sets the numeric format
1149- class SetNumericFormatTask implements Runnable {
1146+ private class SetNumericFormatTask implements Runnable {
11501147
11511148 private int numericFormat ;
11521149
0 commit comments