//Set all cards selected
for (IdragClusterable currentComp : selectedComps){
System.out.print((currentComp).getName() + " "); //remove later
if (currentComp instanceof MTComponent){//Add selected comps to selection - RIGHT NOW ONLY SUPPORTS INSTANCES OF MTCOMPONENT!
MTComponent mtCurrentComp = (MTComponent)currentComp;
///////////////////////////////
// Listen to destroy events of the clustered components, to remove them from
// the cluster and pack the polygon.
mtCurrentComp.addStateChangeListener(StateChange.COMPONENT_DESTROYED, new StateChangeListener(){
public void stateChanged(StateChangeEvent evt) {
if (evt.getSource() instanceof MTComponent) {
MTComponent sourceComp = (MTComponent) evt.getSource();
//Remove component from cluster it is in
Cluster clusterOfComponent = clusterMgr.getCluster(sourceComp);
if (clusterOfComponent != null){
((IdragClusterable)sourceComp).setSelected(false);
//Remvove the component from its former selection
clusterOfComponent.removeChild(sourceComp);
//System.out.println("Comp destroyed and removed from cluster: " + sourceComp.getName());
//remove the former selection from the selectionmanager if it consists only of 1 less components
if (clusterOfComponent.getChildCount() <= 2){
clusterMgr.removeCluster(clusterOfComponent);
}else{
//Tighten convex hull of reduced cluster
clusterOfComponent.packClusterPolygon();
}
}
}
}
});
////////////////////////////////
//Remove comp from former selection if it is in a new selection
Cluster formerSelection = clusterMgr.getCluster(currentComp);
if (formerSelection != null){
formerSelection.removeChild(mtCurrentComp);
//Remove the former selection from the selectionmanager if it consists only of 1 less components
if (formerSelection.getChildCount() <= 2){ //2 because the selection polygon is also always in the selection
// SceneManager.getInstance().getCurrentScene().getMainCanvas().getSelectionManager().removeSelection(formerSelection);
clusterMgr.removeCluster(formerSelection);
}else{
//Tighten convex hull of reduced cluster
formerSelection.packClusterPolygon();
}
}
//Get the last index of the selected component in the parent list to know where to add the selectionpoly
if (mtCurrentComp.getParent() != null){
int indexInParentList = mtCurrentComp.getParent().getChildIndexOf(mtCurrentComp);
// if (indexInParentList > n){
// n = indexInParentList;
// }
if (indexInParentList < n){
n = indexInParentList;
}
}
//ADD components to the selection and set it to selected
cluster.addChild(mtCurrentComp);
currentComp.setSelected(true);
}//if instance mtbasecomp
}//for
//Draw a convex hull around all selected shapes
cluster.packClusterPolygon();
dse.getSelectionPoly().setLineStipple((short)0xDDDD);
dse.getSelectionPoly().setStrokeColor(new MTColor(0,0,0,255));
//Add the selection poly 1 index after the index of the highest index of the selected components
if (selectedComps[0] instanceof MTComponent
&& ((MTComponent)selectedComps[0]).getParent() != null){
MTComponent firstSelectedComp = (MTComponent)selectedComps[0];
// System.out.println("n:" + n);
// System.out.println("Parent childcount: " + firstSelectedComp.getParent().getChildCount());
firstSelectedComp.getParent().addChild(n, dse.getSelectionPoly());
// if (n < firstSelectedComp.getParent().getChildCount())
// firstSelectedComp.getParent().addChild(n+1, dse.getSelectionPoly());
// else //FIXME this has caused out of bounds
// firstSelectedComp.getParent().addChild(n, dse.getSelectionPoly());