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