Xcos: remove block default values
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / BlockFactory.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - 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.xcos.block;
14
15 import org.scilab.modules.xcos.block.io.EventInBlock;
16 import org.scilab.modules.xcos.block.io.EventOutBlock;
17 import org.scilab.modules.xcos.block.io.ExplicitInBlock;
18 import org.scilab.modules.xcos.block.io.ExplicitOutBlock;
19 import org.scilab.modules.xcos.block.io.ImplicitInBlock;
20 import org.scilab.modules.xcos.block.io.ImplicitOutBlock;
21 import org.scilab.modules.xcos.block.positionning.BigSom;
22 import org.scilab.modules.xcos.block.positionning.GroundBlock;
23 import org.scilab.modules.xcos.block.positionning.Product;
24 import org.scilab.modules.xcos.block.positionning.RoundBlock;
25 import org.scilab.modules.xcos.block.positionning.Summation;
26 import org.scilab.modules.xcos.block.positionning.VoltageSensorBlock;
27 import org.scilab.modules.xcos.utils.XcosMessages;
28
29 import com.mxgraph.model.mxICell;
30 /**
31  * Ease the creation of blocks
32  */
33 public final class BlockFactory {
34
35         // DAC: As this is the constructor for all the block classes, this class is
36         // very coupled with *Block classes
37         // CSOFF: ClassDataAbstractionCoupling
38         /**
39          * List the specific block interface function name.
40          */
41         public static enum BlockInterFunction {
42                 /** @see TextBlock */
43                 TEXT_f(new TextBlock()),
44                 /** @see SuperBlock */
45                 SUPER_f(new SuperBlock()),
46                 /** @see SuperBlock */
47                 DSUPER(new SuperBlock(true)),
48                 /** @see ConstBlock */
49                 CONST_m(new ConstBlock()),
50                 CONST(CONST_m.getSharedInstance()),
51                 /** @see ConstBlock */
52                 CONST_f(CONST_m.getSharedInstance()),
53                 /** @see PrintBlock */
54                 FROM(new PrintBlock(XcosMessages.BLOCK_FROM)),
55                 /** @see PrintBlock */
56                 GOTO(new PrintBlock(XcosMessages.BLOCK_GOTO)),
57                 /** @see PrintBlock */
58                 CLKFROM(FROM.getSharedInstance()),
59                 /** @see PrintBlock */
60                 CLKGOTO(GOTO.getSharedInstance()),
61                 /** @see PrintBlock */
62                 FROMMO(FROM.getSharedInstance()),
63                 /** @see PrintBlock */
64                 GOTOMO(GOTO.getSharedInstance()),
65                 /** @see AfficheBlock */
66                 AFFICH_m(new AfficheBlock()),
67                 /** @see AfficheBlock */
68                 AFFICH_f(AFFICH_m.getSharedInstance()),
69                 /** @see GainBlock */
70                 GAINBLK_f(new GainBlock()),
71                 /** @see GainBlock */
72                 GAINBLK(GAINBLK_f.getSharedInstance()),
73                 /** @see GainBlock */
74                 GAIN_f(GAINBLK_f.getSharedInstance()),
75                 /** @see ExplicitInBlock */
76                 IN_f(new ExplicitInBlock()),
77                 /** @see ExplicitOutBlock */
78                 OUT_f(new ExplicitOutBlock()),
79                 /** @see ImplicitInBlock */
80                 INIMPL_f(new ImplicitInBlock()),
81                 /** @see ImplicitOutBlock */
82                 OUTIMPL_f(new ImplicitOutBlock()),
83                 /** @see EventInBlock */
84                 CLKINV_f(new EventInBlock()),
85                 /** @see EventOutBlock */
86                 CLKOUTV_f(new EventOutBlock()),
87                 /** @see EventOutBlock */
88                 CLKOUT_f(CLKOUTV_f.getSharedInstance()),
89                 /** @see SplitBlock */
90                 SPLIT_f(new SplitBlock()),
91                 /** @see SplitBlock */
92                 IMPSPLIT_f(SPLIT_f.getSharedInstance()),
93                 /** @see SplitBlock */
94                 CLKSPLIT_f(SPLIT_f.getSharedInstance()),
95                 /** @see GroundBlock */
96                 Ground(new GroundBlock()),
97                 /** @see VoltageSensorBlock */
98                 VoltageSensor(new VoltageSensorBlock()),
99                 /** @see RoundBlock */
100                 SUM_f(new RoundBlock("SUM_f")),
101                 /** @see RoundBlock */
102                 PROD_f(new RoundBlock("PROD_f")),
103                 /** @see RoundBlock */
104                 CLKSOMV_f(new RoundBlock("CLKSOMV_f")),
105                 /** @see BigSom */
106                 BIGSOM_f(new BigSom()),
107                 /** @see Summation */
108                 SUMMATION(new Summation()),
109                 /** @see Product */
110                 PRODUCT(new Product()),
111                 ;
112                 
113                 private BasicBlock block;
114                 /**
115                  * Default constructor
116                  * @param block The reference instance
117                  */
118                 private BlockInterFunction(BasicBlock block) {
119                         this.block = block;
120                 }
121                 
122                 /**
123                  * Create a block instance
124                  * @return The new block instance
125                  */
126                 private BasicBlock createInstance() {
127                         BasicBlock clone = null;
128                         
129                         if (block != null) {
130                                 clone = (BasicBlock) BlockFactory.createClone(block);
131                         }
132                         return clone;
133                 }
134                 
135                 /**
136                  * Get the reference shared block instance for this BlockInterFunction.
137                  * @return The shared block instance
138                  */
139                 public BasicBlock getSharedInstance() {
140                         return block;
141                 }
142         }
143         // CSON: ClassDataAbstractionCoupling
144         
145         /** Default singleton constructor */
146         private BlockFactory() {
147                 // This class is a static singleton
148         }
149
150         /**
151          * Instantiate a new block with the specified interface function name.
152          * @param label The interface function name.
153          * @return A new instance of a block.
154          */
155         public static BasicBlock createBlock(String label) {
156                 BasicBlock block = null;
157                 
158                 for (BlockInterFunction func : BlockInterFunction.values()) {
159                         if (label.compareTo(func.name()) == 0) {
160                                 block = func.createInstance();
161                                 break;
162                         }
163                 }
164                 
165                 // Not specific block
166                 if (block == null) {
167                         block = new BasicBlock(label);
168                 }
169                 
170                 return block;
171         }
172         
173         /**
174          * Instantiate a new block with the specified interface function.
175          * @param func the interface function
176          * @return A new instance of a block. 
177          */
178         public static BasicBlock createBlock(BlockInterFunction func) {
179                 return func.createInstance();
180         }
181
182         /**
183          * Create a clone for a block
184          * 
185          * @param block
186          *            The block to be cloned
187          * @return the clone
188          */
189         public static Object createClone(BasicBlock block) {
190                 try {
191                         BasicBlock clone = (BasicBlock) block.clone();
192
193                         /* Clone children */
194                         for (int i = 0; i < block.getChildCount(); i++) {
195                                 mxICell port = block.getChildAt(i);
196                                 clone.insert((mxICell) port.clone());
197                         }
198                         
199                         return clone;
200                 } catch (CloneNotSupportedException e) {
201                         e.printStackTrace();
202                         return null;
203                 }
204         }
205 }