001: /*
002: * $Id: JGraphpadViewAction.java,v 1.5 2005/08/07 10:28:29 gaudenz Exp $
003: * Copyright (c) 2001-2005, Gaudenz Alder
004: *
005: * All rights reserved.
006: *
007: * See LICENSE file for license details. If you are unable to locate
008: * this file please contact info (at) jgraph (dot) com.
009: */
010: package com.jgraph.pad.action;
011:
012: import java.awt.Color;
013: import java.awt.Component;
014: import java.awt.event.ActionEvent;
015: import java.io.File;
016: import java.io.FileNotFoundException;
017: import java.io.IOException;
018: import java.net.MalformedURLException;
019:
020: import org.jgraph.JGraph;
021: import org.jgraph.graph.GraphLayoutCache;
022: import org.jgraph.graph.PortView;
023:
024: import com.jgraph.JGraphEditor;
025: import com.jgraph.editor.JGraphEditorAction;
026: import com.jgraph.editor.factory.JGraphEditorDiagramPane;
027: import com.jgraph.pad.dialog.JGraphpadDialogs;
028: import com.jgraph.pad.factory.JGraphpadLibraryPane;
029: import com.jgraph.pad.graph.JGraphpadVertexRenderer;
030: import com.jgraph.pad.util.JGraphpadImageIcon;
031:
032: /**
033: * Implements all actions of the view menu. The layout item is added by a the
034: * layout plugin.
035: */
036: public class JGraphpadViewAction extends JGraphEditorAction {
037:
038: /**
039: * Shortcut to the shared JGraphpad dialogs.
040: */
041: private static JGraphpadDialogs dlgs = JGraphpadDialogs
042: .getSharedInstance();
043:
044: /**
045: * Specifies the name for the <code>togglerRulers</code> action.
046: */
047: public static final String NAME_TOGGLERULERS = "toggleRulers";
048:
049: /**
050: * Specifies the name for the <code>togglePorts</code> action.
051: */
052: public static final String NAME_TOGGLEPORTS = "togglePorts";
053:
054: /**
055: * Specifies the name for the <code>handleColor</code> action.
056: */
057: public static final String NAME_HANDLECOLOR = "handleColor";
058:
059: /**
060: * Specifies the name for the <code>handleSize</code> action.
061: */
062: public static final String NAME_LOCKEDHANDLECOLOR = "lockedHandleColor";
063:
064: /**
065: * Specifies the name for the <code>handleColor</code> action.
066: */
067: public static final String NAME_MARQUEECOLOR = "marqueeColor";
068:
069: /**
070: * Specifies the name for the <code>handleSize</code> action.
071: */
072: public static final String NAME_HANDLESIZE = "handleSize";
073:
074: /**
075: * Specifies the name for the <code>toggleGrid</code> action.
076: */
077: public static final String NAME_TOGGLEGRID = "toggleGrid";
078:
079: /**
080: * Specifies the name for the <code>gridColor</code> action.
081: */
082: public static final String NAME_GRIDCOLOR = "gridColor";
083:
084: /**
085: * Specifies the name for the <code>switchDotGrid</code> action.
086: */
087: public static final String NAME_SWITCHDOTGRID = "switchDotGrid";
088:
089: /**
090: * Specifies the name for the <code>switchDotGrid</code> action.
091: */
092: public static final String NAME_SWITCHLINEGRID = "switchLineGrid";
093:
094: /**
095: * Specifies the name for the <code>switchDotGrid</code> action.
096: */
097: public static final String NAME_SWITCHCROSSGRID = "switchCrossGrid";
098:
099: /**
100: * Specifies the name for the <code>toggleAntiAlias</code> action.
101: */
102: public static final String NAME_TOGGLEANTIALIAS = "toggleAntiAlias";
103:
104: /**
105: * Specifies the name for the <code>toggleRootHandles</code> action.
106: */
107: public static final String NAME_TOGGLEROOTHANDLES = "toggleRootHandles";
108:
109: /**
110: * Specifies the name for the <code>toggleDragEnabled</code> action.
111: */
112: public static final String NAME_TOGGLEDRAGENABLED = "toggleDragEnabled";
113:
114: /**
115: * Specifies the name for the <code>fitNone</code> action.
116: */
117: public static final String NAME_FITNONE = "fitNone";
118:
119: /**
120: * Specifies the name for the <code>fitPage</code> action.
121: */
122: public static final String NAME_FITPAGE = "fitPage";
123:
124: /**
125: * Specifies the name for the <code>fitWindow</code> action.
126: */
127: public static final String NAME_FITWINDOW = "fitWindow";
128:
129: /**
130: * Specifies the name for the <code>fitWidth</code> action.
131: */
132: public static final String NAME_FITWIDTH = "fitWidth";
133:
134: /**
135: * Specifies the name for the <code>togglePage</code> action.
136: */
137: public static final String NAME_TOGGLEPAGE = "togglePage";
138:
139: /**
140: * Specifies the name for the <code>background</code> action.
141: */
142: public static final String NAME_BACKGROUND = "background";
143:
144: /**
145: * Specifies the name for the <code>backgroundImage</code> action.
146: */
147: public static final String NAME_BACKGROUNDIMAGE = "backgroundImage";
148:
149: /**
150: * Specifies the name for the <code>backgroundImageURL</code> action.
151: */
152: public static final String NAME_BACKGROUNDIMAGEURL = "backgroundImageURL";
153:
154: /**
155: * Specifies the name for the <code>clearBackground</code> action.
156: */
157: public static final String NAME_CLEARBACKGROUND = "clearBackground";
158:
159: /**
160: * Specifies the name for the <code>zoomActual</code> action.
161: */
162: public static final String NAME_ZOOMACTUAL = "zoomActual";
163:
164: /**
165: * Specifies the name for the <code>zoomCustom</code> action.
166: */
167: public static final String NAME_ZOOMCUSTOM = "zoomCustom";
168:
169: /**
170: * Specifies the name for the <code>zoomIn</code> action.
171: */
172: public static final String NAME_ZOOMIN = "zoomIn";
173:
174: /**
175: * Specifies the name for the <code>zoomOut</code> action.
176: */
177: public static final String NAME_ZOOMOUT = "zoomOut";
178:
179: /**
180: * Specifies the name for the <code>tolerance</code> action.
181: */
182: public static final String NAME_TOLERANCE = "tolerance";
183:
184: /**
185: * Specifies the name for the <code>portSize</code> action.
186: */
187: public static final String NAME_PORTSIZE = "portSize";
188:
189: /**
190: * Specifies the name for the <code>gridSize</code> action.
191: */
192: public static final String NAME_GRIDSIZE = "gridSize";
193:
194: /**
195: * Specifies the name for the <code>toggleMetric</code> action.
196: */
197: public static final String NAME_TOGGLEMETRIC = "toggleMetric";
198:
199: /**
200: * Specifies the name for the <code>toggleEditable</code> action.
201: */
202: public static final String NAME_TOGGLEEDITABLE = "toggleEditable";
203:
204: /**
205: * Specifies the name for the <code>editClickCount</code> action.
206: */
207: public static final String NAME_EDITCLICKCOUNT = "editClickCount";
208:
209: /**
210: * Specifies the name for the <code>toggleDisconnectOnMove</code> action.
211: */
212: public static final String NAME_TOGGLEDISCONNECTONMOVE = "toggleDisconnectOnMove";
213:
214: /**
215: * Specifies the name for the <code>toggleMoveBelowZero</code> action.
216: */
217: public static final String NAME_TOGGLEMOVEBELOWZERO = "toggleMoveBelowZero";
218:
219: /**
220: * Specifies the name for the <code>toggleGraphCloneable</code> action.
221: */
222: public static final String NAME_TOGGLEGRAPHCLONEABLE = "toggleGraphCloneable";
223:
224: /**
225: * Specifies the name for the <code>toggleGraphMoveable</code> action.
226: */
227: public static final String NAME_TOGGLEGRAPHMOVEABLE = "toggleGraphMoveable";
228:
229: /**
230: * Specifies the name for the <code>toggleGraphSizeable</code> action.
231: */
232: public static final String NAME_TOGGLEGRAPHSIZEABLE = "toggleGraphSizeable";
233:
234: /**
235: * Specifies the name for the <code>toggleGraphBendable</code> action.
236: */
237: public static final String NAME_TOGGLEGRAPHBENDABLE = "toggleGraphBendable";
238:
239: /**
240: * Specifies the name for the <code>toggleGraphConnectable</code> action.
241: */
242: public static final String NAME_TOGGLEGRAPHCONNECTABLE = "toggleGraphConnectable";
243:
244: /**
245: * Specifies the name for the <code>toggleGraphDisconnectable</code>
246: * action.
247: */
248: public static final String NAME_TOGGLEGRAPHDISCONNECTABLE = "toggleGraphDisconnectable";
249:
250: /**
251: * Specifies the name for the <code>toggleSelectsLocalInsertedCells</code>
252: * action.
253: */
254: public static final String NAME_TOGGLESELECTSLOCALINSERTEDCELLS = "toggleSelectsLocalInsertedCells";
255:
256: /**
257: * Specifies the name for the <code>toggleSelectsAllInsertedCells</code>
258: * action.
259: */
260: public static final String NAME_TOGGLESELECTSALLINSERTEDCELLS = "toggleSelectsAllInsertedCells";
261:
262: /**
263: * Specifies the name for the <code>toggleShowsExistingConnections</code>
264: * action.
265: */
266: public static final String NAME_TOGGLESHOWSEXISTINGCONNECTIONS = "toggleShowsExistingConnections";
267:
268: /**
269: * Specifies the name for the <code>toggleShowsInsertedConnections</code>
270: * action.
271: */
272: public static final String NAME_TOGGLESHOWSINSERTEDCONNECTIONS = "toggleShowsInsertedConnections";
273:
274: /**
275: * Specifies the name for the <code>toggleShowsChangedConnections</code>
276: * action.
277: */
278: public static final String NAME_TOGGLESHOWSCHANGEDCONNECTIONS = "toggleShowsChangedConnections";
279:
280: /**
281: * Specifies the name for the <code>toggleHidesExistingConnections</code>
282: * action.
283: */
284: public static final String NAME_TOGGLEHIDESEXISTINGCONNECTIONS = "toggleHidesExistingConnections";
285:
286: /**
287: * Specifies the name for the <code>toggleHidesDanglingConnections</code>
288: * action.
289: */
290: public static final String NAME_TOGGLEHIDESDANGLINGCONNECTIONS = "toggleHidesDanglingConnections";
291:
292: /**
293: * Specifies the name for the <code>toggleRemembersCellViews</code>
294: * action.
295: */
296: public static final String NAME_TOGGLEREMEMBERSCELLVIEWS = "toggleRemembersCellViews";
297:
298: /**
299: * Specifies the name for the <code>toggleDoubleBuffered</code> action.
300: */
301: public static final String NAME_TOGGLEDOUBLEBUFFERED = "toggleDoubleBuffered";
302:
303: /**
304: * Specifies the name for the <code>togglePortsScaled</code> action.
305: */
306: public static final String NAME_TOGGLEPORTSSCALED = "togglePortsScaled";
307:
308: /**
309: * Specifies the name for the <code>toggleJumpsToDefaultPort</code>
310: * action.
311: */
312: public static final String NAME_TOGGLEJUMPSTODEFAULTPORT = "toggleJumpsToDefaultPort";
313:
314: /**
315: * Specifies the name for the <code>toggleMovesIntoGroups</code> action.
316: */
317: public static final String NAME_TOGGLEMOVESINTOGROUPS = "toggleMovesIntoGroups";
318:
319: /**
320: * Specifies the name for the <code>toggleMovesOutOfGroups</code> action.
321: */
322: public static final String NAME_TOGGLEMOVESOUTOFGROUPS = "toggleMovesOutOfGroups";
323:
324: /**
325: * Specifies the name for the <code>libraryLarger</code> action.
326: */
327: public static final String NAME_LIBRARYLARGER = "libraryLarger";
328:
329: /**
330: * Specifies the name for the <code>librarySmaller</code> action.
331: */
332: public static final String NAME_LIBRARYSMALLER = "librarySmaller";
333:
334: /**
335: * Holds the last directory for file operations.
336: */
337: protected File lastDirectory = null;
338:
339: /**
340: * Constructs a new view action for the specified name. If the action name
341: * starts with <code>toggle</code>, <code>fit</code> or
342: * <code>switch</code> then the action is configured to be a toggle
343: * action.
344: *
345: * @param name
346: * The name of the action to be created.
347: */
348: public JGraphpadViewAction(String name) {
349: super (name);
350: setToggleAction(name.startsWith("toggle")
351: || name.startsWith("fit") || name.startsWith("switch"));
352: }
353:
354: /**
355: * Executes the action based on the action name.
356: *
357: * @param event
358: * The object that describes the event.
359: */
360: public void actionPerformed(ActionEvent event) {
361: try {
362:
363: // Actions that require a focused graph
364: JGraph graph = getPermanentFocusOwnerGraph();
365: if (graph != null) {
366: GraphLayoutCache cache = graph.getGraphLayoutCache();
367: if (getName().equals(NAME_TOGGLEDRAGENABLED))
368: graph.setDragEnabled(!graph.isDragEnabled());
369: else if (getName().equals(NAME_TOGGLEPORTS))
370: graph.setPortsVisible(!graph.isPortsVisible());
371: else if (getName().equals(NAME_TOGGLEANTIALIAS))
372: graph.setAntiAliased(!graph.isAntiAliased());
373: else if (getName().equals(NAME_TOGGLEROOTHANDLES)) {
374:
375: // ToggleRootHandles is implemented as a client property
376: // stored in JGraph and used in JGraphpadVertexRenderer.
377: String key = JGraphpadVertexRenderer.CLIENTPROPERTY_SHOWFOLDINGICONS;
378: Boolean b = (Boolean) graph.getClientProperty(key);
379: if (b == null)
380: b = new Boolean(true);
381: graph.putClientProperty(key, new Boolean(!b
382: .booleanValue()));
383: graph.repaint();
384: } else if (getName().equals(NAME_TOGGLEEDITABLE))
385: graph.setEditable(!graph.isEditable());
386: else if (getName().equals(NAME_TOGGLEDOUBLEBUFFERED))
387: graph.setDoubleBuffered(!graph.isDoubleBuffered());
388: else if (getName().equals(NAME_TOGGLEDISCONNECTONMOVE))
389: graph.setDisconnectOnMove(!graph
390: .isDisconnectOnMove());
391: else if (getName().equals(NAME_TOGGLEMOVEBELOWZERO))
392: graph.setMoveBelowZero(!graph.isMoveBelowZero());
393: else if (getName().equals(NAME_TOGGLEGRAPHCLONEABLE))
394: graph.setCloneable(!graph.isCloneable());
395: else if (getName().equals(NAME_TOGGLEGRAPHMOVEABLE))
396: graph.setMoveable(!graph.isMoveable());
397: else if (getName().equals(NAME_TOGGLEGRAPHSIZEABLE))
398: graph.setSizeable(!graph.isSizeable());
399: else if (getName().equals(NAME_TOGGLEGRAPHBENDABLE))
400: graph.setBendable(!graph.isBendable());
401: else if (getName().equals(NAME_TOGGLEGRAPHCONNECTABLE))
402: graph.setConnectable(!graph.isConnectable());
403: else if (getName().equals(
404: NAME_TOGGLEGRAPHDISCONNECTABLE))
405: graph.setDisconnectable(!graph.isDisconnectable());
406: else if (getName().equals(
407: NAME_TOGGLESELECTSLOCALINSERTEDCELLS))
408: cache.setSelectsLocalInsertedCells(!cache
409: .isSelectsLocalInsertedCells());
410: else if (getName().equals(
411: NAME_TOGGLESELECTSALLINSERTEDCELLS))
412: cache.setSelectsAllInsertedCells(!cache
413: .isSelectsAllInsertedCells());
414: else if (getName().equals(
415: NAME_TOGGLESHOWSEXISTINGCONNECTIONS))
416: cache.setShowsExistingConnections(!cache
417: .isShowsExistingConnections());
418: else if (getName().equals(
419: NAME_TOGGLESHOWSINSERTEDCONNECTIONS))
420: cache.setShowsInsertedConnections(!cache
421: .isShowsInsertedConnections());
422: else if (getName().equals(
423: NAME_TOGGLESHOWSCHANGEDCONNECTIONS))
424: cache.setShowsChangedConnections(!cache
425: .isShowsChangedConnections());
426: else if (getName().equals(
427: NAME_TOGGLEHIDESEXISTINGCONNECTIONS))
428: cache.setHidesExistingConnections(!cache
429: .isHidesExistingConnections());
430: else if (getName().equals(
431: NAME_TOGGLEHIDESDANGLINGCONNECTIONS))
432: cache.setHidesDanglingConnections(!cache
433: .isHidesDanglingConnections());
434: else if (getName()
435: .equals(NAME_TOGGLEREMEMBERSCELLVIEWS))
436: cache.setRemembersCellViews(!cache
437: .isRemembersCellViews());
438: else if (getName()
439: .equals(NAME_TOGGLEJUMPSTODEFAULTPORT))
440: graph.setJumpToDefaultPort(!graph
441: .isJumpToDefaultPort());
442: else if (getName().equals(NAME_TOGGLEPORTSSCALED))
443: graph.setPortsScaled(!graph.isPortsScaled());
444: else if (getName().equals(NAME_TOGGLEMOVESINTOGROUPS))
445: graph.setMoveIntoGroups(!graph.isMoveIntoGroups());
446: else if (getName().equals(NAME_TOGGLEMOVESOUTOFGROUPS))
447: graph
448: .setMoveOutOfGroups(!graph
449: .isMoveOutOfGroups());
450: else if (getName().equals(NAME_TOGGLEGRID)) {
451: graph.setGridEnabled(!graph.isGridEnabled());
452: graph.setGridVisible(graph.isGridEnabled());
453: } else if (getName().equals(NAME_ZOOMACTUAL))
454: doZoom(graph, false, false, false);
455: else if (getName().equals(NAME_ZOOMIN))
456: doZoom(graph, true, false, false);
457: else if (getName().equals(NAME_ZOOMOUT))
458: doZoom(graph, false, true, false);
459: else if (getName().equals(NAME_ZOOMCUSTOM))
460: doZoom(graph, false, false, true);
461: else if (getName().equals(NAME_TOLERANCE))
462: graph.setTolerance(dlgs.intDialog(
463: getString("EnterTolerance"), graph
464: .getTolerance(), false, true));
465: else if (getName().equals(NAME_PORTSIZE)) {
466: PortView.SIZE = dlgs.intDialog(
467: getString("EnterPortSize"), PortView.SIZE,
468: false, true);
469: graph.repaint();
470: } else if (getName().equals(NAME_GRIDSIZE))
471: graph.setGridSize(dlgs.doubleDialog(
472: getString("EnterGridSize"), graph
473: .getGridSize(), false, false));
474: else if (getName().equals(NAME_GRIDCOLOR))
475: graph.setGridColor(dlgs.colorDialog(graph,
476: getString("SelectColor"), graph
477: .getGridColor()));
478: else if (getName().equals(NAME_HANDLECOLOR))
479: graph.setHandleColor(dlgs.colorDialog(graph,
480: getString("SelectColor"), graph
481: .getHandleColor()));
482: else if (getName().equals(NAME_HANDLESIZE))
483: graph.setHandleSize(dlgs.intDialog(
484: getString("EnterNumber"), graph
485: .getHandleSize(), false, false));
486: else if (getName().equals(NAME_LOCKEDHANDLECOLOR))
487: graph.setLockedHandleColor(dlgs.colorDialog(graph,
488: getString("SelectColor"), graph
489: .getLockedHandleColor()));
490: else if (getName().equals(NAME_MARQUEECOLOR))
491: graph.setMarqueeColor(dlgs.colorDialog(graph,
492: getString("SelectColor"), graph
493: .getMarqueeColor()));
494: else if (getName().equals(NAME_EDITCLICKCOUNT))
495: graph
496: .setEditClickCount(dlgs.intDialog(
497: getString("EnterClickCount"), graph
498: .getEditClickCount(),
499: false, false));
500: else if (getName().equals(NAME_SWITCHDOTGRID))
501: graph.setGridMode(JGraph.DOT_GRID_MODE);
502: else if (getName().equals(NAME_SWITCHLINEGRID))
503: graph.setGridMode(JGraph.LINE_GRID_MODE);
504: else if (getName().equals(NAME_SWITCHCROSSGRID))
505: graph.setGridMode(JGraph.CROSS_GRID_MODE);
506: }
507:
508: // Actions that require a focused diagram pane
509: JGraphEditorDiagramPane diagramPane = getPermanentFocusOwnerDiagramPane();
510: if (diagramPane != null) {
511: if (getName().equals(NAME_TOGGLERULERS))
512: diagramPane.setRulersVisible(!diagramPane
513: .isRulersVisible());
514: else if (getName().equals(NAME_TOGGLEPAGE))
515: diagramPane.setPageVisible(!diagramPane
516: .isPageVisible());
517: else if (getName().equals(NAME_TOGGLEMETRIC))
518: diagramPane.setMetric(!diagramPane.isMetric());
519: else if (getName().equals(NAME_BACKGROUND))
520: doBackground(diagramPane);
521: else if (getName().equals(NAME_BACKGROUNDIMAGE))
522: doBackgroundImage(diagramPane, dlgs
523: .imageFileDialog(diagramPane,
524: getString("SelectImage"), true,
525: lastDirectory));
526: else if (getName().equals(NAME_BACKGROUNDIMAGEURL))
527: doBackgroundImage(diagramPane, dlgs
528: .valueDialog(getString("EnterURL")));
529: else if (getName().equals(NAME_CLEARBACKGROUND)) {
530: diagramPane.setBackgroundImage(null);
531: diagramPane.repaint();
532: } else if (getName().equals(NAME_FITNONE))
533: diagramPane
534: .setAutoScalePolicy(JGraphEditorDiagramPane.AUTOSCALE_POLICY_NONE);
535: else if (getName().equals(NAME_FITPAGE))
536: diagramPane
537: .setAutoScalePolicy(JGraphEditorDiagramPane.AUTOSCALE_POLICY_PAGE);
538: else if (getName().equals(NAME_FITWIDTH))
539: diagramPane
540: .setAutoScalePolicy(JGraphEditorDiagramPane.AUTOSCALE_POLICY_PAGEWIDTH);
541: else if (getName().equals(NAME_FITWINDOW))
542: diagramPane
543: .setAutoScalePolicy(JGraphEditorDiagramPane.AUTOSCALE_POLICY_WINDOW);
544: }
545:
546: // Actions that require a focused library
547: JGraphpadLibraryPane libraryPane = JGraphpadFileAction
548: .getPermanentFocusOwnerLibraryPane();
549: if (libraryPane != null) {
550: if (getName().equals(NAME_LIBRARYLARGER))
551: doScaleLibraryEntries(libraryPane, true);
552: else if (getName().equals(NAME_LIBRARYSMALLER))
553: doScaleLibraryEntries(libraryPane, false);
554: }
555: } catch (JGraphpadDialogs.CancelException e) {
556: // ignore
557: } catch (Exception e) {
558: dlgs.errorDialog(getPermanentFocusOwner(), e.getMessage());
559: }
560: }
561:
562: /**
563: * Doubles or halves the current size of the entries in the specified
564: * library pane based on <code>larger</code>.
565: *
566: * @param pane
567: * The layout pane to change the entries of.
568: * @param larger
569: * Whether to make the entries larger.
570: */
571: protected void doScaleLibraryEntries(JGraphpadLibraryPane pane,
572: boolean larger) {
573: int w = pane.getEntrywidth();
574: int h = pane.getEntryheight();
575: if (larger && w < 800) {
576: w *= 2;
577: h *= 2;
578: } else if (!larger && w > 4) {
579: w /= 2;
580: h /= 2;
581: }
582: pane.setEntrywidth(w);
583: pane.setEntryheight(h);
584: pane.invalidate();
585: pane.repaint();
586: }
587:
588: /**
589: * Sets the zoom on the specified graph depending on the paramters, which
590: * are interpreted to be exlusive. If <code>custom</code> is true this
591: * method displays a dialog to enter the custom zoom and sets the zoom to
592: * the specified value. If custom is false then the method will double or
593: * half the current zoom of <code>graph</code> depending on the value of
594: * <code>in</code> and <code>out</code>. If all values are false then
595: * the zoom is reset to 1.<br>
596: * This implementation makes sure the zoom is between 0.01 and 300 and
597: * scrolls to the selection cell if the selection is not empty.
598: *
599: * @param graph
600: * The graph to perform the operation in.
601: * @param in
602: * Whether to zoom in if no custom dialog is used.
603: * @param out
604: * Whether to zoom out if no custom dialog is used.
605: * @param custom
606: * Whether to display a dialog to ask for a custom zoom.
607: */
608: protected void doZoom(JGraph graph, boolean in, boolean out,
609: boolean custom) {
610: double scale = 1;
611: if (custom) {
612: String value = dlgs.valueDialog(getString("EnterZoom"),
613: String.valueOf(graph.getScale() * 100));
614: if (value != null && value.length() > 0) {
615: if (value.endsWith("%"))
616: value = value.substring(0, value.length() - 1);
617: scale = Double.parseDouble(value) / 100;
618: } else
619: scale = -1;
620: } else if (in)
621: scale = graph.getScale() * 2;
622: else if (out)
623: scale = graph.getScale() / 2;
624: if (scale > 0.01 && scale < 300) {
625: graph.setScale(scale);
626: if (graph.getSelectionCell() != null)
627: graph.scrollCellToVisible(graph.getSelectionCell());
628: }
629: }
630:
631: /**
632: * Displays a color dialog and sets the background color on the specified
633: * diagram pane.
634: *
635: * @param diagramPane
636: * The diagramPane to perform the operation in.
637: */
638: protected void doBackground(JGraphEditorDiagramPane diagramPane) {
639: Color color = dlgs.colorDialog(diagramPane,
640: getString("SelectColor"), diagramPane.getGraph()
641: .getBackground());
642: if (color != null)
643: diagramPane.getGraph().setBackground(color);
644: }
645:
646: /**
647: * Displays a file- or URL-dialog and uses the filename to set the
648: * background image on the specified diagram pane.
649: *
650: * @param diagramPane
651: * The diagramPane to perform the operation in.
652: * @param filename
653: * The filename or URL to load the image from.
654: */
655: protected void doBackgroundImage(
656: JGraphEditorDiagramPane diagramPane, String filename)
657: throws MalformedURLException, FileNotFoundException,
658: IOException {
659: if (filename != null) {
660: JGraphpadImageIcon icon = (JGraphEditor.isURL(filename)) ? new JGraphpadImageIcon(
661: JGraphEditor.toURL(filename))
662: : new JGraphpadImageIcon(filename);
663: if (icon != null)
664: diagramPane.setBackgroundImage(icon);
665: diagramPane.repaint();
666: if (!JGraphEditor.isURL(filename))
667: lastDirectory = new File(filename).getParentFile();
668: }
669: }
670:
671: /**
672: * Bundle of all actions in this class.
673: */
674: public static class AllActions implements Bundle {
675:
676: /**
677: * Holds the actions.
678: */
679: public JGraphEditorAction actionToggleRulers = new JGraphpadViewAction(
680: NAME_TOGGLERULERS),
681: actionTogglePorts = new JGraphpadViewAction(
682: NAME_TOGGLEPORTS),
683: actionToggleGrid = new JGraphpadViewAction(
684: NAME_TOGGLEGRID),
685: actionToggleAntiAlias = new JGraphpadViewAction(
686: NAME_TOGGLEANTIALIAS),
687: actionToggleRootHandles = new JGraphpadViewAction(
688: NAME_TOGGLEROOTHANDLES),
689: actionToggleDragEnabled = new JGraphpadViewAction(
690: NAME_TOGGLEDRAGENABLED),
691: actionToggleDoubleBuffered = new JGraphpadViewAction(
692: NAME_TOGGLEDOUBLEBUFFERED),
693: actionTogglePage = new JGraphpadViewAction(
694: NAME_TOGGLEPAGE),
695: actionBackgroundImage = new JGraphpadViewAction(
696: NAME_BACKGROUNDIMAGE),
697: actionBackgroundImageURL = new JGraphpadViewAction(
698: NAME_BACKGROUNDIMAGEURL),
699: actionBackground = new JGraphpadViewAction(
700: NAME_BACKGROUND),
701: actionClearBackground = new JGraphpadViewAction(
702: NAME_CLEARBACKGROUND),
703: actionZoomActual = new JGraphpadViewAction(
704: NAME_ZOOMACTUAL),
705: actionZoomIn = new JGraphpadViewAction(NAME_ZOOMIN),
706: actionZoomOut = new JGraphpadViewAction(NAME_ZOOMOUT),
707: actionZoomCustom = new JGraphpadViewAction(
708: NAME_ZOOMCUSTOM),
709: actionFitNone = new JGraphpadViewAction(NAME_FITNONE),
710: actionFitWindow = new JGraphpadViewAction(
711: NAME_FITWINDOW),
712: actionFitWidth = new JGraphpadViewAction(NAME_FITWIDTH),
713: actionFitPage = new JGraphpadViewAction(NAME_FITPAGE),
714: actionTolerance = new JGraphpadViewAction(
715: NAME_TOLERANCE),
716: actionPortSize = new JGraphpadViewAction(NAME_PORTSIZE),
717: actionGridSize = new JGraphpadViewAction(NAME_GRIDSIZE),
718: actionGridColor = new JGraphpadViewAction(
719: NAME_GRIDCOLOR),
720: actionToggleMetric = new JGraphpadViewAction(
721: NAME_TOGGLEMETRIC),
722: actionToggleEditable = new JGraphpadViewAction(
723: NAME_TOGGLEEDITABLE),
724: actionEditClickCount = new JGraphpadViewAction(
725: NAME_EDITCLICKCOUNT),
726: actionToggleDisconnectOnMove = new JGraphpadViewAction(
727: NAME_TOGGLEDISCONNECTONMOVE),
728: actionToggleMoveBelowZero = new JGraphpadViewAction(
729: NAME_TOGGLEMOVEBELOWZERO),
730: actionToggleGraphCloneable = new JGraphpadViewAction(
731: NAME_TOGGLEGRAPHCLONEABLE),
732: actionToggleGraphMoveable = new JGraphpadViewAction(
733: NAME_TOGGLEGRAPHMOVEABLE),
734: actionToggleGraphSizeable = new JGraphpadViewAction(
735: NAME_TOGGLEGRAPHSIZEABLE),
736: actionToggleGraphBendable = new JGraphpadViewAction(
737: NAME_TOGGLEGRAPHBENDABLE),
738: actionToggleGraphConnectable = new JGraphpadViewAction(
739: NAME_TOGGLEGRAPHCONNECTABLE),
740: actionToggleGraphDisconnectable = new JGraphpadViewAction(
741: NAME_TOGGLEGRAPHDISCONNECTABLE),
742: actionToggleSelectsLocalInsertedCells = new JGraphpadViewAction(
743: NAME_TOGGLESELECTSLOCALINSERTEDCELLS),
744: actionToggleSelectsAllInsertedCells = new JGraphpadViewAction(
745: NAME_TOGGLESELECTSALLINSERTEDCELLS),
746: actionToggleShowsExistingConnections = new JGraphpadViewAction(
747: NAME_TOGGLESHOWSEXISTINGCONNECTIONS),
748: actionToggleShowsInsertedConnections = new JGraphpadViewAction(
749: NAME_TOGGLESHOWSINSERTEDCONNECTIONS),
750: actionToggleShowsChangedConnections = new JGraphpadViewAction(
751: NAME_TOGGLESHOWSCHANGEDCONNECTIONS),
752: actionToggleHidesExistingConnections = new JGraphpadViewAction(
753: NAME_TOGGLEHIDESEXISTINGCONNECTIONS),
754: actionToggleHidesDanglingConnections = new JGraphpadViewAction(
755: NAME_TOGGLEHIDESDANGLINGCONNECTIONS),
756: actionToggleRemembersCellViews = new JGraphpadViewAction(
757: NAME_TOGGLEREMEMBERSCELLVIEWS),
758: actionToggleJumpsToDefaultPort = new JGraphpadViewAction(
759: NAME_TOGGLEJUMPSTODEFAULTPORT),
760: actionTogglePortsScaled = new JGraphpadViewAction(
761: NAME_TOGGLEPORTSSCALED),
762: actionToggleMovesIntoGroups = new JGraphpadViewAction(
763: NAME_TOGGLEMOVESINTOGROUPS),
764: actionToggleMovesOutOfGroups = new JGraphpadViewAction(
765: NAME_TOGGLEMOVESOUTOFGROUPS),
766: actionSwitchDotGrid = new JGraphpadViewAction(
767: NAME_SWITCHDOTGRID),
768: actionSwitchLineGrid = new JGraphpadViewAction(
769: NAME_SWITCHLINEGRID),
770: actionSwitchCrossGrid = new JGraphpadViewAction(
771: NAME_SWITCHCROSSGRID),
772: actionHandleColor = new JGraphpadViewAction(
773: NAME_HANDLECOLOR),
774: actionLockedHandleColor = new JGraphpadViewAction(
775: NAME_LOCKEDHANDLECOLOR),
776: actionHandleSize = new JGraphpadViewAction(
777: NAME_HANDLESIZE),
778: actionMarqueeColor = new JGraphpadViewAction(
779: NAME_MARQUEECOLOR),
780: actionLibraryLarger = new JGraphpadViewAction(
781: NAME_LIBRARYLARGER),
782: actionLibrarySmaller = new JGraphpadViewAction(
783: NAME_LIBRARYSMALLER);
784:
785: /*
786: * (non-Javadoc)
787: */
788: public JGraphEditorAction[] getActions() {
789: return new JGraphEditorAction[] { actionToggleRulers,
790: actionTogglePorts, actionToggleGrid,
791: actionToggleAntiAlias, actionToggleRootHandles,
792: actionToggleDragEnabled,
793: actionToggleDoubleBuffered, actionTogglePage,
794: actionBackgroundImage, actionBackgroundImageURL,
795: actionBackground, actionClearBackground,
796: actionZoomActual, actionZoomIn, actionZoomOut,
797: actionZoomCustom, actionFitNone, actionFitWindow,
798: actionFitWidth, actionFitPage, actionTolerance,
799: actionPortSize, actionGridSize, actionGridColor,
800: actionToggleMetric, actionToggleEditable,
801: actionEditClickCount, actionToggleDisconnectOnMove,
802: actionToggleMoveBelowZero,
803: actionToggleGraphMoveable,
804: actionToggleGraphSizeable,
805: actionToggleGraphBendable,
806: actionToggleGraphConnectable,
807: actionToggleGraphDisconnectable,
808: actionToggleSelectsLocalInsertedCells,
809: actionToggleSelectsAllInsertedCells,
810: actionToggleShowsExistingConnections,
811: actionToggleShowsInsertedConnections,
812: actionToggleShowsChangedConnections,
813: actionToggleHidesExistingConnections,
814: actionToggleHidesDanglingConnections,
815: actionToggleRemembersCellViews,
816: actionToggleJumpsToDefaultPort,
817: actionTogglePortsScaled,
818: actionToggleMovesIntoGroups,
819: actionToggleMovesOutOfGroups, actionSwitchDotGrid,
820: actionSwitchLineGrid, actionSwitchCrossGrid,
821: actionHandleColor, actionLockedHandleColor,
822: actionHandleSize, actionMarqueeColor,
823: actionLibraryLarger, actionLibrarySmaller };
824: }
825:
826: /*
827: * (non-Javadoc)
828: */
829: public void update() {
830: Component component = getPermanentFocusOwner();
831: boolean isGraphFocused = component instanceof JGraph;
832: boolean isLibraryFocused = JGraphpadFileAction
833: .getPermanentFocusOwnerLibraryPane() != null;
834:
835: // Enabling actions
836: actionLibraryLarger.setEnabled(isLibraryFocused);
837: actionLibrarySmaller.setEnabled(isLibraryFocused);
838:
839: actionTogglePorts.setEnabled(isGraphFocused);
840: actionToggleGrid.setEnabled(isGraphFocused);
841: actionToggleAntiAlias.setEnabled(isGraphFocused);
842: actionToggleRootHandles.setEnabled(isGraphFocused);
843: actionToggleDragEnabled.setEnabled(isGraphFocused);
844: actionToggleDoubleBuffered.setEnabled(isGraphFocused);
845: actionBackground.setEnabled(isGraphFocused);
846: actionZoomActual.setEnabled(isGraphFocused);
847: actionZoomIn.setEnabled(isGraphFocused);
848: actionZoomOut.setEnabled(isGraphFocused);
849: actionZoomCustom.setEnabled(isGraphFocused);
850: actionTolerance.setEnabled(isGraphFocused);
851: actionPortSize.setEnabled(isGraphFocused);
852: actionGridSize.setEnabled(isGraphFocused);
853: actionGridColor.setEnabled(isGraphFocused);
854: actionToggleEditable.setEnabled(isGraphFocused);
855: actionEditClickCount.setEnabled(isGraphFocused);
856: actionToggleDisconnectOnMove.setEnabled(isGraphFocused);
857: actionToggleMoveBelowZero.setEnabled(isGraphFocused);
858: actionToggleGraphMoveable.setEnabled(isGraphFocused);
859: actionToggleGraphSizeable.setEnabled(isGraphFocused);
860: actionToggleGraphBendable.setEnabled(isGraphFocused);
861: actionToggleGraphConnectable.setEnabled(isGraphFocused);
862: actionToggleGraphDisconnectable.setEnabled(isGraphFocused);
863: actionToggleSelectsLocalInsertedCells
864: .setEnabled(isGraphFocused);
865: actionToggleSelectsAllInsertedCells
866: .setEnabled(isGraphFocused);
867: actionToggleShowsExistingConnections
868: .setEnabled(isGraphFocused);
869: actionToggleShowsInsertedConnections
870: .setEnabled(isGraphFocused);
871: actionToggleShowsChangedConnections
872: .setEnabled(isGraphFocused);
873: actionToggleHidesExistingConnections
874: .setEnabled(isGraphFocused);
875: actionToggleHidesDanglingConnections
876: .setEnabled(isGraphFocused);
877: actionToggleRemembersCellViews.setEnabled(isGraphFocused);
878: actionToggleJumpsToDefaultPort.setEnabled(isGraphFocused);
879: actionTogglePortsScaled.setEnabled(isGraphFocused);
880: actionToggleMovesIntoGroups.setEnabled(isGraphFocused);
881: actionToggleMovesOutOfGroups.setEnabled(isGraphFocused);
882: actionSwitchDotGrid.setEnabled(isGraphFocused);
883: actionSwitchLineGrid.setEnabled(isGraphFocused);
884: actionSwitchCrossGrid.setEnabled(isGraphFocused);
885: actionHandleColor.setEnabled(isGraphFocused);
886: actionLockedHandleColor.setEnabled(isGraphFocused);
887: actionHandleSize.setEnabled(isGraphFocused);
888: actionMarqueeColor.setEnabled(isGraphFocused);
889:
890: // Sets the states of the toggleable actions that require
891: // a focused graph.
892: JGraph graph = getPermanentFocusOwnerGraph();
893: if (graph != null) {
894: GraphLayoutCache cache = graph.getGraphLayoutCache();
895: actionTogglePorts.setSelected(graph.isPortsVisible());
896: actionToggleGrid.setSelected(graph.isGridVisible());
897: actionToggleAntiAlias
898: .setSelected(graph.isAntiAliased());
899: actionToggleDragEnabled.setSelected(graph
900: .isDragEnabled());
901: actionToggleDoubleBuffered.setSelected(graph
902: .isDoubleBuffered());
903: actionToggleEditable.setSelected(graph.isEditable());
904: actionToggleDisconnectOnMove.setSelected(graph
905: .isDisconnectOnMove());
906: actionToggleMoveBelowZero.setSelected(graph
907: .isMoveBelowZero());
908: actionToggleGraphMoveable.setSelected(graph
909: .isMoveable());
910: actionToggleGraphSizeable.setSelected(graph
911: .isSizeable());
912: actionToggleGraphBendable.setSelected(graph
913: .isBendable());
914: actionToggleGraphConnectable.setSelected(graph
915: .isConnectable());
916: actionToggleGraphDisconnectable.setSelected(graph
917: .isDisconnectable());
918: actionToggleSelectsLocalInsertedCells.setSelected(cache
919: .isSelectsLocalInsertedCells());
920: actionToggleSelectsAllInsertedCells.setSelected(cache
921: .isSelectsAllInsertedCells());
922: actionToggleShowsExistingConnections.setSelected(cache
923: .isShowsExistingConnections());
924: actionToggleShowsInsertedConnections.setSelected(cache
925: .isShowsInsertedConnections());
926: actionToggleShowsChangedConnections.setSelected(cache
927: .isShowsChangedConnections());
928: actionToggleHidesExistingConnections.setSelected(cache
929: .isHidesExistingConnections());
930: actionToggleHidesDanglingConnections.setSelected(cache
931: .isHidesDanglingConnections());
932: actionToggleRemembersCellViews.setSelected(cache
933: .isRemembersCellViews());
934: actionToggleJumpsToDefaultPort.setSelected(graph
935: .isJumpToDefaultPort());
936: actionTogglePortsScaled.setSelected(graph
937: .isPortsScaled());
938: actionToggleMovesIntoGroups.setSelected(graph
939: .isMoveIntoGroups());
940: actionToggleMovesOutOfGroups.setSelected(graph
941: .isMoveOutOfGroups());
942:
943: // ToggleRootHandles is implemented as a client property
944: Boolean b = (Boolean) graph
945: .getClientProperty(JGraphpadVertexRenderer.CLIENTPROPERTY_SHOWFOLDINGICONS);
946: actionToggleRootHandles.setSelected((b != null) ? b
947: .booleanValue() : true);
948:
949: int gridMode = graph.getGridMode();
950: actionSwitchDotGrid
951: .setSelected(gridMode == JGraph.DOT_GRID_MODE);
952: actionSwitchLineGrid
953: .setSelected(gridMode == JGraph.LINE_GRID_MODE);
954: actionSwitchCrossGrid
955: .setSelected(gridMode == JGraph.CROSS_GRID_MODE);
956: }
957:
958: JGraphEditorDiagramPane diagramPane = JGraphEditorDiagramPane
959: .getParentDiagramPane(component);
960: boolean isDiagramFocused = diagramPane != null;
961:
962: actionToggleRulers.setEnabled(isDiagramFocused);
963: actionTogglePage.setEnabled(isDiagramFocused);
964: actionBackgroundImage.setEnabled(isDiagramFocused);
965: actionBackgroundImageURL.setEnabled(isDiagramFocused);
966: actionFitNone.setEnabled(isDiagramFocused);
967: actionFitWindow.setEnabled(isDiagramFocused);
968: actionFitWidth.setEnabled(isDiagramFocused);
969: actionFitPage.setEnabled(isDiagramFocused);
970: actionClearBackground.setEnabled(isDiagramFocused);
971: actionToggleMetric.setEnabled(isDiagramFocused);
972:
973: // Sets the states of the toggleable actions that require
974: // a focused diagram pane.
975: actionFitNone.setSelected(true);
976: if (diagramPane != null) {
977: actionToggleRulers.setSelected(diagramPane
978: .isRulersVisible());
979: actionTogglePage.setSelected(diagramPane
980: .isPageVisible());
981: actionToggleMetric.setSelected(diagramPane.isMetric());
982: actionFitNone
983: .setSelected(diagramPane.getAutoScalePolicy() == JGraphEditorDiagramPane.AUTOSCALE_POLICY_NONE);
984: actionFitWindow
985: .setSelected(diagramPane.getAutoScalePolicy() == JGraphEditorDiagramPane.AUTOSCALE_POLICY_WINDOW);
986: actionFitPage
987: .setSelected(diagramPane.getAutoScalePolicy() == JGraphEditorDiagramPane.AUTOSCALE_POLICY_PAGE);
988: actionFitWidth
989: .setSelected(diagramPane.getAutoScalePolicy() == JGraphEditorDiagramPane.AUTOSCALE_POLICY_PAGEWIDTH);
990: }
991: }
992:
993: }
994:
995: }
|