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: ControlDisplay.java,v 1.3 2005/09/14 18:39:31 mat007 Exp $ 29 */ 30 31 package battlehex.ui.main; 32 33 import java.awt.BorderLayout; 34 import java.awt.Color; 35 import java.awt.Dimension; 36 import java.awt.event.ActionEvent; 37 import java.awt.event.ActionListener; 38 import java.awt.event.MouseAdapter; 39 import java.awt.event.MouseEvent; 40 import javax.swing.BorderFactory; 41 import javax.swing.Box; 42 import javax.swing.BoxLayout; 43 import javax.swing.JButton; 44 import javax.swing.JComponent; 45 import javax.swing.JLabel; 46 import javax.swing.JPanel; 47 import javax.swing.border.Border; 48 import jtge.engine.data.IContext; 49 import battlehex.model.ITurnObserver; 50 import battlehex.model.Turn; 51 52 /*** 53 * Implements the right vertical bar of the display. 54 * 55 * @author Mathieu Champlon 56 * @version $Revision: 1.3 $ $Date: 2005/09/14 18:39:31 $ 57 */ 58 public class ControlDisplay extends JPanel implements ITurnObserver 59 { 60 private static final long serialVersionUID = 3849609201160907585L; 61 private static final int VERTICAL_STRUT_SIZE = 3; 62 private static final Dimension CURRENT_TURN_DIMENSION = new Dimension( 150, 50 ); 63 private static final Dimension STATUS_DIMENSION = new Dimension( 150, 100 ); 64 private static final Dimension END_TURN_DIMENSION = new Dimension( 100, 30 ); 65 private JLabel turnLabel; 66 67 /*** 68 * Create the control panel. 69 * 70 * @param controller the controller 71 * @param context the context 72 * @param status the status component 73 * @param info the info component 74 */ 75 public ControlDisplay( final IController controller, final IContext context, final JComponent status, 76 final JComponent info ) 77 { 78 setOpaque( false ); 79 setLayout( new BorderLayout() ); 80 add( createBox( status, info, createEndTurnButton( controller ) ), BorderLayout.EAST ); 81 context.register( this ); 82 } 83 84 private Box createBox( final JComponent status, final JComponent info, final JComponent button ) 85 { 86 final Border border = BorderFactory.createLineBorder( Color.BLACK ); 87 final Box box = Box.createVerticalBox(); 88 box.addMouseListener( new NullMouseAdapter() ); 89 box.add( Box.createVerticalStrut( VERTICAL_STRUT_SIZE ) ); 90 box.add( createLabel( " Welcome to... ", border ) ); 91 box.add( Box.createVerticalStrut( VERTICAL_STRUT_SIZE ) ); 92 box.add( createLabel( " ...Battle Hex ", border ) ); 93 box.add( Box.createVerticalStrut( VERTICAL_STRUT_SIZE ) ); 94 box.add( createSubPanel( status, border ) ); 95 box.add( Box.createVerticalStrut( VERTICAL_STRUT_SIZE ) ); 96 info.setBorder( border ); 97 info.setOpaque( true ); 98 box.add( info ); 99 box.add( Box.createGlue() ); 100 button.setBorder( border ); 101 box.add( button ); 102 return box; 103 } 104 105 private class NullMouseAdapter extends MouseAdapter 106 { 107 public final void mouseClicked( final MouseEvent event ) 108 { 109 } 110 } 111 112 private JComponent createSubPanel( final JComponent status, final Border border ) 113 { 114 final JPanel result = new JPanel(); 115 result.setLayout( new BoxLayout( result, BoxLayout.Y_AXIS ) ); 116 result.add( createCurrentTurnDisplayer() ); 117 result.add( status ); 118 result.setPreferredSize( STATUS_DIMENSION ); 119 result.setMaximumSize( STATUS_DIMENSION ); 120 result.setOpaque( true ); 121 result.setBorder( border ); 122 result.addMouseListener( new NullMouseAdapter() ); 123 return result; 124 } 125 126 private JComponent createCurrentTurnDisplayer() 127 { 128 turnLabel = new JLabel( "", JLabel.CENTER ); 129 turnLabel.setPreferredSize( STATUS_DIMENSION ); 130 turnLabel.setMaximumSize( STATUS_DIMENSION ); 131 return turnLabel; 132 } 133 134 private JComponent createLabel( final String text, final Border border ) 135 { 136 final JLabel result = new JLabel( text, JLabel.CENTER ); 137 result.setPreferredSize( CURRENT_TURN_DIMENSION ); 138 result.setMaximumSize( CURRENT_TURN_DIMENSION ); 139 result.setOpaque( true ); 140 result.setBorder( border ); 141 result.addMouseListener( new NullMouseAdapter() ); 142 return result; 143 } 144 145 private JComponent createEndTurnButton( final IController controller ) 146 { 147 final JButton result = new JButton( "End Turn" ); 148 result.setPreferredSize( END_TURN_DIMENSION ); 149 result.setMaximumSize( END_TURN_DIMENSION ); 150 result.addActionListener( new ActionListener() 151 { 152 public final void actionPerformed( final ActionEvent e ) 153 { 154 controller.endTurn(); 155 } 156 } ); 157 return result; 158 } 159 160 /*** 161 * {@inheritDoc} 162 */ 163 public final void update( final Turn turn ) 164 { 165 if( turn == null ) 166 turnLabel.setText( "" ); 167 else 168 turnLabel.setText( "turn : " + turn ); 169 } 170 }