Fix misc. issues with Tab opening
[scilab.git] / scilab / modules / xcos / src / java / org / scilab / modules / xcos / block / actions / BlockParametersAction.java
1 /*
2  * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
3  * Copyright (C) 2009 - DIGITEO - Vincent COUVERT
4  * Copyright (C) 2010 - DIGITEO - Clement DAVID
5  * Copyright (C) 2011-2015 - Scilab Enterprises - Clement DAVID
6  * Copyright (C) 2017 - ESI Group - Clement DAVID
7  *
8  * Copyright (C) 2012 - 2016 - Scilab Enterprises
9  *
10  * This file is hereby licensed under the terms of the GNU GPL v2.0,
11  * pursuant to article 5.3.4 of the CeCILL v.2.1.
12  * This file was originally licensed under the terms of the CeCILL v2.1,
13  * and continues to be available under such terms.
14  * For more information, see the COPYING file which you should have received
15  * along with this program.
16  *
17  */
18
19 package org.scilab.modules.xcos.block.actions;
20
21 import java.awt.Cursor;
22 import java.awt.Toolkit;
23 import java.awt.event.ActionEvent;
24 import java.awt.event.ActionListener;
25 import java.awt.event.KeyEvent;
26
27 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
28 import org.scilab.modules.graph.ScilabGraph;
29 import org.scilab.modules.graph.actions.base.VertexSelectionDependantAction;
30 import org.scilab.modules.gui.menuitem.MenuItem;
31 import org.scilab.modules.xcos.JavaController;
32 import org.scilab.modules.xcos.Kind;
33 import org.scilab.modules.xcos.ObjectProperties;
34 import org.scilab.modules.xcos.Xcos;
35 import org.scilab.modules.xcos.XcosTab;
36 import org.scilab.modules.xcos.block.BasicBlock;
37 import org.scilab.modules.xcos.graph.XcosDiagram;
38 import org.scilab.modules.xcos.graph.model.BlockInterFunction;
39 import org.scilab.modules.xcos.graph.model.ScicosObjectOwner;
40 import org.scilab.modules.xcos.graph.model.XcosCell;
41 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
42 import org.scilab.modules.xcos.io.scicos.ScilabDirectHandler;
43 import org.scilab.modules.xcos.utils.BlockPositioning;
44 import org.scilab.modules.xcos.utils.XcosMessages;
45
46 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
47 import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.asynchronousScilabExec;
48 import org.scilab.modules.xcos.io.HandledElement;
49
50 /**
51  * Open dialog to set block parameters
52  */
53 @SuppressWarnings(value = { "serial" })
54 public class BlockParametersAction extends VertexSelectionDependantAction {
55     /** Name of the action */
56     public static final String NAME = XcosMessages.BLOCK_PARAMETERS;
57     /** Icon name of the action */
58     public static final String SMALL_ICON = "applications-system";
59     /** Mnemonic key of the action */
60     public static final int MNEMONIC_KEY = KeyEvent.VK_B;
61     /** Accelerator key for the action */
62     public static final int ACCELERATOR_KEY = Toolkit.getDefaultToolkit()
63             .getMenuShortcutKeyMask();
64
65     /**
66      * Constructor
67      *
68      * @param scilabGraph
69      *            associated diagram
70      */
71     public BlockParametersAction(ScilabGraph scilabGraph) {
72         super(scilabGraph);
73     }
74
75     /**
76      * Menu for diagram menubar
77      *
78      * @param scilabGraph
79      *            associated diagram
80      * @return the menu
81      */
82     public static MenuItem createMenu(ScilabGraph scilabGraph) {
83         return createMenu(scilabGraph, BlockParametersAction.class);
84     }
85
86     /**
87      * @param e
88      *            parameter
89      * @see org.scilab.modules.graph.actions.base.DefaultAction#actionPerformed(java.awt.event.ActionEvent)
90      */
91     @Override
92     public void actionPerformed(ActionEvent e) {
93         actionPerformed();
94     }
95
96     public void actionPerformed() {
97         XcosDiagram graph = (XcosDiagram) getGraph(null);
98         Object selectedCell = graph.getSelectionCell();
99         if (selectedCell != null && selectedCell instanceof XcosCell) {
100             XcosCell cell = (XcosCell) selectedCell;
101
102             if (cell.getKind() != Kind.BLOCK) {
103                 return;
104             }
105
106             final JavaController controller = new JavaController();
107
108             String[] interfaceFunction = new String[1];
109             controller.getObjectProperty(cell.getUID(), cell.getKind(), ObjectProperties.INTERFACE_FUNCTION, interfaceFunction);
110
111             BlockInterFunction func = XcosCellFactory.lookForInterfunction(interfaceFunction[0]);
112             if (func.equals(BlockInterFunction.SUPER_f)) {
113                 // this is a super-block, open it
114
115                 XcosDiagram sub = Xcos.findDiagram(cell.getUID());
116                 if (sub == null) {
117                     sub = new XcosDiagram(controller, cell.getUID(), cell.getKind(), cell.getId());
118                     XcosCellFactory.insertChildren(controller, sub);
119
120                     ScicosObjectOwner root = Xcos.findRoot(graph);
121                     Xcos.getInstance().addDiagram(root, sub);
122
123                     // propagate the modified status after discarding modification
124                     // done on children insertion
125                     sub.setModified(false);
126                     sub.setModified(Xcos.getInstance().isModified(root));
127
128                     // setup graphical interface
129                     sub.getUndoManager().clear();
130                     sub.installListeners();
131                 }
132
133                 // restore the parent graph tab
134                 final XcosTab tab = XcosTab.get(sub);
135                 if (tab == null) {
136                     XcosTab.restore(sub);
137                 } else {
138                     tab.setCurrent();
139                     tab.requestFocus();
140                 }
141             } else {
142                 BasicBlock block = (BasicBlock) cell;
143                 // prevent to open twice
144                 if (block.isLocked()) {
145                     return;
146                 }
147
148                 graph.setCellsLocked(true);
149                 graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
150                 JavaController.register_view(Xcos.class.getName(), Xcos.getInstance().getXcosView());
151
152                 try {
153
154                     final ActionListener action = new ActionListener() {
155                         @Override
156                         public void actionPerformed(ActionEvent e) {
157
158                             graph.getView().clear(this, true, true);
159
160                             // Now read new Block
161                             graph.getModel().beginUpdate();
162                             try {
163                                 ScicosObjectOwner last = XcosCellFactory.getLastCreated();
164                                 if (last != null && last.getUID() != 0l) {
165                                     BasicBlock modified = XcosCellFactory.createBlock(controller, last);
166                                     if (modified != null) {
167                                         block.updateBlockSettings(controller, graph, modified);
168                                     }
169                                 }
170                                 BlockPositioning.updateBlockView(graph, block);
171                             } finally {
172                                 graph.getModel().endUpdate();
173                                 block.setLocked(false);
174
175                                 graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
176                                 graph.setCellsLocked(false);
177                                 JavaController.unregister_view(Xcos.getInstance().getXcosView());
178                             }
179                         }
180                     };
181
182                     ScilabDirectHandler handler = ScilabDirectHandler.acquire();
183                     try {
184                         handler.writeContext(graph.getContext());
185                     } finally {
186                         handler.release();
187                     }
188
189                     block.setLocked(true);
190                     String blk = buildCall("scicos_new", Long.toHexString(cell.getUID()));
191                     String xcosBlockInterface = buildCall("xcosBlockInterface", interfaceFunction[0].toCharArray(), "set", blk.toCharArray(), ScilabDirectHandler.CONTEXT.toCharArray());
192
193                     asynchronousScilabExec(action, "xcosCellCreated", xcosBlockInterface.toCharArray());
194                 } catch (InterpreterException ex) {
195                     block.setLocked(false);
196
197                     graph.getAsComponent().getGraphControl().setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
198                     graph.setCellsLocked(false);
199                 }
200             }
201         }
202     }
203 }