View Javadoc

1   /***
2    * Redistribution  and use  in source  and binary  forms, with  or without
3    * modification, are permitted provided  that the following conditions are
4    * met :
5    *
6    * . Redistributions  of  source  code  must  retain  the  above copyright
7    *   notice, this list of conditions and the following disclaimer.
8    *
9    * . Redistributions in  binary form  must reproduce  the above  copyright
10   *   notice, this list of conditions  and the following disclaimer in  the
11   *   documentation and/or other materials provided with the distribution.
12   *
13   * . The name of the author may not be used to endorse or promote products
14   *   derived from this software without specific prior written permission.
15   *
16   * THIS SOFTWARE IS  PROVIDED BY THE  AUTHOR ``AS IS''  AND ANY EXPRESS  OR
17   * IMPLIED  WARRANTIES,  INCLUDING,  BUT   NOT  LIMITED  TO,  THE   IMPLIED
18   * WARRANTIES OF MERCHANTABILITY AND  FITNESS FOR A PARTICULAR  PURPOSE ARE
19   * DISCLAIMED.  IN NO  EVENT SHALL  THE AUTHOR  BE LIABLE  FOR ANY  DIRECT,
20   * INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR  CONSEQUENTIAL  DAMAGES
21   * (INCLUDING,  BUT  NOT LIMITED  TO,  PROCUREMENT OF  SUBSTITUTE  GOODS OR
22   * SERVICES;  LOSS  OF USE,  DATA,  OR PROFITS;  OR  BUSINESS INTERRUPTION)
23   * HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN  CONTRACT,
24   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25   * ANY  WAY  OUT OF  THE  USE OF  THIS  SOFTWARE, EVEN  IF  ADVISED OF  THE
26   * POSSIBILITY OF SUCH DAMAGE.
27   *
28   * $Id: TerritoryInfoDisplayer.java,v 1.4 2005/09/14 18:39:31 mat007 Exp $
29   */
30  
31  package battlehex.ui.main;
32  
33  import java.awt.Dimension;
34  import javax.swing.JLabel;
35  import jtge.engine.data.IContext;
36  import jtge.util.grid.IGrid;
37  import battlehex.model.IGridObserver;
38  import battlehex.model.map.ITerritory;
39  import battlehex.model.map.ITerritoryVisitor;
40  
41  /***
42   * Displays territory information.
43   *
44   * @author Mathieu Champlon
45   * @version $Revision: 1.4 $ $Date: 2005/09/14 18:39:31 $
46   */
47  public class TerritoryInfoDisplayer extends JLabel implements ISelection, IGridObserver
48  {
49      private static final long serialVersionUID = 3683973995326991972L;
50      private static final Dimension SIZE = new Dimension( 150, 120 );
51      private ITerritory territory;
52  
53      /***
54       * Create a component displaying territory information.
55       *
56       * @param controller the controller
57       * @param context the context
58       */
59      public TerritoryInfoDisplayer( final IController controller, final IContext context )
60      {
61          super( "", JLabel.CENTER );
62          if( controller == null )
63              throw new IllegalArgumentException( "argument 'controller' is null" );
64          if( context == null )
65              throw new IllegalArgumentException( "argument 'model' is null" );
66          setPreferredSize( SIZE );
67          setMaximumSize( SIZE );
68          setOpaque( false );
69          controller.register( this );
70          context.register( this );
71      }
72  
73      /***
74       * {@inheritDoc}
75       */
76      public final void select( final IGrid grid, final ITerritory territory )
77      {
78          this.territory = null;
79          if( grid == null )
80              setText( "" );
81          else
82          {
83              if( territory == null )
84                  setText( "select a territory" );
85              else
86              {
87                  this.territory = territory;
88                  setText( format( territory ) );
89              }
90          }
91      }
92  
93      private String format( final ITerritory territory )
94      {
95          final StringBuffer result = new StringBuffer();
96          territory.accept( new ITerritoryVisitor()
97          {
98              public void visit( final int savings, final int income, final int wages, final int money )
99              {
100                 result.append( "<html>" );
101                 result.append( "<table>" );
102                 buildRow( result, "savings", savings );
103                 buildRow( result, "income", income );
104                 buildRow( result, "wages", wages );
105                 buildRow( result, "balance", savings + income - wages );
106                 buildRow( result, "money", money );
107                 result.append( "</table>" );
108                 result.append( "</html>" );
109             }
110 
111             private void buildRow( final StringBuffer result, final String name, final int value )
112             {
113                 result.append( "<tr>" );
114                 result.append( "<td>" );
115                 result.append( name );
116                 result.append( "</td>" );
117                 result.append( "<td>" );
118                 result.append( value );
119                 result.append( "</td>" );
120                 result.append( "</tr>" );
121             }
122         } );
123         return result.toString();
124     }
125 
126     /***
127      * {@inheritDoc}
128      */
129     public final void update( final IGrid grid )
130     {
131         select( grid, territory ); // FIXME should reset this.territory if the grid is new
132     }
133 }