a35d3f4a86a026845d6d2e25575af3dec006f42a
[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  * This file must be used under the terms of the CeCILL.
6  * This source file is licensed as described in the file COPYING, which
7  * you should have received as part of this distribution.  The terms
8  * are also available at
9  * http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt
10  *
11  */
12 package org.scilab.modules.xcos.actions;
13
14 import java.awt.event.ActionEvent;
15 import java.awt.event.ActionListener;
16 import java.util.logging.Logger;
17
18 import javax.swing.Action;
19
20 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
21 import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
22 import org.scilab.modules.graph.ScilabGraph;
23 import org.scilab.modules.graph.actions.base.DefaultAction;
24 import org.scilab.modules.xcos.block.BasicBlock;
25 import org.scilab.modules.xcos.graph.XcosDiagram;
26 import org.scilab.modules.xcos.io.scicos.ScicosFormatException;
27 import org.scilab.modules.xcos.io.scicos.ScilabDirectHandler;
28 import org.scilab.modules.xcos.utils.XcosConstants;
29 import org.scilab.modules.xcos.utils.XcosEvent;
30
31 import com.mxgraph.util.mxEventObject;
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     private static final Logger LOG = Logger.getLogger(ExternalAction.class.getName());
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     @Override
79     public void actionPerformed(ActionEvent e) {
80         final XcosDiagram graph = (XcosDiagram) getGraph(e);
81         final ScilabDirectHandler handler = ScilabDirectHandler.acquire();
82         if (handler == null) {
83             return;
84         }
85
86         final BasicBlock block;
87         final ActionListener callback;
88         try {
89
90             /*
91              * First export the selected block
92              */
93             Object cell = graph.getSelectionCell();
94             if (cell instanceof BasicBlock) {
95                 block = (BasicBlock) cell;
96                 handler.writeBlock(block);
97             } else {
98                 block = null;
99             }
100
101             /*
102              * Export the whole diagram
103              */
104             handler.writeDiagram(graph.getRootDiagram());
105
106             /*
107              * Import the updated block
108              */
109             if (block != null) {
110                 callback = new ActionListener() {
111                     @Override
112                     public void actionPerformed(ActionEvent e) {
113                         try {
114
115                             final BasicBlock modifiedBlock = handler.readBlock();
116                             block.updateBlockSettings(modifiedBlock);
117
118                             graph.fireEvent(new mxEventObject(XcosEvent.ADD_PORTS, XcosConstants.EVENT_BLOCK_UPDATED, block));
119                         } catch (ScicosFormatException e1) {
120                             LOG.severe(e1.getMessage());
121                         } finally {
122                             handler.release();
123                         }
124                     }
125                 };
126             } else {
127                 callback = new ActionListener() {
128                     @Override
129                     public void actionPerformed(ActionEvent e) {
130                         handler.release();
131                     }
132                 };
133             }
134
135             ScilabInterpreterManagement.asynchronousScilabExec(callback, localCommand);
136         } catch (InterpreterException e2) {
137             LOG.warning(e2.toString());
138
139             handler.release();
140         }
141     }
142 }