📄 edgebase.java
字号:
fClickCount = clickCount;
return newNode;
}
}
if ( occupies ) {
fClickCount = clickCount;
return null;
}
// setting n2 to the first node now
n1 = n2;
}
fClickCount = -1;
return null;
}
/**
* Adds a node to the list of nodes on this edge.
* @param node Node to be added.
* @param n1 Behind this node <code>node</code> will be added to
* the list of nodes.
*/
private void addNode( NodeOnEdge node, NodeOnEdge n1 ) {
fNodesOnEdge.add( fNodesOnEdge.indexOf( n1 )+1, node );
reIDNodes();
}
/**
* Resets the ids of all nodes on this edge. The nodes have
* allways an increasing ID starting at the source node.
*/
void reIDNodes() {
int counter = INITIAL_COUNTER;
Iterator it = fNodesOnEdge.iterator();
while ( it.hasNext() ) {
NodeOnEdge n = (NodeOnEdge) it.next();
n.setID( counter );
counter++;
}
}
public boolean isNodeSpecial( NodeOnEdge node ) {
if ( node.getSpecialID() == EdgeBase.SOURCE
|| node.getSpecialID() == EdgeBase.TARGET
|| node.getSpecialID() == EdgeBase.ASSOC_CLASS
|| node.getSpecialID() == EdgeBase.ASSOC_CLASS_CON
|| node.getSpecialID() == EdgeBase.REFLEXIVE_1
|| node.getSpecialID() == EdgeBase.REFLEXIVE_2
|| node.getSpecialID() == EdgeBase.REFLEXIVE_3 ) {
return true;
}
return false;
}
private boolean shouldNodeBeMoveableRightNow( NodeOnEdge node ) {
if ( node.getSpecialID() == EdgeBase.SOURCE // source node
|| node.getSpecialID() == EdgeBase.TARGET // target node
|| node.getSpecialID() == EdgeBase.ASSOC_CLASS // associactioclass node
|| ( node.getSpecialID() == EdgeBase.ASSOC_CLASS_CON // associationclass
&& getNodesOnEdge().size() <= 3 )
|| ( isReflexive() // reflexive edge
&& getNodesOnEdge().size() <= 5 )
|| ( isReflexive() && this instanceof NodeEdge // selfreflexive associationclass
&& getNodesOnEdge().size() <= 6 ) ) {
return false;
}
return true;
}
/**
* Removes a node from the edge.
* @param node Node to be removed.
*/
public void removeNodeOnEdge( NodeOnEdge node ) {
if ( isNodeSpecial( node ) ) {
return;
}
fNodesOnEdge.remove( node );
}
/**
* Checkes if there is a node laying under the x,y coordinate.
*
* @return true if the position occupies a node otherwise false.
*/
public boolean occupiesNodeOnEdge( int x, int y ) {
Iterator it = fNodesOnEdge.iterator();
while ( it.hasNext() ) {
NodeOnEdge node = (NodeOnEdge) it.next();
if ( node.occupies( x, y )
&& ( !isNodeSpecial( node ) || shouldNodeBeMoveableRightNow( node ) ) ) {
return true;
}
}
return false;
}
/**
* Returns the node laying under the x,y coordinate, otherwise null
* is returnd.
*/
public EdgeProperty getNodeOnEdge( int x, int y ) {
Iterator it = fNodesOnEdge.iterator();
while ( it.hasNext() ) {
NodeOnEdge node = (NodeOnEdge) it.next();
if ( node.occupies( x, y ) ) {
return node;
}
}
return null;
}
/**
* Sets the correct intersection points on the target and source
* nodes.
*/
private void updateNodeOnEdges() {
NodeOnEdge n1 = (NodeOnEdge) fNodesOnEdge.get( 1 );
Point2D sp = getIntersectionCoordinate( fSource, fX1, fY1,
(int) n1.x(), (int) n1.y() );
fSNode.setPosition( sp.getX(), sp.getY() );
NodeOnEdge n2 = (NodeOnEdge) fNodesOnEdge.get( fNodesOnEdge.size()-2 );
Point2D tp = getIntersectionCoordinate( fTarget, fX2, fY2,
(int) n2.x(), (int) n2.y() );
fTNode.setPosition( tp.getX(), tp.getY() );
}
/**
* Sets the start and end point of this Edge.
*/
void setPoint( int side, double x, double y ) {
switch ( side ) {
case SOURCE:
fX1 = (int) x;
fY1 = (int) y;
fSNode.setPosition( fX1, fY1 );
if ( fAssocName != null ) {
fAssocName.updateSourceEdgePoint( x, y );
}
if ( fSourceRolename != null ) {
fSourceRolename.updateSourceEdgePoint( x, y );
}
if ( fTargetRolename != null ) {
fTargetRolename.updateTargetEdgePoint( x, y );
}
if ( fSourceMultiplicity != null ) {
fSourceMultiplicity.updateSourceEdgePoint( x, y );
}
if ( fTargetMultiplicity != null ) {
fTargetMultiplicity.updateTargetEdgePoint( x, y );
}
break;
case TARGET:
fX2 = (int) x;
fY2 = (int) y;
fTNode.setPosition( fX2, fY2 );
if ( fAssocName != null ) {
fAssocName.updateTargetEdgePoint( x, y );
}
if ( fSourceRolename != null ) {
fSourceRolename.updateTargetEdgePoint( x, y );
}
if ( fTargetRolename != null ) {
fTargetRolename.updateSourceEdgePoint( x, y );
}
if ( fSourceMultiplicity != null ) {
fSourceMultiplicity.updateTargetEdgePoint( x, y );
}
if ( fTargetMultiplicity != null ) {
fTargetMultiplicity.updateSourceEdgePoint( x, y );
}
break;
default:
break;
}
}
/**
* @return Returns the fSourceRolename.
*/
public EdgeProperty getSourceRolename() {
return fSourceRolename;
}
/**
* @return Returns the fTargetRolename.
*/
public EdgeProperty getTargetRolename() {
return fTargetRolename;
}
/**
* @return Returns the fSourceMultiplicity.
*/
public EdgeProperty getSourceMultiplicity() {
return fSourceMultiplicity;
}
/**
* @return Returns the fTargetMultiplicity.
*/
public EdgeProperty getTargetMultiplicity() {
return fTargetMultiplicity;
}
/**
* @return Returns the fAssocName.
*/
public EdgeProperty getAssocName() {
return fAssocName;
}
/**
* Returns true if the source or target of this edge is an
* ObjectNode otherwise the method returns false.
*/
protected boolean isUnderlinedLabel() {
return fSource instanceof ObjectNode || fTarget instanceof ObjectNode;
}
/**
* Checks if there is more than one edge between two nodes.
*/
public Set checkForNewPositionAndDraw( DirectedGraph graph, Graphics g,
FontMetrics fm ) {
Set edges = null;
if ( graph.existsPath( fSource, fTarget ) ) {
edges = graph.edgesBetween( fSource, fTarget );
calculateNewPosition( edges );
}
if ( edges != null ) {
Iterator it = edges.iterator();
while ( it.hasNext() ) {
EdgeBase e = (EdgeBase) it.next();
e.draw( g, fm );
}
}
return edges;
}
/**
* Calculates the space between the lines if there are more than one
* edge between two nodes.
*/
private double calculateSpaces( double length, Set edges ) {
return length / ( (double) edges.size() + 1 );
}
/**
* Calculates and sets the new position of the edges between two
* nodes.
*/
private void calculateNewPosition( Set edges ) {
Polygon sourceRec = fSource.dimension();
Polygon targetRec = fTarget.dimension();
double sWidth = sourceRec.getBounds().getWidth();
double sHeight = sourceRec.getBounds().getHeight();
double tWidth = targetRec.getBounds().getWidth();
double tHeight = targetRec.getBounds().getHeight();
// midpoints
double sX = fSource.x();
double sY = fSource.y();
double tX = fTarget.x();
double tY = fTarget.y();
// source corner points
double uLeftX = sX - sWidth / 2.0;
double uRightX = sX + sWidth / 2.0;
double lLeftX = sX - sWidth / 2.0;
double lRightX = sX + sWidth / 2.0;
double uLeftY = sY - sHeight / 2.0;
double uRightY = sY - sHeight / 2.0;
double lLeftY = sY + sHeight / 2.0;
double lRightY = sY + sHeight / 2.0;
double space = 0;
double projection = 0;
// line from midpoint to midpoint
Line2D.Double line = new Line2D.Double( fSource.x(), fSource.y(),
fTarget.x(), fTarget.y() );
// if there is just one link between two objects set the
// midpoint of the objects as start and end point of the
// link. Otherwise calculate the new positions.
if ( edges.size() == 1 ) {
Iterator it = edges.iterator();
while ( it.hasNext() ) {
EdgeBase e = (EdgeBase) it.next();
// edge is reflexive
if ( isReflexive() ) {
setCorrectPoints( sX + sWidth/3, sY - sHeight/2,
tX + tWidth/2, tY - 4, e );
updateNodeOnEdges();
} else {
setCorrectPoints( fSource.x(), fSource.y(),
fTarget.x(), fTarget.y(), e );
updateNodeOnEdges();
}
}
} else {
// there are more then just one link between source and target
// node.
// there are up to four reflexive edges
if ( isReflexive() ) {
Iterator it = edges.iterator();
int counter = 0;
while ( it.hasNext() ) {
EdgeBase e = (EdgeBase) it.next();
counter++;
switch ( counter ) {
case 1:
setCorrectPoints( sX + sWidth/3, sY - sHeight/2,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -