📄 freegrapheditor.java
字号:
symbolsPalette .addTemplate( "Fork", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/fork.png")), "rhombusImage;image=/com/mxgraph/swing/examples/images/fork.png", 80, 80, "Fork"); symbolsPalette .addTemplate( "Inclusive", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/inclusive.png")), "rhombusImage;image=/com/mxgraph/swing/examples/images/inclusive.png", 80, 80, "Inclusive"); symbolsPalette.addTemplate("Link", new ImageIcon(FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/link.png")), "roundImage;image=/com/mxgraph/swing/examples/images/link.png", 80, 80, "Link"); symbolsPalette .addTemplate( "Merge", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/merge.png")), "rhombusImage;image=/com/mxgraph/swing/examples/images/merge.png", 80, 80, "Merge"); symbolsPalette .addTemplate( "Message", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/message.png")), "roundImage;image=/com/mxgraph/swing/examples/images/message.png", 80, 80, "Message"); symbolsPalette .addTemplate( "Multiple", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/multiple.png")), "roundImage;image=/com/mxgraph/swing/examples/images/multiple.png", 80, 80, "Multiple"); symbolsPalette.addTemplate("Rule", new ImageIcon(FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/rule.png")), "roundImage;image=/com/mxgraph/swing/examples/images/rule.png", 80, 80, "Rule"); symbolsPalette .addTemplate( "Terminate", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/terminate.png")), "roundImage;image=/com/mxgraph/swing/examples/images/terminate.png", 80, 80, "Terminate"); symbolsPalette .addTemplate( "Timer", new ImageIcon( FreeGraphEditor.class .getResource("/com/mxgraph/swing/examples/images/timer.png")), "roundImage;image=/com/mxgraph/swing/examples/images/timer.png", 80, 80, "Timer"); } /** * */ public static class CustomGraphComponent extends mxGraphComponent { /** * * @param graph */ public CustomGraphComponent(mxGraph graph) { super(graph); // Sets switches typically used in an editor setPageVisible(true); setGridVisible(true); setAntiAlias(false); setToolTips(true); getConnectionHandler().setCreateTarget(true); // Loads the defalt stylesheet from an external file mxCodec codec = new mxCodec(); Document doc = mxUtils .loadDocument(FreeGraphEditor.class .getResource( "/com/mxgraph/swing/examples/resources/default-style.xml") .toString()); codec.decode(doc.getDocumentElement(), graph.getStylesheet()); // Sets the background to white getViewport().setOpaque(false); setBackground(Color.WHITE); } /** * Overrides drop behaviour to set the cell style if the target * is not a valid drop target and the cells are of the same * type (eg. both vertices or both edges). */ public Object[] importCells(Object[] cells, double dx, double dy, Object target, Point location) { if (target == null && cells.length == 1 && location != null) { target = getCellAt(location.x, location.y); if (target instanceof mxICell && cells[0] instanceof mxICell) { mxICell targetCell = (mxICell) target; mxICell dropCell = (mxICell) cells[0]; if (targetCell.isVertex() == dropCell.isVertex() || targetCell.isEdge() == dropCell.isEdge()) { mxIGraphModel model = graph.getModel(); model.setStyle(target, model.getStyle(cells[0])); graph.setSelectionCell(target); return null; } } } return super.importCells(cells, dx, dy, target, location); } } /** * A graph that creates new edges from a given template edge. */ public static class CustomGraph extends mxGraph { /** * Holds the edge to be used as a template for inserting new edges. */ protected Object edgeTemplate; /** * Custom graph that defines the alternate edge style to be used when * the middle control point of edges is double clicked (flipped). */ public CustomGraph() { setAlternateEdgeStyle("edgeStyle=mxEdgeStyle.ElbowConnector;elbow=vertical"); } /** * Sets the edge template to be used to inserting edges. */ public void setEdgeTemplate(Object template) { edgeTemplate = template; } /** * Prints out some useful information about the cell in the tooltip. */ public String getToolTipForCell(Object cell) { String tip = "<html>"; mxGeometry geo = getModel().getGeometry(cell); mxCellState state = getView().getState(cell); if (getModel().isEdge(cell)) { tip += "points={"; if (geo != null) { List points = geo.getPoints(); if (points != null) { Iterator it = points.iterator(); while (it.hasNext()) { mxPoint point = (mxPoint) it.next(); tip += "[x=" + numberFormat.format(point.getX()) + ",y=" + numberFormat.format(point.getY()) + "],"; } tip = tip.substring(0, tip.length() - 1); } } tip += "}<br>"; tip += "absPoints={"; if (state != null) { for (int i = 0; i < state.getAbsolutePointCount(); i++) { mxPoint point = state.getAbsolutePoint(i); tip += "[x=" + numberFormat.format(point.getX()) + ",y=" + numberFormat.format(point.getY()) + "],"; } tip = tip.substring(0, tip.length() - 1); } tip += "}"; } else { tip += "geo=["; if (geo != null) { tip += "x=" + numberFormat.format(geo.getX()) + ",y=" + numberFormat.format(geo.getY()) + ",width=" + numberFormat.format(geo.getWidth()) + ",height=" + numberFormat.format(geo.getHeight()); } tip += "]<br>"; tip += "state=["; if (state != null) { tip += "x=" + numberFormat.format(state.getX()) + ",y=" + numberFormat.format(state.getY()) + ",width=" + numberFormat.format(state.getWidth()) + ",height=" + numberFormat.format(state.getHeight()); } tip += "]"; } mxPoint trans = getView().getTranslate(); tip += "<br>scale=" + numberFormat.format(getView().getScale()) + ", translate=[x=" + numberFormat.format(trans.getX()) + ",y=" + numberFormat.format(trans.getY()) + "]"; tip += "</html>"; return tip; } /** * Overrides the method to use the currently selected edge template for * new edges. * * @param graph * @param parent * @param id * @param value * @param source * @param target * @param style * @return */ public Object createEdge(Object parent, String id, Object value, Object source, Object target, String style) { if (edgeTemplate != null) { mxCell edge = (mxCell) cloneCells(new Object[] { edgeTemplate })[0]; edge.setId(id); return edge; } return super.createEdge(parent, id, value, source, target, style); } } /** * * @param args */ public static void main(String[] args) { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (Exception e1) { e1.printStackTrace(); } mxConstants.SHADOW_COLOR = Color.LIGHT_GRAY; FreeGraphEditor editor = new FreeGraphEditor(); editor.createFrame(new EditorMenuBar(editor)).setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -