Search in sources :

Example 1 with DefaultGraphCell

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

the class BaseGraphBuilder method addMouseListeners.

private void addMouseListeners() {
    graph.addMouseListener(new MouseAdapter() {

        @Override
        public void mouseReleased(MouseEvent e) {
            if (e.isPopupTrigger()) {
                Object selected = graph.getSelectionCell();
                if (selected != null && selected instanceof DefaultGraphCell) {
                    Object userObject = ((DefaultGraphCell) selected).getUserObject();
                    if (userObject instanceof EntityCellMetadata) {
                        showPopup(e.getPoint(), ((EntityCellMetadata) userObject).fetchEntity());
                    }
                }
            }
        }
    });
    graph.addMouseWheelListener(e -> {
        // limit scale
        double scale = graph.getScale() / Math.pow(ZOOM_FACTOR, e.getWheelRotation());
        scale = Math.max(scale, 0.1);
        scale = Math.min(scale, 3);
        graph.setScale(scale);
    });
}
Also used : MouseEvent(java.awt.event.MouseEvent) DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) MouseAdapter(java.awt.event.MouseAdapter)

Example 2 with DefaultGraphCell

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

the class BaseGraphBuilder method encodeAsXML.

@Override
public void encodeAsXML(XMLEncoder encoder, ConfigurationNodeVisitor delegate) {
    encoder.start("graph").attribute("type", getType().toString()).attribute("scale", String.valueOf(graph.getScale()));
    for (Entry<String, DefaultGraphCell> entry : entityCells.entrySet()) {
        Rectangle2D rect = graph.getCellBounds(entry.getValue());
        encoder.start("entity").attribute("name", entry.getKey()).attribute("x", String.valueOf(Math.round(100 * rect.getX()) / 100.0)).attribute("y", String.valueOf(Math.round(100 * rect.getY()) / 100.0)).attribute("width", String.valueOf(rect.getWidth())).attribute("height", String.valueOf(rect.getHeight())).end();
    }
    encoder.end();
}
Also used : DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) Rectangle2D(java.awt.geom.Rectangle2D)

Example 3 with DefaultGraphCell

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

the class BaseGraphBuilder method insertEntityCell.

protected void insertEntityCell(Entity entity) {
    DefaultGraphCell cell = createEntityCell(entity);
    // putting cell to a random posistion..
    GraphConstants.setBounds(cell.getAttributes(), new Rectangle2D.Double(Math.random() * graph.getWidth(), Math.random() * graph.getHeight(), 10, 10));
    // setting graph type-specific attrs
    postProcessEntity(entity, cell);
    insert(cell);
}
Also used : DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) Rectangle2D(java.awt.geom.Rectangle2D)

Example 4 with DefaultGraphCell

use of org.jgraph.graph.DefaultGraphCell 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 5 with DefaultGraphCell

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

the class BaseGraphBuilder method updateEntityCell.

/**
 * Updates specified entity on the graph
 */
protected void updateEntityCell(Entity e) {
    DefaultGraphCell cell = entityCells.get(e.getName());
    if (cell != null) {
        GraphConstants.setValue(cell.getAttributes(), getCellMetadata(e));
        GraphConstants.setResize(cell.getAttributes(), true);
        Map<DefaultGraphCell, AttributeMap> nested = new HashMap<>();
        nested.put(cell, cell.getAttributes());
        edit(nested);
    }
}
Also used : DefaultGraphCell(org.jgraph.graph.DefaultGraphCell) AttributeMap(org.jgraph.graph.AttributeMap) HashMap(java.util.HashMap)

Aggregations

DefaultGraphCell (org.jgraph.graph.DefaultGraphCell)9 Rectangle2D (java.awt.geom.Rectangle2D)2 MouseAdapter (java.awt.event.MouseAdapter)1 MouseEvent (java.awt.event.MouseEvent)1 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LineBorder (javax.swing.border.LineBorder)1 DataMap (org.apache.cayenne.map.DataMap)1 Entity (org.apache.cayenne.map.Entity)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 JGraph (org.jgraph.JGraph)1 AttributeMap (org.jgraph.graph.AttributeMap)1 DefaultCellViewFactory (org.jgraph.graph.DefaultCellViewFactory)1 DefaultEdge (org.jgraph.graph.DefaultEdge)1 DefaultGraphModel (org.jgraph.graph.DefaultGraphModel)1 GraphLayoutCache (org.jgraph.graph.GraphLayoutCache)1 GraphModel (org.jgraph.graph.GraphModel)1