View Javadoc

1   // START LICENSE
2   // JSet - a Java JSet card board game implementation
3   // Copyright (C) 2004 Jerome Lacoste
4   //
5   // This program is free software; you can redistribute it and/or modify
6   // it under the terms of the GNU General Public License as published by
7   // the Free Software Foundation; either version 2 of the License, or (at
8   // your option) any later version.
9   //
10  // This program is distributed in the hope that it will be useful, but
11  // WITHOUT ANY WARRANTY; without even the implied warranty of
12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  // General Public License for more details.
14  //
15  // You should have received a copy of the GNU General Public License
16  // along with this program; if not, write to the Free Software
17  // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  // END LICENSE
19  
20  package org.cb.jset.client;
21  
22  import org.cb.jset.SetGameBoardListener;
23  import org.cb.jset.CardProperties;
24  import org.cb.jset.CardSet;
25  
26  import javax.swing.event.EventListenerList;
27  
28  /***
29   * An abstract <code>SetGame</code>, responsible of the event management. 
30   *
31   * @author jerome@coffeebreaks.org - last modified by $LastModifiedBy$
32   * @version $Id: AbstractGame.java 123 2004-04-14 23:45:53Z jerome $
33   */
34  public abstract class AbstractGame implements SetGame
35  {
36    protected final EventListenerList _listenerList = new EventListenerList();
37  
38    // void addPlayersListener()
39    public void addBoardListener(final SetGameBoardListener listener)
40    {
41      _listenerList.add(SetGameBoardListener.class, listener);
42    }
43  
44    public void removeBoardListener(final SetGameBoardListener listener)
45    {
46      _listenerList.remove(SetGameBoardListener.class, listener);
47    }
48  
49    public void addGameListener(final SetGameListener listener)
50    {
51      _listenerList.add(SetGameListener.class, listener);
52    }
53  
54    public void removeGameListener(final SetGameListener listener)
55    {
56      _listenerList.remove(SetGameListener.class, listener);
57    }
58  
59  
60    protected void fireGameChange(final GameEvent event)
61    {
62      // Guaranteed to return a non-null array
63      final Object[] listeners = _listenerList.getListenerList();
64      // Process the listeners last to first, notifying
65      // those that are interested in this event
66      for (int i = listeners.length - 2; i >= 0; i -= 2)
67      {
68        if (listeners[i] == SetGameListener.class)
69        {
70          ((SetGameListener) listeners[i + 1]).gameChange(event);
71        }
72      }
73    }
74  
75    protected void fireCardsAdded(final CardProperties[] cards)
76    {
77      // Guaranteed to return a non-null array
78      final Object[] listeners = _listenerList.getListenerList();
79      // Process the listeners last to first, notifying
80      // those that are interested in this event
81      for (int i = listeners.length - 2; i >= 0; i -= 2)
82      {
83        if (listeners[i] == SetGameBoardListener.class)
84        {
85          ((SetGameBoardListener) listeners[i + 1]).cardsAdded(cards);
86        }
87      }
88    }
89  
90    protected void fireSetRemoved(final CardSet set)
91    {
92      // Guaranteed to return a non-null array
93      final Object[] listeners = _listenerList.getListenerList();
94      // Process the listeners last to first, notifying
95      // those that are interested in this event
96      for (int i = listeners.length - 2; i >= 0; i -= 2)
97      {
98        if (listeners[i] == SetGameBoardListener.class)
99        {
100         ((SetGameBoardListener) listeners[i + 1]).setRemoved(set);
101       }
102     }
103   }
104 }