public org.jgraph.graph.CellView createView(GraphModel model, Object cell) { org.jgraph.graph.CellView view = null; if (model.isPort(cell)) view = new PortView(cell); else if (model.isEdge(cell)) view = createEdgeView(cell); else view = createVertexView(cell); return view; }
/** * 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; }
@Override public void write(GraphModel graphModel) { TreeMap<Integer, List<Pair>> treeMap = new TreeMap<Integer, List<Pair>>(); for (String lineId : graphModel.getLineIds()) { List<TestCaseResult> propertyList = graphModel.get(lineId); for (TestCaseResult properies : propertyList) { String x = properies.get(keyName); String y = properies.get(valueName); List<Pair> valueList = treeMap.get(new Integer(x)); if (valueList == null) { valueList = new LinkedList<Pair>(); treeMap.put(new Integer(x), valueList); } valueList.add(new Pair(lineId, y)); } } StringBuffer sb = new StringBuffer(); for (Integer x : treeMap.descendingKeySet()) { sb.append(x); for (String lineId : graphModel.getLineIds()) { String result = ""; for (Pair pair : treeMap.get(x)) { if (pair.getLineId().equals(lineId)) { result = pair.getValue(); } } sb.append(" " + result); } sb.append("\n"); } write(sb.toString()); }
/** * Set the graph being viewed. If there is a graph already and it contains data, delete the * figures of that graph's nodes and edges (but don't modify the graph itself). */ public void setGraphModel(GraphModel model) { Iterator i; // FIXME we shouldn't have to cast this. // FigureLayer layer = getGraphPane().getForegroundLayer(); if (_model != null) { // Clear existing figures Object root = _model.getRoot(); if (_model.getNodeCount(root) != 0) { for (i = _model.nodes(root); i.hasNext(); ) { clearNode(i.next()); } for (i = GraphUtilities.totallyContainedEdges(root, _model); i.hasNext(); ) { clearEdge(i.next()); } } _model.removeGraphListener(_localListener); } // Set the graph _model = model; if (_model != null) { _model.addGraphListener(_localListener); Object root = _model.getRoot(); GraphEvent evt = new GraphEvent(new Object(), GraphEvent.STRUCTURE_CHANGED, root); _localListener.structureChanged(evt); } }
/** * Render the current graph again by recreating the figures for all nodes and edges, but do not * alter the connectivity in the graph. This should be called when changes to renderers are made. */ public void rerender() { // FIXME: it would be nice to be able to do this without all // stupid business with the selection model. List selectedEdges = new LinkedList(); List selectedNodes = new LinkedList(); // Remove any old objects that no longer exist in the model Do // the edges first, then the nodes, since we cannot blow away // selection on an edge before getting rid of the endpoints of // the edge. Iterator figures = (new HashSet(_map.values())).iterator(); while (figures.hasNext()) { Figure figure = (Figure) figures.next(); Object object = figure.getUserObject(); if (_model.isEdge(object)) { if (!GraphUtilities.isPartiallyContainedEdge(object, _model.getRoot(), _model)) { if (_selectionModel.containsSelection(figure)) { _selectionModel.removeSelection(figure); } clearEdge(object); // Previously, the figure was being left in _map, // which results in an accumulation over time // of figures that should not be rendered. // EAL 4/29/04. _map.remove(object); } } } figures = (new HashSet(_map.values())).iterator(); while (figures.hasNext()) { Figure figure = (Figure) figures.next(); Object object = figure.getUserObject(); if (_model.isNode(object)) { if (!GraphUtilities.isContainedNode(object, _model.getRoot(), _model)) { if (_selectionModel.containsSelection(figure)) { _selectionModel.removeSelection(figure); } clearNode(object); // Previously, the figure was being left in _map, // which results in an accumulation over time // of figures that should not be rendered. // EAL 4/29/04. _map.remove(object); } } } // Save the selected edges. Iterator edges = GraphUtilities.totallyContainedEdges(_model.getRoot(), _model); while (edges.hasNext()) { Object edge = edges.next(); Figure oldFigure = getFigure(edge); boolean selected = _selectionModel.containsSelection(oldFigure); if (selected) { selectedEdges.add(edge); } } // Save the selected nodes. Iterator nodes = (GraphUtilities.nodeSet(_model.getRoot(), _model)).iterator(); while (nodes.hasNext()) { Object node = nodes.next(); Figure oldFigure = getFigure(node); boolean selected = _selectionModel.containsSelection(oldFigure); if (selected) { selectedNodes.add(node); } } // Clear all the selections (note that there may be selected objects // which no longer exist!) _selectionModel.clearSelection(); // Draw the nodes to be rendered before the edges. nodes = _model.nodesBeforeEdges(_model.getRoot()); while (nodes.hasNext()) { Object node = nodes.next(); drawNode(node); } // Draw the edges that are connected to any of the above nodes. edges = GraphUtilities.partiallyContainedEdges(_model.getRoot(), _model); while (edges.hasNext()) { Object edge = edges.next(); drawEdge(edge); if (selectedEdges.contains(edge)) { _selectionModel.addSelection(getFigure(edge)); } } // Draw the nodes to be rendered after the edges. nodes = _model.nodesAfterEdges(_model.getRoot()); while (nodes.hasNext()) { Object node = nodes.next(); drawNode(node); } // Restore the selected nodes. nodes = (GraphUtilities.nodeSet(_model.getRoot(), _model)).iterator(); while (nodes.hasNext()) { Object node = nodes.next(); if (selectedNodes.contains(node)) { _selectionModel.addSelection(getFigure(node)); } } }
public void process() { // Workspace ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); if (workspace == null) { workspace = pc.newWorkspace(pc.getCurrentProject()); pc.openWorkspace(workspace); } if (container.getSource() != null) { pc.setSource(workspace, container.getSource()); } // Architecture GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(workspace); HierarchicalGraph graph = null; switch (container.getEdgeDefault()) { case DIRECTED: graph = graphModel.getHierarchicalDirectedGraph(); break; case UNDIRECTED: graph = graphModel.getHierarchicalUndirectedGraph(); break; case MIXED: graph = graphModel.getHierarchicalMixedGraph(); break; default: graph = graphModel.getHierarchicalMixedGraph(); break; } GraphFactory factory = graphModel.factory(); // Attributes - Creates columns for properties attributeModel = Lookup.getDefault().lookup(AttributeController.class).getModel(workspace); attributeModel.mergeModel(container.getAttributeModel()); // Dynamic if (container.getTimeFormat() != null) { DynamicController dynamicController = Lookup.getDefault().lookup(DynamicController.class); if (dynamicController != null) { dynamicController.setTimeFormat(container.getTimeFormat()); } } int nodeCount = 0; // Create all nodes for (NodeDraftGetter draftNode : container.getNodes()) { Node n = factory.newNode(draftNode.isAutoId() ? null : draftNode.getId()); flushToNode(draftNode, n); draftNode.setNode(n); nodeCount++; } // Push nodes in data structure for (NodeDraftGetter draftNode : container.getNodes()) { Node n = draftNode.getNode(); NodeDraftGetter[] parents = draftNode.getParents(); if (parents != null) { for (int i = 0; i < parents.length; i++) { Node parent = parents[i].getNode(); graph.addNode(n, parent); } } else { graph.addNode(n); } } // Create all edges and push to data structure int edgeCount = 0; for (EdgeDraftGetter edge : container.getEdges()) { Node source = edge.getSource().getNode(); Node target = edge.getTarget().getNode(); Edge e = null; switch (container.getEdgeDefault()) { case DIRECTED: e = factory.newEdge( edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), true); break; case UNDIRECTED: e = factory.newEdge( edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), false); break; case MIXED: e = factory.newEdge( edge.isAutoId() ? null : edge.getId(), source, target, edge.getWeight(), edge.getType().equals(EdgeType.UNDIRECTED) ? false : true); break; } flushToEdge(edge, e); edgeCount++; graph.addEdge(e); } workspace = null; }
/** 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(); } }
/** * Converts a JGraph cell into an mxGraph with the specified mxGraph parent * * @param cell the JGraph cell * @param parent the mxGraph parent */ protected void insertCell(Object cell, Object parent) { Object mxCell = null; GraphModel jModel = jgraph.getModel(); Object value = jModel.getValue(cell); if (jModel.isEdge(cell)) { mxCell = graph.insertEdge(parent, null, value, null, null); if (edges == null) { edges = new Hashtable<Object, Object>(); } edges.put(cell, mxCell); } else if (jModel.isPort(cell)) { // TODO } else { // A vertex Rectangle2D bounds = null; if (facade != null) { bounds = facade.getBounds(cell); } else { GraphLayoutCache cache = jgraph.getGraphLayoutCache(); CellView cellView = cache.getMapping(cell, false); bounds = cellView.getBounds(); } if (bounds != null) { mxCell = graph.insertVertex( parent, null, value, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight()); } else { mxCell = graph.insertVertex(parent, null, value, 100, 100, 80, 30); } if (vertices == null) { vertices = new Hashtable<Object, Object>(); } vertices.put(cell, mxCell); } // Process all the children of this cell int childCount = jModel.getChildCount(cell); if (mxCell != null) { for (int i = 0; i < childCount; i++) { Object childCell = jModel.getChild(cell, i); if (cell != null) { insertCell(childCell, mxCell); } } } }
private void doPoetGraph(String path, String poet) { System.out.println(poet); ProjectController pc = Lookup.getDefault().lookup(ProjectController.class); pc.newProject(); Workspace workspace = pc.getCurrentWorkspace(); GraphModel graphModel = Lookup.getDefault().lookup(GraphController.class).getModel(); UndirectedGraph directedGraph = graphModel.getUndirectedGraph(); ExportController ec = Lookup.getDefault().lookup(ExportController.class); ArrayList<String> queue = new ArrayList<String>(); ArrayList<Integer> depthqueue = new ArrayList<Integer>(); queue.add(poet); depthqueue.add(0); int depth = 0; int nodecount = 0; while (queue.size() != 0 && depth < 3 && nodecount < 20) { String name = queue.remove(0); depth = depthqueue.remove(0); Node n = directedGraph.getNode(name); if (n == null) { n = graphModel.factory().newNode(name); n.getNodeData().setLabel(name); directedGraph.addNode(n); nodecount++; } Iterator<String> it = edges.get(poet).keySet().iterator(); while (it.hasNext()) { String to = it.next(); Node slave = directedGraph.getNode(to); if (slave == null) { slave = graphModel.factory().newNode(to); slave.getNodeData().setLabel(to); directedGraph.addNode(slave); } Edge e = graphModel.factory().newEdge(n, slave, edges.get(poet).get(to) * 15, false); directedGraph.addEdge(e); queue.add(to); depthqueue.add(depth + 1); } } // Preview PreviewModel previewModel = Lookup.getDefault().lookup(PreviewController.class).getModel(); previewModel.getProperties().putValue(PreviewProperty.SHOW_NODE_LABELS, Boolean.TRUE); previewModel.getProperties().putValue(PreviewProperty.EDGE_COLOR, new EdgeColor(Color.BLUE)); previewModel.getProperties().putValue(PreviewProperty.EDGE_THICKNESS, new Float(0.005f)); previewModel .getProperties() .putValue(PreviewProperty.NODE_LABEL_COLOR, new DependantOriginalColor(Color.RED)); previewModel .getProperties() .putValue( PreviewProperty.NODE_LABEL_FONT, previewModel .getProperties() .getFontValue(PreviewProperty.NODE_LABEL_FONT) .deriveFont(12)); YifanHuLayout layout = new YifanHuLayout(null, new StepDisplacement(1f)); layout.setGraphModel(graphModel); layout.resetPropertiesValues(); layout.setOptimalDistance(120f); layout.initAlgo(); for (int i = 0; i < 100 && layout.canAlgo(); i++) { layout.goAlgo(); } try { ec.exportFile(new File(path + poet + ".svg")); } catch (IOException ex) { ex.printStackTrace(); return; } }