Graph: import sources from Xcos
[scilab.git] / scilab / modules / graph / src / java / org / scilab / modules / graph / actions / base / VertexSelectionDependantAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2010 - DIGITEO - ClĂ©ment DAVID
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-en.txt
10  *
11  */
12
13 package org.scilab.modules.graph.actions.base;
14
15 import org.scilab.modules.graph.ScilabGraph;
16
17 import com.mxgraph.model.mxCell;
18 import com.mxgraph.util.mxEvent;
19 import com.mxgraph.util.mxEventObject;
20 import com.mxgraph.view.mxGraphSelectionModel;
21
22 /**
23  * Common class for selection dependent actions.
24  * 
25  * Children of this class will be activated when there something selected. If
26  * not, the action will be disabled.
27  */
28 public abstract class VertexSelectionDependantAction extends DefaultAction {
29
30         /**
31          * Enable the selection if there is at least a vertex in the selection.
32          */
33         private final class VertexSelectionDependantConstraint extends ActionConstraint {
34
35                 /**
36                  * Default constructor
37                  */
38                 public VertexSelectionDependantConstraint() {
39                         super();
40                 }
41
42                 /**
43                  * @param action the action
44                  * @param scilabGraph the current graph
45                  * @see org.scilab.modules.graph.actions.base.ActionConstraint#install(org.scilab.modules.graph.actions.base.DefaultAction,
46                  *      org.scilab.modules.graph.ScilabGraph)
47                  */
48                 @Override
49                 public void install(DefaultAction action, ScilabGraph scilabGraph) {
50                         super.install(action, scilabGraph);
51                         
52                         scilabGraph.getSelectionModel().addListener(mxEvent.UNDO, this);
53                 }
54                 
55                 /**
56                  * @param sender the sender
57                  * @param evt the event
58                  * @see com.mxgraph.util.mxEventSource.mxIEventListener#invoke(java.lang.Object, com.mxgraph.util.mxEventObject)
59                  */
60                 public void invoke(Object sender, mxEventObject evt) {
61                         mxGraphSelectionModel selection = (mxGraphSelectionModel) sender;
62                         Object[] cells = selection.getCells();
63                         
64                         boolean vertexFound = false;
65                         if (cells != null) {
66                                 for (Object object : cells) {
67                                         if (object instanceof mxCell) {
68                                                 mxCell cell = (mxCell) object;
69                                                 vertexFound = cell.isVertex();
70                                         }
71                                         
72                                         if (vertexFound) {
73                                                 break;
74                                         }
75                                 }
76
77                                 setEnabled(vertexFound);
78                         }
79                 }
80                 
81         }
82         
83         /**
84          * Default constructor
85          * 
86          * @param scilabGraph
87          *            The associated graph
88          */
89         public VertexSelectionDependantAction(ScilabGraph scilabGraph) {
90                 super(scilabGraph);
91                 
92                 if (scilabGraph != null) {
93                         VertexSelectionDependantConstraint c = new VertexSelectionDependantConstraint();
94                         c.install(this, scilabGraph);
95                 }
96         }
97 }