Adding more semantic when using Scilab command execution
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / actions / ViewDetailsAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Vincent COUVERT
4  * Copyright (C) 2009 - DIGITEO - Bruno JOFRET
5  * 
6  * This file must be used under the terms of the CeCILL.
7  * This source file is licensed as described in the file COPYING, which
8  * you should have received as part of this distribution.  The terms
9  * are also available at    
10  * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
11  *
12  */
13
14 package org.scilab.modules.xcos.block.actions;
15
16 import java.io.File;
17
18 import org.scilab.modules.graph.ScilabGraph;
19 import org.scilab.modules.graph.actions.DefaultAction;
20 import org.scilab.modules.gui.menuitem.MenuItem;
21 import org.scilab.modules.hdf5.write.H5Write;
22 import org.scilab.modules.xcos.block.BasicBlock;
23 import org.scilab.modules.xcos.block.SplitBlock;
24 import org.scilab.modules.xcos.graph.XcosDiagram;
25 import org.scilab.modules.xcos.io.BasicBlockInfo;
26 import org.scilab.modules.xcos.link.BasicLink;
27 import org.scilab.modules.xcos.utils.XcosInterpreterManagement;
28 import org.scilab.modules.xcos.utils.XcosMessages;
29
30
31 public class ViewDetailsAction extends DefaultAction {
32
33     private ViewDetailsAction(ScilabGraph scilabGraph) {
34         super(XcosMessages.DETAILS, scilabGraph);
35     }
36
37     public static MenuItem createMenu(ScilabGraph scilabGraph) {
38         return createMenu(XcosMessages.DETAILS, null, new ViewDetailsAction(scilabGraph), null);
39     }
40
41     public void doAction() {
42         XcosDiagram graph = (XcosDiagram) getGraph(null);
43         Object[] selectedCells = graph.getSelectionCells();
44
45         //if no cells are selected : Do nothing
46         if (selectedCells.length == 0) { return; }
47
48         for (int i = 0; i < selectedCells.length ; ++i) {
49             if ((selectedCells[i] instanceof BasicBlock) && !(selectedCells[i] instanceof SplitBlock)) {
50                 try {
51                     File temp = File.createTempFile("xcos",".h5");
52                     temp.deleteOnExit();
53                     int file_id = H5Write.createFile(temp.getAbsolutePath());
54                     H5Write.writeInDataSet(file_id, "scs_m", BasicBlockInfo.getAsScilabObj((BasicBlock) selectedCells[i]));
55                     H5Write.closeFile(file_id);
56                     XcosInterpreterManagement.SynchronousScilabExec("import_from_hdf5(\""+temp.getAbsolutePath()+"\");tree_show(scs_m);deletefile(\"" + temp.getAbsolutePath()+"\");");
57                 }
58                 catch (Exception e) {
59                     // Do Nothing !!!
60                 }
61             }
62             if (selectedCells[i] instanceof BasicLink) {
63                 try {
64                     File temp = File.createTempFile("xcos",".h5");
65                     temp.deleteOnExit();
66                     int file_id = H5Write.createFile(temp.getAbsolutePath());
67                     H5Write.writeInDataSet(file_id, "scs_m", ((BasicLink) selectedCells[i]).getAsScilabObj());
68                     H5Write.closeFile(file_id);
69                     XcosInterpreterManagement.SynchronousScilabExec("import_from_hdf5(\""+temp.getAbsolutePath()+"\");tree_show(scs_m);deletefile(\"" + temp.getAbsolutePath()+"\");");;
70                 }
71                 catch (Exception e) {
72                     // Do Nothing !!!
73                 }
74             }
75         }
76
77     }
78 }