Xcos src: Java cleanup
[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 javax.swing.Action;
21
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;
32
33 /**
34  * External action
35  */
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;
46
47     final String localCommand;
48
49     /**
50      * Default constructor
51      *
52      * @param scilabGraph
53      *            the graph
54      */
55     public ExternalAction(ScilabGraph scilabGraph, String command) {
56         super(scilabGraph);
57
58         this.localCommand = command;
59     }
60
61     /**
62      * Copy constructor
63      *
64      * @param action
65      *            the action to copy
66      * @param graph
67      *            the graph
68      */
69     public ExternalAction(ExternalAction action, XcosDiagram graph) {
70         super(graph);
71
72         putValue(Action.NAME, action.getValue(Action.NAME));
73         localCommand = action.localCommand;
74     }
75
76     static private void reset_view(final XcosDiagram graph, final BasicBlock block, boolean start) {
77         if (block != null) {
78             block.setLocked(start);
79         }
80
81         int cursor;
82         if (start) {
83             cursor = Cursor.WAIT_CURSOR;
84         } else {
85             cursor = Cursor.DEFAULT_CURSOR;
86         }
87
88         graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(cursor));
89         graph.setCellsLocked(start);
90
91         graph.getView().clear(graph.getCurrentRoot(), true, true);
92     }
93
94     @Override
95     public void actionPerformed(ActionEvent e) {
96         final XcosDiagram graph = (XcosDiagram) getGraph(e);
97         final StringBuilder command = new StringBuilder();
98
99         final BasicBlock block;
100         final ActionListener callback;
101
102         /*
103          * Export the whole graph, to update all the sub-graphs on demand.
104          */
105         command.append("scs_m = scicos_new(\"0x").append(Long.toHexString(graph.getUID())).append("\"); \n");
106
107         /*
108          * Then export the selected block
109          */
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");
116         } else {
117             block = null;
118         }
119
120
121
122         /*
123          * Import the updated block
124          */
125         if (block != null) {
126             callback = new ActionListener() {
127                 @Override
128                 public void actionPerformed(ActionEvent e) {
129                     // Now update the Block
130                     graph.getModel().beginUpdate();
131                     try {
132                         ScicosObjectOwner last = XcosCellFactory.getLastCreated();
133                         if (last != null && last.getUID() != 0l) {
134                             JavaController controller = new JavaController();
135
136                             BasicBlock modified = XcosCellFactory.createBlock(controller, last);
137                             if (modified != null) {
138                                 block.updateBlockSettings(controller, graph, modified);
139                             }
140                         }
141                         BlockPositioning.updateBlockView(graph, block);
142                     } finally {
143                         graph.getModel().endUpdate();
144                         reset_view(graph, block, false);
145                     }
146                 }
147             };
148         } else {
149             callback = (ActionEvent ev) -> {
150                 reset_view(graph, block, false);
151             };
152         }
153
154         try {
155             reset_view(graph, block, true);
156             ScilabInterpreterManagement.asynchronousScilabExec(callback, command.toString());
157         } catch (InterpreterException e1) {
158             reset_view(graph, block, false);
159         }
160     }
161 }