1 package com.mxgraph.examples.swing.editor;
2 
3 import java.awt.Dimension;
4 import java.awt.event.ActionEvent;
5 import java.awt.event.ActionListener;
6 
7 import javax.swing.BorderFactory;
8 import javax.swing.JComboBox;
9 import javax.swing.JOptionPane;
10 import javax.swing.JToolBar;
11 import javax.swing.TransferHandler;
12 
13 import com.mxgraph.examples.swing.editor.EditorActions.HistoryAction;
14 import com.mxgraph.examples.swing.editor.EditorActions.NewAction;
15 import com.mxgraph.examples.swing.editor.EditorActions.OpenAction;
16 import com.mxgraph.examples.swing.editor.EditorActions.PrintAction;
17 import com.mxgraph.examples.swing.editor.EditorActions.SaveAction;
18 import com.mxgraph.swing.mxGraphComponent;
19 import com.mxgraph.swing.util.mxGraphActions;
20 import com.mxgraph.util.mxEvent;
21 import com.mxgraph.util.mxEventObject;
22 import com.mxgraph.util.mxResources;
23 import com.mxgraph.util.mxEventSource.mxIEventListener;
24 import com.mxgraph.view.mxGraphView;
25 
26 public class SchemaEditorToolBar extends JToolBar
27 {
28 
29 	/**
30 	 *
31 	 */
32 	private static final long serialVersionUID = -3979320704834605323L;
33 
34 	/**
35 	 *
36 	 * @param frame
37 	 * @param orientation
38 	 */
39 	private boolean ignoreZoomChange = false;
40 
41 	/**
42 	 *
43 	 */
SchemaEditorToolBar(final BasicGraphEditor editor, int orientation)44 	public SchemaEditorToolBar(final BasicGraphEditor editor, int orientation)
45 	{
46 		super(orientation);
47 		setBorder(BorderFactory.createCompoundBorder(BorderFactory
48 				.createEmptyBorder(3, 3, 3, 3), getBorder()));
49 		setFloatable(false);
50 
51 		add(editor.bind("New", new NewAction(),
52 				"/com/mxgraph/examples/swing/images/new.gif"));
53 		add(editor.bind("Open", new OpenAction(),
54 				"/com/mxgraph/examples/swing/images/open.gif"));
55 		add(editor.bind("Save", new SaveAction(false),
56 				"/com/mxgraph/examples/swing/images/save.gif"));
57 
58 		addSeparator();
59 
60 		add(editor.bind("Print", new PrintAction(),
61 				"/com/mxgraph/examples/swing/images/print.gif"));
62 
63 		addSeparator();
64 
65 		add(editor.bind("Cut", TransferHandler.getCutAction(),
66 				"/com/mxgraph/examples/swing/images/cut.gif"));
67 		add(editor.bind("Copy", TransferHandler.getCopyAction(),
68 				"/com/mxgraph/examples/swing/images/copy.gif"));
69 		add(editor.bind("Paste", TransferHandler.getPasteAction(),
70 				"/com/mxgraph/examples/swing/images/paste.gif"));
71 
72 		addSeparator();
73 
74 		add(editor.bind("Delete", mxGraphActions.getDeleteAction(),
75 				"/com/mxgraph/examples/swing/images/delete.gif"));
76 
77 		addSeparator();
78 
79 		add(editor.bind("Undo", new HistoryAction(true),
80 				"/com/mxgraph/examples/swing/images/undo.gif"));
81 		add(editor.bind("Redo", new HistoryAction(false),
82 				"/com/mxgraph/examples/swing/images/redo.gif"));
83 
84 		addSeparator();
85 
86 		final mxGraphView view = editor.getGraphComponent().getGraph()
87 				.getView();
88 		final JComboBox zoomCombo = new JComboBox(new Object[] { "400%",
89 				"200%", "150%", "100%", "75%", "50%", mxResources.get("page"),
90 				mxResources.get("width"), mxResources.get("actualSize") });
91 		zoomCombo.setEditable(true);
92 		zoomCombo.setMinimumSize(new Dimension(75, 0));
93 		zoomCombo.setPreferredSize(new Dimension(75, 0));
94 		zoomCombo.setMaximumSize(new Dimension(75, 100));
95 		zoomCombo.setMaximumRowCount(9);
96 		add(zoomCombo);
97 
98 		// Sets the zoom in the zoom combo the current value
99 		mxIEventListener scaleTracker = new mxIEventListener()
100 		{
101 			/**
102 			 *
103 			 */
104 			public void invoke(Object sender, mxEventObject evt)
105 			{
106 				ignoreZoomChange = true;
107 
108 				try
109 				{
110 					zoomCombo.setSelectedItem((int) Math.round(100 * view
111 							.getScale())
112 							+ "%");
113 				}
114 				finally
115 				{
116 					ignoreZoomChange = false;
117 				}
118 			}
119 		};
120 
121 		// Installs the scale tracker to update the value in the combo box
122 		// if the zoom is changed from outside the combo box
123 		view.getGraph().getView().addListener(mxEvent.SCALE, scaleTracker);
124 		view.getGraph().getView().addListener(mxEvent.SCALE_AND_TRANSLATE,
125 				scaleTracker);
126 
127 		// Invokes once to sync with the actual zoom value
128 		scaleTracker.invoke(null, null);
129 
130 		zoomCombo.addActionListener(new ActionListener()
131 		{
132 			/**
133 			 *
134 			 */
135 			public void actionPerformed(ActionEvent e)
136 			{
137 				mxGraphComponent graphComponent = editor.getGraphComponent();
138 
139 				// Zoomcombo is changed when the scale is changed in the diagram
140 				// but the change is ignored here
141 				if (!ignoreZoomChange)
142 				{
143 					String zoom = zoomCombo.getSelectedItem().toString();
144 
145 					if (zoom.equals(mxResources.get("page")))
146 					{
147 						graphComponent.setPageVisible(true);
148 						graphComponent
149 								.setZoomPolicy(mxGraphComponent.ZOOM_POLICY_PAGE);
150 					}
151 					else if (zoom.equals(mxResources.get("width")))
152 					{
153 						graphComponent.setPageVisible(true);
154 						graphComponent
155 								.setZoomPolicy(mxGraphComponent.ZOOM_POLICY_WIDTH);
156 					}
157 					else if (zoom.equals(mxResources.get("actualSize")))
158 					{
159 						graphComponent.zoomActual();
160 					}
161 					else
162 					{
163 						try
164 						{
165 							zoom = zoom.replace("%", "");
166 							double scale = Math.min(16, Math.max(0.01,
167 									Double.parseDouble(zoom) / 100));
168 							graphComponent.zoomTo(scale, graphComponent
169 									.isCenterZoom());
170 						}
171 						catch (Exception ex)
172 						{
173 							JOptionPane.showMessageDialog(editor, ex
174 									.getMessage());
175 						}
176 					}
177 				}
178 			}
179 		});
180 	}
181 }
182