Move IOBlocks to a block/io
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / io / ContextUpdate.java
1 package org.scilab.modules.xcos.block.io;
2
3 import java.io.File;
4 import java.io.IOException;
5
6 import org.scilab.modules.hdf5.scilabTypes.ScilabDouble;
7 import org.scilab.modules.hdf5.scilabTypes.ScilabList;
8 import org.scilab.modules.xcos.block.BasicBlock;
9 import org.scilab.modules.xcos.io.BlockReader;
10 import org.scilab.modules.xcos.utils.XcosInterpreterManagement;
11 import org.scilab.modules.xcos.utils.XcosInterpreterManagement.InterpreterException;
12
13 public abstract class ContextUpdate extends BasicBlock{
14
15     private static final long serialVersionUID = 6076826729067963560L;
16     
17     /**
18      * This enum represent all the subclasses of ContextUpdate .
19      *
20      * It is used to easily loop over a BasicBlock I/O blocks
21      */
22     public enum IOBlocks {
23         EventInBlock(EventInBlock.class),
24         EventOutBlock(EventOutBlock.class),
25         ExplicitInBlock(ExplicitInBlock.class),
26         ExplicitOutBlock(ExplicitOutBlock.class),
27         ImplicitInBlock(ImplicitInBlock.class),
28         ImplicitOutBlock(ImplicitOutBlock.class),
29         Unknow(ContextUpdate.class);
30         
31         private Class<? extends ContextUpdate> klass;
32         
33         private IOBlocks(Class<? extends ContextUpdate> klass) {
34             this.klass = klass;
35         }
36         
37         public Class<? extends ContextUpdate> getReferencedClass() {
38             return klass;
39         }
40     }
41     
42         public ContextUpdate() {
43                 super();
44         }
45
46         protected ContextUpdate(String label) {
47                 this();
48                 setDefaultValues();
49                 setValue(label);
50         }
51
52         /**
53          * Initialize the block with the default values
54          */
55         @Override
56         protected void setDefaultValues() {
57                 super.setDefaultValues();
58                 setNbZerosCrossing(new ScilabDouble(0));
59                 setNmode(new ScilabDouble(0));
60                 setODState(new ScilabList());
61                 setValue(1);
62         }
63     
64     public void onContextChange(String[] context) {
65         //prevent to open twice
66         if(isLocked()) {
67             return;
68         }
69         
70         final File tempOutput;
71         final File tempInput;
72         final File tempContext;
73         try {
74             tempInput = File.createTempFile("xcos",".h5");
75             tempInput.deleteOnExit();
76
77             // Write scs_m
78             tempOutput = exportBlockStruct();
79             // Write context
80             tempContext = exportContext(context);
81
82             String cmd;
83             
84             cmd = "xcosBlockEval(\""+tempOutput.getAbsolutePath()+"\"";
85             cmd += ", \""+tempInput.getAbsolutePath()+"\"";
86             cmd += ", "+getInterfaceFunctionName();
87             cmd += ", \""+tempContext.getAbsolutePath()+"\");";
88
89                 try {
90                         XcosInterpreterManagement.synchronousScilabExec(cmd);
91                 } catch (InterpreterException e) {
92                         e.printStackTrace();
93                 }
94                 BasicBlock modifiedBlock = BlockReader.readBlockFromFile(tempInput.getAbsolutePath());
95                 updateBlockSettings(modifiedBlock);
96             
97         } catch (IOException e) {
98             e.printStackTrace();
99         }
100     }
101 }