Xcos: report a user visible error when the interface function is not callable 29/17729/2
Clément DAVID [Tue, 26 Jan 2016 16:15:10 +0000 (17:15 +0100)]
Change-Id: I776102fd5fe03dce40b1dbc7115596a157668185

scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/actions/RegionToSuperblockAction.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/XcosDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/model/XcosCellFactory.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/swing/handler/GraphHandler.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/PaletteBlockCtrl.java

index e8dd3f6..61551fb 100644 (file)
@@ -52,6 +52,7 @@ import java.util.ArrayList;
 import java.util.List;
 import java.util.logging.Level;
 import static java.util.stream.Collectors.toList;
+import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
 import org.scilab.modules.types.ScilabString;
 import org.scilab.modules.xcos.JavaController;
 import org.scilab.modules.xcos.Kind;
@@ -400,6 +401,8 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
             moveToChild(controller, parentGraph, superBlock, brokenLinks, toBeMoved);
 
             BlockPositioning.updateBlockView(parentGraph, superBlock);
+        } catch (ScilabInterpreterManagement.InterpreterException ex) {
+            // Scilab seems to be blocked, just consume the exception at this point
         } finally {
             parentGraph.getModel().endUpdate();
             parentGraph.info(XcosMessages.EMPTY_INFO);
@@ -415,7 +418,7 @@ public class RegionToSuperblockAction extends VertexSelectionDependantAction {
      *            the selected blocks
      * @return the allocated super block (without specific listeners)
      */
-    private SuperBlock allocateSuperBlock(final JavaController controller, final XcosDiagram parentGraph, final Object[] selection) {
+    private SuperBlock allocateSuperBlock(final JavaController controller, final XcosDiagram parentGraph, final Object[] selection) throws ScilabInterpreterManagement.InterpreterException {
         final SuperBlock superBlock = (SuperBlock) XcosCellFactory.createBlock(INTERFUNCTION_NAME);
 
         /*
index 901821a..783f5c0 100644 (file)
@@ -878,7 +878,13 @@ public class XcosDiagram extends ScilabGraph {
             f = BlockInterFunction.SPLIT_f;
         }
 
-        final SplitBlock splitBlock = (SplitBlock) XcosCellFactory.createBlock(f);
+        final SplitBlock splitBlock;
+        try {
+            splitBlock = (SplitBlock) XcosCellFactory.createBlock(f);
+        } catch (InterpreterException ex) {
+            // something goes wrong
+            throw new RuntimeException(ex);
+        }
 
         // snap the center of the split block on the grid
         mxGeometry geom = splitBlock.getGeometry();
index 8d5b397..853aff7 100644 (file)
@@ -242,7 +242,7 @@ public final class XcosCellFactory {
      *            the interface function
      * @return A new instance of a block.
      */
-    public static BasicBlock createBlock(BlockInterFunction func) {
+    public static BasicBlock createBlock(BlockInterFunction func) throws InterpreterException {
         return createBlock(func, func.name());
     }
 
@@ -255,7 +255,7 @@ public final class XcosCellFactory {
      *            the interface function
      * @return A new instance of a block.
      */
-    public static BasicBlock createBlock(String interfaceFunction) {
+    public static BasicBlock createBlock(String interfaceFunction) throws InterpreterException {
         Optional<BlockInterFunction> func = EnumSet.allOf(BlockInterFunction.class).stream().filter(f -> f.name().equals(interfaceFunction)).findFirst();
 
         final BasicBlock block;
@@ -269,36 +269,31 @@ public final class XcosCellFactory {
         return block;
     }
 
-    private static BasicBlock createBlock(BlockInterFunction func, String interfaceFunction) {
+    private static BasicBlock createBlock(BlockInterFunction func, String interfaceFunction) throws InterpreterException {
         return createBlock(new JavaController(), func, interfaceFunction);
     }
 
-    private static BasicBlock createBlock(final JavaController controller, BlockInterFunction func, String interfaceFunction) {
+    private static BasicBlock createBlock(final JavaController controller, BlockInterFunction func, String interfaceFunction) throws InterpreterException {
         BasicBlock block;
-        try {
-            ScicosObjectOwner last;
+        ScicosObjectOwner last;
 
-            if (BlockInterFunction.BASIC_BLOCK.name().equals(interfaceFunction)) {
-                // deliver all the MVC speed for the casual case
-                last = new ScicosObjectOwner(controller.createObject(Kind.BLOCK), Kind.BLOCK);
-            } else {
-                // allocate an empty block that will be filled later
-                synchronousScilabExec("xcosCellCreated(" + interfaceFunction + "(\"define\")); ");
-                last = getLastCreated();
-            }
+        if (BlockInterFunction.BASIC_BLOCK.name().equals(interfaceFunction)) {
+            // deliver all the MVC speed for the casual case
+            last = new ScicosObjectOwner(controller.createObject(Kind.BLOCK), Kind.BLOCK);
+        } else {
+            // allocate an empty block that will be filled later
+            synchronousScilabExec("xcosCellCreated(" + interfaceFunction + "(\"define\")); ");
+            last = getLastCreated();
+        }
 
-            // defensive programming
-            if (last == null) {
-                System.err.println("XcosCellFactory#createBlock : unable to allocate " + interfaceFunction);
-                return null;
-            }
+        // defensive programming
+        if (last == null) {
+            throw new InterpreterException("XcosCellFactory#createBlock : unable to allocate " + interfaceFunction);
+        }
 
-            if (EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(last.getKind())) {
-                block = createBlock(controller, func, interfaceFunction, last.getUID(), last.getKind());
-            } else {
-                block = null;
-            }
-        } catch (InterpreterException e) {
+        if (EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(last.getKind())) {
+            block = createBlock(controller, func, interfaceFunction, last.getUID(), last.getKind());
+        } else {
             block = null;
         }
 
index 87be625..9337d71 100644 (file)
@@ -44,6 +44,7 @@ import com.mxgraph.swing.handler.mxGraphHandler;
 import com.mxgraph.swing.util.mxGraphTransferable;
 import com.mxgraph.util.mxPoint;
 import com.mxgraph.view.mxGraph;
+import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
 
 /**
  * Handle double click operation on the graph.
@@ -144,7 +145,14 @@ public class GraphHandler extends mxGraphHandler {
      */
     private void createTextBlock(MouseEvent e) {
         // allocate
-        final TextBlock textBlock = (TextBlock) XcosCellFactory.createBlock(BlockInterFunction.TEXT_f);
+        final TextBlock textBlock;
+        try {
+            textBlock = (TextBlock) XcosCellFactory.createBlock(BlockInterFunction.TEXT_f);
+        } catch (ScilabInterpreterManagement.InterpreterException ex) {
+            // this is unexpected report : Scilab might not be accessible at all
+            throw new RuntimeException(ex);
+        }
+
 
         // set the position of the block
         final mxPoint pt = graphComponent.getPointForEvent(e);
index 54366d9..d696e8f 100644 (file)
@@ -47,6 +47,7 @@ import org.scilab.modules.xcos.utils.XcosMessages;
 
 import com.mxgraph.swing.handler.mxGraphTransferHandler;
 import com.mxgraph.swing.util.mxGraphTransferable;
+import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement;
 
 /**
  * A palette block is the representation of the block in the palette. All the
@@ -122,11 +123,11 @@ public final class PaletteBlockCtrl {
     public synchronized Transferable getTransferable() throws ScicosFormatException {
         Transferable transfer = transferable.get();
         if (transfer == null) {
-            BasicBlock block = XcosCellFactory.createBlock(model.getName());
-            if (block == null) {
-                if (LOG.isLoggable(Level.FINEST)) {
-                    LOG.finest(String.format(UNABLE_TO_LOAD_BLOCK, getModel().getData().getEvaluatedPath()));
-                }
+            BasicBlock block;
+            try {
+                block = XcosCellFactory.createBlock(model.getName());
+            } catch (ScilabInterpreterManagement.InterpreterException ex) {
+                LOG.finest(String.format(UNABLE_TO_LOAD_BLOCK, model.getName()));
                 getView().setEnabled(false);
                 throw new InvalidDnDOperationException();
             }