View Javadoc

1   /*
2   @See License.txt@
3    */
4   
5   package spellcast.ui;
6   
7   import java.awt.Color;
8   import java.awt.Component;
9   import java.awt.GridBagConstraints;
10  import java.awt.GridBagLayout;
11  import java.beans.PropertyChangeEvent;
12  import java.util.ArrayList;
13  
14  import javax.swing.DefaultListModel;
15  import javax.swing.JLabel;
16  import javax.swing.JList;
17  import javax.swing.JPanel;
18  import javax.swing.SwingConstants;
19  import javax.swing.UIManager;
20  
21  import spellcast.beings.IWizard;
22  import spellcast.beings.Monster;
23  import spellcast.beings.Wizard;
24  import spellcast.gestures.Gesture;
25  import spellcast.gestures.GestureFactory;
26  
27  /***
28   * This component displays the wizards details.  This includes gesture history,
29   * current gestures, what effects the wizard, what monsters they control.
30   * Only if this component is for the client are the GestureButtons enabled.
31   *
32   * @author Barrie Treloar
33   */
34  public class WizardDetails
35      extends JPanel
36  {
37      private static final int MAX_GESTURE_HISTORY = 7;
38  
39      private IWizard wizard;
40  
41      private JLabel title;
42      private JLabel isReadyLabel;
43      private DefaultListModel leftHandHistory = new DefaultListModel();
44      private DefaultListModel rightHandHistory = new DefaultListModel();
45      private JList leftHandHistoryList = new JList( leftHandHistory );
46      private JList rightHandHistoryList = new JList( rightHandHistory );
47      private GestureButton leftHandGesture = new GestureButton( GestureIcon.LEFT_HANDED );
48      private GestureButton rightHandGesture = new GestureButton( GestureIcon.RIGHT_HANDED );
49      private JLabel hitPoints;
50      private JLabel effects;
51      private JLabel monsterListLabel;
52      private ArrayList monsterList = new ArrayList( 1 );
53      private Color readyColor;
54      private Color notReadyColor;
55      private boolean client;
56  
57      private GridBagLayout gridbag = new GridBagLayout();
58  
59      public WizardDetails( IWizard wizard )
60      {
61          super();
62          setLayout( gridbag );
63          this.wizard = wizard;
64          installDefaults();
65          createTitle();
66          createHistoryLists();
67          createGestureButtons();
68          createHitPoints();
69          createEffects();
70          createMonsterList();
71      }
72  
73      protected void installDefaults()
74      {
75          readyColor = (Color) UIManager.getDefaults().get( "WizardDetails.readyColor" );
76          notReadyColor = (Color) UIManager.getDefaults().get( "WizardDetails.notReadyColor" );
77      }
78  
79      /***
80       * The title spans both columns and all the extra vertical space should go to this
81       * component.
82       */
83      private void createTitle()
84      {
85          GridBagConstraints c = new GridBagConstraints();
86          c.weighty = 1.0;
87          c.fill = GridBagConstraints.BOTH;
88          c.gridwidth = GridBagConstraints.REMAINDER;
89          title = new JLabel( wizard.getName(), SwingConstants.CENTER );
90          gridbag.setConstraints( title, c );
91          add( title );
92      }
93  
94      /***
95       * The History lists are anchored EAST (for the left one) and WEST (for the right one)
96       * so that they are next to each other with no extra white space.
97       * Extra horizontal space is allocated evenly between the two components.
98       */
99      private void createHistoryLists()
100     {
101         GridBagConstraints c = new GridBagConstraints();
102         // Common constraints
103         c.weightx = 1.0;
104 
105         c.anchor = GridBagConstraints.EAST;
106         leftHandHistoryList.setCellRenderer( new GestureHistoryCellRenderer( GestureIcon.LEFT_HANDED ) );
107         leftHandHistoryList.setVisibleRowCount( MAX_GESTURE_HISTORY );
108         for ( int i = 0; i < MAX_GESTURE_HISTORY; i++ )
109         {
110             leftHandHistory.addElement( GestureFactory.instance().getEmptyHand() );
111         }
112         gridbag.setConstraints( leftHandHistoryList, c );
113         add( leftHandHistoryList );
114 
115         c.gridwidth = GridBagConstraints.REMAINDER;
116         c.anchor = GridBagConstraints.WEST;
117         rightHandHistoryList.setCellRenderer( new GestureHistoryCellRenderer( GestureIcon.RIGHT_HANDED ) );
118         rightHandHistoryList.setVisibleRowCount( MAX_GESTURE_HISTORY );
119         for ( int i = 0; i < MAX_GESTURE_HISTORY; i++ )
120         {
121             rightHandHistory.addElement( GestureFactory.instance().getEmptyHand() );
122         }
123         gridbag.setConstraints( rightHandHistoryList, c );
124         add( rightHandHistoryList );
125     }
126 
127     /***
128      * The Gesture buttons are anchored and weighted identically to the histories.
129      */
130     private void createGestureButtons()
131     {
132         GridBagConstraints c = new GridBagConstraints();
133         // Common constraints
134         c.weightx = 1.0;
135 
136         c.anchor = GridBagConstraints.EAST;
137         gridbag.setConstraints( leftHandGesture, c );
138         add( leftHandGesture );
139         leftHandGesture.setEnabled( false );
140 
141         c.gridwidth = GridBagConstraints.REMAINDER;
142         c.anchor = GridBagConstraints.WEST;
143         gridbag.setConstraints( rightHandGesture, c );
144         add( rightHandGesture );
145         rightHandGesture.setEnabled( false );
146     }
147 
148     /***
149      * Create a label for title hitpoints and a label displaying the value of the hitpoints.
150      * The labels are textually aligned so that they are adjacent to the center of the grid.
151      * The labels will span the entire grid it occupies.
152      */
153     private void createHitPoints()
154     {
155         GridBagConstraints c = new GridBagConstraints();
156         // Common constraints
157         c.fill = GridBagConstraints.BOTH;
158 
159         JLabel hitPointsLabel = new JLabel( "Hitpoints: ", SwingConstants.RIGHT );
160         gridbag.setConstraints( hitPointsLabel, c );
161         add( hitPointsLabel );
162 
163         c.gridwidth = GridBagConstraints.REMAINDER;
164         hitPoints = new JLabel();
165         hitPoints.setHorizontalAlignment( SwingConstants.LEFT );
166         setHitPoints( wizard.getHitPoints() );
167         gridbag.setConstraints( hitPoints, c );
168         add( hitPoints );
169     }
170 
171     /***
172      * Create a label for title effects and a label displaying the value of the effects.
173      * The labels are textually aligned so that they are adjacent to the center of the grid.
174      * The labels will span the entire grid it occupies.
175      */
176     private void createEffects()
177     {
178         GridBagConstraints c = new GridBagConstraints();
179         // Common constraints
180         c.fill = GridBagConstraints.BOTH;
181 
182         JLabel effectsLabel = new JLabel( "Effects: ", SwingConstants.RIGHT );
183         gridbag.setConstraints( effectsLabel, c );
184         add( effectsLabel );
185 
186         c.gridwidth = GridBagConstraints.REMAINDER;
187         effects = new JLabel();
188         effects.setHorizontalAlignment( SwingConstants.LEFT );
189         gridbag.setConstraints( effects, c );
190         add( effects );
191     }
192 
193     /***
194      * Create a title for the monster list.
195      * Any monsters added will go below this label.
196      */
197     private void createMonsterList()
198     {
199         GridBagConstraints c = new GridBagConstraints();
200         // Common constraints
201         c.fill = GridBagConstraints.BOTH;
202 
203         JLabel monsterListLabel = new JLabel( "Monsters: ", SwingConstants.RIGHT );
204         gridbag.setConstraints( monsterListLabel, c );
205         add( monsterListLabel );
206 
207         c.gridwidth = GridBagConstraints.REMAINDER;
208         JLabel emptyLabel = new JLabel( " " );
209         gridbag.setConstraints( emptyLabel, c );
210         add( emptyLabel );
211 
212     }
213 
214     /***
215      * Adds a monster to the control of this wizard.
216      */
217     public void addMonster( Monster m )
218     {
219         MonsterDetails md = new MonsterDetails( m );
220         Component comp = null;
221 
222         GridBagConstraints c = new GridBagConstraints();
223         // Common constraints
224         c.fill = GridBagConstraints.BOTH;
225 
226         c.gridwidth = GridBagConstraints.REMAINDER;
227         comp = md.getMonsterName();
228         gridbag.setConstraints( comp, c );
229         add( comp );
230 
231         c.gridwidth = GridBagConstraints.RELATIVE;
232         comp = md.getMonsterTitle();
233         gridbag.setConstraints( comp, c );
234         add( comp );
235 
236         c.gridwidth = GridBagConstraints.REMAINDER;
237         comp = md.getHitPoints();
238         gridbag.setConstraints( comp, c );
239         add( comp );
240 
241         monsterList.add( md );
242     }
243 
244     /***
245      * Set the value of the hit points display to the value specified.
246      */
247     private void setHitPoints( int currentHitPoints )
248     {
249         hitPoints.setText( " " + String.valueOf( currentHitPoints ) );
250     }
251 
252     /***
253      * Get the value of client.
254      *
255      * @return value of client.
256      */
257     public boolean isClient()
258     {
259         return client;
260     }
261 
262     /***
263      * Set the value of client.
264      *
265      * @param v Value to assign to client.
266      */
267     public void setClient( boolean v )
268     {
269         client = v;
270         leftHandGesture.setEnabled( v );
271         rightHandGesture.setEnabled( v );
272     }
273 
274     /***
275      * Return the gesture made with the left hand for the wizard specified by the current client ID.
276      */
277     public Gesture getLeftHandGesture()
278     {
279         return leftHandGesture.getGesture();
280     }
281 
282     /***
283      * Return the gesture made with the right hand for the wizard specified by the current client ID.
284      */
285     public Gesture getRightHandGesture()
286     {
287         return rightHandGesture.getGesture();
288     }
289 
290 }