Search in sources :

Example 1 with GraphLayoutCache

use of org.jgraph.graph.GraphLayoutCache in project cayenne by apache.

the class BaseGraphBuilder method buildGraph.

@Override
public void buildGraph(ProjectController mediator, DataChannelDescriptor domain, boolean doLayout) {
    if (graph != null) {
        // graph already built, exiting silently
        return;
    }
    graph = new JGraph();
    GraphModel model = new DefaultGraphModel();
    graph.setModel(model);
    setProjectController(mediator);
    setDataDomain(domain);
    GraphLayoutCache view = new GraphLayoutCache(model, new DefaultCellViewFactory());
    graph.setGraphLayoutCache(view);
    addMouseListeners();
    entityCells = new HashMap<>();
    createdObjects = new ArrayList<>();
    relCells = new HashMap<>();
    /*
         * an array for entities that are not connected to anyone. We add them
         * separately so that layout doesn't touch them
         */
    List<DefaultGraphCell> isolatedObjects = new ArrayList<>();
    /*
         * 1. Add all entities
         */
    for (DataMap map : domain.getDataMaps()) {
        DefaultGraphCell mapCell = new DefaultGraphCell();
        createdObjects.add(mapCell);
        for (Entity entity : getEntities(map)) {
            DefaultGraphCell cell = createEntityCell(entity);
            // mapCell.add(cell);
            // cell.setParent(mapCell);
            List<DefaultGraphCell> array = !isIsolated(domain, entity) ? createdObjects : isolatedObjects;
            array.add(cell);
            // port
            array.add((DefaultGraphCell) cell.getChildAt(0));
        }
    }
    /*
         * 2. Add all relationships
         */
    for (DataMap map : domain.getDataMaps()) {
        for (Entity entity : getEntities(map)) {
            DefaultGraphCell sourceCell = entityCells.get(entity.getName());
            postProcessEntity(entity, sourceCell);
        }
    }
    view.insert(createdObjects.toArray());
    setLayout(doLayout);
    /*
         * Adding isolated objects
         * 
         * We're placing them so that they will take maximum space in left top
         * corner. The sample order is below:
         * 
         * 1 2 6 7... 3 5 8 ... 4 9... 10 ...
         */
    addIsolatedObjects(isolatedObjects);
    view.insert(isolatedObjects.toArray());
    graph.getModel().addUndoableEditListener(this);
}
Also used : JGraph(org.jgraph.JGraph) Entity(org.apache.cayenne.map.Entity) DefaultCellViewFactory(org.jgraph.graph.DefaultCellViewFactory) DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) GraphLayoutCache(org.jgraph.graph.GraphLayoutCache) DefaultGraphModel(org.jgraph.graph.DefaultGraphModel) GraphModel(org.jgraph.graph.GraphModel) ArrayList(java.util.ArrayList) DefaultGraphModel(org.jgraph.graph.DefaultGraphModel) DataMap(org.apache.cayenne.map.DataMap)

Example 2 with GraphLayoutCache

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

the class Sketch method addEntity.

/**
 * Adds a collection (set, list, etc.) of EntityNodes to the graph.
 *
 * @param theEntities
 *            the collection of entities to be added.
 */
public void addEntity(final Collection<EntityNode> theEntities) {
    // Push loading state
    _stateManager.pushState(new LoadingState<>(this));
    final GraphLayoutCache glc = getGraphLayoutCache();
    model.beginUpdate();
    for (final EntityNode node : theEntities) {
        // Set the on-screen position of our entity to the attributes of the
        // entity
        final AttributeMap nAttribs = node.getAttributes();
        GraphConstants.setAutoSize(nAttribs, true);
        GraphConstants.setBounds(nAttribs, new Rectangle2D.Double(node.getX(), node.getY(), 0, 0));
        if (_nodes.containsKey(node.getName())) {
            node.setName(node.getName());
        }
        // Add our entity to the graph
        glc.insert(node);
        // Add our entity to our table of entities
        _nodes.put(node.getName(), node);
        // Add Entity to tree
        _Frame.getInfoTreeUI().addNode(node);
    }
    model.postEdit(new AbstractUndoableEdit() {

        /**
         */
        private static final long serialVersionUID = -74767611415529681L;

        @Override
        public void undo() {
            super.undo();
            for (final EntityNode node : theEntities) {
                _nodes.remove(node.getName());
            }
        }

        @Override
        public void redo() {
            super.redo();
            for (final EntityNode node : theEntities) {
                _nodes.put(node.getName(), node);
            }
        }
    });
    model.endUpdate();
    // Reload the graph to respect the new changes
    glc.reload();
    // Pop state
    _stateManager.popState();
}
Also used : AbstractUndoableEdit(javax.swing.undo.AbstractUndoableEdit) AttributeMap(org.jgraph.graph.AttributeMap) GraphLayoutCache(org.jgraph.graph.GraphLayoutCache) Rectangle2D(java.awt.geom.Rectangle2D) EntityNode(easik.sketch.vertex.EntityNode)

Example 3 with GraphLayoutCache

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

the class Overview method initializeOverview.

