001package 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. 002 003import com.google.gwt.core.client.*; 004import votorola.a.count.gwt.*; 005import votorola.g.web.gwt.*; 006 007import static votorola.a.count.CountNode.DART_SECTOR_MAX; 008 009 010/** A group of dart-sectored count nodes. 011 */ 012public class Board 013{ 014 015 016 /** Constructs a Board. 017 */ 018 Board( VoteTrack _track ) { track = _track; } 019 020 021 022 // ------------------------------------------------------------------------------------ 023 024 025 /** Disables the board. 026 * 027 * @see #isEnabled() 028 */ 029 final void disable() 030 { 031 if( !isEnabled ) return; 032 033 mosquito = null; 034 nodes = CountNodeJS.NULL_BOARD; 035 isEnabled = false; 036 isEndBoard = false; 037 } 038 039 040 041 /** Enables the board. 042 * 043 * @param _nodes the backing for the {@linkplain #node(int) nodes}, or null if 044 * all nodes are null. 045 * 046 * @see #isEnabled() 047 * @see #isEndBoard() 048 */ 049 void enable( CountNodeJS _mosquito, JsArray<CountNodeJS> _nodes, boolean _isEndBoard ) 050 { 051 mosquito = _mosquito; 052 if( _nodes == null ) nodes = CountNodeJS.NULL_BOARD; 053 else 054 { 055 nodes = _nodes; 056 assert nodes.length() == DART_SECTOR_MAX; 057 } 058 isEnabled = true; 059 isEndBoard = _isEndBoard; 060 } 061 062 063 064 /** Answers whether this board is enabled. A disabled board is invisible and without 065 * function in the user interface. 066 * 067 * @see #disable() 068 * @see #enable(CountNodeJS,JsArray,boolean) 069 */ 070 public final boolean isEnabled() { return isEnabled; }; 071 072 073 private boolean isEnabled; 074 075 076 077 /** Answers whether the nodes of this board are base candidates. 078 */ 079 final boolean isEndBoard() { return isEndBoard; }; 080 081 082 private boolean isEndBoard; 083 084 085 086 /** A node that belongs to this board but lacks a dart sector, or null if no such node 087 * is included here. Any number of mosquitoes may belong to a board, but at most one 088 * may be included at a time. 089 */ 090 public final CountNodeJS mosquito() { return mosquito; } 091 092 093 private CountNodeJS mosquito; 094 095 096 097 /** Returns the node occupying the specified dart sector, or null if there is none. 098 */ 099 public final CountNodeJS node( final int sector ) 100 { 101 assert sector >= 1 && sector <= DART_SECTOR_MAX; 102 return nodes.get( sector - 1 ); 103 } 104 105 106 private JsArray<CountNodeJS> nodes = CountNodeJS.NULL_BOARD; 107 108 109 110 /** The logical container of this board, whence its change events are fired. 111 */ 112 final VoteTrack track() { return track; } 113 114 115 private final VoteTrack track; 116 117 118 119 // - O b j e c t ---------------------------------------------------------------------- 120 121 122 /** Returns a descripion of this geocode, including the address and its geographic 123 * coordinates. 124 */ 125 public @Override final String toString() 126 { 127 final StringBuilder b = GWTX.stringBuilderClear(); 128 b.append( "board[" ); 129 if( isEnabled ) 130 { 131 toString( 0, mosquito, b ); 132 for( int s = 1; s < DART_SECTOR_MAX; ++s ) toString( s, node(s), b ); 133 final int bN = b.length(); 134 b.delete( bN - 2, bN ); // chop trailing ", " 135 } 136 else b.append( "disabled" ); 137 b.append( ']' ); 138 return b.toString(); 139 } 140 141 142 private void toString( final int s, final CountNodeJS node, final StringBuilder b ) 143 { 144 if( node == null ) return; 145 146 b.append( s ); 147 b.append( '=' ); 148 b.append( node.name() ); 149 b.append( ", " ); 150 } 151 152 153}