package votorola.s.gwt.stage.vote; // Copyright 2012, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import com.google.gwt.core.client.*; import votorola.a.count.gwt.*; import votorola.g.web.gwt.*; import static votorola.a.count.CountNode.DART_SECTOR_MAX; /** A group of dart-sectored count nodes. */ public class Board { /** Constructs a Board. */ Board( VoteTrack _track ) { track = _track; } // ------------------------------------------------------------------------------------ /** Disables the board. * * @see #isEnabled() */ final void disable() { if( !isEnabled ) return; mosquito = null; nodes = CountNodeJS.NULL_BOARD; isEnabled = false; isEndBoard = false; } /** Enables the board. * * @param _nodes the backing for the {@linkplain #node(int) nodes}, or null if * all nodes are null. * * @see #isEnabled() * @see #isEndBoard() */ void enable( CountNodeJS _mosquito, JsArray _nodes, boolean _isEndBoard ) { mosquito = _mosquito; if( _nodes == null ) nodes = CountNodeJS.NULL_BOARD; else { nodes = _nodes; assert nodes.length() == DART_SECTOR_MAX; } isEnabled = true; isEndBoard = _isEndBoard; } /** Answers whether this board is enabled. A disabled board is invisible and without * function in the user interface. * * @see #disable() * @see #enable(CountNodeJS,JsArray,boolean) */ public final boolean isEnabled() { return isEnabled; }; private boolean isEnabled; /** Answers whether the nodes of this board are base candidates. */ final boolean isEndBoard() { return isEndBoard; }; private boolean isEndBoard; /** A node that belongs to this board but lacks a dart sector, or null if no such node * is included here. Any number of mosquitoes may belong to a board, but at most one * may be included at a time. */ public final CountNodeJS mosquito() { return mosquito; } private CountNodeJS mosquito; /** Returns the node occupying the specified dart sector, or null if there is none. */ public final CountNodeJS node( final int sector ) { assert sector >= 1 && sector <= DART_SECTOR_MAX; return nodes.get( sector - 1 ); } private JsArray nodes = CountNodeJS.NULL_BOARD; /** The logical container of this board, whence its change events are fired. */ final VoteTrack track() { return track; } private final VoteTrack track; // - O b j e c t ---------------------------------------------------------------------- /** Returns a descripion of this geocode, including the address and its geographic * coordinates. */ public @Override final String toString() { final StringBuilder b = GWTX.stringBuilderClear(); b.append( "board[" ); if( isEnabled ) { toString( 0, mosquito, b ); for( int s = 1; s < DART_SECTOR_MAX; ++s ) toString( s, node(s), b ); final int bN = b.length(); b.delete( bN - 2, bN ); // chop trailing ", " } else b.append( "disabled" ); b.append( ']' ); return b.toString(); } private void toString( final int s, final CountNodeJS node, final StringBuilder b ) { if( node == null ) return; b.append( s ); b.append( '=' ); b.append( node.name() ); b.append( ", " ); } }