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

📄 graphextends.java

📁 JGraph扩展应用。自定义Renderer,自定义视图View实现自定义工作流控件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * Copyright (C) 2001-2004 Gaudenz Alder
 *
 * GPGraphpad is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * GPGraphpad is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GPGraphpad; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

package flow.graph.gui.graph;

import java.awt.Color;
import java.awt.event.MouseEvent;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;

import javax.swing.JComponent;

import org.jgraph.JGraph;
import org.jgraph.graph.CellView;
import org.jgraph.graph.DefaultEdge;
import org.jgraph.graph.DefaultGraphCell;
import org.jgraph.graph.DefaultGraphModel;
import org.jgraph.graph.DefaultPort;
import org.jgraph.graph.Edge;
import org.jgraph.graph.GraphConstants;
import org.jgraph.graph.GraphLayoutCache;
import org.jgraph.graph.GraphModel;
import org.jgraph.graph.Port;

import flow.graph.gui.graph.cell.BPELFlag;
import flow.graph.gui.graph.cell.FlowGraphCell;
import flow.graph.gui.graph.cell.bpel.Message;
import flow.graph.gui.graph.cell.bpel.Messages;


/**
 * The base class JGraph subclassers for GPGraphpad. It adds some hooks
 * to JGraph and have a few more serializable properties.
 * To use a custom JGraph subcalsser, subclass GPGraph and register
 * your subclass in the Graph factory.
 */
public class GraphExtends extends JGraph {
	protected boolean saved;

    public static String FILE_FORMAT_VERSION = "PAD-1.1";

    public GraphExtends() {
        super();
    }

    /**
     * Required for serialization
     * 
     * @param model
     * @param cache
     */
    public GraphExtends(GraphModel model) {
        this(model, null);
    }

    public GraphExtends(GraphModel model, GraphLayoutCache cache) {
        super(model, cache);
		setPortsVisible(true);
		// Use the Grid (but don't make it Visible)
		setGridEnabled(true);
		// Set the Grid Size to 10 Pixel
		setGridSize(6);
		// Set the Tolerance to 2 Pixel
		setTolerance(2);
		// Accept edits if click on background
		setInvokesStopCellEditing(true);
		// Allows control-drag
		setCloneable(true);
		// Jump to default port on connect
		setJumpToDefaultPort(true);
		//setHighlightColor(Color.green);
		setMarqueeColor(Color.GREEN);
		setCloneable(true);
		this.setMoveIntoGroups(true);
		this.setMoveOutOfGroups(true);
    }
	
	public void setSaved(boolean flag){
		saved = flag;
	}
	
	public boolean getSaved(){
		return saved;
	}

    public static void addSampleData(GraphModel model) {
    }

    //
    // Defines Semantics of the Graph
    //

    /**
     * Returns true if <code>object</code> is a vertex, that is, if it is not
     * an instance of Port or Edge, and all of its children are ports, or it has
     * no children.
     */
    public boolean isGroup(Object cell) {
        // Map the Cell to its View
        CellView view = getGraphLayoutCache().getMapping(cell, false);
        if (view != null)
            return !view.isLeaf();
        return false;
    }

    /**
     * Returns true if <code>object</code> is a vertex, that is, if it is not
     * an instance of Port or Edge, and all of its children are ports, or it has
     * no children.
     */
    public boolean isVertex(Object object) {
        if (!(object instanceof Port) && !(object instanceof Edge))
            return !isGroup(object) && object != null;
        return false;
    }

    public Object[] getSelectionVertices() {
        Object[] tmp = getSelectionCells();
        Object[] all = DefaultGraphModel.getDescendants(getModel(), tmp)
                .toArray();
        return getVertices(all);
    }

    public Object[] getVertices(Object[] cells) {
        if (cells != null) {
            ArrayList result = new ArrayList();
            for (int i = 0; i < cells.length; i++)
                if (isVertex(cells[i]))
                    result.add(cells[i]);
            return result.toArray();
        }
        return null;
    }

    public Object[] getSelectionEdges() {
        return getEdges(getSelectionCells());
    }

    public Object[] getAll() {
        return getDescendants(getRoots());
    }

