*/
private void drawGraph( HttpServletRequest request, HttpServletResponse response )
{
try
{
PaintBean pb = (PaintBean)ContextLookup.lookup( "paintBean", request );
if( pb == null )
{
System.out.println( "paintBean is null" );
return;
}
ImageCacher.updateConstants( request );
DNVGraph graph = pb.getGraph();
int level = (int)pb.getLevel();
// pb.setHasBeenDisplayed( true );
int width = -1;
width = getWidth( request, width );
int height = -1;
height = getHeight( request, height );
boolean overview = false;
overview = getOverview( request, overview );
if( width == -1 )
width = (int)pb.getWidth();
if( height == -1 )
height = (int)pb.getHeight();
double minX = 0;
double minY = 0;
double maxX = 1;
double maxY = 1;
String minXStr = request.getParameter( "minX" );
if( minXStr != null && !minXStr.equals( "" ) )
{
minX = getMinX( request, minX );
pb.setMinX( minX );
}
String minYStr = request.getParameter( "minY" );
if( minYStr != null && !minYStr.equals( "" ) )
{
minY = getMinY( request, minY );
pb.setMinY( minY );
}
String maxXStr = request.getParameter( "maxX" );
if( maxXStr != null && !maxXStr.equals( "" ) )
{
maxX = getMaxX( request, maxX );
pb.setMaxX( maxX );
}
String maxYStr = request.getParameter( "maxY" );
if( maxYStr != null && !maxYStr.equals( "" ) )
{
maxY = getMaxY( request, maxY );
pb.setMaxY( maxY );
}
String renderingStr = request.getParameter( "r" );
int rendering = BufferedImage.TYPE_BYTE_INDEXED;
if( renderingStr != null && renderingStr.equals( "qual" ) )
{
rendering = BufferedImage.TYPE_INT_RGB;
}
Timer pickingTimer = new Timer( Timer.MILLISECONDS );
// ------------------------------------
// interaction with static image
// ------------------------------------
String mouseDownXstr = request.getParameter( "mouseDownX" );
// boolean mouseDown = false;
if( mouseDownXstr != null && !mouseDownXstr.equals( "" ) )
{
// mouseDown = true;
pickingTimer.setStart();
// drag closest node to this position
int mouseDownX = Integer.parseInt( mouseDownXstr );
int mouseDownY = Integer.parseInt( request.getParameter( "mouseDownY" ) );
// drag it to here
int mouseUpX = Integer.parseInt( request.getParameter( "mouseUpX" ) );
int mouseUpY = Integer.parseInt( request.getParameter( "mouseUpY" ) );
boolean sameNode = Boolean.parseBoolean( request.getParameter( "sameNode" ) );
boolean ctrlPressed = Boolean.parseBoolean( request.getParameter( "ctrlPressed" ) );
// - - - - - - - - - - -
// find closest node
// - - - - - - - - - - -
// float maxDepth = Integer.MAX_VALUE;
double globalMinX = GraphFunctions.getMinXPosition( graph, level, true );
double globalMaxX = GraphFunctions.getMaxXPosition( graph, level, true );
double globalMinY = GraphFunctions.getMinYPosition( graph, level, true );
double globalMaxY = GraphFunctions.getMaxYPosition( graph, level, true );
if( globalMinY == globalMaxY )
{
globalMinY -= 10;
globalMaxY += 10;
}
if( globalMinX == globalMaxX )
{
globalMinX -= 10;
globalMaxX += 10;
}
double yBuffer = ( globalMaxY - globalMinY ) * pb.getWhiteSpaceBuffer();
double xBuffer = ( globalMaxX - globalMinX ) * pb.getWhiteSpaceBuffer();
DNVNode selectedNode = null;
globalMaxY += yBuffer;
globalMinY -= yBuffer;
globalMaxX += xBuffer;
globalMinX -= xBuffer;
if( !sameNode )
{
List<DNVNode> nodes = graph.getNodes( level );
SortByLabelSize sbls = new SortByLabelSize( pb.isHighlightNeighbors() );
Collections.sort( nodes, sbls );
DNVNode node;
Vector2D screenPosition;
double distance;
double minDistance = Integer.MAX_VALUE;
int nodeI = -1;
int distX = 0; // dist b/w this node and mouse click
int distY = 0;
// Check if user clicked on a solid node label
for( int i = nodes.size() - 1; i >= 0; i-- )
{
node = nodes.get( i );
if( node.isVisible() && ( node.isForceLabel() || pb.isShowLabels() ) && node.getProperty( "faded" ) == null )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX, minY, maxY,
width, height, node.getPosition( true ) );
ImageRenderer.Rectangle boundingRectangle = ImageRenderer.getRectangleBoundingTheLabel( node, screenPosition, null,
(int)Math.round( pb.getNodeSize() * node.getRadius() ), node.getLabel( pb.isInterpolationLabels() ), pb
.isCurvedLabels()
|| node.isCurvedLabel(), pb.getLabelSize(), minX, maxX, width / pb.getWidth(), pb.isScaleLabels(), pb
.getMaxLabelLength(), pb.getCurvedLabelAngle(), pb.isBoldLabels(), false, false );
if( mouseDownX >= boundingRectangle.left() && mouseDownX <= boundingRectangle.right()
&& mouseDownY <= boundingRectangle.bottom() && mouseDownY >= boundingRectangle.top() )
{
distX = (int)( mouseDownX - screenPosition.getX() );
distY = (int)( mouseDownY - screenPosition.getY() );
node.setProperty( "distX", "" + distX );
node.setProperty( "distY", "" + distY );
minDistance = 0;
nodeI = i;
break;
}
}
}
if( nodeI == -1 )
{
// loop thru all nodes to find closest node
for( int i = nodes.size() - 1; i >= 0; i-- )
{
node = nodes.get( i );
if( node.isVisible() )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX, minY,
maxY, width, height, node.getPosition( true ) );
// find node closest to mouseDown
distX = (int)( mouseDownX - screenPosition.getX() );
distY = (int)( mouseDownY - screenPosition.getY() );
distance = distX * distX + distY * distY;
if( distance < minDistance )
{
node.setProperty( "distX", "" + distX );
node.setProperty( "distY", "" + distY );
minDistance = distance;
nodeI = i;
}
}
}
}
if( nodes.size() > 0 && nodeI != -1 )
{
node = nodes.get( nodeI );
double nodeWidth;
nodeWidth = getNodeWidth( pb, width, minX, maxX, node.getRadius() );
// check if selected node is close enough to mouseDown
if( Settings.DEBUG )
System.out.println( "Minimum distance was " + Math.sqrt( minDistance ) );
if( Math.sqrt( minDistance ) >= nodeWidth )
{
// Still no node selected so check nodes with faded
// labels
for( int i = nodes.size() - 1; i >= 0; i-- )
{
node = nodes.get( i );
if( node.isVisible() && ( node.isForceLabel() || pb.isShowLabels() ) && node.getProperty( "faded" ) != null
&& Float.parseFloat( node.getProperty( "faded" ) ) > 0.1 )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX,
minY, maxY, width, height, node.getPosition( true ) );
ImageRenderer.Rectangle boundingRectangle = ImageRenderer.getRectangleBoundingTheLabel( node, screenPosition,
null, (int)Math.round( pb.getNodeSize() * node.getRadius() ),
node.getLabel( pb.isInterpolationLabels() ), pb.isCurvedLabels() || node.isCurvedLabel(), pb
.getLabelSize(), minX, maxX, width / pb.getWidth(), pb.isScaleLabels(), pb.getMaxLabelLength(),
pb.getCurvedLabelAngle(), pb.isBoldLabels(), false, false );
if( mouseDownX >= boundingRectangle.left() && mouseDownX <= boundingRectangle.right()
&& mouseDownY <= boundingRectangle.bottom() && mouseDownY >= boundingRectangle.top() )
{
distX = (int)( mouseDownX - screenPosition.getX() );
distY = (int)( mouseDownY - screenPosition.getY() );
node.setProperty( "distX", "" + distX );
node.setProperty( "distY", "" + distY );
minDistance = 0;
nodeI = i;
break;
}
}
}
}
node = nodes.get( nodeI );
nodeWidth = getNodeWidth( pb, width, minX, maxX, node.getRadius() );
// check if selected node is close enough to mouseDown
if( Settings.DEBUG )
System.out.println( "Minimum distance was " + Math.sqrt( minDistance ) );
if( Math.sqrt( minDistance ) < nodeWidth )
{
// if( node.isSelected() )
// {
// sameNode = true;
// }
pb.setSelectedNode( node, ctrlPressed );
selectedNode = node;
}
else
{
if( pb.getSelectedNode() != null )
{
pb.setSelectedNode( null, ctrlPressed );
// runDocumentTopicsCircularLayout( request, pb, graph, level );
}
}
}
if( selectedNode == null )
{
minDistance = Integer.MAX_VALUE;
List<DNVEdge> edges = graph.getEdges( level );
DNVEdge edge;
Vector2D screenPosition2;
int edgeI = 0;
for( int i = 0; i < edges.size(); i++ )
{
edge = edges.get( i );
if( edge.isVisible() )
{
screenPosition = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX, minY,
maxY, width, height, edge.getFrom().getPosition( true ) );
screenPosition2 = ImageRenderer.transformPosition( globalMinX, globalMaxX, globalMinY, globalMaxY, minX, maxX, minY,
maxY, width, height, edge.getTo().getPosition( true ) );
distance = getPointLineDistance( screenPosition, screenPosition2, mouseDownX, mouseDownY );
if( distance < minDistance )
{
minDistance = distance;
edgeI = i;
}
}
}
if( edges.size() > 0 )
{
edge = edges.get( edgeI );
double edgeWidth = Math.max( edge.getThickness(), 4 );
// check if selected node is close enough to
// mouseDown
if( Settings.DEBUG )
System.out.println( "Minimum distance was " + Math.sqrt( minDistance ) );
if( Math.sqrt( minDistance ) < edgeWidth / 2.0 )
{
if( edge.isSelected() )
{
sameNode = true;
}
pb.setSelectedEdge( edge, ctrlPressed );
}
else
{
pb.setSelectedEdge( null, ctrlPressed );
}
}
}
}
pickingTimer.setEnd();
if( Settings.DEBUG )
System.out.println( "Picking took " + pickingTimer.getLastSegment( Timer.SECONDS ) + " seconds." );
String releasedStr = request.getParameter( "released" );
boolean released = false;
if( releasedStr != null )
{
try
{
released = Boolean.parseBoolean( releasedStr );
}
catch( Exception e )
{}
}
moveSelectedNode( request, pb, graph, level, width, height, minX, minY, maxX, maxY, mouseUpX, mouseUpY, sameNode, globalMinX,
globalMaxX, globalMinY, globalMaxY, selectedNode, released );
}
// ------------------------------------
Timer paintTimer = new Timer( Timer.MILLISECONDS );
paintTimer.setStart();
response.setContentType( "image/gif" );
pb.paint( response.getOutputStream(), width, height, overview, rendering );
paintTimer.setEnd();
if( Settings.DEBUG && !overview && !pb.isRenderJS() )
System.out.println( "Drawing took " + paintTimer.getLastSegment( Timer.SECONDS ) + " seconds." );
}
catch( IOException e )
{
// e.printStackTrace();