View Javadoc
1   /**
2    * This file is part of Indicators.
3    *
4    * Indicators is free software: you can redistribute it and/or modify
5    * it under the terms of the GNU General Public License as published by
6    * the Free Software Foundation, either version 3 of the License, or
7    * (at your option) any later version.
8    *
9    * Indicators is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with Indicators. If not, see <https://www.gnu.org/licenses/>.
16   */
17  package fr.inrae.agroclim.indicators.model.data;
18  
19  import java.io.Serializable;
20  
21  import javax.swing.event.EventListenerList;
22  
23  import fr.inrae.agroclim.indicators.model.data.DataLoadingListener.DataFile;
24  import lombok.extern.log4j.Log4j2;
25  
26  /**
27   * Listener handler for data loading.
28   *
29   * This is a utility class that can be used by beans that support listeners for
30   * data loading events. It manages a list of listeners and dispatches messages
31   * to them. You can use an instance of this class as a member field of your bean
32   * and delegate these types of work to it.
33   *
34   * Last changed : $Date$
35   *
36   * @author $Author$
37   * @version $Revision$
38   */
39  @Log4j2
40  public class DataLoadingListenerHandler implements HasDataLoadingListener,
41  Serializable {
42      /**
43       * UID for serialization.
44       */
45      private static final long serialVersionUID = -2141365352070901695L;
46  
47      /**
48       * List for all listeners.
49       */
50      private final EventListenerList listeners;
51  
52      /**
53       * Constructor.
54       */
55      public DataLoadingListenerHandler() {
56          listeners = new EventListenerList();
57      }
58  
59      /**
60       * Constructor with a list.
61       *
62       * @param eventListenerList
63       *            list for event listeners
64       */
65      public DataLoadingListenerHandler(final EventListenerList eventListenerList) {
66          listeners = eventListenerList;
67      }
68  
69      @Override
70      public final void addDataLoadingListener(final DataLoadingListener l) {
71          for (final DataLoadingListener listener : getDataLoadingListeners()) {
72              if (listener.equals(l)) {
73                  return;
74              }
75          }
76          listeners.add(DataLoadingListener.class, l);
77      }
78  
79      @Override
80      public final void addDataLoadingListeners(final DataLoadingListener[] ls) {
81          for (final DataLoadingListener l : ls) {
82              addDataLoadingListener(l);
83          }
84      }
85  
86      @Override
87      public final void fireDataLoadingAddEvent(final Data data) {
88          for (final DataLoadingListener listener : getDataLoadingListeners()) {
89              listener.onDataLoadingAdd(data);
90          }
91      }
92  
93      @Override
94      public final void fireDataLoadingEndEvent(final String text) {
95          for (final DataLoadingListener listener : getDataLoadingListeners()) {
96              listener.onDataLoadingEnd(text);
97          }
98      }
99  
100     @Override
101     public final void fireDataLoadingStartEvent(final String text) {
102         for (final DataLoadingListener listener : getDataLoadingListeners()) {
103             listener.onDataLoadingStart(text);
104         }
105     }
106 
107     @Override
108     public final void fireDataSetEvent(final DataFile type) {
109         LOGGER.traceEntry();
110         for (final DataLoadingListener listener : getDataLoadingListeners()) {
111             listener.onFileSet(type);
112         }
113     }
114 
115     @Override
116     public final DataLoadingListener[] getDataLoadingListeners() {
117         return listeners.getListeners(DataLoadingListener.class);
118     }
119 }