📄 jgraphgraphfactory.java
字号:
// Create array big enough for all cells Object[] cells = new DefaultGraphCell[numNodes + numEdges]; initialise(graph); int gridWidth = (int) Math.sqrt(numNodes); for (int i = 0; i < numNodes; i++) { Point2D cellPosition = calcCellPosition(i, gridWidth); DefaultGraphCell cell = createVertex(new Integer(i).toString(), cellPosition, defaultVertexAttributes); cells[numEdges + i] = cell; } int cellCount = 0; // Connect every cell to each other for (int i = 0; i < numNodes; i++) { // Port of child i Port sourcePort; if (cells[numEdges + i] instanceof Port) { sourcePort = (Port)cells[numEdges + i]; } else { sourcePort = (Port)model.getChild(cells[numEdges + i], 0); } for (int j = i + 1; j < numNodes; j++) { Port targetPort; if (cells[numEdges + j] instanceof Port) { targetPort = (Port)cells[numEdges + j]; } else { targetPort = (Port)model.getChild(cells[numEdges + j], 0); } Edge edge = createEdge(defaultEdgeAttributes, sourcePort, targetPort); cells[cellCount++] = edge; } } insertIntoGraph(graph, cells); } /** * clears the graph and inserts a fully connected graph. The nodes are * initially placed a grid. There are the same number of cells and edges in * the graph, all cells have at least one edge connected to them. * * @param graph * the JGraph instance to act upon * @param defaultVertexAttributes * the default attributes to use for vertices * @param defaultEdgeAttributes * the default attributes to use for edges */ public void insertSampleFlowGraph(JGraph graph, Map defaultVertexAttributes, Map defaultEdgeAttributes) { } /** * Returns a point on a square grid given the index into the total number of * cells and the width of one line of the grid * * @param i * index of cell * @param gridWidth * width of each grid line * @return the position of the cell */ private Point2D calcCellPosition(int i, int gridWidth) { if (i != 0) { return new Point2D.Double(20 + (60 * (i % gridWidth)), 20 + (40 * (i / gridWidth))); } else { return new Point2D.Double(20, 20); } } /** * Method hook to create custom vertices * * @param userObject * the user object to pass to the cell * @return the new vertex instance */ protected DefaultGraphCell createVertex(Object userObject, Point2D position, Map defaultVertexAttributes) { AttributeMap attributes = new AttributeMap(defaultVertexAttributes); GraphConstants.setBounds(attributes, new Rectangle2D.Double(position .getX(), position.getY(), 40, 20)); DefaultGraphCell cell = new DefaultGraphCell(userObject, attributes); // Add a Port cell.addPort(); return cell; } /** * Method hook to create custom edges * * @return the new vertex instance */ protected Edge createEdge(Map defaultEdgeAttributes, Port sourcePort, Port targetPort) { AttributeMap edgeAttrib = null; if (defaultEdgeAttributes != null) { edgeAttrib = new AttributeMap(defaultEdgeAttributes); } else { edgeAttrib = new AttributeMap(6); } Edge edge = new DefaultEdge(null, edgeAttrib); edge.setSource(sourcePort); edge.setTarget(targetPort); return edge; } /** * Common initialization functionality * */ protected void initialise(JGraph graph) { // Remove all previous cells graph.getModel().remove(graph.getDescendants(graph.getRoots())); } /** * Common insert functionality */ protected void insertIntoGraph(JGraph graph, Object[] cells) { // For performance, don't select inserted cells boolean selectsAll = graph.getGraphLayoutCache() .isSelectsAllInsertedCells(); boolean selectsLocal = graph.getGraphLayoutCache() .isSelectsLocalInsertedCells(); graph.getGraphLayoutCache().setSelectsAllInsertedCells(false); graph.getGraphLayoutCache().setSelectsLocalInsertedCells(false); if (insertIntoModel) { graph.getModel().insert(cells, null, null, null, null); } else { graph.getGraphLayoutCache().insert(cells); } graph.getGraphLayoutCache().setSelectsAllInsertedCells(selectsAll); graph.getGraphLayoutCache().setSelectsLocalInsertedCells(selectsLocal); } /** * Inserts the specified cells into the graph model. This method is a * general implementation of cell insertion. If the source and target port * are null, then no connection set is created. The method uses the * attributes from the specified edge and the egdge's children to construct * the insert call. This example shows how to insert an edge with a special * arrow between two known vertices: * * <pre> * Object source = graph.getDefaultPortForCell(sourceVertex).getCell(); * Object target = graph.getDefaultPortForCell(targetVertex).getCell(); * DefaultEdge edge = new DefaultEdge("Hello, world!"); * edge.setSource(source); * edge.setTarget(target); * Map attrs = edge.getAttributes(); * GraphConstants.setLineEnd(attrs, GraphConstants.ARROW_TECHNICAL); * graph.getGraphLayoutCache().insert(edge); * </pre> */ public static void insert(GraphModel model, Object[] cells) { insert(model, cells, new Hashtable(), new ConnectionSet(), new ParentMap()); } /** * Variant of the insert method that allows to pass a default connection set * and parent map and nested map. */ public static void insert(GraphModel model, Object[] cells, Map nested, ConnectionSet cs, ParentMap pm) { if (cells != null) { if (nested == null) nested = new Hashtable(); if (cs == null) cs = new ConnectionSet(); if (pm == null) pm = new ParentMap(); for (int i = 0; i < cells.length; i++) { // Using the children of the vertex we construct the parent map. int childCount = model.getChildCount(cells[i]); for (int j = 0; j < childCount; j++) { Object child = model.getChild(cells[i], j); pm.addEntry(child, cells[i]); // And add their attributes to the nested map AttributeMap attrs = model.getAttributes(child); if (attrs != null) nested.put(child, attrs); } // A nested map with the vertex as key // and its attributes as the value // is required for the model. Map attrsTmp = (Map) nested.get(cells[i]); Map attrs = model.getAttributes(cells[i]); if (attrsTmp != null) attrs.putAll(attrsTmp); nested.put(cells[i], attrs); // Check if we have parameters for a connection set. Object sourcePort = model.getSource(cells[i]); if (sourcePort != null) cs.connect(cells[i], sourcePort, true); Object targetPort = model.getTarget(cells[i]); if (targetPort != null) cs.connect(cells[i], targetPort, false); } // Create an array with the parent and its children. cells = DefaultGraphModel.getDescendants(model, cells) .toArray(); // Finally call the insert method on the parent class. model.insert(cells, nested, cs, pm, null); } } /** * @return Returns the insertIntoModel. */ public boolean isInsertIntoModel() { return insertIntoModel; } /** * @param insertIntoModel * The insertIntoModel to set. */ public void setInsertIntoModel(boolean insertIntoModel) { this.insertIntoModel = insertIntoModel; } /** * @return Returns the numEdges. */ public int getNumEdges() { return numEdges; } /** * @param numEdges * The numEdges to set. */ public void setNumEdges(int numEdges) { if (numEdges < 1) { numEdges = 1; } else if (numEdges > 2000000) { numEdges = 2000000; } this.numEdges = numEdges; } /** * @return Returns the numNodes. */ public int getNumNodes() { return numNodes; } /** * @param numNodes * The numNodes to set. */ public void setNumNodes(int numNodes) { if (numNodes < 1) { numNodes = 1; } else if (numNodes > 2000000) { numNodes = 2000000; } this.numNodes = numNodes; } /** * @return Returns the maxNodesPerTreeLevel. */ public int getMaxNodesPerTreeLevel() { return maxNodesPerTreeLevel; } /** * @param maxNodesPerTreeLevel * The maxNodesPerTreeLevel to set. */ public void setMaxNodesPerTreeLevel(int maxNodesPerTreeLevel) { this.maxNodesPerTreeLevel = maxNodesPerTreeLevel; } public static void center(Window wnd) { Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); Dimension frameSize = wnd.getSize(); wnd.setLocation(screenSize.width / 2 - (frameSize.width / 2), screenSize.height / 2 - (frameSize.height / 2)); } /** * Simple Dialog that configures how many nodes and edges the graph factory * is to create */ public class FactoryConfigDialog extends JDialog { protected boolean insertGraph = false; protected JGraph graph; protected int graphType; protected Map defaultVertexAttributes; protected Map defaultEdgeAttributes; protected JTextField maxTreeNodeChildren = new JTextField(); protected JTextField numNodes = new JTextField(); protected JTextField numEdges = new JTextField(); protected JCheckBox insertIntoModel = new JCheckBox(); public FactoryConfigDialog() { super((Frame) null, "Configure Sample Graph", true); JPanel panel = new JPanel(new GridLayout(4, 2, 4, 4)); panel.add(new JLabel("Max Child Nodes in Tree")); panel.add(maxTreeNodeChildren); panel.add(new JLabel("Number of nodes")); panel.add(numNodes); panel.add(new JLabel("Number of edges")); panel.add(numEdges); panel.add(new JLabel("Insert into model")); panel.add(insertIntoModel); JPanel panelBorder = new JPanel(); panelBorder.setBorder(new EmptyBorder(10, 10, 10, 10)); panelBorder.add(panel); JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT)); panel.setBorder(BorderFactory.createCompoundBorder(BorderFactory .createMatteBorder(1, 0, 0, 0, Color.GRAY), BorderFactory .createEmptyBorder(16, 8, 8, 8))); JButton applyButton = new JButton("Insert"); JButton closeButton = new JButton("Cancel"); buttonPanel.add(closeButton); buttonPanel.add(applyButton); getRootPane().setDefaultButton(applyButton); applyButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { applyValues(); if (graphType == TREE) { insertTreeSampleData(graph, defaultVertexAttributes, defaultEdgeAttributes); } else if (graphType == RANDOM_CONNECTED) { insertConnectedGraphSampleData(graph, defaultVertexAttributes, defaultEdgeAttributes); } else if (graphType == FULLY_CONNECTED) { insertFullyConnectedGraphSampleData(graph, defaultVertexAttributes, defaultEdgeAttributes); } else if (graphType == FLOW) { insertSampleFlowGraph(graph, defaultVertexAttributes, defaultEdgeAttributes); } setVisible(false); } }); closeButton.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { insertGraph = false; setVisible(false); } }); getContentPane().add(panelBorder, BorderLayout.CENTER); getContentPane().add(buttonPanel, BorderLayout.SOUTH); pack(); setResizable(false); // setLocationRelativeTo(parent); } public void configureLayout(JGraph graph, int graphType, Map defaultVertexAttributes, Map defaultEdgeAttributes) { this.graph = graph; this.graphType = graphType; this.defaultVertexAttributes = defaultVertexAttributes; this.defaultEdgeAttributes = defaultEdgeAttributes; maxTreeNodeChildren.setText(String .valueOf(getMaxNodesPerTreeLevel())); this.numNodes.setText(String.valueOf(getNumNodes())); this.numEdges.setText(String.valueOf(getNumEdges())); this.insertIntoModel.setSelected(isInsertIntoModel()); } protected void applyValues() { setMaxNodesPerTreeLevel(Integer.parseInt(maxTreeNodeChildren .getText())); setNumNodes(Integer.parseInt(this.numNodes.getText())); setNumEdges(Integer.parseInt(this.numEdges.getText())); setInsertIntoModel(this.insertIntoModel.isSelected()); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -