/src/org/mt4j/input/inputData/ActiveCursorPool.java

http://mt4j.googlecode.com/ · Java · 128 lines · 35 code · 21 blank · 72 comment · 2 complexity · 422d8d6901518a03de4c467a983a722c MD5 · raw file

  1. /***********************************************************************
  2. * mt4j Copyright (c) 2008 - 2009, C.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.inputData;
  19. import java.util.Collection;
  20. import java.util.HashMap;
  21. import java.util.Map;
  22. /**
  23. * This class is basically a centralized wrapper for a HashMap acting as a store
  24. * for active inputs.
  25. * cursors are added when a new Touch is registered. cursors should be put in the map by
  26. * using their cursor-ID. When the cursor has ended they have to be removed again.
  27. *
  28. * @author Christopher Ruff
  29. */
  30. public class ActiveCursorPool {
  31. /** The cursor pool. */
  32. private static ActiveCursorPool cursorPool;
  33. /** The cursors map. */
  34. private Map<Long, InputCursor> cursorMap;
  35. /**
  36. * Instantiates a new cursor pool.
  37. */
  38. private ActiveCursorPool(){
  39. cursorMap = new HashMap<Long, InputCursor>();
  40. // cursorMap = new WeakHashMap<Long, InputCursor>();
  41. }
  42. /**
  43. * Gets the single instance of cursorPool.
  44. *
  45. * @return single instance of cursorPool
  46. */
  47. public static ActiveCursorPool getInstance(){
  48. if (cursorPool == null){
  49. cursorPool = new ActiveCursorPool();
  50. return cursorPool;
  51. }else{
  52. return cursorPool;
  53. }
  54. }
  55. /**
  56. * Put cursor. The id is assumed to be the cursors ID.
  57. *
  58. * @param cursorID the cursor id
  59. * @param m the m
  60. */ //TODO automate because id = cursorID?
  61. public void putActiveCursor(long cursorID, InputCursor m){
  62. cursorMap.put(cursorID, m);
  63. }
  64. /**
  65. * Gets the cursor by id.
  66. *
  67. * @param ID the iD
  68. *
  69. * @return the cursor by id
  70. */
  71. public InputCursor getActiveCursorByID(long ID){
  72. return cursorMap.get(ID);
  73. }
  74. public InputCursor[] getActiveCursors(){
  75. Collection<InputCursor> values = cursorMap.values();
  76. return values.toArray(new InputCursor[values.size()]);
  77. /*
  78. Set<Long> keys = cursorsMap.keySet();
  79. for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
  80. Long long1 = (Long) iterator.next();
  81. Inputcursor cursor = cursorsMap.get(long1);
  82. MTCursorInputEvt lastEvt = cursor.getLastEvent();
  83. MTCursorInputEvt copy = (MTCursorInputEvt) lastEvt.clone();
  84. copy.setId(MTCursorInputEvt.INPUT_ENDED);
  85. }
  86. return null;
  87. */
  88. }
  89. /**
  90. * Removes the cursor.
  91. *
  92. * @param ID the iD
  93. * @return the input cursor
  94. */
  95. public InputCursor removeCursor(long ID){
  96. return cursorMap.remove(ID);
  97. }
  98. /**
  99. * Gets the active cursor count.
  100. *
  101. * @return the active cursor count
  102. */
  103. public long getActiveCursorCount(){
  104. return cursorMap.size();
  105. }
  106. }