1 package com.mxgraph.examples.swing.editor;
2 
3 import java.awt.event.ActionEvent;
4 import java.awt.event.ActionListener;
5 
6 import javax.swing.AbstractAction;
7 import javax.swing.JMenu;
8 import javax.swing.JMenuBar;
9 import javax.swing.JMenuItem;
10 import javax.swing.SwingUtilities;
11 import javax.swing.TransferHandler;
12 import javax.swing.UIManager;
13 
14 import com.mxgraph.examples.swing.editor.EditorActions.BackgroundAction;
15 import com.mxgraph.examples.swing.editor.EditorActions.BackgroundImageAction;
16 import com.mxgraph.examples.swing.editor.EditorActions.ExitAction;
17 import com.mxgraph.examples.swing.editor.EditorActions.GridColorAction;
18 import com.mxgraph.examples.swing.editor.EditorActions.GridStyleAction;
19 import com.mxgraph.examples.swing.editor.EditorActions.HistoryAction;
20 import com.mxgraph.examples.swing.editor.EditorActions.NewAction;
21 import com.mxgraph.examples.swing.editor.EditorActions.OpenAction;
22 import com.mxgraph.examples.swing.editor.EditorActions.PageBackgroundAction;
23 import com.mxgraph.examples.swing.editor.EditorActions.PageSetupAction;
24 import com.mxgraph.examples.swing.editor.EditorActions.PrintAction;
25 import com.mxgraph.examples.swing.editor.EditorActions.PromptPropertyAction;
26 import com.mxgraph.examples.swing.editor.EditorActions.SaveAction;
27 import com.mxgraph.examples.swing.editor.EditorActions.ScaleAction;
28 import com.mxgraph.examples.swing.editor.EditorActions.SelectShortestPathAction;
29 import com.mxgraph.examples.swing.editor.EditorActions.SelectSpanningTreeAction;
30 import com.mxgraph.examples.swing.editor.EditorActions.StylesheetAction;
31 import com.mxgraph.examples.swing.editor.EditorActions.ToggleDirtyAction;
32 import com.mxgraph.examples.swing.editor.EditorActions.ToggleGridItem;
33 import com.mxgraph.examples.swing.editor.EditorActions.ToggleOutlineItem;
34 import com.mxgraph.examples.swing.editor.EditorActions.TogglePropertyItem;
35 import com.mxgraph.examples.swing.editor.EditorActions.ToggleRulersItem;
36 import com.mxgraph.examples.swing.editor.EditorActions.WarningAction;
37 import com.mxgraph.examples.swing.editor.EditorActions.ZoomPolicyAction;
38 import com.mxgraph.swing.mxGraphComponent;
39 import com.mxgraph.swing.util.mxGraphActions;
40 import com.mxgraph.util.mxPoint;
41 import com.mxgraph.util.mxResources;
42 import com.mxgraph.view.mxGraph;
43 
44 public class SchemaEditorMenuBar extends JMenuBar
45 {
46 
47 	/**
48 	 *
49 	 */
50 	private static final long serialVersionUID = 6776304509649205465L;
51 
52 	@SuppressWarnings("serial")
SchemaEditorMenuBar(final BasicGraphEditor editor)53 	public SchemaEditorMenuBar(final BasicGraphEditor editor)
54 	{
55 		final mxGraphComponent graphComponent = editor.getGraphComponent();
56 		final mxGraph graph = graphComponent.getGraph();
57 		JMenu menu = null;
58 		JMenu submenu = null;
59 
60 		// Creates the file menu
61 		menu = add(new JMenu(mxResources.get("file")));
62 
63 		menu.add(editor.bind(mxResources.get("new"), new NewAction(),
64 				"/com/mxgraph/examples/swing/images/new.gif"));
65 		menu.add(editor.bind(mxResources.get("openFile"), new OpenAction(),
66 				"/com/mxgraph/examples/swing/images/open.gif"));
67 
68 		menu.addSeparator();
69 
70 		menu.add(editor.bind(mxResources.get("save"), new SaveAction(false),
71 				"/com/mxgraph/examples/swing/images/save.gif"));
72 		menu.add(editor.bind(mxResources.get("saveAs"), new SaveAction(true),
73 				"/com/mxgraph/examples/swing/images/saveas.gif"));
74 
75 		menu.addSeparator();
76 
77 		menu.add(editor.bind(mxResources.get("pageSetup"),
78 				new PageSetupAction(),
79 				"/com/mxgraph/examples/swing/images/pagesetup.gif"));
80 		menu.add(editor.bind(mxResources.get("print"), new PrintAction(),
81 				"/com/mxgraph/examples/swing/images/print.gif"));
82 
83 		menu.addSeparator();
84 
85 		menu.add(editor.bind(mxResources.get("exit"), new ExitAction()));
86 
87 		// Creates the edit menu
88 		menu = add(new JMenu(mxResources.get("edit")));
89 
90 		menu.add(editor.bind(mxResources.get("undo"), new HistoryAction(true),
91 				"/com/mxgraph/examples/swing/images/undo.gif"));
92 		menu.add(editor.bind(mxResources.get("redo"), new HistoryAction(false),
93 				"/com/mxgraph/examples/swing/images/redo.gif"));
94 
95 		menu.addSeparator();
96 
97 		menu.add(editor.bind(mxResources.get("cut"), TransferHandler
98 				.getCutAction(), "/com/mxgraph/examples/swing/images/cut.gif"));
99 		menu.add(editor
100 				.bind(mxResources.get("copy"), TransferHandler.getCopyAction(),
101 						"/com/mxgraph/examples/swing/images/copy.gif"));
102 		menu.add(editor.bind(mxResources.get("paste"), TransferHandler
103 				.getPasteAction(),
104 				"/com/mxgraph/examples/swing/images/paste.gif"));
105 
106 		menu.addSeparator();
107 
108 		menu.add(editor.bind(mxResources.get("delete"), mxGraphActions
109 				.getDeleteAction(),
110 				"/com/mxgraph/examples/swing/images/delete.gif"));
111 
112 		menu.addSeparator();
113 
114 		menu.add(editor.bind(mxResources.get("selectAll"), mxGraphActions
115 				.getSelectAllAction()));
116 		menu.add(editor.bind(mxResources.get("selectNone"), mxGraphActions
117 				.getSelectNoneAction()));
118 
119 		menu.addSeparator();
120 
121 		menu.add(editor.bind(mxResources.get("warning"), new WarningAction()));
122 		menu.add(editor.bind(mxResources.get("edit"), mxGraphActions
123 				.getEditAction()));
124 
125 		// Creates the view menu
126 		menu = add(new JMenu(mxResources.get("view")));
127 
128 		JMenuItem item = menu.add(new TogglePropertyItem(graphComponent,
129 				mxResources.get("pageLayout"), "PageVisible", true,
130 				new ActionListener()
131 				{
132 					/**
133 					 *
134 					 */
135 					public void actionPerformed(ActionEvent e)
136 					{
137 						if (graphComponent.isPageVisible()
138 								&& graphComponent.isCenterPage())
139 						{
140 							graphComponent.zoomAndCenter();
141 						}
142 					}
143 				}));
144 
145 		item.addActionListener(new ActionListener()
146 		{
147 			/*
148 			 * (non-Javadoc)
149 			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
150 			 */
151 			public void actionPerformed(ActionEvent e)
152 			{
153 				if (e.getSource() instanceof TogglePropertyItem)
154 				{
155 					final mxGraphComponent graphComponent = editor
156 							.getGraphComponent();
157 					TogglePropertyItem toggleItem = (TogglePropertyItem) e
158 							.getSource();
159 
160 					if (toggleItem.isSelected())
161 					{
162 						// Scrolls the view to the center
163 						SwingUtilities.invokeLater(new Runnable()
164 						{
165 							/*
166 							 * (non-Javadoc)
167 							 * @see java.lang.Runnable#run()
168 							 */
169 							public void run()
170 							{
171 								graphComponent.scrollToCenter(true);
172 								graphComponent.scrollToCenter(false);
173 							}
174 						});
175 					}
176 					else
177 					{
178 						// Resets the translation of the view
179 						mxPoint tr = graphComponent.getGraph().getView()
180 								.getTranslate();
181 
182 						if (tr.getX() != 0 || tr.getY() != 0)
183 						{
184 							graphComponent.getGraph().getView().setTranslate(
185 									new mxPoint());
186 						}
187 					}
188 				}
189 			}
190 		});
191 
192 		menu.add(new TogglePropertyItem(graphComponent, mxResources
193 				.get("antialias"), "AntiAlias", true));
194 
195 		menu.addSeparator();
196 
197 		menu.add(new ToggleGridItem(editor, mxResources.get("grid")));
198 		menu.add(new ToggleRulersItem(editor, mxResources.get("rulers")));
199 
200 		menu.addSeparator();
201 
202 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("zoom")));
203 
204 		submenu.add(editor.bind("400%", new ScaleAction(4)));
205 		submenu.add(editor.bind("200%", new ScaleAction(2)));
206 		submenu.add(editor.bind("150%", new ScaleAction(1.5)));
207 		submenu.add(editor.bind("100%", new ScaleAction(1)));
208 		submenu.add(editor.bind("75%", new ScaleAction(0.75)));
209 		submenu.add(editor.bind("50%", new ScaleAction(0.5)));
210 
211 		submenu.addSeparator();
212 
213 		submenu.add(editor.bind(mxResources.get("custom"), new ScaleAction(0)));
214 
215 		menu.addSeparator();
216 
217 		menu.add(editor.bind(mxResources.get("zoomIn"), mxGraphActions
218 				.getZoomInAction()));
219 		menu.add(editor.bind(mxResources.get("zoomOut"), mxGraphActions
220 				.getZoomOutAction()));
221 
222 		menu.addSeparator();
223 
224 		menu.add(editor.bind(mxResources.get("page"), new ZoomPolicyAction(
225 				mxGraphComponent.ZOOM_POLICY_PAGE)));
226 		menu.add(editor.bind(mxResources.get("width"), new ZoomPolicyAction(
227 				mxGraphComponent.ZOOM_POLICY_WIDTH)));
228 
229 		menu.addSeparator();
230 
231 		menu.add(editor.bind(mxResources.get("actualSize"), mxGraphActions
232 				.getZoomActualAction()));
233 
234 		// Creates the diagram menu
235 		menu = add(new JMenu(mxResources.get("diagram")));
236 
237 		menu.add(new ToggleOutlineItem(editor, mxResources.get("outline")));
238 
239 		menu.addSeparator();
240 
241 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("background")));
242 
243 		submenu.add(editor.bind(mxResources.get("backgroundColor"),
244 				new BackgroundAction()));
245 		submenu.add(editor.bind(mxResources.get("backgroundImage"),
246 				new BackgroundImageAction()));
247 
248 		submenu.addSeparator();
249 
250 		submenu.add(editor.bind(mxResources.get("pageBackground"),
251 				new PageBackgroundAction()));
252 
253 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("grid")));
254 
255 		submenu.add(editor.bind(mxResources.get("gridSize"),
256 				new PromptPropertyAction(graph, "Grid Size", "GridSize")));
257 		submenu.add(editor.bind(mxResources.get("gridColor"),
258 				new GridColorAction()));
259 
260 		submenu.addSeparator();
261 
262 		submenu.add(editor.bind(mxResources.get("dashed"), new GridStyleAction(
263 				mxGraphComponent.GRID_STYLE_DASHED)));
264 		submenu.add(editor.bind(mxResources.get("dot"), new GridStyleAction(
265 				mxGraphComponent.GRID_STYLE_DOT)));
266 		submenu.add(editor.bind(mxResources.get("line"), new GridStyleAction(
267 				mxGraphComponent.GRID_STYLE_LINE)));
268 		submenu.add(editor.bind(mxResources.get("cross"), new GridStyleAction(
269 				mxGraphComponent.GRID_STYLE_CROSS)));
270 
271 		menu.addSeparator();
272 
273 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("layout")));
274 
275 		submenu.add(editor.graphLayout("verticalHierarchical", true));
276 		submenu.add(editor.graphLayout("horizontalHierarchical", true));
277 
278 		submenu.addSeparator();
279 
280 		submenu.add(editor.graphLayout("verticalPartition", false));
281 		submenu.add(editor.graphLayout("horizontalPartition", false));
282 
283 		submenu.addSeparator();
284 
285 		submenu.add(editor.graphLayout("verticalStack", false));
286 		submenu.add(editor.graphLayout("horizontalStack", false));
287 
288 		submenu.addSeparator();
289 
290 		submenu.add(editor.graphLayout("verticalTree", true));
291 		submenu.add(editor.graphLayout("horizontalTree", true));
292 
293 		submenu.addSeparator();
294 
295 		submenu.add(editor.graphLayout("parallelEdges", false));
296 
297 		submenu.addSeparator();
298 
299 		submenu.add(editor.graphLayout("organicLayout", true));
300 
301 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("selection")));
302 
303 		submenu.add(editor.bind(mxResources.get("selectPath"),
304 				new SelectShortestPathAction(false)));
305 		submenu.add(editor.bind(mxResources.get("selectDirectedPath"),
306 				new SelectShortestPathAction(true)));
307 
308 		submenu.addSeparator();
309 
310 		submenu.add(editor.bind(mxResources.get("selectTree"),
311 				new SelectSpanningTreeAction(false)));
312 		submenu.add(editor.bind(mxResources.get("selectDirectedTree"),
313 				new SelectSpanningTreeAction(true)));
314 
315 		menu.addSeparator();
316 
317 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("stylesheet")));
318 
319 		submenu
320 				.add(editor
321 						.bind(
322 								mxResources.get("basicStyle"),
323 								new StylesheetAction(
324 										"/com/mxgraph/examples/swing/resources/basic-style.xml")));
325 		submenu
326 				.add(editor
327 						.bind(
328 								mxResources.get("defaultStyle"),
329 								new StylesheetAction(
330 										"/com/mxgraph/examples/swing/resources/default-style.xml")));
331 
332 		// Creates the options menu
333 		menu = add(new JMenu(mxResources.get("options")));
334 
335 		submenu = (JMenu) menu.add(new JMenu(mxResources.get("display")));
336 		submenu.add(new TogglePropertyItem(graphComponent, mxResources
337 				.get("buffering"), "TripleBuffered", true));
338 		submenu.add(editor.bind(mxResources.get("dirty"),
339 				new ToggleDirtyAction()));
340 
341 		submenu.addSeparator();
342 
343 		item = submenu.add(new TogglePropertyItem(graphComponent, mxResources
344 				.get("centerPage"), "CenterPage", true, new ActionListener()
345 		{
346 			/**
347 			 *
348 			 */
349 			public void actionPerformed(ActionEvent e)
350 			{
351 				if (graphComponent.isPageVisible()
352 						&& graphComponent.isCenterPage())
353 				{
354 					graphComponent.zoomAndCenter();
355 				}
356 			}
357 		}));
358 
359 		submenu.add(new TogglePropertyItem(graphComponent, mxResources
360 				.get("centerZoom"), "CenterZoom", true));
361 		submenu.add(new TogglePropertyItem(graphComponent, mxResources
362 				.get("zoomToSelection"), "KeepSelectionVisibleOnZoom", true));
363 
364 		submenu.addSeparator();
365 
366 		submenu.add(new TogglePropertyItem(graphComponent, mxResources
367 				.get("preferPagesize"), "PreferPageSize", true));
368 
369 		// This feature is not yet implemented
370 		//submenu.add(new TogglePropertyItem(graphComponent, mxResources
371 		//		.get("pageBreaks"), "PageBreaksVisible", true));
372 
373 		submenu.addSeparator();
374 
375 		submenu.add(editor.bind(mxResources.get("tolerance"),
376 				new PromptPropertyAction(graph, "Tolerance")));
377 
378 		// Creates the window menu
379 		menu = add(new JMenu(mxResources.get("window")));
380 
381 		UIManager.LookAndFeelInfo[] lafs = UIManager.getInstalledLookAndFeels();
382 
383 		for (int i = 0; i < lafs.length; i++)
384 		{
385 			final String clazz = lafs[i].getClassName();
386 			menu.add(new AbstractAction(lafs[i].getName())
387 			{
388 				public void actionPerformed(ActionEvent e)
389 				{
390 					editor.setLookAndFeel(clazz);
391 				}
392 			});
393 		}
394 
395 		// Creates the help menu
396 		menu = add(new JMenu(mxResources.get("help")));
397 
398 		item = menu.add(new JMenuItem(mxResources.get("aboutGraphEditor")));
399 		item.addActionListener(new ActionListener()
400 		{
401 			/*
402 			 * (non-Javadoc)
403 			 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
404 			 */
405 			public void actionPerformed(ActionEvent e)
406 			{
407 				editor.about();
408 			}
409 		});
410 	}
411 
412 }
413