001/** 002 * GRANITE DATA SERVICES 003 * Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S. 004 * 005 * This file is part of the Granite Data Services Platform. 006 * 007 * *** 008 * 009 * Community License: GPL 3.0 010 * 011 * This file is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as published 013 * by the Free Software Foundation, either version 3 of the License, 014 * or (at your option) any later version. 015 * 016 * This file is distributed in the hope that it will be useful, but 017 * WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public License 022 * along with this program. If not, see <http://www.gnu.org/licenses/>. 023 * 024 * *** 025 * 026 * Available Commercial License: GraniteDS SLA 1.0 027 * 028 * This is the appropriate option if you are creating proprietary 029 * applications and you are not prepared to distribute and share the 030 * source code of your application under the GPL v3 license. 031 * 032 * Please visit http://www.granitedataservices.com/license for more 033 * details. 034 */ 035package org.granite.client.collection; 036 037import org.granite.client.collection.CollectionChangeEvent.Kind; 038 039/** 040 * @author William DRAI 041 */ 042public class CollectionChangeSupport { 043 044 private final Object collection; 045 private CollectionChangeListener[] listeners = null; 046 047 public CollectionChangeSupport(Object collection) { 048 this.collection = collection; 049 } 050 051 public void addCollectionChangeListener(CollectionChangeListener listener) { 052 if (listeners == null) 053 listeners = new CollectionChangeListener[] { listener }; 054 else { 055 CollectionChangeListener[] newListeners = new CollectionChangeListener[listeners.length+1]; 056 System.arraycopy(listeners, 0, newListeners, 0, listeners.length); 057 newListeners[listeners.length] = listener; 058 listeners = newListeners; 059 } 060 } 061 062 public void fireCollectionChangeEvent(Kind kind, Object key, Object[] values) { 063 if (listeners == null) 064 return; 065 CollectionChangeEvent event = new CollectionChangeEvent(collection, kind, key, values); 066 for (CollectionChangeListener listener : listeners) 067 listener.collectionChange(event); 068 } 069 070 public void removeCollectionChangeListener(CollectionChangeListener listener) { 071 if (listeners == null) 072 return; 073 if (listeners.length == 1) { 074 if (listeners[0] == listener) 075 listeners = null; 076 } 077 else { 078 int index = -1; 079 for (int i = 0; i < listeners.length; i++) { 080 if (listeners[i] == listener) { 081 index = i; 082 break; 083 } 084 } 085 if (index >= 0) { 086 CollectionChangeListener[] newListeners = new CollectionChangeListener[listeners.length-1]; 087 if (index > 0) 088 System.arraycopy(listeners, 0, newListeners, 0, index); 089 if (index < listeners.length-1) 090 System.arraycopy(listeners, index+1, newListeners, index, listeners.length-index-1); 091 listeners = newListeners; 092 } 093 } 094 } 095}