Move IOBlocks to a block/io
[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.GroundBlock;
22 import org.scilab.modules.xcos.block.positionning.VoltageSensorBlock;
23 import org.scilab.modules.xcos.port.command.CommandPort;
24 import org.scilab.modules.xcos.port.control.ControlPort;
25 import org.scilab.modules.xcos.port.input.InputPort;
26 import org.scilab.modules.xcos.port.output.OutputPort;
27 import org.scilab.modules.xcos.utils.XcosMessages;
28
29 /**
30  * Ease the creation of blocks
31  */
32 public final class BlockFactory {
33
34         /**
35          * List the specific block interface function name.
36          * 
37          * @checkstyle DAC: As this is the constructor for all the block classes,
38          *             this class is very coupled with *Block classes
39          */
40         public static enum BlockInterFunction {
41                 TEXT_f(new TextBlock(XcosMessages.DOTS)),
42                 SUPER_f(new SuperBlock()),
43                 DSUPER(new SuperBlock(true)),
44                 CONST_m(new ConstBlock()),
45                 CONST(CONST_m.getSharedInstance()),
46                 CONST_f(CONST_m.getSharedInstance()),
47                 AFFICH_m(new AfficheBlock()),
48                 AFFICH_f(AFFICH_m.getSharedInstance()),
49                 GAINBLK_f(new GainBlock()),
50                 GAINBLK(GAINBLK_f.getSharedInstance()),
51                 GAIN_f(GAINBLK_f.getSharedInstance()),
52                 IN_f(new ExplicitInBlock()),
53                 OUT_f(new ExplicitOutBlock()),
54                 INIMPL_f(new ImplicitInBlock()),
55                 OUTIMPL_f(new ImplicitOutBlock()),
56                 CLKINV_f(new EventInBlock()),
57                 /* TODO: What about CLKIN_F ? */
58                 CLKOUTV_f(new EventOutBlock()),
59                 CLKOUT_f(CLKOUTV_f.getSharedInstance()),
60                 SPLIT_f(new SplitBlock()),
61                 IMPSPLIT_f(SPLIT_f.getSharedInstance()),
62                 CLKSPLIT_f(SPLIT_f.getSharedInstance()),
63                 Ground(new GroundBlock()),
64                 VoltageSensor(new VoltageSensorBlock());
65                 
66                 private BasicBlock block;
67                 /**
68                  * Default constructor
69                  * @param block The reference instance
70                  */
71                 private BlockInterFunction(BasicBlock block) {
72                         this.block = block;
73                         block.setDefaultValues();
74                 }
75                 
76                 /**
77                  * Create a block instance
78                  * @return The new block instance
79                  */
80                 private BasicBlock createInstance() {
81                         BasicBlock clone = null;
82                         
83                         if (block != null) {
84                                 clone = (BasicBlock) BlockFactory.createClone(block);
85                         }
86                         return clone;
87                 }
88                 
89                 /**
90                  * Get the reference shared block instance for this BlockInterFunction.
91                  * @return The shared block instance
92                  */
93                 public BasicBlock getSharedInstance() {
94                         return block;
95                 }
96         }
97         
98         /** Default singleton constructor */
99         private BlockFactory() {
100                 // This class is a static singleton
101         }
102
103         /**
104          * Instantiate a new block with the specified interface function name.
105          * @param label The interface function name.
106          * @return A new instance of a block.
107          */
108         public static BasicBlock createBlock(String label) {
109                 BasicBlock block = null;
110                 
111                 for (BlockInterFunction func : BlockInterFunction.values()) {
112                         if (label.compareTo(func.name()) == 0) {
113                                 block = func.createInstance();
114                                 break;
115                         }
116                 }
117                 
118                 // Not specific block
119                 if (block == null) {
120                         block = new BasicBlock(label);
121                 }
122                 
123                 return block;
124         }
125         
126         /**
127          * Instantiate a new block with the specified interface function.
128          * @param func the interface function
129          * @return A new instance of a block. 
130          */
131         public static BasicBlock createBlock(BlockInterFunction func) {
132                 return func.createInstance();
133         }
134
135         /**
136          * Create a clone for a block
137          * 
138          * @param block
139          *            The block to be cloned
140          * @return the clone
141          */
142         public static Object createClone(BasicBlock block) {
143                 try {
144                         BasicBlock clone = (BasicBlock) block.clone();
145
146                         /* Clone children */
147                         for (int i = 0; i < block.getChildCount(); i++) {
148                                 if (block.getChildAt(i) instanceof InputPort) {
149                                         clone.addPort((InputPort) block.getChildAt(i).clone());
150                                 } else if (block.getChildAt(i) instanceof OutputPort) {
151                                         clone.addPort((OutputPort) block.getChildAt(i).clone());
152                                 } else if (block.getChildAt(i) instanceof CommandPort) {
153                                         clone.addPort((CommandPort) block.getChildAt(i).clone());
154                                 } else if (block.getChildAt(i) instanceof ControlPort) {
155                                         clone.addPort((ControlPort) block.getChildAt(i).clone());
156                                 }
157                         }
158
159                         return clone;
160                 } catch (CloneNotSupportedException e) {
161                         e.printStackTrace();
162                         return null;
163                 }
164         }
165 }