Search in sources :

Example 1 with CellView

use of org.jgraph.graph.CellView in project fql by CategoricalData.

the class EdgeRouter method routeLoop.

/**
 * Calculates intermediate points for multiple loops. This is the same
 * algorithm used by DefaultEdge.LoopRouting, but we scale the loop box for
 * parallel self-referencing edges.
 *
 * @param cache
 * @param edge
 *
 * @return
 */
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public List routeLoop(final GraphLayoutCache cache, final EdgeView edge) {
    final CellView sourceNode = edge.getSource();
    final List newPoints = new ArrayList();
    newPoints.add(sourceNode);
    final CellView sourceParent = (sourceNode != null) ? sourceNode.getParentView() : edge.getSourceParentView();
    if (sourceNode == null) {
        throw new RuntimeException("Internal EASIK error, please report");
    }
    final Object[] edges = DefaultGraphModel.getEdgesBetween(cache.getModel(), sourceNode.getCell(), sourceNode.getCell(), true);
    int position = 0;
    if (edges != null) {
        for (int i = 0; i < edges.length; i++) {
            if (edges[i] == edge.getCell()) {
                position = i;
                break;
            }
        }
    }
    if (sourceParent != null) {
        final Point2D from = AbstractCellView.getCenterPoint(sourceParent);
        final Rectangle2D rect = sourceParent.getBounds();
        final double posWidthFactor = 1.25 + 0.75 * position;
        final double posHeightFactor = 1.5 + position;
        final double width = rect.getWidth();
        final double height2 = rect.getHeight() / 2;
        double loopWidth = Math.min(20, Math.max(10, width / 8));
        double loopHeight = Math.min(30, Math.max(12, Math.max(loopWidth + 4, height2 / 2)));
        loopWidth *= posWidthFactor;
        loopHeight *= posHeightFactor;
        newPoints.add(edge.getAttributes().createPoint(from.getX() - loopWidth, from.getY() - height2 - loopHeight * 1.0));
        final double midpointY = from.getY() - height2 - 1.5 * loopHeight;
        newPoints.add(edge.getAttributes().createPoint(from.getX(), midpointY));
        newPoints.add(edge.getAttributes().createPoint(from.getX() + loopWidth, from.getY() - height2 - loopHeight * 1.0));
        newPoints.add(edge.getTarget());
        return newPoints;
    }
    return null;
}
Also used : Point2D(java.awt.geom.Point2D) ArrayList(java.util.ArrayList) Rectangle2D(java.awt.geom.Rectangle2D) List(java.util.List) ArrayList(java.util.ArrayList) CellView(org.jgraph.graph.CellView) AbstractCellView(org.jgraph.graph.AbstractCellView)

Example 2 with CellView

use of org.jgraph.graph.CellView in project cytoscape-impl by cytoscape.

the class JGraphLayoutWrapperTask method construct.

/**
 *  DOCUMENT ME!
 */
