📄 graph.java
字号:
case VERTICAL_CHILD_PARENT:
p.x = ORG_DX; p.y = ORG_DY;
enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( icon.isVisible() && node.getNodeType() == GraphNode.PARENT )
computeVerticalPosition(node,doneList,p);
}
enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( icon.isVisible() && node.getNodeType() == GraphNode.CHILD &&
!doneList.contains(node) )
computeVerticalPosition(node,doneList,p);
}
if ( viewMode == VERTICAL_PARENT_CHILD )
break;
rect = getDrawArea(doneList);
int ym = rect.y + rect.height/2;
enum = doneList.elements();
while( enum.hasMoreElements() ) {
node = (GraphNode)enum.nextElement();
icon = (GraphIcon)ViewList.get(node);
bb = icon.getBounds();
pt = new Point(bb.x,bb.y+bb.height);
// transform
pt = new Point(pt.x, 2*ym - pt.y);
// set new Location
icon.setLocation(pt);
}
break;
case HORIZONTAL_PARENT_CHILD:
case HORIZONTAL_CHILD_PARENT:
enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( icon.isVisible() && node.getNodeType() == GraphNode.PARENT )
computeHorizontalPosition(node,doneList,p);
}
enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( icon.isVisible() && node.getNodeType() == GraphNode.CHILD &&
!doneList.contains(node) )
computeHorizontalPosition(node,doneList,p);
}
rect = getDrawArea(doneList);
enum = doneList.elements();
while( enum.hasMoreElements() ) {
node = (GraphNode)enum.nextElement();
icon = (GraphIcon)ViewList.get(node);
pt = icon.getLocation();
icon.setLocation(pt.x-rect.x+ORG_DX,pt.y-rect.y+ORG_DY);
}
if ( viewMode == HORIZONTAL_CHILD_PARENT )
break;
rect = getDrawArea(doneList);
int xm = rect.x + rect.width/2;
enum = doneList.elements();
while( enum.hasMoreElements() ) {
node = (GraphNode)enum.nextElement();
icon = (GraphIcon)ViewList.get(node);
bb = icon.getBounds();
pt = new Point(bb.x + bb.width, bb.y);
// transform
pt = new Point(2*xm - pt.x + ORG_DX, pt.y - ORG_DY);
icon.setLocation(pt);
}
break;
case CENTRED:
GraphNode[] nodes = getSelectedNodes();
node = null;
boolean found = false;
if ( nodes.length > 0 ) {
for(int i = 0; !found && i < nodes.length; i++ ) {
node = nodes[i];
icon = (GraphIcon)ViewList.get(node);
found = icon.isVisible();
}
}
if ( !found && !ViewList.isEmpty() ) {
enum = ViewList.keys();
while( !found && enum.hasMoreElements() ) {
node = (GraphNode)enum.nextElement();
icon = (GraphIcon)ViewList.get(node);
found = icon.isVisible();
}
}
if ( !found ) break;
GraphNode middleNode = node;
enum = ViewList.keys();
while( enum.hasMoreElements() ) {
node = (GraphNode)enum.nextElement();
icon = (GraphIcon)ViewList.get(node);
if ( icon.isVisible() )
doneList.addElement(node);
}
centerView(middleNode,doneList);
break;
case CIRCLES:
p.x = ORG_DX; p.y = ORG_DY;
enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( icon.isVisible() )
computeCircularPosition(node,doneList,p);
}
break;
}
redraw();
}
protected Rectangle getDrawArea(Vector doneList) {
Rectangle rect = new Rectangle(0,0,0,0);
Enumeration enum = ViewList.elements();
GraphIcon icon;
GraphNode node;
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( doneList.contains(node) )
rect = rect.union(icon.getBounds());
}
return rect;
}
public void redraw() {
invalidate();
repaint();
}
public void paintComponent(Graphics g){
Dimension d = this.getSize();
super.paintComponent(g);
drawLinks(g);
}
protected void drawLinks(Graphics graphics) {
GraphNode node;
GraphIcon icon;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
node = icon.getGraphNode();
if ( icon.isVisible() )
drawLinks(node,graphics);
}
revalidate();
}
protected void getRelations(Vector doList, Vector doneList) {
Vector items;
GraphNode node;
for(int i = 0; i < doList.size(); i++ ) {
node = (GraphNode)doList.elementAt(i);
if ( !doneList.contains(node) ) {
doneList.addElement(node);
items = model.getViewRelations(node);
items = Misc.difference(items,doneList);
getRelations(items,doneList);
}
}
}
protected boolean computeCircularPosition(GraphNode self, Vector doneList,
Point p) {
if ( doneList.contains(self) ) return false;
GraphIcon icon;
GraphNode node;
Dimension size;
int Wm, Hm, Wt, Ht, R;
double theta, phi;
int xx, yy;
Point pa = new Point(0,0);
Vector aList = new Vector();
aList.addElement(self);
getRelations(model.getViewRelations(self),aList);
// remove invisible items from aList;
// and simultaneously add all items in aList to the doneList
for(int i = 0; i < aList.size(); i++ ) {
node = (GraphNode)aList.elementAt(i);
icon = (GraphIcon)ViewList.get(node);
doneList.addElement(node);
if ( !icon.isVisible() )
aList.removeElementAt(i--);
}
if ( aList.size() <= 2 ) {
for(int i = 0; i < aList.size(); i++ ) {
node = (GraphNode)aList.elementAt(i);
icon = (GraphIcon)ViewList.get(node);
size = icon.getSize();
pa.x = p.x+(BETA+size.width)/2;
pa.y = p.y+(3*GAMMA+size.height)/2;
icon.setLocation(new Point(pa.x - size.width/2, pa.y - size.height/2));
p.x += size.width+BETA;
}
return true;
}
Dimension[] wh = new Dimension[aList.size()];
Wm = Hm = 0;
for(int i = 0; i < aList.size(); i++ ) {
node = (GraphNode)aList.elementAt(i);
icon = (GraphIcon)ViewList.get(node);
wh[i] = icon.getSize();
Wm = Math.max(Wm,wh[i].width);
Hm = Math.max(Hm,wh[i].height);
}
R = (int)(HEIGHT_FACTOR*Hm/Math.tan(Math.PI/aList.size()));
Wt = (2*Wm+BETA+2*R);
Ht = (2*Hm+GAMMA+2*R);
xx = p.x + Wt/2;
yy = p.y + Ht/2;
p.x += Wt;
theta = (2*Math.PI/aList.size());
for(int i = 0; i < aList.size(); i++ ) {
phi = i*theta;
pa.x = (int)(xx + R*Math.cos(phi)) - wh[i].width/2;
pa.y = (int)(yy + R*Math.sin(phi)) - wh[i].height/2;
node = (GraphNode)aList.elementAt(i);
icon = (GraphIcon)ViewList.get(node);
icon.setLocation(pa);
}
return true;
}
protected boolean centerView(GraphNode self, Vector aList) {
/**
Centers everyone about self.
Only visible nodes are passed to this method.
*/
GraphIcon icon = (GraphIcon)ViewList.get(self);
if ( icon == null )
return false;
Dimension ps;
if ( (getParent()).getClass() == JViewport.class ) {
// If the panel is in a scrollpane
JViewport viewport = (JViewport)getParent();
viewport.setViewPosition(new Point(0,0));
ps = viewport.getSize();
}
else {
// The panel is not in a scrollpane
ps = getSize();
}
Dimension ms = icon.getSize();
int R = Math.min(ps.width,ps.height)/2;
icon.setLocation(new Point(ps.width/2 - ms.width/2,
ps.height/2 - ms.height/2));
int sr = (ms.width+ms.height)/2;
int n = 0;
GraphNode node;
for(int i = 0; i < aList.size(); i++ ) {
node = (GraphNode)aList.elementAt(i);
if ( node != self && (icon = (GraphIcon)ViewList.get(node)) != null ) {
ms = icon.getSize();
sr += (ms.width+ms.height)/2;
n++;
}
}
if ( n != 0 ) {
R -= sr/n;
double phi;
Point pa = new Point(0,0);
double theta = (2*Math.PI/n);
int j = 0;
for(int i = 0; i < aList.size(); i++ ) {
node = (GraphNode)aList.elementAt(i);
if ( node != self && (icon = (GraphIcon)ViewList.get(node)) != null ) {
phi = j*theta;
pa.x = (int)(ps.width/2 + R*Math.cos(phi));
pa.y = (int)(ps.height/2 + R*Math.sin(phi));
ms = icon.getSize();
icon.setLocation(new Point(pa.x - ms.width/2,pa.y - ms.height/2));
j++;
}
}
}
return false;
}
public boolean computeHorizontalPosition(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.y = p.y+(3*GAMMA+size.height)/2 - size.height/2;
loc.x = p.x-(BETA+size.width)/2 - size.width/2;
icon.setLocation(loc);
p.y += size.height+3*GAMMA;
}
else {
p0.y = p.y;
p0.x = p.x-(size.width+BETA);
for(int i = 0; i < Items.size(); i++ ) {
node = (GraphNode) Items.elementAt(i);
computeHorizontalPosition(node,doneList,p0);
}
loc.y = (p.y+p0.y)/2 - size.height/2;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -