2 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3 * Copyright (C) 2011 - Scilab Enterprises - Clement DAVID
5 * Copyright (C) 2012 - 2016 - Scilab Enterprises
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.
15 package org.scilab.modules.xcos.actions;
17 import java.awt.Cursor;
18 import java.awt.event.ActionEvent;
19 import java.awt.event.ActionListener;
20 import javax.swing.Action;
22 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
23 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
24 import org.scilab.modules.graph.ScilabGraph;
25 import org.scilab.modules.graph.actions.base.DefaultAction;
26 import org.scilab.modules.xcos.JavaController;
27 import org.scilab.modules.xcos.block.BasicBlock;
28 import org.scilab.modules.xcos.graph.XcosDiagram;
29 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
30 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
31 import org.scilab.modules.xcos.utils.BlockPositioning;
36 @SuppressWarnings(value = { "serial" })
37 public final class ExternalAction extends DefaultAction {
38 /** Name of the action */
39 public static final String NAME = "";
40 /** Icon name of the action */
41 public static final String SMALL_ICON = "";
42 /** Mnemonic key of the action */
43 public static final int MNEMONIC_KEY = 0;
44 /** Accelerator key for the action */
45 public static final int ACCELERATOR_KEY = 0;
47 final String localCommand;
55 public ExternalAction(ScilabGraph scilabGraph, String command) {
58 this.localCommand = command;
69 public ExternalAction(ExternalAction action, XcosDiagram graph) {
72 putValue(Action.NAME, action.getValue(Action.NAME));
73 localCommand = action.localCommand;
76 static private void reset_view(final XcosDiagram graph, final BasicBlock block, boolean start) {
78 block.setLocked(start);
83 cursor = Cursor.WAIT_CURSOR;
85 cursor = Cursor.DEFAULT_CURSOR;
88 graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(cursor));
89 graph.setCellsLocked(start);
91 graph.getView().clear(graph.getCurrentRoot(), true, true);
95 public void actionPerformed(ActionEvent e) {
96 final XcosDiagram graph = (XcosDiagram) getGraph(e);
97 final StringBuilder command = new StringBuilder();
99 final BasicBlock block;
100 final ActionListener callback;
103 * Export the whole graph, to update all the sub-graphs on demand.
105 command.append("scs_m = scicos_new(\"0x").append(Long.toHexString(graph.getUID())).append("\"); \n");
108 * Then export the selected block
110 Object cell = graph.getSelectionCell();
111 if (cell instanceof BasicBlock) {
112 block = (BasicBlock) cell;
113 command.append("blk = scicos_new(\"0x").append(Long.toHexString(block.getUID())).append("\"); \n");
114 command.append(localCommand).append('\n');
115 command.append("if exists('blk') then xcosCellCreated(blk); end\n");
123 * Import the updated block
126 callback = new ActionListener() {
128 public void actionPerformed(ActionEvent e) {
129 // Now update the Block
130 graph.getModel().beginUpdate();
132 ScicosObjectOwner last = XcosCellFactory.getLastCreated();
133 if (last != null && last.getUID() != 0l) {
134 JavaController controller = new JavaController();
136 BasicBlock modified = XcosCellFactory.createBlock(controller, last);
137 if (modified != null) {
138 block.updateBlockSettings(controller, graph, modified);
141 BlockPositioning.updateBlockView(graph, block);
143 graph.getModel().endUpdate();
144 reset_view(graph, block, false);
149 callback = (ActionEvent ev) -> {
150 reset_view(graph, block, false);
155 reset_view(graph, block, true);
156 ScilabInterpreterManagement.asynchronousScilabExec(callback, command.toString());
157 } catch (InterpreterException e1) {
158 reset_view(graph, block, false);