⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jgraphpane.java

📁 JPowerGraph is a Java library for creating directed graphs for Swing. It supports graph movement, se
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package net.sourceforge.jpowergraph.pane;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.RenderingHints;
import java.awt.event.ComponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
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 javax.swing.Action;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.ToolTipManager;

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;


/**
 * The panel capable of visualizing the graph.
 */
public class JGraphPane extends JPanel {
    /** The graph being visualized. */
    protected Graph m_graph;
    /** The legend of the graph instance **/
    protected Legend m_legend;
    /** The listener for the graph. */
    protected GraphListener m_graphListner;
    /** The listener for the lens. */
    protected LensListener m_lensListener;
    /** The currently active lens. */
    protected Lens m_lens;
    /** The map of node positions. */
    protected Map m_nodePositions;
    /** The array of registered manipulators in the order they were registered. */
    protected List m_manipulators;
    /** The map of rigestered manipulators keyed by their name. */
    protected Map m_manipulatorsByName;
    
    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(Graph graph) {
        super(null);
        
        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 = Color.white;
        defaultNodePainter = new ShapeNodePainter(ShapeNodePainter.RECTANGLE);
        nodePainters = new HashMap();
        defaultEdgePainter = new ArrowEdgePainter();
        edgePainters = new HashMap();
        defaultLegendPainter = new LegendPainter();
        
        setGraph(graph);
        refreshLegend(graph);
        enableEvents(MouseEvent.MOUSE_EVENT_MASK | MouseEvent.MOUSE_MOTION_EVENT_MASK | KeyEvent.KEY_EVENT_MASK | FocusEvent.FOCUS_EVENT_MASK | ComponentEvent.COMPONENT_EVENT_MASK);
        ToolTipManager.sharedInstance().registerComponent(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);
    }
    /**
     * 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);
        }
    }
    /**
     * 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_nodePositions.clear();
        m_legend.setLens(lens);
        repaint();
        firePropertyChange("lens",oldLens,m_lens);
    }
    /**
     * 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();
        repaint();
        firePropertyChange("graph",oldGraph,m_graph);
    }
    
    public boolean isAntialias() {
        return antialias;
    }
    
    public void setAntialias(boolean antialias) {
        this.antialias = antialias;
    }
    
    /**
     * Updates the component.
     *
     * @param g                         the graphics
     */
    public void paintComponent(Graphics g) {
        Graphics2D g2d=(Graphics2D)g;
        if (isAntialias()){
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON); 
        }
        Rectangle clipRectangle=g.getClipBounds();
        Color oldColor=g.getColor();
        g.setColor(backgroundColor);
        g.fillRect(clipRectangle.x,clipRectangle.y,clipRectangle.width,clipRectangle.height);
        g.setColor(oldColor);
        if (m_graph!=null)
            synchronized (m_graph) {
                Rectangle bounds=new Rectangle();
                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(g2d,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(g2d,node);
                        }
                    }
                }
                for (int i=0;i<m_manipulators.size();i++){
                    ((Manipulator)m_manipulators.get(i)).paint(g2d);
                }
                paintLegend(g2d, m_legend);
            }
    }
    /**
     * Paints the edge.
     *
     * @param g                         the graphics
     * @param edge                      the edge
     */
    protected void paintEdge(Graphics2D 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(Graphics2D 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(Graphics2D 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();
            }
        }
        
        legend.setLocation(new Rectangle());
        legend.clearActionMap();
        
        if (drawLegend){
            LegendPainter legendPainter = getPainterForLegend(legend);
            Rectangle r = legendPainter.paintLegend(this, g, legend);
            legend.setLocation(r);
        }
    }
    
    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-getWidth()/2,point.y-getHeight()/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(Point2D graphPoint,Point point) {
        double oldX=graphPoint.getX();
        double oldY=graphPoint.getY();
        if (m_lens!=null)
            m_lens.applyLens(this, graphPoint);
        point.setLocation((int)graphPoint.getX()+getWidth()/2,(int)graphPoint.getY()+getHeight()/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)
     */
    public Node getNodeAtPoint(Point point) {
        synchronized (m_graph) {
            List nodes=m_graph.getNodes();
            ListIterator iterator=nodes.listIterator(nodes.size());
            while (iterator.hasPrevious()) {
                Node node=(Node)iterator.previous();
                NodePainter nodePainter=getPainterForNode(node);
                if (nodePainter.isInNode(this,node,point, nodeSize))
                    return node;
            }
            return null;
        }
    }
    /**
     * Returns the set of nodes in given rectangle.
     *
     * @param rectangle                 the rectangle in which the nodes must be located
     * @return                          the set of nodes in the region
     */
    public Set getNodesInRectangle(Rectangle rectangle) {
        synchronized (m_graph) {
            Set nodesInRectangle=new HashSet();
            Rectangle nodeRectangle=new Rectangle();
            Iterator nodes=m_graph.getNodes().iterator();
            while (nodes.hasNext()) {
                Node node=(Node)nodes.next();
                getNodeScreenBounds(node,nodeRectangle);
                if (rectangle.contains(nodeRectangle))
                    nodesInRectangle.add(node);
            }
            return nodesInRectangle;
        }
    }
    /**
     * Returns the edge at given point, or <code>null</code> if there is no such edge.
     *
     * @param point                     the screen point
     * @return                          the edge at given point (or <code>null</code> if there is no such edge)
     */
    public Edge getNearestEdge(Point point) {
        Edge nearestEdge=null;
        double minDistance=4;
        synchronized (m_graph) {
            List edges=m_graph.getEdges();
            ListIterator iterator=edges.listIterator(edges.size());
            while (iterator.hasPrevious()) {

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -