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