2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
4 * Copyright (C) 2009 - DIGITEO - Vincent COUVERT
5 * Copyright (C) 2010 - DIGITEO - Clement DAVID
7 * This file must be used under the terms of the CeCILL.
8 * This source file is licensed as described in the file COPYING, which
9 * you should have received as part of this distribution. The terms
10 * are also available at
11 * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
15 package org.scilab.modules.graph.actions;
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.KeyEvent;
20 import java.lang.ref.WeakReference;
22 import org.scilab.modules.graph.ScilabGraph;
23 import org.scilab.modules.graph.actions.base.ActionConstraint;
24 import org.scilab.modules.graph.actions.base.DefaultAction;
25 import org.scilab.modules.graph.utils.ScilabGraphMessages;
26 import org.scilab.modules.gui.menuitem.MenuItem;
27 import org.scilab.modules.gui.pushbutton.PushButton;
29 import com.mxgraph.util.mxEvent;
30 import com.mxgraph.util.mxEventObject;
31 import com.mxgraph.util.mxUndoManager;
36 @SuppressWarnings(value = { "serial" })
37 public class UndoAction extends DefaultAction {
38 /** Name of the action */
39 public static final String NAME = ScilabGraphMessages.UNDO;
40 /** Icon name of the action */
41 public static final String SMALL_ICON = "edit-undo";
42 /** Mnemonic key of the action */
43 public static final int MNEMONIC_KEY = KeyEvent.VK_Z;
44 /** Accelerator key for the action */
45 public static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit().getMenuShortcutKeyMask();
48 * Manage enable modification
50 private final class UndoConstraint extends ActionConstraint {
51 private final WeakReference<ScilabGraph> scilabGraph;
55 * @param scilabGraph the associated scilab graph
57 public UndoConstraint(ScilabGraph scilabGraph) {
58 this.scilabGraph = new WeakReference<ScilabGraph>(scilabGraph);
65 * the associated graph
66 * @see org.scilab.modules.graph.actions.base.ActionConstraint#install(org.scilab.modules.graph.actions.base.DefaultAction,
67 * org.scilab.modules.graph.ScilabGraph)
70 public void install(DefaultAction action, ScilabGraph scilabGraph) {
71 super.install(action, scilabGraph);
72 registerAsListener(scilabGraph.getUndoManager());
76 * @param manager the associated UndoManager
78 private void registerAsListener(mxUndoManager manager) {
79 manager.addListener(mxEvent.UNDO, this);
80 manager.addListener(mxEvent.REDO, this);
81 manager.addListener(mxEvent.ADD, this);
82 manager.addListener(mxEvent.CLEAR, this);
87 * @param sender the event sender
88 * @param evt the current event
89 * @see com.mxgraph.util.mxEventSource.mxIEventListener#invoke(java.lang.Object, com.mxgraph.util.mxEventObject)
92 public void invoke(Object sender, mxEventObject evt) {
93 final ScilabGraph graph = scilabGraph.get();
98 boolean canUndo = graph.getUndoManager().canUndo();
99 super.setEnabled(canUndo);
105 * @param scilabGraph corresponding Scilab Graph
107 public UndoAction(ScilabGraph scilabGraph) {
110 UndoConstraint c = new UndoConstraint(scilabGraph);
111 c.install(this, scilabGraph);
115 * Create a button for a graph toolbar
116 * @param scilabGraph corresponding Scilab Graph
119 public static PushButton undoButton(ScilabGraph scilabGraph) {
120 return createButton(scilabGraph, UndoAction.class);
124 * Create a menu for a graph menubar
125 * @param scilabGraph corresponding Scilab Graph
128 public static MenuItem undoMenu(ScilabGraph scilabGraph) {
129 return createMenu(scilabGraph, UndoAction.class);
137 * @see org.scilab.modules.gui.events.callback.CallBack#actionPerformed(java.awt.event.ActionEvent)
140 public void actionPerformed(ActionEvent e) {
141 final ScilabGraph graph = getGraph(e);
143 graph.removeUndoHandler();
144 graph.getUndoManager().undo();
145 graph.registerUndoHandler();
147 // revalidate the graph
148 graph.getAsComponent().clearCellOverlays();
149 graph.getAsComponent().validateGraph();