1 package com.mxgraph.examples.swing;
2 
3 import javax.swing.BorderFactory;
4 import javax.swing.CellRendererPane;
5 import javax.swing.JFrame;
6 import javax.swing.JLabel;
7 import javax.swing.border.BevelBorder;
8 
9 import com.mxgraph.canvas.mxICanvas;
10 import com.mxgraph.canvas.mxImageCanvas;
11 import com.mxgraph.swing.mxGraphComponent;
12 import com.mxgraph.swing.handler.mxRubberband;
13 import com.mxgraph.swing.view.mxInteractiveCanvas;
14 import com.mxgraph.view.mxCellState;
15 import com.mxgraph.view.mxGraph;
16 
17 public class CustomCanvas extends JFrame
18 {
19 
20 	/**
21 	 *
22 	 */
23 	private static final long serialVersionUID = -844106998814982739L;
24 
CustomCanvas()25 	public CustomCanvas()
26 	{
27 		super("Custom Canvas");
28 
29 		// Demonstrates the use of a Swing component for rendering vertices.
30 		// Note: Use the heavyweight feature to allow for event handling in
31 		// the Swing component that is used for rendering the vertex.
32 
33 		mxGraph graph = new mxGraph()
34 		{
35 			public void drawState(mxICanvas canvas, mxCellState state,
36 					boolean drawLabel)
37 			{
38 				String label = (drawLabel) ? state.getLabel() : "";
39 
40 				// Indirection for wrapped swing canvas inside image canvas (used for creating
41 				// the preview image when cells are dragged)
42 				if (getModel().isVertex(state.getCell())
43 						&& canvas instanceof mxImageCanvas
44 						&& ((mxImageCanvas) canvas).getGraphicsCanvas() instanceof SwingCanvas)
45 				{
46 					((SwingCanvas) ((mxImageCanvas) canvas).getGraphicsCanvas())
47 							.drawVertex(state, label);
48 				}
49 				// Redirection of drawing vertices in SwingCanvas
50 				else if (getModel().isVertex(state.getCell())
51 						&& canvas instanceof SwingCanvas)
52 				{
53 					((SwingCanvas) canvas).drawVertex(state, label);
54 				}
55 				else
56 				{
57 					super.drawState(canvas, state, drawLabel);
58 				}
59 			}
60 		};
61 
62 		Object parent = graph.getDefaultParent();
63 
64 		graph.getModel().beginUpdate();
65 		try
66 		{
67 
68 			Object v1 = graph.insertVertex(parent, null, "Hello", 20, 20, 80,
69 					30);
70 			Object v2 = graph.insertVertex(parent, null, "World!", 240, 150,
71 					80, 30);
72 			graph.insertEdge(parent, null, "Edge", v1, v2);
73 		}
74 		finally
75 		{
76 			graph.getModel().endUpdate();
77 		}
78 
79 		mxGraphComponent graphComponent = new mxGraphComponent(graph)
80 		{
81 			/**
82 			 *
83 			 */
84 			private static final long serialVersionUID = 4683716829748931448L;
85 
86 			public mxInteractiveCanvas createCanvas()
87 			{
88 				return new SwingCanvas(this);
89 			}
90 		};
91 
92 		getContentPane().add(graphComponent);
93 
94 		// Adds rubberband selection
95 		new mxRubberband(graphComponent);
96 	}
97 
98 	public class SwingCanvas extends mxInteractiveCanvas
99 	{
100 		protected CellRendererPane rendererPane = new CellRendererPane();
101 
102 		protected JLabel vertexRenderer = new JLabel();
103 
104 		protected mxGraphComponent graphComponent;
105 
SwingCanvas(mxGraphComponent graphComponent)106 		public SwingCanvas(mxGraphComponent graphComponent)
107 		{
108 			this.graphComponent = graphComponent;
109 
110 			vertexRenderer.setBorder(BorderFactory
111 					.createBevelBorder(BevelBorder.RAISED));
112 			vertexRenderer.setHorizontalAlignment(JLabel.CENTER);
113 			vertexRenderer.setBackground(graphComponent.getBackground()
114 					.darker());
115 			vertexRenderer.setOpaque(true);
116 		}
117 
drawVertex(mxCellState state, String label)118 		public void drawVertex(mxCellState state, String label)
119 		{
120 			vertexRenderer.setText(label);
121 			// TODO: Configure other properties...
122 
123 			rendererPane.paintComponent(g, vertexRenderer, graphComponent,
124 					(int) state.getX() + translate.x, (int) state.getY()
125 							+ translate.y, (int) state.getWidth(),
126 					(int) state.getHeight(), true);
127 		}
128 
129 	}
130 
main(String[] args)131 	public static void main(String[] args)
132 	{
133 		CustomCanvas frame = new CustomCanvas();
134 		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
135 		frame.setSize(400, 320);
136 		frame.setVisible(true);
137 	}
138 
139 }
140