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