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 );
@@ -260,6 +265,7 @@ public HackController(HackSimulator simulator, String scriptFileName) {
260265 public HackController (ControllerGUI gui , HackSimulator simulator , String defaultScriptName )
261266 throws ScriptException , ControllerException {
262267
268+ this .preferences = Preferences .userNodeForPackage (simulator .getClass ());
263269 this .gui = gui ;
264270 this .simulator = simulator ;
265271 singleStepTask = new SingleStepTask ();
@@ -276,26 +282,27 @@ public HackController(ControllerGUI gui, HackSimulator simulator, String default
276282 for (int i = 0 ; i < NUMBER_OF_SPEED_UNITS ; i ++)
277283 delays [i ] = (int )(MAX_MS - SPEED_FUNCTION [i ] * (float )(MAX_MS - MIN_MS ));
278284
279- currentSpeedUnit = INITIAL_SPEED_UNIT ;
280- animationMode = simulator .getInitialAnimationMode ();
285+ currentSpeedUnit = preferences . getInt ( SPEED , INITIAL_SPEED_UNIT ) ;
286+ animationMode = preferences . getInt ( ANIMATION_MODE , simulator .getInitialAnimationMode () );
281287 simulator .setAnimationMode (animationMode );
282- simulator .setAnimationSpeed (INITIAL_SPEED_UNIT );
283- simulator .setNumericFormat (simulator .getInitialNumericFormat ());
288+ simulator .setAnimationSpeed (currentSpeedUnit );
289+ final int numericFormat = preferences .getInt (NUMERIC_FORMAT , simulator .getInitialNumericFormat ());
290+ simulator .setNumericFormat (numericFormat );
284291 timer = new Timer (delays [currentSpeedUnit - 1 ], this );
285292
286293 // adds the simulator component to the controller component
287294 gui .setSimulator (simulator .getGUI ());
288295 gui .setTitle (simulator .getName () + getVersionString ());
289296
290297 // load and set working dir
291- File file = loadWorkingDir ( );
298+ File file = new File ( preferences . get ( DIRECTORY , "." ) );
292299 simulator .setWorkingDir (file );
293300 gui .setWorkingDir (file );
294301
295302 gui .addControllerListener (this );
296303 gui .setSpeed (currentSpeedUnit );
297304 gui .setAnimationMode (animationMode );
298- gui .setNumericFormat (simulator . getInitialNumericFormat () );
305+ gui .setNumericFormat (numericFormat );
299306 gui .setAdditionalDisplay (simulator .getInitialAdditionalDisplay ());
300307 gui .setVariables (simulator .getVariables ());
301308
@@ -736,7 +743,7 @@ private void outputAndCompare(String line) throws ControllerException {
736743 }
737744
738745 // loads the given script file and restarts the GUI.
739- protected void loadNewScript (File file , boolean displayMessage )
746+ private void loadNewScript (File file , boolean displayMessage )
740747 throws ControllerException , ScriptException {
741748 currentScriptFile = file ;
742749 script = new Script (file .getPath ());
@@ -793,6 +800,8 @@ private void setSpeed(int newSpeedUnit) {
793800 currentSpeedUnit = newSpeedUnit ;
794801 timer .setDelay (delays [currentSpeedUnit - 1 ]);
795802 simulator .setAnimationSpeed (newSpeedUnit );
803+ preferences .putInt (SPEED , newSpeedUnit );
804+ savePreferences ();
796805 }
797806
798807 // Sets the animation mode with the given one.
@@ -806,12 +815,16 @@ private void setAnimationMode(int newAnimationMode) {
806815
807816 gui .setAnimationMode (newAnimationMode );
808817 animationMode = newAnimationMode ;
818+ preferences .putInt (ANIMATION_MODE , newAnimationMode );
819+ savePreferences ();
809820 }
810821
811822 // Sets the numeric format with the given code.
812823 private void setNumericFormat (int formatCode ) {
813824 simulator .setNumericFormat (formatCode );
814825 gui .setNumericFormat (formatCode );
826+ preferences .putInt (NUMERIC_FORMAT , formatCode );
827+ savePreferences ();
815828 }
816829
817830 // Sets the additional display with the given code.
@@ -865,15 +878,8 @@ private void displayMessage(String message, boolean error) {
865878 }
866879 }
867880
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-
875881 // Saves the given working dir into the data file and gui's.
876- protected void saveWorkingDir (File file ) {
882+ private void saveWorkingDir (File file ) {
877883 final File parent = file .getParentFile ();
878884
879885 if (gui != null )
@@ -883,8 +889,11 @@ protected void saveWorkingDir(File file) {
883889
884890 final File dir = file .isDirectory () ? file : parent ;
885891
886- final Preferences preferences = Preferences .userNodeForPackage (simulator .getClass ());
887892 preferences .put (DIRECTORY , dir .toString ());
893+ savePreferences ();
894+ }
895+
896+ private void savePreferences () {
888897 try {
889898 preferences .sync ();
890899 } catch (BackingStoreException ignored ) {
@@ -1077,7 +1086,7 @@ protected void doUnknownAction(byte action, Object data) throws ControllerExcept
10771086 }
10781087
10791088 // Performs the single step task
1080- class SingleStepTask implements Runnable {
1089+ private class SingleStepTask implements Runnable {
10811090
10821091 public void run () {
10831092 singleStep ();
@@ -1100,7 +1109,7 @@ public void run() {
11001109 }
11011110
11021111 // Performs the fast forward task
1103- class FastForwardTask implements Runnable {
1112+ private class FastForwardTask implements Runnable {
11041113 public synchronized void run () {
11051114 try {
11061115 System .runFinalization ();
@@ -1132,7 +1141,7 @@ public synchronized void run() {
11321141 }
11331142
11341143 // Sets the animation mode
1135- class SetAnimationModeTask implements Runnable {
1144+ private class SetAnimationModeTask implements Runnable {
11361145
11371146 private int animationMode ;
11381147
@@ -1146,7 +1155,7 @@ public void run() {
11461155 }
11471156
11481157 // Sets the numeric format
1149- class SetNumericFormatTask implements Runnable {
1158+ private class SetNumericFormatTask implements Runnable {
11501159
11511160 private int numericFormat ;
11521161
0 commit comments