if ( SwingUtilities . isLeftMouseButton ( e ) ) {

//reset nodes prior to painting

for ( Node n : NodeIDsAndColors. keySet ( ) ) {

n. AssignedAColor = false ;

n. setColor ( Color . BLUE ) ;

NodeIDsAndColors. put ( n, Color . BLUE ) ;

}

//reset the graphical clusters that are drawn with left click.

resetGraphicalClusters ( ) ;

LinkedList < ClickableNode > ToBeClustered = new LinkedList < ClickableNode > ( ) ;

List cna = getClickableNodes ( ) ;

boolean AtLeastOneNode = false ;

float xPointClick = e. getX ( ) ;

// LinkedHashMap<Node,Color> ReplacementMap = new LinkedHashMap<Node,Color>();

if ( cna. size ( ) > 0 ) {

AtLeastOneNode = true ;

}

for ( int i = 0 ; i < cna. size ( ) ; i ++ ) {

ClickableNode cn = ( ClickableNode ) cna. get ( i ) ;

//retrieve node

Node n = cn. getTarget ( ) ;

currentNodePointX = cn. getTargetArea ( ) . x ;

currentNodePointY = cn. getTargetArea ( ) . y ;

//figure out how to actually color the nodes

//if (currentNodePointX >= xPointClick){

if ( currentNodePointX <= xPointClick ) { //left

ToBeClustered. add ( cn ) ;

//NodeIDsAndColors.put(n,Color.RED);

} else {

NodeIDsAndColors. put ( n, Color . BLUE ) ;

}

}

//sort the list (bubble sorting)

for ( int i = 0 ; i < ToBeClustered. size ( ) - 1 ; i ++ ) {

for ( int j = 0 ; j < ToBeClustered. size ( ) - 1 ; j ++ ) {

if ( Math . abs ( ToBeClustered. get ( j ) . getTargetArea ( ) . x - xPointClick ) >

Math . abs ( ToBeClustered. get ( j + 1 ) . getTargetArea ( ) . x - xPointClick ) ) {

ClickableNode TempNode = ToBeClustered. get ( j ) ;

ToBeClustered. set ( j,ToBeClustered. get ( j + 1 ) ) ;

ToBeClustered. set ( j + 1 ,TempNode ) ;

}

}

}

//each linked list is an individual cluster.

//the 'clusters' object contains all clusters.

LinkedList < LinkedList < Node >> ClustersPreFiltering = new LinkedList < LinkedList < Node >> ( ) ;

int NumberOfClusters = 0 ;

for ( int i = 0 ; i < ToBeClustered. size ( ) ; i ++ ) {

boolean ClusterThisNode = true ;

for ( int q = 0 ; q < ToBeClustered. get ( i ) . getTarget ( ) . getNumberOfSons ( ) ; q ++ ) {

if ( ToBeClustered. get ( i ) . getTarget ( ) . getSon ( q ) . AssignedAColor ) {

ClusterThisNode = false ;

}

}

if ( ClusterThisNode == true ) {

//ToBeClustered.get(i).AssignedAColor = false;

LinkedList < Node > ThisCluster = new LinkedList < Node > ( ) ;

NumberOfClusters ++;

/*Set the color of each node in the selected area to a color

*depending on its spatial proximity to the clicked line.

*The nodes are added to the list 'ThisCluster' and at the

*end of the loops, the cluster is added to the meta list of

* clusters. The intended result is to have the cut tree group

* clusters at the loosely specified clustering threshold

* (the clicked point) by highlighting the descended trees in

* different colors. The cluster lists give the names of nodes.

*/

//store a list of all nodes

LinkedList < Node > AllDescendants = new LinkedList < Node > ( ) ;

//initialize: check for additional nodes

boolean ThereAreMoreSons = true ;

//start from the root node

LinkedList < Node > SomeNodes = new LinkedList < Node > ( ) ;

SomeNodes. add ( ToBeClustered. get ( i ) . getTarget ( ) ) ;

AllDescendants. add ( ToBeClustered. get ( i ) . getTarget ( ) ) ;

while ( ThereAreMoreSons == true ) {

//don't keep going, unless you have a reason to

ThereAreMoreSons = false ;

//initialize a list for the next search

LinkedList < Node > AdditionalNodes = new LinkedList < Node > ( ) ;

//initialize through the old list, add hits to the new list

for ( int j = 0 ; j < SomeNodes. size ( ) ; j ++ ) {

if ( SomeNodes. get ( j ) . getNumberOfSons ( ) != 0 ) {

ThereAreMoreSons = true ;

AdditionalNodes. addAll ( SomeNodes. get ( j ) . getSons ( ) ) ;

AllDescendants. addAll ( SomeNodes. get ( j ) . getSons ( ) ) ;

}

}

//replace the old list with the new list

SomeNodes = AdditionalNodes ;

}

// Node OneSon = ToBeClustered.get(i).getTarget().getSon(0)

Color theColor ;

for ( int a = 0 ; a < AllDescendants. size ( ) ; a ++ ) {

Node noodle = AllDescendants. get ( a ) ;

//ClickableNode noodle = (ClickableNode) n.get(a);

// Color theColor = clusterColor(ToBeClustered.size());

if ( noodle. AssignedAColor == false ) {

theColor = clusterColor ( NumberOfClusters ) ;

noodle. setColor ( theColor ) ;

if ( noodle. hasFather ( ) ) {

noodle. getFather ( ) . AssignedAColor = true ;

}

} else {

theColor = noodle. getColor ( ) ;

}

NodeIDsAndColors. put ( noodle,theColor ) ;

noodle. AssignedAColor = true ;

if ( noodle. isLeaf ( ) ) {

ThisCluster. add ( noodle ) ;

noodle. AssignedAColor = true ;

}

}

ClustersPreFiltering. add ( ThisCluster ) ;

}

}

if ( AtLeastOneNode ) {

isMouseClicked = true ;

} else {

isMouseClicked = false ;

}

//re-make clusters on click

Clusters = new LinkedList < LinkedList < Node >> ( ) ;

for ( int k = 0 ; k < ClustersPreFiltering. size ( ) ; k ++ ) {

boolean KeepThisCluster = false ;

for ( int l = 0 ; l < ClustersPreFiltering. get ( k ) . size ( ) ; l ++ ) {

if ( ClustersPreFiltering. get ( k ) . get ( l ) . isLeaf ( ) ) {

KeepThisCluster = true ;

}

}

//this cluster is retained.

if ( KeepThisCluster == true ) {

Clusters. add ( ClustersPreFiltering. get ( k ) ) ;

} else { //color blue

for ( int l = 0 ; l < ClustersPreFiltering. get ( k ) . size ( ) ; l ++ ) {

ClustersPreFiltering. get ( k ) . get ( l ) . AssignedAColor = false ;

ClustersPreFiltering. get ( k ) . get ( l ) . setColor ( Color . BLUE ) ;

NodeIDsAndColors. put ( ClustersPreFiltering. get ( k ) . get ( l ) , Color . BLUE ) ;

// System.out.println("Line 1068 - You made it to the coloring, bro.");

}

}

}

// System.out.println("Line 1253 - Number of Clusters: " + Clusters.size());

/*At this point, the program takes the Clusters and 'converts' them to

* the GraphicalCluster class, so that the plottable rectangles are

* available for plot.

*/

for ( int i = 0 ; i < Clusters. size ( ) ; i ++ ) {

GraphicalCluster gc = new GraphicalCluster ( ) ;

LinkedList < String > clusterNames = new LinkedList < String > ( ) ;

for ( int j = 0 ; j < Clusters. get ( i ) . size ( ) ; j ++ ) {

if ( Clusters. get ( i ) . get ( j ) . hasName ( ) ) {

clusterNames. add ( Clusters. get ( i ) . get ( j ) . getName ( ) ) ;

}

}

gc. setAssociatedCluster ( Clusters. get ( i ) ) ;

gc. GraphicalCluster (

Clusters. get ( i ) ,

clusterNames,

LeafName,

LeafNameX,

LeafNameY ) ;

Color color = Clusters. get ( i ) . get ( 0 ) . getColor ( ) ;

int red = color. getRed ( ) ;

int blue = color. getBlue ( ) ;

int green = color. getGreen ( ) ;

int alpha = 250 ;

Color newColor = new Color ( red, green, blue, alpha ) ;

gc. setColor ( newColor ) ;

lemon. add ( gc ) ;

}

repaint ( ) ;