/**
 * When we initialise the overview, we flush out all the data concerning the
 * sketch itself.
 *
 * This methods serves as a "new overview" function.
 */
public void initializeOverview() {
    clearSelection();
    if (_sketchNodes != null) {
        for (SketchNode node : _sketchNodes.values()) {
            node.getFrame().dispose();
        }
    }
    if (_viewNodes != null) {
        for (ViewNode node : _viewNodes.values()) {
            node.getFrame().dispose();
        }
    }
    setFile(null);
    _sketchNodes = new HashMap<>();
    _viewNodes = new HashMap<>();
    _viewEdges = new HashMap<>();
    _docInfo = new DocumentInfo(_appFrame);
    if (_appFrame.getInfoTreeUI() != null) {
        _appFrame.setInfoTreeUI(new OverviewInfoTreeUI(_appFrame));
        _appFrame.getInfoTreeUI().refreshTree();
    }
    OverviewGraphModel model = new OverviewGraphModel(this);
    GraphLayoutCache glc = new GraphLayoutCache(model, new DefaultCellViewFactory());
    setModel(model);
    setGraphLayoutCache(glc);
}
Also used : DefaultCellViewFactory(org.jgraph.graph.DefaultCellViewFactory) GraphLayoutCache(org.jgraph.graph.GraphLayoutCache) ViewNode(easik.overview.vertex.ViewNode) OverviewGraphModel(easik.overview.util.graph.OverviewGraphModel) OverviewInfoTreeUI(easik.ui.tree.OverviewInfoTreeUI) SketchNode(easik.overview.vertex.SketchNode) DocumentInfo(easik.DocumentInfo)

Example 4 with GraphLayoutCache

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

the class View method addEntity.

/**
 * Adds a collection (set, list, etc.) of QueryNodes to the graph.
 *
 * @param theEntities
 *            the collection of entities to be added.
 */
public void addEntity(final Collection<QueryNode> theEntities) {
    // Push loading state
    _stateManager.pushState(new LoadingState<>(this));
    final GraphLayoutCache glc = getGraphLayoutCache();
    model.beginUpdate();
    for (final QueryNode node : theEntities) {
        // Set the on-screen position of our entity to the attributes of the
        // entity
        final AttributeMap nAttribs = node.getAttributes();
        GraphConstants.setAutoSize(nAttribs, true);
        GraphConstants.setBounds(nAttribs, new Rectangle2D.Double(node.getX(), node.getY(), 0, 0));
        if (_nodes.containsKey(node.getName())) {
            node.setName(node.getName());
        }
        // Add our entity to the graph
        glc.insert(node);
        // Add our entity to our table of entities
        _nodes.put(node.toString(), node);
        // Add Entity to tree
        _Frame.getInfoTreeUI().addNode(node);
    }
    model.postEdit(new AbstractUndoableEdit() {

        /**
         */
        private static final long serialVersionUID = -74767611415529681L;

        @Override
        public void undo() {
            super.undo();
            for (final QueryNode node : theEntities) {
                _nodes.remove(node.toString());
            }
        }

        @Override
        public void redo() {
            super.redo();
            for (final QueryNode node : theEntities) {
                _nodes.put(node.toString(), node);
            }
        }
    });
    model.endUpdate();
    autoAddExistingEdges();
    // Reload the graph to respect the new changes
    glc.reload();
    // Pop state
    _stateManager.popState();
}
Also used : AbstractUndoableEdit(javax.swing.undo.AbstractUndoableEdit) AttributeMap(org.jgraph.graph.AttributeMap) GraphLayoutCache(org.jgraph.graph.GraphLayoutCache) QueryNode(easik.view.vertex.QueryNode) Rectangle2D(java.awt.geom.Rectangle2D)

Example 5 with GraphLayoutCache

use of org.jgraph.graph.GraphLayoutCache 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

GraphLayoutCache (org.jgraph.graph.GraphLayoutCache)11 Rectangle2D (java.awt.geom.Rectangle2D)4 GuideEdge (easik.model.edge.GuideEdge)3 AbstractUndoableEdit (javax.swing.undo.AbstractUndoableEdit)3 AttributeMap (org.jgraph.graph.AttributeMap)3 SketchEdge (easik.sketch.edge.SketchEdge)2 EntityNode (easik.sketch.vertex.EntityNode)2 QueryNode (easik.view.vertex.QueryNode)2 JGraph (org.jgraph.JGraph)2 DefaultCellViewFactory (org.jgraph.graph.DefaultCellViewFactory)2 DefaultGraphCell (org.jgraph.graph.DefaultGraphCell)2 DefaultGraphModel (org.jgraph.graph.DefaultGraphModel)2 GraphModel (org.jgraph.graph.GraphModel)2 DocumentInfo (easik.DocumentInfo)1 KosarajuSCC (easik.model.util.graph.KosarajuSCC)1 OverviewGraphModel (easik.overview.util.graph.OverviewGraphModel)1 SketchNode (easik.overview.vertex.SketchNode)1 ViewNode (easik.overview.vertex.ViewNode)1 SketchGraphModel (easik.sketch.util.graph.SketchGraphModel)1 SketchFrame (easik.ui.SketchFrame)1