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 javafx.beans.InvalidationListener;
024 import javafx.collections.ListChangeListener;
025
026 /**
027 * @author William DRAI
028 */
029 public class ListListenerHelper<E> {
030
031 private InvalidationListener[] invalidationListeners = null;
032 private ListChangeListener<? super E>[] listChangeListeners = null;
033
034 public void addListener(InvalidationListener listener) {
035 invalidationListeners = ListenerUtil.add(InvalidationListener.class, invalidationListeners, listener);
036 }
037
038 public void removeListener(InvalidationListener listener) {
039 invalidationListeners = ListenerUtil.remove(InvalidationListener.class, invalidationListeners, listener);
040 }
041
042 public void addListener(ListChangeListener<? super E> listener) {
043 listChangeListeners = ListenerUtil.add(ListChangeListener.class, listChangeListeners, listener);
044 }
045
046 public void removeListener(ListChangeListener<? super E> listener) {
047 listChangeListeners = ListenerUtil.remove(ListChangeListener.class, listChangeListeners, listener);
048 }
049
050 public void fireValueChangedEvent(ListChangeListener.Change<E> change) {
051 if (invalidationListeners != null) {
052 for (InvalidationListener invalidationListener : invalidationListeners)
053 invalidationListener.invalidated(change.getList());
054 }
055 if (listChangeListeners != null) {
056 for (ListChangeListener<? super E> listChangeListener : listChangeListeners) {
057 change.reset();
058 listChangeListener.onChanged(change);
059 }
060 }
061 }
062
063 public boolean hasListeners() {
064 return invalidationListeners != null || listChangeListeners != null;
065 }
066
067 public int getInvalidationListenersSize() {
068 return invalidationListeners.length;
069 }
070
071 public int getListChangeListenersListenersSize() {
072 return listChangeListeners.length;
073 }
074
075 public void clear() {
076 invalidationListeners = null;
077 listChangeListeners = null;
078 }
079
080 }