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

📄 jgraphpane.java

📁 JPowerGraph is a Java library for creating directed graphs for SWT. It supports graph movement, sele
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    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(0, 0, 0, 0);
            Iterator nodes = m_graph.getNodes().iterator();
            while (nodes.hasNext()) {
                Node node = (Node) nodes.next();
                getNodeScreenBounds(node, nodeRectangle);
                if (SWTRectangleUtil.contains(rectangle, 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()) {
                Edge edge = (Edge) iterator.previous();
                EdgePainter edgePainter = getPainterForEdge(edge);
                double distance = edgePainter.screenDistanceFromEdge(this, edge, point);
                if (distance < minDistance) {
                    minDistance = distance;
                    nearestEdge = edge;
                }
            }
            return nearestEdge;
        }
    }
    
    /**
     * Returns the position of the node on the screen.
     * 
     * @param node
     *            the node whose on-screen position is required
     * @return the position of the node on screen
     */
    public Point getScreenPointForNode(Node node) {
        Point point = (Point) m_nodePositions.get(node);
        if (point == null) {
            point = new Point(0, 0);
            graphToScreenPoint(new Point2D.Double(node.getX(), node.getY()), point);
            m_nodePositions.put(node, point);
        }
        return point;
    }

    /**
     * Updates the map of screen positions of nodes.
     */
    protected void updateNodeScreenPositions() {
        synchronized (m_graph) {
            Point2D graphPoint = new Point2D.Double();
            Iterator nodes = m_graph.getNodes().iterator();
            while (nodes.hasNext()) {
                Node node = (Node) nodes.next();
                Point point = (Point) m_nodePositions.get(node);
                if (point == null) {
                    point = new Point(0, 0);
                    m_nodePositions.put(node, point);
                }
                graphPoint.setLocation(node.getX(), node.getY());
                graphToScreenPoint(graphPoint, point);
            }
        }
    }

    /**
     * Returns the screen bounds of given node.
     * 
     * @param node
     *            the node for which the bounds must be returned
     * @param nodeScreenRectangle
     *            the rectangle receiving the node's coordinates
     */
    public void getNodeScreenBounds(Node node, Rectangle nodeScreenRectangle) {
        NodePainter nodePainter = getPainterForNode(node);
        nodePainter.getNodeScreenBounds(this, node, nodeSize, nodeScreenRectangle);
    }

    /**
     * Repaints the given node.
     * 
     * @param node
     *            the node that needs to be repainted
     */
    public void repaintNode(Node node) {
        Rectangle nodeScreenRectangle = new Rectangle(0, 0, 0, 0);
        getNodeScreenBounds(node, nodeScreenRectangle);
        redraw();
    }

    /**
     * Returns the screen bounds of given edge.
     * 
     * @param edge
     *            the edge for which the bounds must be returned
     * @param edgeScreenRectangle
     *            the rectangle receiving the edge's coordinates
     */
    public void getEdgeScreenBounds(Edge edge, Rectangle edgeScreenRectangle) {
        EdgePainter edgePainter = getPainterForEdge(edge);
        edgePainter.getEdgeScreenBounds(this, edge, edgeScreenRectangle);
    }

    /**
     * Repaints the given edge.
     * 
     * @param edge
     *            the edge that needs to be repainted
     */
    public void repaintEdge(Edge edge) {
        Rectangle edgeScreenRectangle = new Rectangle(0, 0, 0, 0);
        getEdgeScreenBounds(edge, edgeScreenRectangle);

        edgeScreenRectangle.x -= 5;
        edgeScreenRectangle.y -= 5;
        edgeScreenRectangle.width += 10;
        edgeScreenRectangle.height += 10;

        redraw();
    }
    
    public void refreshLegend(Graph graph) {
        m_legend.clear();
        for (Iterator i = graph.getNodes().iterator(); i.hasNext();) {
            Node node = (Node) i.next();
            NodePainter nodePainter = getPainterForNode(node);
            m_legend.add(nodePainter, node.getClass(), node.getNodeType());
        }
    }
    
    /**
     * Returns the node painter for nodes of a type that does not 
     * have a specific node painter. 
     * 
     * @see setDefaultNodePainter(NodePainter theNodePainter)
     * @see setNodePainter(Class theClass, NodePainter theNodePainter)
     * @see getNodePainter(Class theClass)
     * 
     * @return NodePainter
     */
    public NodePainter getDefaultNodePainter() {
        return defaultNodePainter;
    }
    
    /**
     * Sets the node painter for nodes of a type that does not 
     * have a specific node painter. 
     * 
     * @see setNodePainter(Class theClass, NodePainter theNodePainter)
     * @see getNodePainter(Class theClass)
     * 
     * @param theNodePainter The new default NodePainter
     */
    public void setDefaultNodePainter(NodePainter theNodePainter) {
        this.defaultNodePainter = theNodePainter;
        refreshLegend(m_graph);
    }
    
    /**
     * Returns the node painter for nodes of the specified class. This
     * method will return null if no node painter is specified for this 
     * class.
     * 
     * @see setNodePainter(Class theClass, NodePainter theNodePainter)
     * 
     * @param theClass The Node class type
     * 
     * @return NodePainter
     */
    public NodePainter getNodePainter(Class theClass) {
        return (NodePainter) nodePainters.get(theClass);
    }
    
    /**
     * Sets the node painter for nodes of the specified class. To clear the 
     * specified node painter specify null for the nodePainter
     * 
     * @see getNodePainter(Class theClass)
     * 
     * @param theClass The Node class type
     * @param theNodePainter The new NodePainter for the specified class type
     * 
     * @return NodePainter
     */
    public void setNodePainter(Class theClass, NodePainter theNodePainter) {
        nodePainters.put(theClass, theNodePainter);
        refreshLegend(m_graph);
    }
    
    /**
     * Returns the edge painter for edges of a type that does not 
     * have a specific edge painter. 
     * 
     * @see setDefaultEdgePainter(EdgePainter theEdgePainter)
     * @see setEdgePainter(Class theClass, EdgePainter theEdgePainter)
     * @see getEdgePainter(Class theClass)
     * 
     * @return EdgePainter
     */
    public EdgePainter getDefaultEdgePainter() {
        return defaultEdgePainter;
    }
    
    /**
     * Sets the edge painter for nodes of a type that does not 
     * have a specific edge painter. 
     * 
     * @see getDefaultEdgePainter();
     * @see setEdgePainter(Class theClass, EdgePainter theEdgePainter)
     * @see getEdgePainter(Class theClass)
     * 
     * @param theEdgePainter The new default EdgePainter
     */
    public void setDefaultEdgePainter(EdgePainter theEdgePainter) {
        this.defaultEdgePainter = theEdgePainter;
    }
    
    /**
     * Returns the edge painter for nodes of the specified class. This
     * method will return null if no edge painter is specified for this 
     * class.
     * 
     * @see setEdgePainter(Class theClass, EdgePainter theEdgePainter)
     * 
     * @param theClass The Edge class type
     * 
     * @return EdgePainter
     */
    public EdgePainter getEdgePainter(Class theClass) {
        return (EdgePainter) edgePainters.get(theClass);
    }
    
    /**
     * Sets the edge painter for nodes of the specified class. To clear the 
     * specified edge painter specify null for the edgePainter
     * 
     * @see getEdgePainter(Class theClass)
     * 
     * @param theClass The Edge class type
     * @param theEdgePainter The new EdgePainter for the specified class type
     * 
     * @return EdgePainter
     */
    public void setEdgePainter(Class theClass, EdgePainter theEdgePainter) {
        edgePainters.put(theClass, theEdgePainter);
    }

    public void setNodeSize(int theNodeSize) {
        this.nodeSize = theNodeSize;
    }
    
    public void clearNodeFilter() {
        hiddenNodeTypes.clear();    
    }
    
    public void addFilteredNode(Class class1) {
        hiddenNodeTypes.add(class1);
    }
    
    /**
     * Overridden to notify the manipulators that the scroll position has
     * changed.
     * 
     * @param rectangle
     *            the rectangle
     */
    public void scroll(int destX, int destY, int x, int y, int width, int height, boolean all) {
        super.scroll(destX, destY, x, y, width, height, all);
        for (int i = 0; i < m_manipulators.size(); i++)
            ((Manipulator) m_manipulators.get(i)).notifyGraphPaneScrolled();
    }

    public void scrollRectToVisible(Rectangle rectangle) {
        Composite parent = getParent();
        while (parent != null){
            if (parent instanceof JGraphScrollPane){
                ((JGraphScrollPane) parent).scrollRectToVisible(rectangle);
                break;
            }
        }
        
        for (int i=0;i<m_manipulators.size();i++){
            ((Manipulator)m_manipulators.get(i)).notifyGraphPaneScrolled();
        }
    }
    
    /**
     * The handler of graph events.
     */
    protected class GraphHandler implements GraphListener {
        public void graphLayoutUpdated(Graph graph) {
            parent.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    if (!isDisposed()){
                        updateNodeScreenPositions();
                        redraw();
                    }
                }
            });
        }

        public void graphUpdated(Graph graph) {
            parent.getDisplay().asyncExec(new Runnable() {
                public void run() {
                    if (!isDisposed()){
                        redraw();
                    }
                }
            });
        }

        public void graphContentsChanged(Graph graph) {
            if (!isDisposed()){
                m_nodePositions.clear();
                redraw();
            }
        }

        public void elementsAdded(Graph graph, Collection nodes, Collection edges) {
            if (!isDisposed()){
                refreshLegend(graph);
                redraw();
            }
        }

        public void elementsRemoved(Graph graph, Collection nodes, Collection edges) {
            if (!isDisposed()){
                if (nodes != null) {
                    Iterator iterator = nodes.iterator();
                    while (iterator.hasNext()) {
                        Node node = (Node) iterator.next();
                        m_nodePositions.remove(node);
                    }
                }
                refreshLegend(graph);
                redraw();
            }
        }
    }

    /**
     * The handler of lens events.
     */
    protected class LensHandler implements LensListener {
        public void lensUpdated(Lens lens) {
            updateNodeScreenPositions();
            redraw();
        }
    }
    
    public void dispose() {
        super.dispose();
        if (!m_lens.isDisposed()){
            m_lens.dispose();
        }
        m_nodePositions.clear();
        for (int i = 0; i < m_manipulators.size(); i++){
            Manipulator m = (Manipulator) m_manipulators.get(i);
            if (!m.isDisposed()){
                m.dispose();
            }
        }
        m_manipulators.clear();
        m_manipulatorsByName.clear();
        if (!backgroundColor.isDisposed()){
            backgroundColor.dispose();
        }
        if (!defaultNodePainter.isDisposed()){
            defaultNodePainter.dispose();
        }
        for (Iterator i = nodePainters.keySet().iterator(); i.hasNext();) {
            NodePainter np = (NodePainter) i.next();
            if (!np.isDisposed()){
                np.dispose();
            }
        }
        if (!defaultEdgePainter.isDisposed()){
            defaultEdgePainter.dispose();
        }
        for (Iterator i = edgePainters.keySet().iterator(); i.hasNext();) {
            EdgePainter ep = (EdgePainter) i.next();
            if (!ep.isDisposed()){
                ep.dispose();
            }
        }
        if (!defaultLegendPainter.isDisposed()){
            defaultLegendPainter.dispose();
        }
        hiddenNodeTypes.clear();
    }
}

⌨️ 快捷键说明

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