public void construct() {
    canceled = false;
    // initialize();
    double currentProgress = 0;
    double percentProgressPerIter = 0;
    CyNetwork network = networkView.getModel();
    // PrimeFinder.nextPrime(network.getNodeCount()));
    Map j_giny_node_map = new HashMap();
    // PrimeFinder.nextPrime(network.getNodeCount()));
    Map giny_j_node_map = new HashMap();
    // PrimeFinder.nextPrime(network.getEdgeCount()));
    Map j_giny_edge_map = new HashMap();
    taskMonitor.setStatusMessage("Executing Layout");
    taskMonitor.setProgress(currentProgress / 100.0);
    // Construct Model and Graph
    // 
    GraphModel model = new DefaultGraphModel();
    JGraph graph = new JGraph(model);
    // Create Nested Map (from Cells to Attributes)
    // 
    Map attributes = new Hashtable();
    Set cells = new HashSet();
    // update progress bar
    currentProgress = 20;
    taskMonitor.setProgress(currentProgress / 100.0);
    percentProgressPerIter = 20 / (double) (nodesToLayOut.size());
    // create Vertices
    for (View<CyNode> node_view : nodesToLayOut) {
        if (canceled)
            return;
        CyNode n = node_view.getModel();
        DefaultGraphCell jcell = new DefaultGraphCell(n.getSUID());
        // Set bounds
        Rectangle2D bounds = new Rectangle2D.Double(node_view.getVisualProperty(BasicVisualLexicon.NODE_X_LOCATION), node_view.getVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION), node_view.getVisualProperty(BasicVisualLexicon.NODE_WIDTH), node_view.getVisualProperty(BasicVisualLexicon.NODE_HEIGHT));
        GraphConstants.setBounds(jcell.getAttributes(), bounds);
        j_giny_node_map.put(jcell, n);
        giny_j_node_map.put(n, jcell);
        cells.add(jcell);
        // update progress bar
        currentProgress += percentProgressPerIter;
        taskMonitor.setProgress(currentProgress / 100.0);
    }
    // update progress bar
    percentProgressPerIter = 20 / (double) (networkView.getEdgeViews().size());
    for (CyEdge edge : network.getEdgeList()) {
        if (canceled)
            return;
        DefaultGraphCell j_source = (DefaultGraphCell) giny_j_node_map.get(edge.getSource());
        DefaultGraphCell j_target = (DefaultGraphCell) giny_j_node_map.get(edge.getTarget());
        DefaultPort source_port = new DefaultPort();
        DefaultPort target_port = new DefaultPort();
        j_source.add(source_port);
        j_target.add(target_port);
        source_port.setParent(j_source);
        target_port.setParent(j_target);
        // create the edge
        DefaultEdge jedge = new DefaultEdge();
        j_giny_edge_map.put(jedge, edge);
        // Connect Edge
        // 
        ConnectionSet cs = new ConnectionSet(jedge, source_port, target_port);
        Object[] ecells = new Object[] { jedge, j_source, j_target };
        // Insert into Model
        // 
        model.insert(ecells, attributes, cs, null, null);
        cells.add(jedge);
        // update progress bar
        currentProgress += percentProgressPerIter;
        taskMonitor.setProgress(currentProgress / 100.0);
    }
    layout.run(graph, cells.toArray());
    GraphLayoutCache cache = graph.getGraphLayoutCache();
    CellView[] cellViews = graph.getGraphLayoutCache().getAllDescendants(graph.getGraphLayoutCache().getRoots());
    currentProgress = 80;
    taskMonitor.setProgress(currentProgress / 100.0);
    percentProgressPerIter = 20 / (double) (cellViews.length);
    if (canceled)
        return;
    for (int i = 0; i < cellViews.length; i++) {
        CellView cell_view = cellViews[i];
        if (cell_view instanceof VertexView) {
            // ok, we found a node
            Rectangle2D rect = graph.getCellBounds(cell_view.getCell());
            CyNode giny = (CyNode) j_giny_node_map.get(cell_view.getCell());
            View<CyNode> node_view = networkView.getNodeView(giny);
            node_view.setVisualProperty(BasicVisualLexicon.NODE_X_LOCATION, rect.getX());
            node_view.setVisualProperty(BasicVisualLexicon.NODE_Y_LOCATION, rect.getY());
            // update progress bar
            currentProgress += percentProgressPerIter;
            taskMonitor.setProgress(currentProgress / 100.0);
        }
    }
    // I don't think that any of the current layouts have edge components,
    // so I won't bother for now.
    model = null;
    graph = null;
    attributes = null;
    cells = null;
    System.gc();
}
Also used : JGraph(org.jgraph.JGraph) HashSet(java.util.HashSet) ConnectionSet(org.jgraph.graph.ConnectionSet) Set(java.util.Set) GraphLayoutCache(org.jgraph.graph.GraphLayoutCache) HashMap(java.util.HashMap) CyNetwork(org.cytoscape.model.CyNetwork) ConnectionSet(org.jgraph.graph.ConnectionSet) DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) DefaultGraphModel(org.jgraph.graph.DefaultGraphModel) GraphModel(org.jgraph.graph.GraphModel) CyNode(org.cytoscape.model.CyNode) DefaultGraphModel(org.jgraph.graph.DefaultGraphModel) DefaultPort(org.jgraph.graph.DefaultPort) HashSet(java.util.HashSet) Hashtable(java.util.Hashtable) Rectangle2D(java.awt.geom.Rectangle2D) VertexView(org.jgraph.graph.VertexView) DefaultEdge(org.jgraph.graph.DefaultEdge) CyEdge(org.cytoscape.model.CyEdge) CellView(org.jgraph.graph.CellView) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Rectangle2D (java.awt.geom.Rectangle2D)2 CellView (org.jgraph.graph.CellView)2 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 CyEdge (org.cytoscape.model.CyEdge)1 CyNetwork (org.cytoscape.model.CyNetwork)1 CyNode (org.cytoscape.model.CyNode)1 JGraph (org.jgraph.JGraph)1 AbstractCellView (org.jgraph.graph.AbstractCellView)1 ConnectionSet (org.jgraph.graph.ConnectionSet)1 DefaultEdge (org.jgraph.graph.DefaultEdge)1 DefaultGraphCell (org.jgraph.graph.DefaultGraphCell)1 DefaultGraphModel (org.jgraph.graph.DefaultGraphModel)1 DefaultPort (org.jgraph.graph.DefaultPort)1