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