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);
});
}
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();
}
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);
}
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);
}
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);
}
}
Aggregations