📄 graph.java
字号:
loc.x = p.x-(size.width+BETA)/2 - size.width/2;
icon.setLocation(loc);
p.y = p0.y;
}
GraphNode[] siblings = self.getSiblings();
for(int i = 0; i < siblings.length; i++ ) {
if ( !doneList.contains(siblings[i]) )
computeHorizontalPosition(siblings[i],doneList,p);
}
return true;
}
public boolean computeVerticalPosition(GraphNode self, Vector doneList, Point p) {
GraphNode node;
Dimension size;
Point p0 = new Point(0,0);
Point p1 = new Point(0,0);
Point loc = new Point(0,0);
if ( doneList.contains(self) )
return false;
doneList.addElement(self);
GraphIcon icon = (GraphIcon)ViewList.get(self);
if ( icon == null )
return false;
if ( !icon.isVisible() )
return false;
GraphNode[] children = self.getChildren();
Vector Items = new Vector();
for(int i = 0; i < children.length; i++ ) {
if ( !doneList.contains(children[i]) )
Items.addElement(children[i]);
}
size = icon.getSize();
if ( Items.isEmpty() ) {
loc.x = p.x+(BETA+size.width)/2 - size.width/2;
loc.y = p.y+(3*GAMMA+size.height)/2 - size.height/2;
icon.setLocation(loc);
p.x += size.width+BETA;
}
else {
p0.x = p.x;
p0.y = p.y+size.height+3*GAMMA;
for(int i = 0; i < Items.size(); i++ ) {
node = (GraphNode) Items.elementAt(i);
computeVerticalPosition(node,doneList,p0);
}
loc.x = (p.x+p0.x)/2 - size.width/2;
loc.y = p.y+(size.height+3*GAMMA)/2 - size.height/2;
icon.setLocation(loc);
p.x = p0.x;
}
GraphNode[] siblings = self.getSiblings();
for(int i = 0; i < siblings.length; i++ ) {
if ( !doneList.contains(siblings[i]) )
computeVerticalPosition(siblings[i],doneList,p);
}
return true;
}
protected void drawLinks(GraphNode node, Graphics graphics) {
GraphIcon icon = (GraphIcon)ViewList.get(node);
Point p1 = new Point(0,0);
Point p2 = new Point(0,0);
Color color;
GraphNode[] parent = node.getParents();
for(int i = 0; i < parent.length; i++ ) {
if ( model.isLinkVisible(node,parent[i]) &&
getConnections(node,parent[i],p1,p2) ) {
color = model.getLinkColor(node,parent[i]);
drawEdge(color,p1,p2,graphics);
}
}
GraphNode[] children = node.getChildren();
for(int i = 0; i < children.length; i++ ) {
if ( model.isLinkVisible(node,children[i]) &&
getConnections(node,children[i],p1,p2) ) {
color = model.getLinkColor(node,children[i]);
drawEdge(color,p1,p2,graphics);
}
}
GraphNode[] sibling = node.getSiblings();
for(int i = 0; i < sibling.length; i++ ) {
if ( model.isLinkVisible(node,sibling[i]) &&
getConnections(node,sibling[i],p1,p2) ) {
color = model.getLinkColor(node,sibling[i]);
drawEdge(color,p1,p2,graphics);
}
}
}
protected void getConnection(Rectangle bb, int edge, Point p) {
switch( edge ) {
case BOTTOM_EDGE:
p.x = bb.x + bb.width/2;
p.y = bb.y;
return;
case TOP_EDGE:
p.x = bb.x + bb.width/2;
p.y = bb.y + bb.height;
return;
case LEFT_EDGE:
p.x = bb.x;
p.y = bb.y + bb.height/2;
return;
case RIGHT_EDGE:
p.x = bb.x + bb.width;
p.y = bb.y + bb.height/2;
return;
default:
return;
}
}
protected boolean getConnections(GraphNode node1, GraphNode node2,
Point p1, Point p2) {
Rectangle a, b;
GraphIcon icon1 = (GraphIcon)ViewList.get(node1);
GraphIcon icon2 = (GraphIcon)ViewList.get(node2);
if ( icon1 == null || icon2 == null ||
!icon1.isVisible() || !icon2.isVisible() )
return false;
a = icon1.getBounds();
b = icon2.getBounds();
switch( viewMode ) {
case HORIZONTAL_CHILD_PARENT:
case HORIZONTAL_PARENT_CHILD:
if ( b.x > a.x+a.width ) {
getConnection(a,RIGHT_EDGE,p1);
getConnection(b,LEFT_EDGE,p2);
return true;
}
else if ( b.x+b.width < a.x ) {
getConnection(a,LEFT_EDGE,p1);
getConnection(b,RIGHT_EDGE,p2);
return true;
}
else if ( b.y > a.y+a.height ) {
getConnection(a,TOP_EDGE,p1);
getConnection(b,BOTTOM_EDGE,p2);
return true;
}
else if ( b.y+b.height < a.y ) {
getConnection(a,BOTTOM_EDGE,p1);
getConnection(b,TOP_EDGE,p2);
return true;
}
// in case of overlapping rects -- don't draw links
return false;
default:
if ( b.y > a.y+a.height ) {
getConnection(a,TOP_EDGE,p1);
getConnection(b,BOTTOM_EDGE,p2);
return true;
}
else if ( b.y+b.height < a.y ) {
getConnection(a,BOTTOM_EDGE,p1);
getConnection(b,TOP_EDGE,p2);
return true;
}
else if ( b.x > a.x+a.width ) {
getConnection(a,RIGHT_EDGE,p1);
getConnection(b,LEFT_EDGE,p2);
return true;
}
else if ( b.x+b.width < a.x ) {
getConnection(a,LEFT_EDGE,p1);
getConnection(b,RIGHT_EDGE,p2);
return true;
}
return false;
}
}
protected void drawEdge(Color color, Point p1, Point p2, Graphics g) {
Point[] pts;
Color c = g.getColor();
g.setColor( color );
g.setPaintMode();
g.drawLine(p1.x,p1.y,p2.x,p2.y);
pts = ArrowData.getPoints((double)p1.x, (double)p1.y,
(double)p2.x, (double)p2.y);
for(int i = 0; i < 2; i++ )
g.drawLine(pts[i].x,pts[i].y,pts[i+1].x,pts[i+1].y);
g.setColor(c);
}
protected GraphIcon findIcon(Point pt) {
GraphIcon icon;
Rectangle region;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
region = icon.getBounds();
if ( region.contains(pt.x,pt.y) )
return icon;
}
return null;
}
// GraphIcon Events
public void locationChanged(GraphIconEvent evt) {
redraw();
}
public void performLeftMouseDClickAction(GraphIconEvent evt) {
// if appropriate, configure node editor
if ( nodeEditorComponent == null && isNodeEditable ) {
GraphIcon icon = (GraphIcon)evt.getSource();
GraphNode node = icon.getGraphNode();
if ( model.isNodeEditable(node) ) {
if ( nodeEditor == null ) {
nodeEditor = new DefaultGraphNodeEditor();
nodeEditor.addGraphNodeEditorListener(this);
}
nodeEditorComponent = nodeEditor.getNodeEditorComponent(this,node);
Component root = SwingUtilities.getRoot(this);
if ( !(nodeEditorComponent instanceof Window) ) {
if ( root instanceof JDialog )
pane = ((JDialog)root).getLayeredPane();
else if ( root instanceof JFrame )
pane = ((JFrame)root).getLayeredPane();
else
Core.ERROR(null,1,this);
pane.add(nodeEditorComponent,JLayeredPane.PALETTE_LAYER);
pane.moveToFront(nodeEditorComponent);
}
Point pt = SwingUtilities.convertPoint(icon,0,0,root);
nodeEditorComponent.setLocation(pt);
nodeEditorComponent.setVisible(true);
if ( nodeEditorComponent instanceof JComponent )
nodeEditorComponent.requestFocus();
}
}
}
public void performLeftMouseAction(GraphIconEvent evt) {
}
public void performRightMouseAction(GraphIconEvent evt) {
if ( !isLinkEditable ) return;
Point[] pts;
Graphics g = this.getGraphics();
Color color = g.getColor();
g.setColor(Color.black);
if (evt.isRightMousePressed() ){
if ( !(evt.getSource() instanceof GraphIcon) ) {
sourceIcon = null;
return;
}
sourceIcon = (GraphIcon)evt.getSource();
lastPoint = stretchedPoint= startPoint = SwingUtilities.convertPoint(
sourceIcon,evt.getPoint().x,evt.getPoint().y,this);
}
else if (evt.isRightMouseDragged() ){
if ( sourceIcon == null ) return;
lastPoint = stretchedPoint;
stretchedPoint = SwingUtilities.convertPoint(
sourceIcon,evt.getPoint().x,evt.getPoint().y,this);
g.setXORMode(this.getBackground());
g.drawLine(startPoint.x,startPoint.y,lastPoint.x,lastPoint.y);
g.drawLine(startPoint.x,startPoint.y,stretchedPoint.x,stretchedPoint.y);
}
else if (evt.isRightMouseReleased() ){
if ( sourceIcon == null ) return;
lastPoint = stretchedPoint;
stretchedPoint = SwingUtilities.convertPoint(
sourceIcon,evt.getPoint().x,evt.getPoint().y,this);
g.setXORMode(this.getBackground());
g.drawLine(startPoint.x,startPoint.y,lastPoint.x,lastPoint.y);
destIcon = findIcon(stretchedPoint);
if (destIcon == null) return;
g.drawLine(startPoint.x,startPoint.y,stretchedPoint.x,stretchedPoint.y);
pts = ArrowData.getPoints((double)startPoint.x, (double)startPoint.y,
(double)stretchedPoint.x, (double)stretchedPoint.y);
for(int i = 0; i < 2; i++ )
g.drawLine( pts[i].x, pts[i].y, pts[i+1].x, pts[i+1].y);
}
g.setColor(color);
}
public void performMiddleMouseAction(GraphIconEvent evt) {
}
// GraphNodeEditor events
public void graphNodeEditingStopped(GraphNodeEditorEvent evt) {
if ( nodeEditorComponent != null ) {
nodeEditorComponent.setVisible(false);
if ( !(nodeEditorComponent instanceof Window) )
pane.remove(nodeEditorComponent);
nodeEditorComponent = null;
model.setValue(evt.getNode(),evt.getValue());
}
}
public void graphNodeEditingCancelled(GraphNodeEditorEvent evt) {
if ( nodeEditorComponent != null ) {
nodeEditorComponent.setVisible(false);
if ( !(nodeEditorComponent instanceof Window) )
pane.remove(nodeEditorComponent);
nodeEditorComponent = null;
}
}
// GraphModel Events
public void graphStructureChanged(GraphModelEvent evt) {
if ( model == evt.getModel() )
reset();
}
public void graphNodeAdded(GraphModelEvent evt) {
if ( model == evt.getModel() ) {
addNode(evt.getNode());
redraw();
}
}
public void graphNodeRemoved(GraphModelEvent evt) {
if ( model == evt.getModel() ) {
removeNode(evt.getNode());
redraw();
}
}
public void graphNodeStateChanged(GraphModelEvent evt) {
if ( model == evt.getModel() ) {
updateNode(evt.getNode());
redraw();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -