001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.util.javafx;
022    
023    import java.lang.reflect.Method;
024    
025    /**
026     * @author William DRAI
027     */
028    public class ListenerHelper<L> {
029    
030            private final Class<?> listenerInterface;
031            private final Method listenerMethod;
032            private L[] listeners = null;
033            
034            public ListenerHelper(Class<?> listenerInterface) {
035                    this.listenerInterface = listenerInterface;
036                    Method[] methods = listenerInterface.getMethods();
037                    if (!listenerInterface.isInterface())
038                            throw new RuntimeException("Listener class must be an interface");
039                    if (methods.length != 1)
040                            throw new RuntimeException("Cannot use ListenerHelper with listener interfaces having more than one method");
041                    listenerMethod = methods[0];
042            }
043            
044            public void addListener(L listener) {
045                    listeners = ListenerUtil.add(listenerInterface, listeners, listener);
046            }
047            
048            public void removeListener(L listener) {
049                    listeners = ListenerUtil.remove(listenerInterface, listeners, listener);
050            }
051            
052        public void fireEvent(Object... args) {
053            if (listeners != null) {
054                    for (L listener : listeners) {
055                            try {
056                                    listenerMethod.invoke(listener, args);
057                            }
058                            catch (Exception e) {
059                                    throw new RuntimeException("Could not fire event", e);
060                            }
061                    }
062            }
063        }
064        
065        public boolean hasListeners() {
066            return listeners != null;
067        }
068        
069        public void clear() {
070            listeners = null;
071        }
072            
073    }