Connection.js
Summary
No overview generated for 'Connection.js'
draw2d.Connection=function()
{
draw2d.Line.call(this);
this.sourcePort = null;
this.targetPort = null;
this.sourceDecorator = null;
this.targetDecorator = null;
this.sourceAnchor = new draw2d.ConnectionAnchor();
this.targetAnchor = new draw2d.ConnectionAnchor();
this.router = draw2d.Connection.defaultRouter;
this.lineSegments = new draw2d.ArrayList();
this.children = new draw2d.ArrayList();
this.setColor(new draw2d.Color(0,0,115));
this.setLineWidth(1);
}
draw2d.Connection.prototype = new draw2d.Line;
draw2d.Connection.defaultRouter = new draw2d.ManhattanConnectionRouter();
draw2d.Connection.setDefaultRouter=function( router)
{
draw2d.Connection.defaultRouter = router;
}
draw2d.Connection.prototype.disconnect=function()
{
if(this.sourcePort!=null)
{
this.sourcePort.detachMoveListener(this);
this.fireSourcePortRouteEvent();
}
if(this.targetPort!=null)
{
this.targetPort.detachMoveListener(this);
this.fireTargetPortRouteEvent();
}
}
draw2d.Connection.prototype.reconnect=function()
{
if(this.sourcePort!=null)
{
this.sourcePort.attachMoveListener(this);
this.fireSourcePortRouteEvent();
}
if(this.targetPort!=null)
{
this.targetPort.attachMoveListener(this);
this.fireTargetPortRouteEvent();
}
}
draw2d.Connection.prototype.isResizeable=function()
{
return true;
}
draw2d.Connection.prototype.addFigure=function( figure, locator)
{
var entry = new Object();
entry.figure = figure;
entry.locator = locator;
this.children.add(entry);
if(this.graphics !=null)
this.paint();
}
draw2d.Connection.prototype.setSourceDecorator=function( decorator)
{
this.sourceDecorator = decorator;
if(this.graphics !=null)
this.paint();
}
draw2d.Connection.prototype.setTargetDecorator=function( decorator)
{
this.targetDecorator = decorator;
if(this.graphics !=null)
this.paint();
}
draw2d.Connection.prototype.setSourceAnchor=function( anchor)
{
this.sourceAnchor = anchor;
this.sourceAnchor.setOwner(this.sourcePort);
if(this.graphics !=null)
this.paint();
}
draw2d.Connection.prototype.setTargetAnchor=function( anchor)
{
this.targetAnchor = anchor;
this.targetAnchor.setOwner(this.targetPort);
if(this.graphics !=null)
this.paint();
}
draw2d.Connection.prototype.setRouter=function( router)
{
if(router !=null)
this.router = router;
else
this.router = new draw2d.NullConnectionRouter();
if(this.graphics !=null)
this.paint();
}
draw2d.Connection.prototype.getRouter=function()
{
return this.router;
}
draw2d.Connection.prototype.paint=function()
{
for(var i=0; i<this.children.getSize();i++)
{
var entry = this.children.get(i);
if(entry.isAppended==true)
this.html.removeChild(entry.figure.getHTMLElement());
entry.isAppended=false;
}
if(this.graphics ==null)
this.graphics = new jsGraphics(this.id);
else
this.graphics.clear();
this.graphics.setStroke(this.stroke);
this.graphics.setColor(this.lineColor.getHTMLStyle());
this.startStroke();
this.router.route(this);
if(this.getSource().getParent().isMoving==false && this.getTarget().getParent().isMoving==false )
{
if(this.targetDecorator!=null)
this.targetDecorator.paint(new draw2d.Graphics(this.graphics,this.getEndAngle(),this.getEndPoint()));
if(this.sourceDecorator!=null)
this.sourceDecorator.paint(new draw2d.Graphics(this.graphics,this.getStartAngle(),this.getStartPoint()));
}
this.finishStroke();
for(var i=0; i<this.children.getSize();i++)
{
var entry = this.children.get(i);
this.html.appendChild(entry.figure.getHTMLElement());
entry.isAppended=true;
entry.locator.relocate(entry.figure);
}
}
draw2d.Connection.prototype.getStartPoint= function()
{
if(this.isMoving==false)
return this.sourceAnchor.getLocation(this.targetAnchor.getReferencePoint());
else
return draw2d.Line.prototype.getStartPoint.call(this);
}
draw2d.Connection.prototype.getEndPoint= function()
{
if(this.isMoving==false)
return this.targetAnchor.getLocation(this.sourceAnchor.getReferencePoint());
else
return draw2d.Line.prototype.getEndPoint.call(this);
}
draw2d.Connection.prototype.startStroke=function()
{
this.oldPoint=null;
this.lineSegments = new draw2d.ArrayList();
}
draw2d.Connection.prototype.finishStroke=function()
{
this.graphics.paint();
this.oldPoint=null;
}
draw2d.Connection.prototype.getPoints=function()
{
var result = new draw2d.ArrayList();
var line;
for(var i = 0; i< this.lineSegments.getSize();i++)
{
line = this.lineSegments.get(i);
result.add(line.start);
}
result.add(line.end);
return result;
}
draw2d.Connection.prototype.addPoint=function( p)
{
p = new draw2d.Point(parseInt(p.x), parseInt(p.y));
if(this.oldPoint!=null)
{
this.graphics.drawLine(this.oldPoint.x,this.oldPoint.y, p.x, p.y);
var line = new Object();
line.start = this.oldPoint;
line.end = p;
this.lineSegments.add(line);
}
this.oldPoint = new Object();
this.oldPoint.x = p.x;
this.oldPoint.y = p.y;
}
draw2d.Connection.prototype.setSource=function( port)
{
if(this.sourcePort!=null)
this.sourcePort.detachMoveListener(this);
this.sourcePort = port;
if(this.sourcePort==null)
return;
this.sourceAnchor.setOwner(this.sourcePort);
this.fireSourcePortRouteEvent();
this.sourcePort.attachMoveListener(this);
this.setStartPoint(port.getAbsoluteX(), port.getAbsoluteY());
}
draw2d.Connection.prototype.getSource=function()
{
return this.sourcePort;
}
draw2d.Connection.prototype.setTarget=function( port)
{
if(this.targetPort!=null)
this.targetPort.detachMoveListener(this);
this.targetPort = port;
if(this.targetPort==null)
return;
this.targetAnchor.setOwner(this.targetPort);
this.fireTargetPortRouteEvent();
this.targetPort.attachMoveListener(this);
this.setEndPoint(port.getAbsoluteX(), port.getAbsoluteY());
}
draw2d.Connection.prototype.getTarget=function()
{
return this.targetPort;
}
draw2d.Connection.prototype.onOtherFigureMoved=function( figure)
{
if(figure==this.sourcePort)
this.setStartPoint(this.sourcePort.getAbsoluteX(), this.sourcePort.getAbsoluteY());
else
this.setEndPoint(this.targetPort.getAbsoluteX(), this.targetPort.getAbsoluteY());
}
draw2d.Connection.prototype.containsPoint= function( px, py)
{
for(var i = 0; i< this.lineSegments.getSize();i++)
{
var line = this.lineSegments.get(i);
if(draw2d.Line.hit(line.start.x,line.start.y,line.end.x, line.end.y, px,py))
return true;
}
return false;
}
draw2d.Connection.prototype.getStartAngle=function()
{
var p1 = this.lineSegments.get(0).start;
var p2 = this.lineSegments.get(0).end;
if(this.router instanceof draw2d.BezierConnectionRouter)
{
p2 = this.lineSegments.get(5).end;
}
var length = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
var angle = -(180/Math.PI) *Math.asin((p1.y-p2.y)/length);
if(angle<0)
{
if(p2.x<p1.x)
angle = Math.abs(angle) + 180;
else
angle = 360- Math.abs(angle);
}
else
{
if(p2.x<p1.x)
angle = 180-angle;
}
return angle;
}
draw2d.Connection.prototype.getEndAngle=function()
{
var p1 = this.lineSegments.get(this.lineSegments.getSize()-1).end;
var p2 = this.lineSegments.get(this.lineSegments.getSize()-1).start;
if(this.router instanceof draw2d.BezierConnectionRouter)
{
p2 = this.lineSegments.get(this.lineSegments.getSize()-5).end;
}
var length = Math.sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
var angle = -(180/Math.PI) *Math.asin((p1.y-p2.y)/length);
if(angle<0)
{
if(p2.x<p1.x)
angle = Math.abs(angle) + 180;
else
angle = 360- Math.abs(angle);
}
else
{
if(p2.x<p1.x)
angle = 180-angle;
}
return angle;
}
draw2d.Connection.prototype.fireSourcePortRouteEvent=function()
{
var connections = this.sourcePort.getConnections();
for(var i=0; i<connections.getSize();i++)
{
connections.get(i).paint();
}
}
draw2d.Connection.prototype.fireTargetPortRouteEvent=function()
{
var connections = this.targetPort.getConnections();
for(var i=0; i<connections.getSize();i++)
{
connections.get(i).paint();
}
}
Documentation generated by
JSDoc on Thu Feb 7 23:45:47 2008