📄 graph.java
字号:
/*
* The contents of this file are subject to the BT "ZEUS" Open Source
* Licence (L77741), Version 1.0 (the "Licence"); you may not use this file
* except in compliance with the Licence. You may obtain a copy of the Licence
* from $ZEUS_INSTALL/licence.html or alternatively from
* http://www.labs.bt.com/projects/agents/zeus/licence.htm
*
* Except as stated in Clause 7 of the Licence, software distributed under the
* Licence is distributed WITHOUT WARRANTY OF ANY KIND, either express or
* implied. See the Licence for the specific language governing rights and
* limitations under the Licence.
*
* The Original Code is within the package zeus.*.
* The Initial Developer of the Original Code is British Telecommunications
* public limited company, whose registered office is at 81 Newgate Street,
* London, EC1A 7AJ, England. Portions created by British Telecommunications
* public limited company are Copyright 1996-9. All Rights Reserved.
*
* THIS NOTICE MUST BE INCLUDED ON ANY COPY OF THIS FILE
*/
package zeus.gui.graph;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.event.*;
import zeus.util.*;
public class Graph extends MovePanel
implements GraphIconListener,
GraphModelListener,
GraphNodeEditorListener {
public static final int VERTICAL_PARENT_CHILD = 0;
public static final int VERTICAL_CHILD_PARENT = 1;
public static final int HORIZONTAL_PARENT_CHILD = 2;
public static final int HORIZONTAL_CHILD_PARENT = 3;
public static final int CENTRED = 4;
public static final int CIRCLES = 5;
protected static final int ORG_DX = 20;
protected static final int ORG_DY = 20;
protected static final double HEIGHT_FACTOR = 2.0;
protected static final int BETA = 30;
protected static final int GAMMA = 20;
protected static final int BOTTOM_EDGE = 1;
protected static final int TOP_EDGE = 2;
protected static final int LEFT_EDGE = 3;
protected static final int RIGHT_EDGE = 4;
protected Hashtable ViewList = new Hashtable();
protected GraphModel model;
protected GraphNodeRenderer nodeRenderer = null;
protected GraphNodeEditor nodeEditor = null;
protected Component nodeEditorComponent = null;
protected JLayeredPane pane = null;
protected boolean isNodeEditable;
protected boolean isLinkEditable;
protected int viewMode;
private Point startPoint = null;
private Point lastPoint = null;
private Point stretchedPoint= null;
private GraphIcon sourceIcon = null;
private GraphIcon destIcon = null;
public Graph(int viewMode, GraphModel model, boolean isNodeEditable,
boolean isLinkEditable) {
super(2000,2000);
this.setLayout(new BulletinLayout());
this.model = model;
this.isNodeEditable = isNodeEditable;
this.isLinkEditable = isLinkEditable;
this.viewMode = viewMode;
model.addGraphModelListener(this);
}
public Graph(GraphModel model, boolean isNodeEditable,
boolean isLinkEditable) {
this(HORIZONTAL_CHILD_PARENT,model,isNodeEditable,isLinkEditable);
}
public Graph(GraphModel model) {
this(HORIZONTAL_CHILD_PARENT,model,true,true);
}
public void setModel(GraphModel model) {
this.model.removeGraphModelListener(this);
this.model = model;
this.model.addGraphModelListener(this);
reset();
}
public GraphModel getModel() { return model; }
public void setViewMode(int mode) {
switch( mode ) {
case HORIZONTAL_PARENT_CHILD:
case HORIZONTAL_CHILD_PARENT:
case VERTICAL_PARENT_CHILD:
case VERTICAL_CHILD_PARENT:
case CIRCLES:
case CENTRED:
viewMode = mode;
break;
default:
Core.USER_ERROR("Attempt to set an illegal view mode: " + mode);
break;
}
}
public GraphNodeRenderer getNodeRenderer() {
return nodeRenderer;
}
public void setNodeRenderer(GraphNodeRenderer nodeRenderer) {
this.nodeRenderer = nodeRenderer;
}
public GraphNodeEditor getNodeEditor() {
return nodeEditor;
}
public void setNodeEditor(GraphNodeEditor nodeEditor) {
if ( this.nodeEditor != null )
this.nodeEditor.removeGraphNodeEditorListener(this);
this.nodeEditor = nodeEditor;
if ( this.nodeEditor != null )
this.nodeEditor.addGraphNodeEditorListener(this);
}
public void addNotify() {
super.addNotify();
reset();
}
public boolean isVisible(GraphNode node) {
GraphIcon icon = (GraphIcon)ViewList.get(node);
return icon != null && icon.isVisible();
}
public Rectangle getBounds(GraphNode node) {
GraphIcon icon = (GraphIcon)ViewList.get(node);
if ( icon != null )
return icon.getBounds();
else {
Core.USER_ERROR("getBounds called on a null node");
return new Rectangle(0,0,0,0);
}
}
protected void reset() {
// first remove graph from icon listenerList
GraphIcon icon;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
icon.removeGraphIconListener(this);
}
// next clear ViewList
ViewList.clear();
this.removeAll();
// recreate graph
enum = model.nodes();
while( enum.hasMoreElements() )
addNode((GraphNode)enum.nextElement());
recompute();
}
protected void addNode(GraphNode node) {
GraphIcon icon;
Rectangle rect = new Rectangle(0,0,0,0);
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
rect = rect.union(icon.getBounds());
}
icon = new GraphIcon(node,this);
ViewList.put(node,icon);
this.add(icon);
icon.setLocation(new Point(rect.x+rect.width,rect.y));
icon.addGraphIconListener(this);
}
protected void removeNode(GraphNode node) {
GraphIcon icon = (GraphIcon)ViewList.remove(node);
if ( icon != null ) {
this.remove(icon);
icon.removeGraphIconListener(this);
}
}
protected void updateNode(GraphNode node) {
GraphIcon icon = (GraphIcon)ViewList.get(node);
if ( icon != null )
icon.reset();
}
public void hide() {
GraphIcon icon;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
if ( icon.isSelected() ) icon.setVisible(false);
}
repaint();
}
public void show() {
GraphIcon icon;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
if ( !icon.isVisible() ) icon.setVisible(true);
}
repaint();
}
public void collapse() {
GraphIcon icon;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
if ( icon.isSelected() )
collapseNode(icon.getGraphNode(), new Vector());
}
repaint();
}
public void expand() {
GraphIcon icon;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
if ( icon.isSelected() )
expandNode(icon.getGraphNode(), new Vector());
}
repaint();
}
public GraphNode[] getSelectedNodes() {
GraphIcon icon;
Vector items = new Vector();
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
if ( icon.isSelected() )
items.addElement(icon);
}
GraphNode[] out = new GraphNode[items.size()];
for(int i = 0; i < out.length; i++ )
out[i] = ((GraphIcon)items.elementAt(i)).getGraphNode();
return out;
}
protected void collapseNode(GraphNode node, Vector doneList) {
GraphNode[] children = node.getChildren();
doneList.addElement(node);
for(int i = 0; i < children.length; i++ ) {
GraphIcon icon = (GraphIcon)ViewList.get(children[i]);
if ( icon != null ) {
icon.setVisible(false);
icon.setSelected(false);
if ( !doneList.contains(children[i]) )
collapseNode(children[i],doneList);
}
}
}
protected void expandNode(GraphNode node, Vector doneList) {
GraphNode[] children = node.getChildren();
doneList.addElement(node);
for(int i = 0; i < children.length; i++ ) {
GraphIcon icon = (GraphIcon)ViewList.get(children[i]);
if ( icon != null ) {
icon.setVisible(true);
if ( !doneList.contains(children[i]) )
expandNode(children[i],doneList);
}
}
}
public void select() {
Vector items;
GraphIcon icon;
if ( (items = this.boundedItems()) != null ) {
boolean all_selected = true;
for( int i = 0; i < items.size(); i++ ) {
icon = (GraphIcon)items.elementAt(i);
all_selected = all_selected && icon.isSelected();
if ( !all_selected) break;
}
for( int i = 0; i < items.size(); i++ ) {
icon = (GraphIcon)items.elementAt(i);
if ( all_selected )
icon.setSelected(false);
else
icon.setSelected(true);
}
}
}
public void selectAll() {
GraphIcon icon;
boolean all_selected = true;
Enumeration enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
all_selected = all_selected && icon.isSelected();
if ( !all_selected) break;
}
enum = ViewList.elements();
while( enum.hasMoreElements() ) {
icon = (GraphIcon)enum.nextElement();
if ( all_selected )
icon.setSelected(false);
else
icon.setSelected(true);
}
}
public void recompute() {
Point p = new Point(0,0);
GraphNode node;
GraphIcon icon;
Enumeration enum;
Rectangle bb;
Point pt;
Rectangle rect;
Vector doneList = new Vector();
switch( viewMode ) {
case VERTICAL_PARENT_CHILD:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -