| C:\upload\java\Life\source\java\HandleButtons.java |
/******************************************************************* This class is a "medium" class handling when buttons appear and disappear, and their cursors. It *is* long and bothersome enough that the writer decided to give it its own class for modularity. ******************************************************************** */ import java.awt.*; final class HandleButtons implements Strings { /* The buttons we handle follow. These have the exact same names as their correspondents in the "master" class < Life.java >. */ // Buttons: private static Button keepGoingButton; /* A general-purpose button, "Keep going." */ private static Button editGameButton; /* For when the user wishes to change the game. */ private static Button quitGameButton; // Quit the current game? // And "was this button clicked?" booleans: private static boolean continueButtonClicked; /* Set "true" when the user clicks the < keepGoingButton > above. */ private static boolean editGameButtonClicked; /* Set "true" when the user wants to edit that game, i.e., when < editGameButton > above is clicked, and we change who is alive and/or dead. */ private static boolean quitButtonClicked; // Set "true" when the user // wishes to end the game. private Cursor handCursor; private Cursor defaultCursor; // Constructor follows: HandleButtons() { continueButtonClicked = false; // Booleans. editGameButtonClicked = false; quitButtonClicked = false; // Instantiate buttons keepGoingButton = new Button(CONTINUE); /* A general-purpose button for things like "keep going, next?" */ editGameButton = new Button(EDITGAME); /* For if the user wishes to change the game. */ quitGameButton = new Button(QUIT_GAME); // Quit this game? // Define the cursors handCursor = new Cursor(Cursor.HAND_CURSOR); // "Active" cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR); // "Inactive" cursor } /******************************************************************* The next sets what the buttons should do before starting the game, but after initial decisions have been made. These choices are unfortunately bothersome. We stress: this sets buttons right after the user has chosen how to play, but before the second generation has been computed. ******************************************************************** */ protected final void SetWhichButtonsAreVisibleBeforeSettingBoard(Vars vars) { String errorString = (vars != null) ? NULL_STRING : "vars = " + vars; if (!errorString.equals(NULL_STRING)) { System.out.println("Error, null pointer, HandleButtons " + errorString); return; } // The "Quit game" button is always visible & active. ActivateQuitGameButton(); /* We must set which buttons are visible and activated. This is an unfortunately bothersome choice. If we input the board by clicking on "alive" and "dead" cells, 1) "Continue" button is always set (have to get out and to the game!), and ... 2) The "Edit game" button is disabled -- we ARE editing the game. */ if (vars.GetClickLiveCellsViaMouse()) { ActivateKeepGoingButton(); DeactivateEditGameButton(); } /* Otherwise we input the board probalistically, and this is more complex. 1) We have the "edit game" button regardless. 2) If there is a time delay, we do not have the "Continue" button. 3) If we go from generation to generation by clicking a button, we must have the "Continue" button. */ else { setEditButtonCheckTimeDelay(vars); // Same code in two places, a // routine. } // End "set which buttons are visible" block. } /******************************************************************* The next activates buttons after they have been changed to what they should be for this game. This is NOT called at outset, as these decisions are handled in previous routine < SetWhichButtonsAreVisibleBeforeSettingBoard >. ******************************************************************** */ protected final void SetButtonsForThisGame(Vars vars) { /* Now: what cursor we set depends upon what is happening. If we cycle via button presses (not a time delay), the button is activated. If we cycle automatically (a time delay), the button is nulled out. */ setEditButtonCheckTimeDelay(vars); // Same code in two places, a // subroutine. ActivateQuitGameButton(); } /******************************************************************* Next is solely because writer found exact same code in two routines. ******************************************************************** */ protected void setEditButtonCheckTimeDelay(Vars vars) { ActivateEditGameButton(); if (vars.GetWaitForTimeDelay()) // Time delay. Don't need { // "keep going" button. DeactivateKeepGoingButton(); } else { // User "clicks" when to go to next ActivateKeepGoingButton(); // generation. Activate. } } /******************************************************************* Next hides all buttons ******************************************************************** */ protected final void HideButtons() { DeactivateQuitGameButton(); DeactivateKeepGoingButton(); DeactivateEditGameButton(); } /* Short utility routines follow. These activate and deactivate buttons. */ /******************************************************************* The next activates the "Continue" button." ******************************************************************** */ protected final void ActivateKeepGoingButton() { keepGoingButton.setVisible(true); keepGoingButton.setCursor(handCursor); } /******************************************************************* The next de-activates the "Continue" button." ******************************************************************** */ protected final void DeactivateKeepGoingButton() { keepGoingButton.setVisible(false); keepGoingButton.setCursor(handCursor); } /******************************************************************* The next activates the "edit game" button." ******************************************************************** */ protected final void ActivateEditGameButton() { editGameButton.setVisible(true); editGameButton.setCursor(handCursor); } /******************************************************************* The next de-activates the "edit game" button." ******************************************************************** */ protected final void DeactivateEditGameButton() { editGameButton.setCursor(defaultCursor); // Nullify the cursor. editGameButton.setVisible(false); // Make invisible. } /******************************************************************* The next activates the "quit game" button." ******************************************************************** */ protected final void ActivateQuitGameButton() { quitGameButton.setVisible(true); quitGameButton.setCursor(handCursor); } /******************************************************************* The next de-activates the "quit game" button." ******************************************************************** */ protected final void DeactivateQuitGameButton() { quitGameButton.setCursor(defaultCursor); quitGameButton.setVisible(false); } // "Keep going" button: protected final void SetKeepGoingButton(Button keepGoingButton) { this.keepGoingButton = keepGoingButton; } protected final Button GetKeepGoingButton() { return keepGoingButton; } // "Edit game" button: protected final void SetEditGameButton(Button editGameButton) { this.editGameButton = editGameButton; } protected final Button GetEditGameButton() { return editGameButton; } // "Quit game" button: protected final void SetQuitGameButton(Button quitGameButton) { this.quitGameButton = quitGameButton; } protected final Button GetQuitGameButton() { return quitGameButton; } // Booleans follow: // Continue Button Clicked, *tf* = "True" or "False": protected final void SetContinueButtonClicked(boolean tf) { this.continueButtonClicked = tf; } protected final boolean GetContinueButtonClicked() { return continueButtonClicked; } // Continue Button Clicked *tf* = "True" or "False":: protected final void SetEditGameButtonClicked(boolean tf) { this.editGameButtonClicked = tf; } protected final boolean GetEditGameButtonClicked() { return editGameButtonClicked; } // Quit Button Clicked, *tf* = "True" or "False":: protected final void SetQuitButtonClicked(boolean tf) { this.quitButtonClicked = tf; } protected final boolean GetQuitButtonClicked() { return quitButtonClicked; } protected final boolean QuitOrEditButtonClicked() { return quitButtonClicked || editGameButtonClicked; } } // End class HandleButtons