b0a43fba9039ce123290c895133a3f976e29601c
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / actions / ExternalAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2011 - Scilab Enterprises - Clement DAVID
4  *
5  * Copyright (C) 2012 - 2016 - Scilab Enterprises
6  *
7  * This file is hereby licensed under the terms of the GNU GPL v2.0,
8  * pursuant to article 5.3.4 of the CeCILL v.2.1.
9  * This file was originally licensed under the terms of the CeCILL v2.1,
10  * and continues to be available under such terms.
11  * For more information, see the COPYING file which you should have received
12  * along with this program.
13  *
14  */
15 package org.scilab.modules.xcos.actions;
16
17 import java.awt.Cursor;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import java.util.logging.Logger;
21
22 import javax.swing.Action;
23
24 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
25 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
26 import org.scilab.modules.graph.ScilabGraph;
27 import org.scilab.modules.graph.actions.base.DefaultAction;
28 import org.scilab.modules.xcos.JavaController;
29 import org.scilab.modules.xcos.block.BasicBlock;
30 import org.scilab.modules.xcos.graph.XcosDiagram;
31 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
32 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
33 import org.scilab.modules.xcos.utils.BlockPositioning;
34
35 /**
36  * External action
37  */
38 @SuppressWarnings(value = { "serial" })
39 public final class ExternalAction extends DefaultAction {
40     /** Name of the action */
41     public static final String NAME = "";
42     /** Icon name of the action */
43     public static final String SMALL_ICON = "";
44     /** Mnemonic key of the action */
45     public static final int MNEMONIC_KEY = 0;
46     /** Accelerator key for the action */
47     public static final int ACCELERATOR_KEY = 0;
48
49     final String localCommand;
50
51     /**
52      * Default constructor
53      *
54      * @param scilabGraph
55      *            the graph
56      */
57     public ExternalAction(ScilabGraph scilabGraph, String command) {
58         super(scilabGraph);
59
60         this.localCommand = command;
61     }
62
63     /**
64      * Copy constructor
65      *
66      * @param action
67      *            the action to copy
68      * @param graph
69      *            the graph
70      */
71     public ExternalAction(ExternalAction action, XcosDiagram graph) {
72         super(graph);
73
74         putValue(Action.NAME, action.getValue(Action.NAME));
75         localCommand = action.localCommand;
76     }
77
78     static private void reset_view(final XcosDiagram graph, final BasicBlock block, boolean start) {
79         if (block != null) {
80             block.setLocked(start);
81         }
82
83         int cursor;
84         if (start) {
85             cursor = Cursor.WAIT_CURSOR;
86         } else {
87             cursor = Cursor.DEFAULT_CURSOR;
88         }
89
90         graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(cursor));
91         graph.setCellsLocked(start);
92
93         graph.getView().clear(graph.getCurrentRoot(), true, true);
94     }
95
96     @Override
97     public void actionPerformed(ActionEvent e) {
98         final XcosDiagram graph = (XcosDiagram) getGraph(e);
99         final StringBuilder command = new StringBuilder();
100
101         final BasicBlock block;
102         final ActionListener callback;
103
104         /*
105          * Export the whole graph, to update all the sub-graphs on demand.
106          */
107         command.append("scs_m = scicos_new(\"0x").append(Long.toHexString(graph.getUID())).append("\"); \n");
108
109         /*
110          * Then export the selected block
111          */
112         Object cell = graph.getSelectionCell();
113         if (cell instanceof BasicBlock) {
114             block = (BasicBlock) cell;
115             command.append("blk = scicos_new(\"0x").append(Long.toHexString(block.getUID())).append("\"); \n");
116             command.append(localCommand).append('\n');
117             command.append("if exists('blk') then xcosCellCreated(blk); end\n");
118         } else {
119             block = null;
120         }
121
122
123
124         /*
125          * Import the updated block
126          */
127         if (block != null) {
128             callback = new ActionListener() {
129                 @Override
130                 public void actionPerformed(ActionEvent e) {
131                     // Now update the Block
132                     graph.getModel().beginUpdate();
133                     try {
134                         ScicosObjectOwner last = XcosCellFactory.getLastCreated();
135                         if (last != null && last.getUID() != 0l) {
136                             JavaController controller = new JavaController();
137
138                             BasicBlock modified = XcosCellFactory.createBlock(controller, last);
139                             if (modified != null) {
140                                 block.updateBlockSettings(controller, graph, modified);
141                             }
142                         }
143                         BlockPositioning.updateBlockView(graph, block);
144                     } finally {
145                         graph.getModel().endUpdate();
146                         reset_view(graph, block, false);
147                     }
148                 }
149             };
150         } else {
151             callback = (ActionEvent ev) -> {
152                 reset_view(graph, block, false);
153             };
154         }
155
156         try {
157             reset_view(graph, block, true);
158             ScilabInterpreterManagement.asynchronousScilabExec(callback, command.toString());
159         } catch (InterpreterException e1) {
160             reset_view(graph, block, false);
161         }
162     }
163 }