PageRenderTime 24ms CodeModel.GetById 18ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 1ms

/src/org/mt4j/input/ComponentInputProcessorSupport.java

http://mt4j.googlecode.com/
Java | 126 lines | 48 code | 21 blank | 57 comment | 8 complexity | fa78dc2ce8a583061f7b4064e46f5399 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.ArrayList;
 21import java.util.Collections;
 22import java.util.List;
 23
 24import org.mt4j.components.MTComponent;
 25import org.mt4j.input.inputData.MTInputEvent;
 26import org.mt4j.input.inputProcessors.componentProcessors.AbstractComponentProcessor;
 27
 28import processing.core.PApplet;
 29
 30/**
 31 * The Class ComponentInputProcessorSupport. Keeps the list of registered component input processors for
 32 * one component and sorts them by locking priority.
 33 * 
 34 * @author Christopher Ruff
 35 */
 36public class ComponentInputProcessorSupport implements IMTInputEventListener /*, IGestureEventListener*/ {
 37	
 38	/** The registered processors. */
 39	private List<AbstractComponentProcessor> registeredProcessors;
 40	
 41	/** The associated component. */
 42	private MTComponent associatedComponent;
 43	
 44	/**
 45	 * Instantiates a new component input processor support.
 46	 *
 47	 * @param graphicsContext the graphics context
 48	 * @param associatedComponent the associated component
 49	 */
 50	public ComponentInputProcessorSupport(PApplet graphicsContext, MTComponent associatedComponent) {
 51		super();
 52				
 53		this.associatedComponent = associatedComponent;
 54		this.registeredProcessors = new ArrayList<AbstractComponentProcessor>(5);
 55	}
 56
 57
 58	/* (non-Javadoc)
 59	 * @see org.mt4j.input.IMTInputEventListener#processInputEvent(org.mt4j.input.inputData.MTInputEvent)
 60	 */
 61	public boolean processInputEvent(MTInputEvent inEvt){
 62//		/*
 63		boolean handled = false;
 64		
 65		//Call preProcess on _all_ processors before calling processInputEvent on each of them
 66		//to allow each processor to take part in the locking mechanism before anyone actually locks something
 67		for (AbstractComponentProcessor p : registeredProcessors) {
 68			if (p.isInterestedIn(inEvt) && this.associatedComponent.isGestureAllowed(p.getClass())){
 69				p.preProcess(inEvt);
 70			}
 71		}
 72		
 73		for (int i = 0; i < registeredProcessors.size(); i++) {
 74			AbstractComponentProcessor inputProcessor = registeredProcessors.get(i);
 75			//Send events
 76			if (inputProcessor.isInterestedIn(inEvt) && this.associatedComponent.isGestureAllowed(inputProcessor.getClass())){
 77				handled = true;
 78				inputProcessor.processInputEvent(inEvt);
 79			}
 80		}
 81		return handled;
 82//		*/
 83	}
 84	
 85	
 86	/**
 87	 * Register input processor.
 88	 *
 89	 * @param inputProcessor the input processor
 90	 */
 91	public synchronized void registerInputProcessor(AbstractComponentProcessor inputProcessor){
 92		if (!this.registeredProcessors.contains(inputProcessor)){
 93			this.registeredProcessors.add(inputProcessor);
 94			
 95			//Sort the list so that the cursor processors
 96			//with the highest locking priority get served first
 97			Collections.sort(registeredProcessors,  Collections.reverseOrder());
 98			
 99			//-maybe put all this back to mtcomp?
100			inputProcessor.addGestureListener(associatedComponent);
101		}
102	}
103	
104	/**
105	 * Unregister input processor.
106	 *
107	 * @param inputProcessor the input processor
108	 */
109	public synchronized void unregisterInputProcessor(AbstractComponentProcessor inputProcessor){
110		if (this.registeredProcessors.contains(inputProcessor)){
111			this.registeredProcessors.remove(inputProcessor);
112		}
113	}
114	
115	/**
116	 * Gets the input processors.
117	 *
118	 * @return the input processors
119	 */
120	public AbstractComponentProcessor[] getInputProcessors(){
121		return this.registeredProcessors.toArray(new AbstractComponentProcessor[this.registeredProcessors.size()]);
122	}
123	
124	
125
126}