    public Object[] getEdges(Object[] cells) {
        if (cells != null) {
            ArrayList result = new ArrayList();
            for (int i = 0; i < cells.length; i++)
                if (this.getModel().isEdge(cells[i]))
                    result.add(cells[i]);
            return result.toArray();
        }
        return null;
    }

    public Object getNeighbour(Object edge, Object vertex) {
        Object source = this.getModel().getSource(edge);
        if (vertex == source)
            return this.getModel().getTarget(edge);
        return source;
    }

    public CellView getSourceView(Object edge) {
        Object source = this.getModel().getSource(edge);
        return getGraphLayoutCache().getMapping(source, false);
    }

    public CellView getTargetView(Object edge) {
        Object target = this.getModel().getTarget(edge);
        return getGraphLayoutCache().getMapping(target, false);
    }

    public Object[] getEdgesBetween(Object vertex1, Object vertex2) {
        ArrayList result = new ArrayList();
        Set edges = DefaultGraphModel.getEdges(graphModel,
                new Object[] { vertex1 });
        Iterator it = edges.iterator();
        while (it.hasNext()) {
            Object edge = it.next();
            Object source = this.getModel().getSource(edge);
            Object target = this.getModel().getTarget(edge);
            if ((source == vertex1 && target == vertex2)
                    || (source == vertex2 && target == vertex1))
                result.add(edge);
        }
        return result.toArray();
    }

    /**
     * Overrides <code>JComponent</code>'buttonSelect
     * <code>getToolTipText</code> method in order to allow the graph
     * controller to create a tooltip for the topmost cell under the
     * mousepointer. This differs from JTree where the renderers tooltip is
     * used.
     * <p>
     * NOTE: For <code>JGraph</code> to properly display tooltips of its
     * renderers, <code>JGraph</code> must be a registered component with the
     * <code>ToolTipManager</code>. This can be done by invoking
     * <code>ToolTipManager.sharedInstance().registerComponent(graph)</code>.
     * This is not done automatically!
     * 
     * @param event
     *            the <code>MouseEvent</code> that initiated the
     *            <code>ToolTip</code> display
     * @return a string containing the tooltip or <code>null</code> if
     *         <code>event</code> is null
     */
    public String getToolTipText(MouseEvent event) {
        if (event != null) {
            //Object cell = getFirstCellForLocation(event.getX(), event.getY());
			Object cell = getVertexHandle(this, event.getPoint());
            if (cell != null) {
                String tmp = convertValueToString(cell);
                String s = "<html>";
                if (tmp != null && tmp.length() > 0)
                    s = s + "<strong>" + tmp + "</strong><br>";
                return s + getToolTipForCell(cell) + "</html>";
            }
        }
        return null;
    }

    protected String getToolTipForCell(Object cell) {
        CellView view = getGraphLayoutCache().getMapping(cell, false);
        String s = "";
        Rectangle2D bounds = view.getBounds();
		
		s += "CellID:"+FlowGraphConstants.getCellID(((DefaultGraphCell)cell).getAttributes())+"<br>";
        if (bounds != null) {
            s = s + "Location: " + bounds.getX() + ", " + bounds.getY()
                    + "<br>";
            s = s + "Size: " + bounds.getX() + ", " + bounds.getY() + "<br>";
        }
        java.util.List points = GraphConstants.getPoints(view.getAttributes());
        if (points != null)
            s = s + "Points: " + points.size() + "<br>";
        if (!(cell instanceof Edge) && !(cell instanceof Port)) {
            s = s + "Children: " + graphModel.getChildCount(cell) + "<br>";
            int n = DefaultGraphModel.getEdges(getModel(),
                    new Object[] { cell }).size();
            s = s + "Edges: " + n;
        } else if (cell instanceof Edge) {
            Edge edge = (Edge) cell;
            Object source = graphModel.getSource(edge);
            if (source != null) {
                String host = convertValueToString(graphModel.getParent(source));
                String port = convertValueToString(source);
                s = s + "Source: " + host + ":" + port + "<br>";
            }
            Object target = graphModel.getTarget(edge);
            if (target != null) {

⌨️ 快捷键说明

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