1 package com.mxgraph.examples.swing.editor; 2 3 import java.awt.Dimension; 4 import java.awt.GraphicsEnvironment; 5 import java.awt.event.ActionEvent; 6 import java.awt.event.ActionListener; 7 import java.util.ArrayList; 8 import java.util.Arrays; 9 import java.util.List; 10 11 import javax.swing.BorderFactory; 12 import javax.swing.JComboBox; 13 import javax.swing.JOptionPane; 14 import javax.swing.JToolBar; 15 import javax.swing.TransferHandler; 16 17 import com.mxgraph.examples.swing.editor.EditorActions.ColorAction; 18 import com.mxgraph.examples.swing.editor.EditorActions.FontStyleAction; 19 import com.mxgraph.examples.swing.editor.EditorActions.HistoryAction; 20 import com.mxgraph.examples.swing.editor.EditorActions.KeyValueAction; 21 import com.mxgraph.examples.swing.editor.EditorActions.NewAction; 22 import com.mxgraph.examples.swing.editor.EditorActions.OpenAction; 23 import com.mxgraph.examples.swing.editor.EditorActions.PrintAction; 24 import com.mxgraph.examples.swing.editor.EditorActions.SaveAction; 25 import com.mxgraph.swing.mxGraphComponent; 26 import com.mxgraph.swing.util.mxGraphActions; 27 import com.mxgraph.util.mxConstants; 28 import com.mxgraph.util.mxEvent; 29 import com.mxgraph.util.mxEventObject; 30 import com.mxgraph.util.mxResources; 31 import com.mxgraph.util.mxEventSource.mxIEventListener; 32 import com.mxgraph.view.mxGraph; 33 import com.mxgraph.view.mxGraphView; 34 35 public class EditorToolBar extends JToolBar 36 { 37 38 /** 39 * 40 */ 41 private static final long serialVersionUID = -8015443128436394471L; 42 43 /** 44 * 45 * @param frame 46 * @param orientation 47 */ 48 private boolean ignoreZoomChange = false; 49 50 /** 51 * 52 */ EditorToolBar(final BasicGraphEditor editor, int orientation)53 public EditorToolBar(final BasicGraphEditor editor, int orientation) 54 { 55 super(orientation); 56 setBorder(BorderFactory.createCompoundBorder(BorderFactory 57 .createEmptyBorder(3, 3, 3, 3), getBorder())); 58 setFloatable(false); 59 60 add(editor.bind("New", new NewAction(), 61 "/com/mxgraph/examples/swing/images/new.gif")); 62 add(editor.bind("Open", new OpenAction(), 63 "/com/mxgraph/examples/swing/images/open.gif")); 64 add(editor.bind("Save", new SaveAction(false), 65 "/com/mxgraph/examples/swing/images/save.gif")); 66 67 addSeparator(); 68 69 add(editor.bind("Print", new PrintAction(), 70 "/com/mxgraph/examples/swing/images/print.gif")); 71 72 addSeparator(); 73 74 add(editor.bind("Cut", TransferHandler.getCutAction(), 75 "/com/mxgraph/examples/swing/images/cut.gif")); 76 add(editor.bind("Copy", TransferHandler.getCopyAction(), 77 "/com/mxgraph/examples/swing/images/copy.gif")); 78 add(editor.bind("Paste", TransferHandler.getPasteAction(), 79 "/com/mxgraph/examples/swing/images/paste.gif")); 80 81 addSeparator(); 82 83 add(editor.bind("Delete", mxGraphActions.getDeleteAction(), 84 "/com/mxgraph/examples/swing/images/delete.gif")); 85 86 addSeparator(); 87 88 add(editor.bind("Undo", new HistoryAction(true), 89 "/com/mxgraph/examples/swing/images/undo.gif")); 90 add(editor.bind("Redo", new HistoryAction(false), 91 "/com/mxgraph/examples/swing/images/redo.gif")); 92 93 addSeparator(); 94 95 // Gets the list of available fonts from the local graphics environment 96 // and adds some frequently used fonts at the beginning of the list 97 GraphicsEnvironment env = GraphicsEnvironment 98 .getLocalGraphicsEnvironment(); 99 List<String> fonts = new ArrayList<String>(); 100 fonts.addAll(Arrays.asList(new String[] { "Helvetica", "Verdana", 101 "Times New Roman", "Garamond", "Courier New", "-" })); 102 fonts.addAll(Arrays.asList(env.getAvailableFontFamilyNames())); 103 104 final JComboBox fontCombo = new JComboBox(fonts.toArray()); 105 fontCombo.setEditable(true); 106 fontCombo.setMinimumSize(new Dimension(120, 0)); 107 fontCombo.setPreferredSize(new Dimension(120, 0)); 108 fontCombo.setMaximumSize(new Dimension(120, 100)); 109 add(fontCombo); 110 111 fontCombo.addActionListener(new ActionListener() 112 { 113 /** 114 * 115 */ 116 public void actionPerformed(ActionEvent e) 117 { 118 String font = fontCombo.getSelectedItem().toString(); 119 120 if (font != null && !font.equals("-")) 121 { 122 mxGraph graph = editor.getGraphComponent().getGraph(); 123 graph.setCellStyles(mxConstants.STYLE_FONTFAMILY, font); 124 } 125 } 126 }); 127 128 final JComboBox sizeCombo = new JComboBox(new Object[] { "6pt", "8pt", 129 "9pt", "10pt", "12pt", "14pt", "18pt", "24pt", "30pt", "36pt", 130 "48pt", "60pt" }); 131 sizeCombo.setEditable(true); 132 sizeCombo.setMinimumSize(new Dimension(65, 0)); 133 sizeCombo.setPreferredSize(new Dimension(65, 0)); 134 sizeCombo.setMaximumSize(new Dimension(65, 100)); 135 add(sizeCombo); 136 137 sizeCombo.addActionListener(new ActionListener() 138 { 139 /** 140 * 141 */ 142 public void actionPerformed(ActionEvent e) 143 { 144 mxGraph graph = editor.getGraphComponent().getGraph(); 145 graph.setCellStyles(mxConstants.STYLE_FONTSIZE, sizeCombo 146 .getSelectedItem().toString().replace("pt", "")); 147 } 148 }); 149 150 addSeparator(); 151 152 add(editor.bind("Bold", new FontStyleAction(true), 153 "/com/mxgraph/examples/swing/images/bold.gif")); 154 add(editor.bind("Italic", new FontStyleAction(false), 155 "/com/mxgraph/examples/swing/images/italic.gif")); 156 157 addSeparator(); 158 159 add(editor.bind("Left", new KeyValueAction(mxConstants.STYLE_ALIGN, 160 mxConstants.ALIGN_LEFT), 161 "/com/mxgraph/examples/swing/images/left.gif")); 162 add(editor.bind("Center", new KeyValueAction(mxConstants.STYLE_ALIGN, 163 mxConstants.ALIGN_CENTER), 164 "/com/mxgraph/examples/swing/images/center.gif")); 165 add(editor.bind("Right", new KeyValueAction(mxConstants.STYLE_ALIGN, 166 mxConstants.ALIGN_RIGHT), 167 "/com/mxgraph/examples/swing/images/right.gif")); 168 169 addSeparator(); 170 171 add(editor.bind("Font", new ColorAction("Font", 172 mxConstants.STYLE_FONTCOLOR), 173 "/com/mxgraph/examples/swing/images/fontcolor.gif")); 174 add(editor.bind("Stroke", new ColorAction("Stroke", 175 mxConstants.STYLE_STROKECOLOR), 176 "/com/mxgraph/examples/swing/images/linecolor.gif")); 177 add(editor.bind("Fill", new ColorAction("Fill", 178 mxConstants.STYLE_FILLCOLOR), 179 "/com/mxgraph/examples/swing/images/fillcolor.gif")); 180 181 addSeparator(); 182 183 final mxGraphView view = editor.getGraphComponent().getGraph() 184 .getView(); 185 final JComboBox zoomCombo = new JComboBox(new Object[] { "400%", 186 "200%", "150%", "100%", "75%", "50%", mxResources.get("page"), 187 mxResources.get("width"), mxResources.get("actualSize") }); 188 zoomCombo.setEditable(true); 189 zoomCombo.setMinimumSize(new Dimension(75, 0)); 190 zoomCombo.setPreferredSize(new Dimension(75, 0)); 191 zoomCombo.setMaximumSize(new Dimension(75, 100)); 192 zoomCombo.setMaximumRowCount(9); 193 add(zoomCombo); 194 195 // Sets the zoom in the zoom combo the current value 196 mxIEventListener scaleTracker = new mxIEventListener() 197 { 198 /** 199 * 200 */ 201 public void invoke(Object sender, mxEventObject evt) 202 { 203 ignoreZoomChange = true; 204 205 try 206 { 207 zoomCombo.setSelectedItem((int) Math.round(100 * view 208 .getScale()) 209 + "%"); 210 } 211 finally 212 { 213 ignoreZoomChange = false; 214 } 215 } 216 }; 217 218 // Installs the scale tracker to update the value in the combo box 219 // if the zoom is changed from outside the combo box 220 view.getGraph().getView().addListener(mxEvent.SCALE, scaleTracker); 221 view.getGraph().getView().addListener(mxEvent.SCALE_AND_TRANSLATE, 222 scaleTracker); 223 224 // Invokes once to sync with the actual zoom value 225 scaleTracker.invoke(null, null); 226 227 zoomCombo.addActionListener(new ActionListener() 228 { 229 /** 230 * 231 */ 232 public void actionPerformed(ActionEvent e) 233 { 234 mxGraphComponent graphComponent = editor.getGraphComponent(); 235 236 // Zoomcombo is changed when the scale is changed in the diagram 237 // but the change is ignored here 238 if (!ignoreZoomChange) 239 { 240 String zoom = zoomCombo.getSelectedItem().toString(); 241 242 if (zoom.equals(mxResources.get("page"))) 243 { 244 graphComponent.setPageVisible(true); 245 graphComponent 246 .setZoomPolicy(mxGraphComponent.ZOOM_POLICY_PAGE); 247 } 248 else if (zoom.equals(mxResources.get("width"))) 249 { 250 graphComponent.setPageVisible(true); 251 graphComponent 252 .setZoomPolicy(mxGraphComponent.ZOOM_POLICY_WIDTH); 253 } 254 else if (zoom.equals(mxResources.get("actualSize"))) 255 { 256 graphComponent.zoomActual(); 257 } 258 else 259 { 260 try 261 { 262 zoom = zoom.replace("%", ""); 263 double scale = Math.min(16, Math.max(0.01, 264 Double.parseDouble(zoom) / 100)); 265 graphComponent.zoomTo(scale, graphComponent 266 .isCenterZoom()); 267 } 268 catch (Exception ex) 269 { 270 JOptionPane.showMessageDialog(editor, ex 271 .getMessage()); 272 } 273 } 274 } 275 } 276 }); 277 } 278 } 279