* Bug #13030 fixed - Selection to Super block did not reset the origin
[scilab.git] / scilab / modules / graph / src / java / org / scilab / modules / graph / actions / RedoAction.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  * Redo manager
35  */
36 @SuppressWarnings(value = { "serial" })
37 public class RedoAction extends DefaultAction {
38     /** Name of the action */
39     public static final String NAME = ScilabGraphMessages.REDO;
40     /** Icon name of the action */
41     public static final String SMALL_ICON = "edit-redo";
42     /** Mnemonic key of the action */
43     public static final int MNEMONIC_KEY = KeyEvent.VK_Y;
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 RedoConstraint extends ActionConstraint {
51         private final WeakReference<ScilabGraph> scilabGraph;
52
53         /**
54          * Default constructor
55          * @param scilabGraph the graph to work on
56          */
57         public RedoConstraint(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
73             registerAsListener(scilabGraph.getUndoManager());
74         }
75
76         /**
77          * @param manager the associated UndoManager
78          */
79         public void registerAsListener(mxUndoManager manager) {
80             manager.addListener(mxEvent.UNDO, this);
81             manager.addListener(mxEvent.REDO, this);
82             manager.addListener(mxEvent.ADD, this);
83             manager.addListener(mxEvent.CLEAR, this);
84         }
85
86         /**
87          * To be checked
88          * @param sender the event sender
89          * @param evt the current event
90          * @see com.mxgraph.util.mxEventSource.mxIEventListener#invoke(java.lang.Object, com.mxgraph.util.mxEventObject)
91          */
92         @Override
93         public void invoke(Object sender, mxEventObject evt) {
94             final ScilabGraph graph = scilabGraph.get();
95             if (graph == null) {
96                 return;
97             }
98
99             boolean canRedo = graph.getUndoManager().canRedo();
100             super.setEnabled(canRedo);
101         }
102     }
103
104     /**
105      * Constructor
106      * @param scilabGraph corresponding Scilab Graph
107      */
108     public RedoAction(ScilabGraph scilabGraph) {
109         super(scilabGraph);
110
111         RedoConstraint c = new RedoConstraint(scilabGraph);
112         c.install(this, scilabGraph);
113     }
114
115     /**
116      * Create a button for a graph toolbar
117      * @param scilabGraph corresponding Scilab Graph
118      * @return the button
119      */
120     public static PushButton redoButton(ScilabGraph scilabGraph) {
121         return createButton(scilabGraph, RedoAction.class);
122     }
123
124     /**
125      * Create a menu for a graph menubar
126      * @param scilabGraph corresponding Scilab Graph
127      * @return the menu
128      */
129     public static MenuItem redoMenu(ScilabGraph scilabGraph) {
130         return createMenu(scilabGraph, RedoAction.class);
131     }
132
133     /**
134      * Action associated
135      *
136      * @param e
137      *            the event
138      * @see org.scilab.modules.gui.events.callback.CallBack#actionPerformed(java.awt.event.ActionEvent)
139      */
140     @Override
141     public void actionPerformed(ActionEvent e) {
142         final ScilabGraph graph = getGraph(e);
143
144         graph.removeUndoHandler();
145         graph.getUndoManager().redo();
146         graph.registerUndoHandler();
147
148         // revalidate the graph
149         graph.getAsComponent().clearCellOverlays();
150         graph.getAsComponent().validateGraph();
151     }
152 }