/src/org/mt4j/components/clusters/ClusterManager.java

http://mt4j.googlecode.com/ · Java · 175 lines · 65 code · 23 blank · 87 comment · 11 complexity · 5562a7926e130838319cc2d534491b37 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.components.clusters;
  19. import java.util.ArrayList;
  20. import org.mt4j.components.MTCanvas;
  21. import org.mt4j.components.MTComponent;
  22. import org.mt4j.components.interfaces.IMTComponent3D;
  23. import org.mt4j.components.visibleComponents.shapes.MTPolygon;
  24. import org.mt4j.input.inputProcessors.componentProcessors.lassoProcessor.IdragClusterable;
  25. /**
  26. * The Class ClusterManager.
  27. * @author Christopher Ruff
  28. */
  29. public class ClusterManager {
  30. /** The child objects. */
  31. private ArrayList<Cluster> childObjects;
  32. /** The canvas. */
  33. private MTCanvas canvas;
  34. /**
  35. * Instantiates a new cluster manager.
  36. *
  37. * @param canvas the canvas
  38. */
  39. public ClusterManager(MTCanvas canvas) {
  40. this.canvas = canvas;
  41. childObjects = new ArrayList<Cluster>();
  42. }
  43. /**
  44. * Gets the cluster count.
  45. *
  46. * @return the cluster count
  47. */
  48. public int getClusterCount(){
  49. return childObjects.size();
  50. }
  51. /**
  52. * Adds the cluster.
  53. *
  54. * @param selection the selection
  55. */
  56. public void addCluster(Cluster selection){
  57. childObjects.add(selection);
  58. }
  59. /**
  60. * Adds the all clusters.
  61. *
  62. * @param selections the selections
  63. */
  64. public void addAllClusters(Cluster[] selections){
  65. for (Cluster object : selections) {
  66. childObjects.add(object);
  67. }
  68. }
  69. /**
  70. * removes the Cluster from the Cluster manager and
  71. * also tries to delete its visible polygon from the current scenes'
  72. * main canvas,
  73. * also calls setSelected(false) on the Selections children.
  74. *
  75. * @param selection the selection
  76. */
  77. public void removeCluster(Cluster selection){
  78. //Remove the Selection Polygon of the now empty former selection from the canvas
  79. this.removeClusterPolyFromCanvas(selection.getClusterPolygon());
  80. //mark the remaining components from the deleted slection as not selected
  81. for (int i = 0; i < selection.getChildCount(); i++) {
  82. MTComponent comp = selection.getChildByIndex(i);
  83. if (comp instanceof IdragClusterable)
  84. ((IdragClusterable)comp).setSelected(false);
  85. }
  86. if (this.containsCluster(selection))
  87. childObjects.remove(selection);
  88. }
  89. /**
  90. * removes the visible selection polygon from the canvas.
  91. *
  92. * @param selectionPoly the selection poly
  93. */
  94. public void removeClusterPolyFromCanvas(MTPolygon selectionPoly){
  95. if (selectionPoly != null && canvas.containsChild(selectionPoly)){
  96. selectionPoly.getParent().removeChild(selectionPoly);
  97. }
  98. }
  99. /**
  100. * Removes the cluster.
  101. *
  102. * @param i the i
  103. */
  104. public void removeCluster(int i){
  105. childObjects.remove(i);
  106. }
  107. /**
  108. * Removes the all clusters.
  109. */
  110. public void removeAllClusters(){
  111. childObjects.clear();
  112. }
  113. /**
  114. * Gets the clusters.
  115. *
  116. * @return the clusters
  117. */
  118. public Cluster[] getClusters(){
  119. Cluster[] objects = new Cluster[childObjects.size()];
  120. for (int i = 0; i < childObjects.size(); i++) {
  121. objects[i] = childObjects.get(i);
  122. }
  123. return objects;
  124. }
  125. /**
  126. * Contains cluster.
  127. *
  128. * @param selection the selection
  129. *
  130. * @return true, if successful
  131. */
  132. public boolean containsCluster(Cluster selection){
  133. return (childObjects.contains(selection));
  134. }
  135. //only returns the first selection that contains that component
  136. //only works with mtcomponents right now
  137. /**
  138. * Gets the cluster.
  139. *
  140. * @param component the component
  141. *
  142. * @return the cluster
  143. */
  144. public Cluster getCluster(IMTComponent3D component){
  145. for (Cluster selection : childObjects) {
  146. if (component instanceof MTComponent && selection.containsDirectChild((MTComponent) component)) {
  147. return selection;
  148. }
  149. }
  150. return null;
  151. }
  152. }