PageRenderTime 93ms CodeModel.GetById 65ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

/src/org/mt4j/input/GestureEventSupport.java

http://mt4j.googlecode.com/
Java | 270 lines | 122 code | 47 blank | 101 comment | 47 complexity | c75c7b56256998f522ccaa985b21b64e MD5 | raw file
  1/***********************************************************************
  2 * mt4j Copyright (c) 2008 - 2009 Christopher Ruff, Fraunhofer-Gesellschaft All rights reserved.
  3 *  
  4 *   This program 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 *   This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 16 *
 17 ***********************************************************************/
 18package org.mt4j.input;
 19
 20import java.util.*;
 21import java.util.Map.Entry;
 22
 23import org.mt4j.input.inputProcessors.IGestureEventListener;
 24import org.mt4j.input.inputProcessors.IInputProcessor;
 25import org.mt4j.input.inputProcessors.MTGestureEvent;
 26
 27public class GestureEventSupport {
 28//	private List<IGestureEventListener> gestureListeners;
 29	
 30	/** The gestureEvtSenders to listener. */
 31	private Map<Class<? extends IInputProcessor>, IGestureEventListener[]> gestureSenderToGestureListener;
 32	
 33	/** The Constant EMPTY. */
 34	private static final IGestureEventListener[] EMPTY = {};
 35	
 36	
 37	public GestureEventSupport() {
 38		super();
 39		
 40//		this.gestureListeners = new ArrayList<IGestureEventListener>();
 41	}
 42
 43
 44//	/**
 45//	 * Constructs a <code>PropertyChangeSupport</code> object.
 46//	 * 
 47//	 * @param sourceBean  The bean to be given as the source for any events.
 48//	 */
 49//    public GestureEventSupport(Object sourceBean) {
 50//        if (sourceBean == null) {
 51//            throw new NullPointerException();
 52//        }
 53//        source = sourceBean;
 54//    }
 55    
 56    /**
 57     * Add a gestureEvtSenderChangeListener to the listener map.
 58     * If <code>listener</code> is null, no exception is thrown and no action
 59     * is taken.
 60     * 
 61     * @param gestureEvtSender the gestureEvtSender
 62     * @param listener  The gestureEvtSenderChangeListener to be added
 63     */
 64    public synchronized void addGestureEvtListener(Class<? extends IInputProcessor> gestureEvtSender, IGestureEventListener listener) {
 65        if (listener == null) {
 66            return;
 67        }
 68        
 69        this.lazyInitializeMap();
 70        
 71        IGestureEventListener[] array = this.gestureSenderToGestureListener.get(gestureEvtSender);
 72        
 73        //Add listener to array 
 74        int size = (array != null) ? array.length  : 0;
 75        IGestureEventListener[] clone = newArray(size + 1);
 76        clone[size] = listener;
 77        if (array != null) {
 78            System.arraycopy(array, 0, clone, 0, size);
 79        }
 80        
 81        //Put new listener array into map
 82        this.gestureSenderToGestureListener.put(gestureEvtSender, clone);
 83    }
 84    
 85    
 86    /**
 87     * Creates a new array of listeners of the specified size.
 88     * 
 89     * @param length the length
 90     * 
 91     * @return the gestureEvtSender change listener[]
 92     */
 93    protected IGestureEventListener[] newArray(int length) {
 94        return (0 < length) ? new IGestureEventListener[length] : EMPTY;
 95    }
 96
 97    
 98    /**
 99     * Removes a IGestureEventListener to the listener map.
100     * Throws no error if the listener isnt found.
101     * 
102     * @param gestureEvtSender the gestureEvtSender
103     * @param listener the listener
104     */
105    public synchronized void removeGestureEventListener(Class<? extends IInputProcessor> gestureEvtSender, IGestureEventListener listener) {
106    	if (listener == null || gestureEvtSender == null) {
107            return;
108        }
109    	
110    	 this.lazyInitializeMap();
111    	 
112    	  if (this.gestureSenderToGestureListener != null) {
113              IGestureEventListener[] array = this.gestureSenderToGestureListener.get(gestureEvtSender);
114              if (array != null) {
115            	  
116                  for (int i = 0; i < array.length; i++) {
117                      if (listener.equals(array[i])) {
118                          int size = array.length - 1;
119                          if (size > 0) {
120                        	  IGestureEventListener[] clone = newArray(size);
121                              System.arraycopy(array, 0, clone, 0, i);
122                              System.arraycopy(array, i + 1, clone, i, size - i);
123                              this.gestureSenderToGestureListener.put(gestureEvtSender, clone);
124                          }
125                          else {
126                              this.gestureSenderToGestureListener.remove(gestureEvtSender);
127                              if (this.gestureSenderToGestureListener.isEmpty()) {
128                                  this.gestureSenderToGestureListener = null;
129                              }
130                          }
131                          break;
132                      }
133                  }
134              }
135          }
136    }
137    
138    
139    /**
140     * Clear listeners.
141     */
142    public synchronized void clearListeners(){
143    	if (this.gestureSenderToGestureListener == null){
144    		return;
145    	}
146    	
147    	 this.lazyInitializeMap();
148    	 
149    	 this.gestureSenderToGestureListener.clear();
150    }
151    
152    /**
153     * Fire an existing GestureEvent to any registered listeners.
154     * 
155     * @param evt  The GestureEvent object.
156     */
157    public void fireGestureEvt(MTGestureEvent evt) {
158    	if (this.gestureSenderToGestureListener != null 
159        && !this.gestureSenderToGestureListener.isEmpty()
160        ){
161//	    	Class<? extends GestureEventFireMarker> gestureEvtSenderName = evt.getgestureEvtSender();
162	    	
163	    	Class<? extends IInputProcessor> marker = evt.getSource().getClass();
164	        
165	        IGestureEventListener[] common = this.gestureSenderToGestureListener.get(null);
166	        IGestureEventListener[] named = (marker != null) ? this.gestureSenderToGestureListener.get(marker) : null;
167	
168	        this.fire(common, evt);
169	        this.fire(named, evt);
170    	}
171    }
172
173    
174    
175    /**
176     * Fires the events to the listeners.
177     * 
178     * @param listeners the listeners
179     * @param event the event
180     */
181    private void fire(IGestureEventListener[] listeners, MTGestureEvent event) {
182        if (listeners != null) {
183            for (IGestureEventListener listener : listeners) {
184                listener.processGestureEvent(event);
185            }
186        }
187    }
188    
189    
190    /**
191     * Returns all listeners in the map.
192     * 
193     * @return an array of all listeners
194     */
195    public final synchronized IGestureEventListener[] getListeners() {
196        if (this.gestureSenderToGestureListener == null) {
197            return newArray(0);
198        }
199        List<IGestureEventListener> list = new ArrayList<IGestureEventListener>();
200
201        IGestureEventListener[] listeners = this.gestureSenderToGestureListener.get(null);
202        if (listeners != null) {
203            list.addAll(Arrays.asList(listeners));
204            //for (IGestureEventListener listener : listeners) {
205             //   list.add(listener);
206            //}
207        }
208        
209        for (Entry<Class<? extends IInputProcessor>, IGestureEventListener[]> entry : this.gestureSenderToGestureListener.entrySet()) {
210        	Class<? extends IInputProcessor> gestureEvtSender = entry.getKey();
211            if (gestureEvtSender != null) {
212                list.addAll(Arrays.asList(entry.getValue()));
213                //for (IGestureEventListener listener : entry.getValue()) {
214                 //   list.add(listener);
215                //}
216            }
217        }
218        return list.toArray(newArray(list.size()));
219    }
220
221    
222    /**
223     * Returns listeners that have been associated with the named gestureEvtSender.
224     * 
225     * @param gestureEvtSender  the name of the property
226     * 
227     * @return an array of listeners for the named property
228     */
229    public final IGestureEventListener[] getListeners(Class<? extends IInputProcessor> gestureEvtSender) {
230        if (gestureEvtSender != null) {
231        	IGestureEventListener[] listeners =  this.gestureSenderToGestureListener.get(gestureEvtSender);
232            if (listeners != null) {
233                return listeners.clone();
234            }
235        }
236        return newArray(0);
237    }
238
239    /**
240     * Indicates whether the map contains
241     * at least one listener to be notified.
242     * 
243     * @param gestureEvtSender the gestureEvtSender
244     * 
245     * @return      {@code true} if at least one listener exists or
246     * {@code false} otherwise
247     */
248    public final synchronized boolean hasListeners(Class<? extends IInputProcessor> gestureEvtSender) {
249        if (this.gestureSenderToGestureListener == null) {
250            return false;
251        }
252        IGestureEventListener[] array = this.gestureSenderToGestureListener.get(null);
253        return (array != null) || ((gestureEvtSender != null) && (null != this.gestureSenderToGestureListener.get(gestureEvtSender)));
254    }
255    
256    
257    /**
258     * Checks if the map is null and then lazily initializes it.
259     */
260    private void lazyInitializeMap(){
261    	if (gestureSenderToGestureListener == null){
262    		 gestureSenderToGestureListener = new HashMap<Class<? extends IInputProcessor>, IGestureEventListener[]>();
263    	}
264    }
265
266
267
268	
269	
270}