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.cardboard;
21  
22  import java.util.List;
23  import java.util.ArrayList;
24  
25  /***
26   * Abstract CardBoardModel implementation.
27   *
28   * Provides methods to register remove listeners.
29   *
30   * @author jerome@coffeebreaks.org - last modified by $LastModifiedBy$
31   * @version $Id: AbstractCardBoardModel.java 83 2004-04-13 13:51:09Z jerome $
32   */
33  public abstract class AbstractCardBoardModel implements CardBoardModel
34  {
35    /*** the list of listeners. */
36    private final List _boardListeners = new ArrayList();
37  
38  
39    /***
40     * Adds the specified listener to the instance's listener's list.
41     * @param listener the listener to add
42     */
43    public void addBoardDataListener(final CardBoardModelListener listener)
44    {
45      _boardListeners.add(listener);
46    }
47  
48    /***
49     * Removes the specified listener to the instance's listener's list.
50     * @param listener the listener to remove
51     */
52    public void removeBoardDataListener(final CardBoardModelListener listener)
53    {
54      _boardListeners.remove(listener);
55    }
56  
57    /***
58     * Fire the specified contents changed event
59     * @param event
60     * @see CardBoardModelListener#contentsChanged(org.cb.cardboard.CardBoardEvent)
61     */
62    protected final void fireContentsChanged(final CardBoardEvent event)
63    {
64      if (_boardListeners != null)
65      {
66        for (int i = 0; i < _boardListeners.size(); i++)
67        {
68          final CardBoardModelListener boardListenerIF = (CardBoardModelListener) _boardListeners.get(i);
69          boardListenerIF.contentsChanged(event);
70        }
71      }
72    }
73  
74    /***
75     * Fire the specified interval added event
76     * @param event
77     * @see CardBoardModelListener#intervalAdded(org.cb.cardboard.CardBoardEvent)
78     */
79    protected final void fireIntervalAdded(final CardBoardEvent event)
80    {
81      if (_boardListeners != null)
82      {
83        for (int i = 0; i < _boardListeners.size(); i++)
84        {
85          final CardBoardModelListener boardListenerIF = (CardBoardModelListener) _boardListeners.get(i);
86          boardListenerIF.intervalAdded(event);
87        }
88      }
89    }
90  
91    /***
92     * Fire the specified interval removed event
93     * @param event
94     * @see CardBoardModelListener#intervalRemoved(org.cb.cardboard.CardBoardEvent)
95     */
96    protected final void fireIntervalRemoved(final CardBoardEvent event)
97    {
98      if (_boardListeners != null)
99      {
100       for (int i = 0; i < _boardListeners.size(); i++)
101       {
102         final CardBoardModelListener boardListenerIF = (CardBoardModelListener) _boardListeners.get(i);
103         boardListenerIF.intervalRemoved(event);
104       }
105     }
106   }
107 }