1 package com.mxgraph.examples.swing;
2 
3 import java.util.Map;
4 
5 import javax.swing.JFrame;
6 
7 import com.mxgraph.model.mxCell;
8 import com.mxgraph.model.mxGeometry;
9 import com.mxgraph.swing.mxGraphComponent;
10 import com.mxgraph.util.mxConstants;
11 import com.mxgraph.util.mxPoint;
12 import com.mxgraph.util.mxRectangle;
13 import com.mxgraph.view.mxEdgeStyle;
14 import com.mxgraph.view.mxGraph;
15 
16 public class Port extends JFrame
17 {
18 	/**
19 	 *
20 	 */
21 	private static final long serialVersionUID = -464235672367772404L;
22 
23 	final int PORT_DIAMETER = 20;
24 
25 	final int PORT_RADIUS = PORT_DIAMETER / 2;
26 
Port()27 	public Port()
28 	{
29 		super("Hello, World!");
30 
31 		mxGraph graph = new mxGraph() {
32 
33 			// Ports are not used as terminals for edges, they are
34 			// only used to compute the graphical connection point
35 			public boolean isPort(Object cell)
36 			{
37 				mxGeometry geo = getCellGeometry(cell);
38 
39 				return (geo != null) ? geo.isRelative() : false;
40 			}
41 
42 			// Implements a tooltip that shows the actual
43 			// source and target of an edge
44 			public String getToolTipForCell(Object cell)
45 			{
46 				if (model.isEdge(cell))
47 				{
48 					return convertValueToString(model.getTerminal(cell, true)) + " -> " +
49 						convertValueToString(model.getTerminal(cell, false));
50 				}
51 
52 				return super.getToolTipForCell(cell);
53 			}
54 
55 			// Removes the folding icon and disables any folding
56 			public boolean isCellFoldable(Object cell, boolean collapse)
57 			{
58 				return false;
59 			}
60 		};
61 
62 		// Sets the default edge style
63 		Map<String, Object> style = graph.getStylesheet().getDefaultEdgeStyle();
64 		style.put(mxConstants.STYLE_EDGE, mxEdgeStyle.ElbowConnector);
65 
66 		Object parent = graph.getDefaultParent();
67 
68 		graph.getModel().beginUpdate();
69 		try
70 		{
71 			mxCell v1 = (mxCell) graph.insertVertex(parent, null, "Hello", 20,
72 					20, 100, 100, "");
73 			v1.setConnectable(false);
74 			mxGeometry geo = graph.getModel().getGeometry(v1);
75 			// The size of the rectangle when the minus sign is clicked
76 			geo.setAlternateBounds(new mxRectangle(20, 20, 100, 50));
77 
78 			mxGeometry geo1 = new mxGeometry(0, 0.5, PORT_DIAMETER,
79 					PORT_DIAMETER);
80 			// Because the origin is at upper left corner, need to translate to
81 			// position the center of port correctly
82 			geo1.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
83 			geo1.setRelative(true);
84 
85 			mxCell port1 = new mxCell(null, geo1,
86 					"shape=ellipse;perimter=ellipsePerimeter");
87 			port1.setVertex(true);
88 
89 			mxGeometry geo2 = new mxGeometry(1.0, 0.5, PORT_DIAMETER,
90 					PORT_DIAMETER);
91 			geo2.setOffset(new mxPoint(-PORT_RADIUS, -PORT_RADIUS));
92 			geo2.setRelative(true);
93 
94 			mxCell port2 = new mxCell(null, geo2,
95 					"shape=ellipse;perimter=ellipsePerimeter");
96 			port2.setVertex(true);
97 
98 			graph.addCell(port1, v1);
99 			graph.addCell(port2, v1);
100 
101 			Object v2 = graph.insertVertex(parent, null, "World!", 240, 150, 80, 30);
102 
103 			graph.insertEdge(parent, null, "Edge", port2, v2);
104 		}
105 		finally
106 		{
107 			graph.getModel().endUpdate();
108 		}
109 
110 		mxGraphComponent graphComponent = new mxGraphComponent(graph);
111 		getContentPane().add(graphComponent);
112 		graphComponent.setToolTips(true);
113 	}
114 
main(String[] args)115 	public static void main(String[] args)
116 	{
117 		Port frame = new Port();
118 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
119 		frame.setSize(400, 320);
120 		frame.setVisible(true);
121 	}
122 
123 }
124