Xcos: remove block default values
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / SplitBlock.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
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 package org.scilab.modules.xcos.block;
13
14 import org.scilab.modules.types.scilabTypes.ScilabDouble;
15 import org.scilab.modules.types.scilabTypes.ScilabList;
16 import org.scilab.modules.xcos.link.BasicLink;
17 import org.scilab.modules.xcos.port.BasicPort;
18 import org.scilab.modules.xcos.port.command.CommandPort;
19 import org.scilab.modules.xcos.port.control.ControlPort;
20 import org.scilab.modules.xcos.port.input.ExplicitInputPort;
21 import org.scilab.modules.xcos.port.input.ImplicitInputPort;
22 import org.scilab.modules.xcos.port.output.ExplicitOutputPort;
23 import org.scilab.modules.xcos.port.output.ImplicitOutputPort;
24 import org.scilab.modules.xcos.utils.BlockPositioning;
25
26 import com.mxgraph.model.mxGeometry;
27
28 /**
29  * A SplitBlock is used on a junction between links.
30  */
31 public final class SplitBlock extends BasicBlock {
32
33         /** The default size */
34         public static final int DEFAULT_SIZE = 7;
35         /** The default color value */
36         public static final int DEFAULT_COLOR = 7;
37
38         private static final long serialVersionUID = 5817243367840540106L;
39
40         /**
41          * Constructor
42          */
43         public SplitBlock() {
44                 super();
45         }
46         
47         /**
48          * Connect the splitblock to a source and 2 targets.
49          * 
50          * @param source
51          *            source to be connected with
52          * @param target1
53          *            first target to be connected with
54          * @param target2
55          *            second target to be connected with
56          */
57         public void setConnection(BasicPort source, BasicPort target1,
58                         BasicPort target2) {
59
60                 // source
61                 if (source instanceof ExplicitOutputPort) {
62                         addPort(new ExplicitInputPort());
63                 } else if (source instanceof ImplicitOutputPort
64                                 || source instanceof ImplicitInputPort) {
65                         addPort(new ImplicitInputPort());
66                 } else if (source instanceof CommandPort) {
67                         addPort(new ControlPort());
68                 }
69
70                 // target1 -> add 3 output ports
71                 if (target1 instanceof ExplicitInputPort) {
72                         addPort(new ExplicitOutputPort());
73                         addPort(new ExplicitOutputPort());
74                 } else if (target1 instanceof ImplicitOutputPort
75                                 || target1 instanceof ImplicitInputPort) {
76                         addPort(new ImplicitOutputPort());
77                         addPort(new ImplicitOutputPort());
78                 } else if (target1 instanceof ControlPort) {
79                         addPort(new CommandPort());
80                         addPort(new CommandPort());
81                 }
82
83                 getChildAt(0).setVisible(false);
84                 getChildAt(1).setVisible(false);
85                 getChildAt(2).setVisible(false);
86         }
87
88         /**
89          * Initialize the block with the default values
90          */
91         @Override
92         protected void setDefaultValues() {
93                 super.setDefaultValues();
94                 setInterfaceFunctionName("SPLIT_f");
95                 setStyle(getInterfaceFunctionName()); 
96                 setSimulationFunctionName("lsplit");
97                 setRealParameters(new ScilabDouble());
98                 setIntegerParameters(new ScilabDouble());
99                 setObjectsParameters(new ScilabList());
100                 setExprs(new ScilabDouble());
101         }
102
103         /**
104          * Add a port on the block.
105          * 
106          * @param port
107          *            The port to be added to the block
108          * @see org.scilab.modules.xcos.block.BasicBlock#addPort(org.scilab.modules.xcos.port.BasicPort)
109          */
110         @Override
111         public void addPort(BasicPort port) {
112                 super.addPort(port);
113                 port.setVisible(false);
114         }
115
116         /**
117          * @return input port
118          */
119         public BasicPort getIn() {
120                 return (BasicPort) getChildAt(0);
121         }
122
123         /**
124          * @return first output port
125          */
126         public BasicPort getOut1() {
127                 return (BasicPort) getChildAt(1);
128         }
129
130         /**
131          * @return second ouput port
132          */
133         public BasicPort getOut2() {
134                 return (BasicPort) getChildAt(2);
135         }
136
137         /**
138          * delete split block child before delete
139          */
140         public void unlinkAndClean() {
141
142                 Object[] objs = getParentDiagram().getAllEdges(
143                                 new Object[] {getChildAt(0), getChildAt(1), getChildAt(2)});
144                 getParentDiagram().getModel().beginUpdate();
145                 for (int i = 0; i < objs.length; i++) {
146                         if (objs[i] instanceof BasicLink) {
147                                 BasicLink link = (BasicLink) objs[i];
148                                 getParentDiagram().getModel().remove(link);
149                         }
150                 }
151                 getParentDiagram().getModel().endUpdate();
152         }
153
154         /**
155          * Set the geometry of the block
156          * 
157          * @param geometry
158          *            change split block geometry
159          */
160         @Override
161         public void setGeometry(mxGeometry geometry) {
162                 if (geometry != null) {
163                         geometry.setWidth(DEFAULT_SIZE);
164                         geometry.setHeight(DEFAULT_SIZE);
165                         
166                         /*
167                          * Align the geometry on the grid
168                          */
169                         double gridSize;
170                         if (getParentDiagram() != null) {
171                                 gridSize = getParentDiagram().getGridSize();
172                         } else {
173                                 gridSize = BlockPositioning.DEFAULT_GRIDSIZE;
174                         }
175                         BlockPositioning.alignPoint(geometry, gridSize,
176                                         (geometry.getWidth() / 2.0));
177                 }
178                 
179                 super.setGeometry(geometry);
180         }
181
182 }