Class ChessClock

All Implemented Interfaces:
ActionListener, EventListener, AutoConfigurable, Buildable, Configurable, GameComponent, PropertyNameSource, CommandEncoder, ValidityChecker, Translatable, ImageSearchTarget, SearchTarget

public class ChessClock extends AbstractConfigurable implements CommandEncoder, GameComponent, ActionListener
CHESS CLOCK class for VASSAL. The ChessClock class itself implements a single timer, which is added as a button to the Module toolbar. Clicking the button starts and stops the clock, which we attempt to keep (roughly) in sync across multiple players' computers. A pair (or more, or I suppose only one) of them are then added to ChessClockControl.
Author:
Brian Reynolds and Michael Kiefte Class originally created by Michael Kiefte for Twilight Struggle, including handshaking for verification of time totals between online machines. Made more generically configurable, ChessClockControl added, and integrated into standard VASSAL, by Brian Reynolds.
  • Field Details

  • Constructor Details

    • ChessClock

      public ChessClock(String side)
    • ChessClock

      public ChessClock()
  • Method Details

    • getSide

      public String getSide()
      Returns:
      side this clock is assigned to
    • isTicking

      public boolean isTicking()
      Returns:
      true if this clock is running
    • getElapsed

      public long getElapsed()
      Returns:
      elapsed time from local perspective
    • getVerified

      public long getVerified()
      Returns:
      elapsed time verified by handshake (or fiat, depending on situation)
    • isReferee

      protected static boolean isReferee(String name)
      Parameters:
      name - side to check if a referee
      Returns:
      True if passed side is a referee
    • startTimer

      protected void startTimer()
      Make sure the static millisecond timer is actually running (unless it hasn't even been initialized yet)
    • updateTimerColor

      protected void updateTimerColor()
      Updates the color of this chess clock's button/display. Non-ticking clock uses "default" button look. Ticking clock uses special background and pulsing foreground.
    • hideClock

      public void hideClock()
      Hides this clock
    • showClock

      public void showClock()
      Shows this clock
    • updateDisplay

      public void updateDisplay()
      Updates the clock display for this clock (1) Checks new elapsed value from millisecond timer. (2) Calls setTimerButton to clock time (text) on the button (3) Updates timer button colors if appropriate
    • setTimerButton

      public boolean setTimerButton()
      Updates clock text on the button, based on our configured style
      Returns:
      half second tick-tock pulse on/off
    • getName

      public String getName()
    • addTo

      public void addTo(Buildable parent)
      Registers us with game module, command encoder, and the Chess Clock Control.
      Specified by:
      addTo in interface Buildable
      Parameters:
      parent - Should be ChessClockControl
    • addToToolbar

      public void addToToolbar()
      Adds our clock button to the Module's toolbar
    • removeFrom

      public void removeFrom(Buildable parent)
      Unregisters us when we are shutting down
      Specified by:
      removeFrom in interface Configurable
      Parameters:
      parent - Should be ChessClockControl
    • getAttributeNames

      public String[] getAttributeNames()
      Description copied from class: AbstractBuildable
      Lists all the buildFile (XML) attribute names for this component. If this component is ALSO an AbstractConfigurable, then this list of attributes determines the appropriate attribute order for AbstractConfigurable.getAttributeDescriptions() and AbstractConfigurable.getAttributeTypes().
      Specified by:
      getAttributeNames in interface AutoConfigurable
      Specified by:
      getAttributeNames in class AbstractBuildable
      Returns:
      Key names for our attributes from the buildFile (XML) definition.
    • getAttributeDescriptions

      public String[] getAttributeDescriptions()
      Description copied from interface: AutoConfigurable
      Return an array of Strings describing the attributes of this object. These strings are used as prompts in the Properties window for this object.
      Specified by:
      getAttributeDescriptions in interface AutoConfigurable
      Specified by:
      getAttributeDescriptions in class AbstractConfigurable
      Returns:
      Descriptions for our buildFile (XML) attributes. These appear when our component is configured in the Editor window.
    • getAttributeTypes

      public Class<?>[] getAttributeTypes()
      Description copied from interface: AutoConfigurable
      Return the Class for the attributes of this object. Valid classes are: String, Integer, Double, Boolean, Image, File, Color, and KeyStroke
      Specified by:
      getAttributeTypes in interface AutoConfigurable
      Specified by:
      getAttributeTypes in class AbstractConfigurable
      Returns:
      Class types for configuring each of our buildFile (XML) attributes. Specifies which flavor of configurer to uses.
    • setAttribute

      public void setAttribute(String key, Object value)
      Sets the value of one of this component's XML attributes
      Specified by:
      setAttribute in interface AutoConfigurable
      Specified by:
      setAttribute in interface Translatable
      Specified by:
      setAttribute in class AbstractBuildable
      Parameters:
      key - the name of the attribute. Will be one of those listed in getAttributeNames()
      value - New value for the attribute. Can be either String version or the actual Object.
    • getAttributeValueString

      public String getAttributeValueString(String key)
      Gets the value of one of this component's XML attributes
      Specified by:
      getAttributeValueString in interface AutoConfigurable
      Specified by:
      getAttributeValueString in interface Translatable
      Specified by:
      getAttributeValueString in class AbstractBuildable
      Parameters:
      key - the name of the attribute. Will be one of those listed in getAttributeNames()
      Returns:
      String value of the attribute.
    • getHelpFile

      public HelpFile getHelpFile()
      Specified by:
      getHelpFile in interface Configurable
      Returns:
      The help file for this component. Used when user clicks "Help" button while configuring the component in the Editor.
    • getConfigureTypeName

      public static String getConfigureTypeName()
      Returns:
      Configure Tree name for component. Appears in [..] in module editor.
    • getAllowableConfigureComponents

      public Class[] getAllowableConfigureComponents()
      Specified by:
      getAllowableConfigureComponents in interface Configurable
      Returns:
      Array of subcomponent types that can be added to this component
    • decode

      public Command decode(String command)
      Deserializes our command from a string version, if the command belongs to us.
      Specified by:
      decode in interface CommandEncoder
      Parameters:
      command - Serialized string command
      Returns:
      An ChessClock.UpdateTimerCommand
    • encode

      public String encode(Command c)
      Serializes our command into a string, if it belongs to us
      Specified by:
      encode in interface CommandEncoder
      Parameters:
      c - Command to serialize. Only serialized if it's an UpdateTimerCommand.
      Returns:
      Serialized command, or null if command passed wasn't an UpdateTimerCommand.
    • setup

      public void setup(boolean gameStarting)
      Detect when game is starting.
      Specified by:
      setup in interface GameComponent
      Parameters:
      gameStarting - if true, a game is starting. If false, then a game is ending
    • getRestoreCommand

      public Command getRestoreCommand()
      Description copied from interface: GameComponent
      When saving a game, each GameComponent should return a Command that, when executed, restores the GameComponent to its state when the game was saved If this component has no persistent state, return null
      Specified by:
      getRestoreCommand in interface GameComponent
      Returns:
      Our command for restoring from a saved game (or adding an online player)
    • actionPerformed

      public void actionPerformed(ActionEvent evt)
      This processes our timer updates every 100ms
      Specified by:
      actionPerformed in interface ActionListener
      Parameters:
      evt - Timer event
    • updateState

      public Command updateState(boolean ticking)
      Creates a command to update the clock and/or turn it on/off.
      Parameters:
      ticking - If true, the clock will be set to "running". If false the clock will be set to "stopped".
      Returns:
      a command to communicate the status of this clock to others
    • resetState

      public Command resetState()
      Creates a command to reset the clock and turn it off
      Parameters:
      ticking - If true, the clock will be set to "running". If false the clock will be set to "stopped".
      Returns:
      a command to communicate the status of this clock to others