Menu ? => Links => Mailing lists was KO
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / actions / AutoPositionSplitBlockAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2016 - Chenfeng ZHU
4  *
5  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12 package org.scilab.modules.xcos.block.actions;
13
14 import java.awt.event.ActionEvent;
15 import java.awt.event.KeyEvent;
16
17 import org.scilab.modules.graph.ScilabComponent;
18 import org.scilab.modules.graph.ScilabGraph;
19 import org.scilab.modules.graph.actions.base.ActionConstraint;
20 import org.scilab.modules.graph.actions.base.DefaultAction;
21 import org.scilab.modules.graph.actions.base.VertexSelectionDependantAction;
22 import org.scilab.modules.gui.menuitem.MenuItem;
23 import org.scilab.modules.xcos.block.SplitBlock;
24 import org.scilab.modules.xcos.graph.XcosDiagram;
25 import org.scilab.modules.xcos.utils.BlockAutoPositionUtils;
26 import org.scilab.modules.xcos.utils.XcosMessages;
27
28 import com.mxgraph.model.mxCell;
29 import com.mxgraph.util.mxEvent;
30 import com.mxgraph.util.mxEventObject;
31 import com.mxgraph.view.mxGraphSelectionModel;
32
33 /**
34  * SplitBlock auto Position.
35  */
36 @SuppressWarnings(value = { "serial" })
37 public class AutoPositionSplitBlockAction extends VertexSelectionDependantAction {
38
39     /** Name of the action */
40     public static final String NAME = XcosMessages.BLOCK_AUTO_POSITION_SPLIT_BLOCK;
41     /** Icon name of the action */
42     public static final String SMALL_ICON = "";
43     /** Mnemonic key of the action */
44     public static final int MNEMONIC_KEY = KeyEvent.VK_P;
45     /** Accelerator key for the action */
46     public static final int ACCELERATOR_KEY = 0;
47
48     /**
49      * Default constructor the associated graph
50      *
51      * @param scilabGraph
52      *            the graph to associate
53      */
54     public AutoPositionSplitBlockAction(ScilabGraph scilabGraph) {
55         super(scilabGraph);
56
57         // The MenuItem is enabled only when SplitBlock is selected.
58         if (scilabGraph != null) {
59             SplitBlockSelectionDependantConstraint c = new SplitBlockSelectionDependantConstraint();
60             c.install(this, scilabGraph);
61         }
62     }
63
64     /**
65      * @param scilabGraph
66      * @return menu item
67      */
68     public static MenuItem createMenu(ScilabGraph scilabGraph) {
69         return createMenu(scilabGraph, AutoPositionSplitBlockAction.class);
70     }
71
72     @Override
73     public void actionPerformed(ActionEvent e) {
74         XcosDiagram graph = (XcosDiagram) getGraph(e);
75         if (graph.getSelectionCells().length == 0) {
76             return;
77         }
78
79         // action disabled when the cell is edited
80         final ScilabComponent comp = ((ScilabComponent) graph.getAsComponent());
81         if (comp.isEditing()) {
82             return;
83         }
84
85         Object[] cells = graph.getSelectionCells();
86
87         graph.getModel().beginUpdate();
88         try {
89             double scale = graph.getView().getScale();
90             graph.getView().setScale(1.0);
91             BlockAutoPositionUtils.changeSplitBlocksPosition((XcosDiagram) getGraph(null), cells);
92             graph.getView().setScale(scale);
93         } finally {
94             graph.getModel().endUpdate();
95         }
96     }
97
98
99     /**
100      * Enable the selection if there is at least a SplitBlock in the selection.
101      */
102     private final class SplitBlockSelectionDependantConstraint extends ActionConstraint {
103
104         /**
105          * Default constructor
106          */
107         public SplitBlockSelectionDependantConstraint() {
108             super();
109         }
110
111         /**
112          * @param action the action
113          * @param scilabGraph the current graph
114          * @see org.scilab.modules.graph.actions.base.ActionConstraint#install(org.scilab.modules.graph.actions.base.DefaultAction,
115          *      org.scilab.modules.graph.ScilabGraph)
116          */
117         @Override
118         public void install(DefaultAction action, ScilabGraph scilabGraph) {
119             super.install(action, scilabGraph);
120             scilabGraph.getSelectionModel().addListener(mxEvent.UNDO, this);
121         }
122
123         /**
124          * @param sender the sender
125          * @param evt the event
126          * @see com.mxgraph.util.mxEventSource.mxIEventListener#invoke(java.lang.Object, com.mxgraph.util.mxEventObject)
127          */
128         @Override
129         public void invoke(Object sender, mxEventObject evt) {
130             mxGraphSelectionModel selection = (mxGraphSelectionModel) sender;
131             Object[] cells = selection.getCells();
132             boolean splitblockFound = false;
133             if (cells != null) {
134                 for (Object object : cells) {
135                     if (object instanceof mxCell) {
136                         mxCell cell = (mxCell) object;
137                         splitblockFound = (cell instanceof SplitBlock);
138                     }
139                     if (splitblockFound) {
140                         break;
141                     }
142                 }
143                 setEnabled(splitblockFound);
144             }
145         }
146
147     }
148
149
150 }