* Bug #13030 fixed - Selection to Super block did not reset the origin
[scilab.git] / scilab / modules / graph / src / java / org / scilab / modules / graph / actions / UndoAction.java
1 /*
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
6  *
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
12  *
13  */
14
15 package org.scilab.modules.graph.actions;
16
17 import java.awt.Toolkit;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.KeyEvent;
20 import java.lang.ref.WeakReference;
21
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;
28
29 import com.mxgraph.util.mxEvent;
30 import com.mxgraph.util.mxEventObject;
31 import com.mxgraph.util.mxUndoManager;
32
33 /**
34  * Undo manager
35  */
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();
46
47     /**
48      * Manage enable modification
49      */
50     private final class UndoConstraint extends ActionConstraint {
51         private final WeakReference<ScilabGraph> scilabGraph;
52
53         /**
54          * Default constructor
55          * @param scilabGraph the associated scilab graph
56          */
57         public UndoConstraint(ScilabGraph scilabGraph) {
58             this.scilabGraph = new WeakReference<ScilabGraph>(scilabGraph);
59         }
60
61         /**
62          * @param action
63          *            the current action
64          * @param 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)
68          */
69         @Override
70         public void install(DefaultAction action, ScilabGraph scilabGraph) {
71             super.install(action, scilabGraph);
72             registerAsListener(scilabGraph.getUndoManager());
73         }
74
75         /**
76          * @param manager the associated UndoManager
77          */
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);
83         }
84
85         /**
86          * To be checked
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)
90          */
91         @Override
92         public void invoke(Object sender, mxEventObject evt) {
93             final ScilabGraph graph = scilabGraph.get();
94             if (graph == null) {
95                 return;
96             }
97
98             boolean canUndo = graph.getUndoManager().canUndo();
99             super.setEnabled(canUndo);
100         }
101     }
102
103     /**
104      * Constructor
105      * @param scilabGraph corresponding Scilab Graph
106      */
107     public UndoAction(ScilabGraph scilabGraph) {
108         super(scilabGraph);
109
110         UndoConstraint c = new UndoConstraint(scilabGraph);
111         c.install(this, scilabGraph);
112     }
113
114     /**
115      * Create a button for a graph toolbar
116      * @param scilabGraph corresponding Scilab Graph
117      * @return the button
118      */
119     public static PushButton undoButton(ScilabGraph scilabGraph) {
120         return createButton(scilabGraph, UndoAction.class);
121     }
122
123     /**
124      * Create a menu for a graph menubar
125      * @param scilabGraph corresponding Scilab Graph
126      * @return the menu
127      */
128     public static MenuItem undoMenu(ScilabGraph scilabGraph) {
129         return createMenu(scilabGraph, UndoAction.class);
130     }
131
132     /**
133      * Action associated
134      *
135      * @param e
136      *            the event
137      * @see org.scilab.modules.gui.events.callback.CallBack#actionPerformed(java.awt.event.ActionEvent)
138      */
139     @Override
140     public void actionPerformed(ActionEvent e) {
141         final ScilabGraph graph = getGraph(e);
142
143         graph.removeUndoHandler();
144         graph.getUndoManager().undo();
145         graph.registerUndoHandler();
146
147         // revalidate the graph
148         graph.getAsComponent().clearCellOverlays();
149         graph.getAsComponent().validateGraph();
150     }
151
152 }