/** * Returns an list containing all graph cells taht have the same userObject. * * @param userObject The userObject for which all graph cels should be found. * @return java.util.List */ private List<Object> getCellsWithUserObject(Object userObject) { List<Object> cells = new ArrayList<Object>(); if (userObject != null) { // get the graph model, which contains all the cells of the graph GraphModel model = graph.getModel(); // loop through all the cells of the model for (int i = 0; i < model.getRootCount(); i++) { Object root = model.getRootAt(i); // get the userObject for the curent graph cell Object rootUserObject = getUserObject(root); if (rootUserObject != null) { if (userObject == rootUserObject) { // the userObject of the current graph cell is teh same like the wanted userObject cells.add(root); } } } } return cells; }
/** Populate a fresh mxGraph data model from a JGraph data model */ public void syncMx(JGraph inputGraph) { if (inputGraph != null) { jgraph = inputGraph; } else { return; } GraphModel jModel = jgraph.getModel(); mxIGraphModel mxModel = null; if (graph == null) { mxModel = new mxGraphModel(); this.graph = new mxGraph(mxModel); } mxModel = graph.getModel(); mxModel.beginUpdate(); try { int rootCount = jModel.getRootCount(); // Insert everything in mx in the same order and // hierarchy as jgraph for (int i = 0; i < rootCount; i++) { Object cell = jModel.getRootAt(i); if (cell != null) { insertCell(cell, graph.getDefaultParent()); } } // Edges are inserted without source and targets, // since the vertices may not exist at insertion // time if (edges != null && vertices != null) { Set<Object> keys = edges.keySet(); for (Object edge : keys) { Object source = jModel.getSource(edge); Object target = jModel.getTarget(edge); Object mxEdge = edges.get(edge); if (facade != null) { source = facade.getSource(edge); target = facade.getTarget(edge); } if (source != null) { source = vertices.get(source); graph.connectCell(mxEdge, source, true); } if (target != null) { target = vertices.get(target); graph.connectCell(mxEdge, target, false); } } } } catch (Exception e) { e.printStackTrace(); } finally { mxModel.endUpdate(); } }