📄 jgraphpane.java
字号:
package net.sourceforge.jpowergraph.pane;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import net.sourceforge.jpowergraph.Edge;
import net.sourceforge.jpowergraph.Graph;
import net.sourceforge.jpowergraph.GraphListener;
import net.sourceforge.jpowergraph.Legend;
import net.sourceforge.jpowergraph.Node;
import net.sourceforge.jpowergraph.lens.LegendLens;
import net.sourceforge.jpowergraph.lens.Lens;
import net.sourceforge.jpowergraph.lens.LensListener;
import net.sourceforge.jpowergraph.lens.LensSet;
import net.sourceforge.jpowergraph.manipulator.Manipulator;
import net.sourceforge.jpowergraph.painters.ArrowEdgePainter;
import net.sourceforge.jpowergraph.painters.EdgePainter;
import net.sourceforge.jpowergraph.painters.LegendPainter;
import net.sourceforge.jpowergraph.painters.NodePainter;
import net.sourceforge.jpowergraph.painters.ShapeNodePainter;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.ControlListener;
import org.eclipse.swt.events.FocusEvent;
import org.eclipse.swt.events.FocusListener;
import org.eclipse.swt.events.PaintEvent;
import org.eclipse.swt.events.PaintListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
/**
* The panel capable of visualizing the graph.
*/
public class JGraphPane extends Canvas implements PaintListener {
/** The graph being visualized. */
private Graph m_graph;
/** The legend of the graph instance **/
protected Legend m_legend;
/** The listener for the graph. */
private GraphListener m_graphListner;
/** The listener for the lens. */
private LensListener m_lensListener;
/** The currently active lens. */
private Lens m_lens;
/** The map of node positions. */
private Map m_nodePositions;
/** The array of registered manipulators in the order they were registered. */
private List m_manipulators;
/** The map of rigestered manipulators keyed by their name. */
protected Map m_manipulatorsByName;
private Composite parent;
private Color backgroundColor;
private NodePainter defaultNodePainter;
private HashMap nodePainters;
private EdgePainter defaultEdgePainter;
private HashMap edgePainters;
private LegendPainter defaultLegendPainter;
private int nodeSize = NodePainter.LARGE;
private ArrayList hiddenNodeTypes = new ArrayList();
private boolean antialias = false;
/**
* Creates a graph pane.
*
* @param graph
* the graph
*/
public JGraphPane(Composite theParent, Graph graph) {
super(theParent, SWT.NO_BACKGROUND);
this.parent = theParent;
m_legend = new Legend();
m_graphListner = new GraphHandler();
m_lensListener = new LensHandler();
m_nodePositions = new HashMap();
m_manipulators = new ArrayList();
m_manipulatorsByName = new HashMap();
backgroundColor = parent.getDisplay().getSystemColor(SWT.COLOR_WHITE);
defaultNodePainter = new ShapeNodePainter(parent, ShapeNodePainter.RECTANGLE);
nodePainters = new HashMap();
defaultEdgePainter = new ArrowEdgePainter(parent.getDisplay());
edgePainters = new HashMap();
defaultLegendPainter = new LegendPainter(parent.getDisplay());
setBackground(backgroundColor);
refreshLegend(graph);
setGraph(graph);
this.addFocusListener(new FocusListener() {
public void focusGained(FocusEvent arg0) {
redraw();
}
public void focusLost(FocusEvent arg0) {
redraw();
}
});
this.addControlListener(new ControlListener() {
public void controlMoved(ControlEvent arg0) {
redraw();
}
public void controlResized(ControlEvent arg0) {
redraw();
}
});
this.addPaintListener(this);
}
/**
* Adds a manipulator to this pane. If a manipulator with the same name if
* registered, if is rist removed.
*
* @param manipulator
* the manipulator
*/
public void addManipulator(Manipulator manipulator) {
removeManipulator(manipulator.getName());
m_manipulators.add(manipulator);
m_manipulatorsByName.put(manipulator.getName(), manipulator);
manipulator.setGraphPane(this);
this.addMouseListener(manipulator);
this.addMouseMoveListener(manipulator);
this.addMouseTrackListener(manipulator);
this.addKeyListener(manipulator);
this.addFocusListener(manipulator);
}
/**
* Returns a manipulator with given name.
*
* @param name
* the name of the manpulator
* @return the manipulator with given name (or <code>null</code> if the
* manipualtor with given name is not registered)
*/
public Manipulator getManipulator(String name) {
return (Manipulator) m_manipulatorsByName.get(name);
}
/**
* Removes a manipulator with given name.
*
* @param name
* the name of the manpulator
*/
public void removeManipulator(String name) {
Manipulator manipulator = (Manipulator) m_manipulatorsByName.remove(name);
if (manipulator != null) {
m_manipulators.remove(manipulator);
manipulator.setGraphPane(null);
this.removeMouseListener(manipulator);
this.removeMouseMoveListener(manipulator);
this.removeMouseTrackListener(manipulator);
this.removeKeyListener(manipulator);
this.removeFocusListener(manipulator);
}
}
/**
* Returns the current lens.
*
* @return the current lens (may be <code>null</code>)
*/
public Lens getLens() {
return m_lens;
}
/**
* Sets the new lens.
*
* @param lens
* the new lens
*/
public void setLens(Lens lens) {
Lens oldLens = m_lens;
if (m_lens != null){
m_lens.removeLensListener(m_lensListener);
}
m_lens = lens;
if (m_lens != null){
m_lens.addLensListener(m_lensListener);
}
m_legend.setLens(lens);
m_nodePositions.clear();
redraw();
}
/**
* Returns the current graph.
*
* @return the current graph
*/
public Graph getGraph() {
return m_graph;
}
/**
* Sets the current graph.
*
* @param graph
* the new graph
*/
public void setGraph(Graph graph) {
Graph oldGraph = m_graph;
if (m_graph != null){
m_graph.removeGraphListener(m_graphListner);
}
m_graph = graph;
if (m_graph != null){
m_graph.addGraphListener(m_graphListner);
}
m_nodePositions.clear();
redraw();
}
public boolean isAntialias() {
return antialias;
}
public void setAntialias(boolean antialias) {
this.antialias = antialias;
}
/**
* Updates the component.
*
* @param g
* the graphics
*/
public void paintControl(PaintEvent e) {
Image bufferImage = new Image(parent.getDisplay(), this.getSize().x, this.getSize().y);
GC g = new GC(bufferImage);
if (isAntialias()){
g.setAntialias(SWT.ON);
}
Rectangle clipRectangle = g.getClipping();
if (m_graph != null){
synchronized (m_graph) {
Rectangle bounds = new Rectangle(0, 0, 0, 0);
Iterator iterator = m_graph.getEdges().iterator();
while (iterator.hasNext()) {
Edge edge = (Edge) iterator.next();
if (!hiddenNodeTypes.contains(edge.getFrom().getClass()) &&
!hiddenNodeTypes.contains(edge.getTo().getClass())){
getEdgeScreenBounds(edge, bounds);
if (clipRectangle.intersects(bounds)){
paintEdge(g, edge);
}
}
}
iterator = m_graph.getNodes().iterator();
while (iterator.hasNext()) {
Node node = (Node) iterator.next();
if (!hiddenNodeTypes.contains(node.getClass())){
getNodeScreenBounds(node, bounds);
if (clipRectangle.intersects(bounds)){
paintNode(g, node);
}
}
}
for (int i = 0; i < m_manipulators.size(); i++){
((Manipulator) m_manipulators.get(i)).paint(g);
}
paintLegend(g, m_legend);
}
}
e.gc.drawImage(bufferImage,0,0);
bufferImage.dispose();
}
/**
* Paints the edge.
*
* @param g
* the graphics
* @param edge
* the edge
*/
protected void paintEdge(GC g, Edge edge) {
EdgePainter edgePainter = getPainterForEdge(edge);
edgePainter.paintEdge(this, g, edge);
}
/**
* Returns the painter for the edge.
*
* @param edge
* the edge
* @return the painter for the edge
*/
public EdgePainter getPainterForEdge(Edge edge) {
if (edgePainters.containsKey(edge.getClass())){
return (EdgePainter) edgePainters.get(edge.getClass());
}
return defaultEdgePainter;
}
/**
* Paints the node.
*
* @param g
* the graphics
* @param node
* the node
*/
protected void paintNode(GC g, Node node) {
NodePainter nodePainter = getPainterForNode(node);
nodePainter.paintNode(this, g, node, nodeSize);
}
/**
* Returns the painter for the node.
*
* @param node
* the node
* @return the painter for the node
*/
public NodePainter getPainterForNode(Node node) {
if (nodePainters.containsKey(node.getClass())){
return (NodePainter) nodePainters.get(node.getClass());
}
return defaultNodePainter;
}
protected void paintLegend(GC g, Legend legend){
boolean drawLegend = false;
if (m_lens instanceof LegendLens){
drawLegend = ((LegendLens) m_lens).isShowLegend();
}
else if (m_lens instanceof LensSet){
drawLegend = true;
List lenses = (((LensSet) m_lens)).getLensOfType(LegendLens.class);
for (int i = 0; i < lenses.size(); i++){
drawLegend = drawLegend && ((LegendLens) lenses.get(i)).isShowLegend();
}
}
if (drawLegend){
LegendPainter legendPainter = getPainterForLegend(legend);
Rectangle r = legendPainter.paintLegend(this, g, legend);
legend.setLocation(r);
}
else{
legend.setLocation(new Rectangle(0, 0, 0, 0));
}
}
public LegendPainter getPainterForLegend(Legend theLegend){
return defaultLegendPainter;
}
/**
* Converts a given screen point into a point on the graph.
*
* @param point
* the sreen point
* @param graphPoint
* the calculatedpoint in the graph
*/
public void screenToGraphPoint(Point point, Point2D graphPoint) {
graphPoint.setLocation(point.x - getSize().x / 2, point.y - getSize().y / 2);
if (m_lens != null)
m_lens.undoLens(this, graphPoint);
}
/**
* Converts a given graph point into a point on the screen.
*
* @param graphPoint
* the point in the graph
* @param point
* the calculated screen point
*/
public void graphToScreenPoint(final Point2D graphPoint, final Point point) {
double oldX = graphPoint.getX();
double oldY = graphPoint.getY();
if (m_lens != null) {
m_lens.applyLens(this, graphPoint);
}
point.x = (int) graphPoint.getX() + getSize().x / 2;
point.y = (int) graphPoint.getY() + getSize().y / 2;
graphPoint.setLocation(oldX, oldY);
}
public Legend getLegendAtPoint(Point point){
if (m_legend.getLocation().contains(point)){
return m_legend;
}
return null;
}
/**
* Returns the node at given point, or <code>null</code> if there is no
* such node.
*
* @param point
* the screen point
* @return the node at given point (or <code>null</code> if there is no
* such node)
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -