1 package com.mxgraph.examples.swing;
2 
3 import java.awt.BorderLayout;
4 
5 import javax.swing.ImageIcon;
6 import javax.swing.JToolBar;
7 import javax.swing.UIManager;
8 
9 import com.mxgraph.examples.swing.editor.BasicGraphEditor;
10 import com.mxgraph.examples.swing.editor.EditorPalette;
11 import com.mxgraph.examples.swing.editor.SchemaEditorMenuBar;
12 import com.mxgraph.examples.swing.editor.SchemaEditorToolBar;
13 import com.mxgraph.examples.swing.editor.SchemaGraphComponent;
14 import com.mxgraph.model.mxCell;
15 import com.mxgraph.model.mxGeometry;
16 import com.mxgraph.util.mxRectangle;
17 import com.mxgraph.view.mxCellState;
18 import com.mxgraph.view.mxGraph;
19 
20 public class SchemaEditor extends BasicGraphEditor
21 {
22 
23 	/**
24 	 *
25 	 */
26 	private static final long serialVersionUID = -7007225006753337933L;
27 
28 	/**
29 	 *
30 	 */
SchemaEditor()31 	public SchemaEditor()
32 	{
33 		super("mxGraph for JFC/Swing", new SchemaGraphComponent(new mxGraph()
34 		{
35 			/**
36 			 * Allows expanding tables
37 			 */
38 			public boolean isCellFoldable(Object cell, boolean collapse)
39 			{
40 				return model.isVertex(cell);
41 			}
42 		})
43 
44 		{
45 			/**
46 			 *
47 			 */
48 			private static final long serialVersionUID = -1194463455177427496L;
49 
50 			/**
51 			 * Disables folding icons.
52 			 */
53 			public ImageIcon getFoldingIcon(mxCellState state)
54 			{
55 				return null;
56 			}
57 
58 		}, null);
59 
60 		// Creates a single shapes palette
61 		EditorPalette shapesPalette = insertPalette("Schema");
62 		graphOutline.setVisible(false);
63 
64 		mxCell tableTemplate = new mxCell("New Table", new mxGeometry(0, 0,
65 				200, 280), null);
66 		tableTemplate.getGeometry().setAlternateBounds(
67 				new mxRectangle(0, 0, 140, 25));
68 		tableTemplate.setVertex(true);
69 
70 		shapesPalette
71 				.addTemplate(
72 						"Table",
73 						new ImageIcon(
74 								GraphEditor.class
75 										.getResource("/com/mxgraph/examples/swing/images/rectangle.png")),
76 						tableTemplate);
77 
78 		getGraphComponent().getGraph().setCellsResizable(false);
79 		getGraphComponent().setConnectable(false);
80 		getGraphComponent().getGraphHandler().setCloneEnabled(false);
81 		getGraphComponent().getGraphHandler().setImagePreview(false);
82 
83 		// Prefers default JComponent event-handling before mxCellHandler handling
84 		//getGraphComponent().getGraphHandler().setKeepOnTop(false);
85 
86 		mxGraph graph = getGraphComponent().getGraph();
87 		Object parent = graph.getDefaultParent();
88 		graph.getModel().beginUpdate();
89 		try
90 		{
91 			mxCell v1 = (mxCell) graph.insertVertex(parent, null, "Customers",
92 					20, 20, 200, 280);
93 			v1.getGeometry().setAlternateBounds(new mxRectangle(0, 0, 140, 25));
94 			mxCell v2 = (mxCell) graph.insertVertex(parent, null, "Orders",
95 					280, 20, 200, 280);
96 			v2.getGeometry().setAlternateBounds(new mxRectangle(0, 0, 140, 25));
97 		}
98 		finally
99 		{
100 			graph.getModel().endUpdate();
101 		}
102 	}
103 
104 	/**
105 	 *
106 	 */
installToolBar()107 	protected void installToolBar()
108 	{
109 		add(new SchemaEditorToolBar(this, JToolBar.HORIZONTAL),
110 				BorderLayout.NORTH);
111 	}
112 
113 	/**
114 	 *
115 	 * @param args
116 	 */
main(String[] args)117 	public static void main(String[] args)
118 	{
119 		try
120 		{
121 			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
122 		}
123 		catch (Exception e1)
124 		{
125 			e1.printStackTrace();
126 		}
127 
128 		SchemaEditor editor = new SchemaEditor();
129 		editor.createFrame(new SchemaEditorMenuBar(editor)).setVisible(true);
130 	}
131 
132 }
133