Xcos GUI: split creation now works 57/17257/5
Clément DAVID [Thu, 24 Sep 2015 15:48:44 +0000 (17:48 +0200)]
Change-Id: Ibe82bdb6fa489b2cbf7815f25a6d66dca5b52248

15 files changed:
scilab/modules/xcos/src/java/org/scilab/modules/xcos/block/TextBlock.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/XcosDiagram.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/graph/model/XcosCell.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
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/BasicPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/command/CommandPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/control/ControlPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/input/ExplicitInputPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/input/ImplicitInputPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/input/InputPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/output/ExplicitOutputPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/output/ImplicitOutputPort.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/port/output/OutputPort.java

index a8f2a2b..c6cdfc3 100644 (file)
@@ -33,7 +33,6 @@ public final class TextBlock extends BasicBlock {
     public TextBlock(long uid) {
         super(uid, Kind.ANNOTATION);
 
-        setStyle("TEXT_f");
         setValue(XcosMessages.DOTS);
     }
 
index 32f62ab..deffa5b 100644 (file)
@@ -852,7 +852,19 @@ public class XcosDiagram extends ScilabGraph {
         final BasicPort linkSource = (BasicPort) link.getSource();
         final BasicPort linkTarget = (BasicPort) link.getTarget();
 
-        final SplitBlock splitBlock = (SplitBlock) XcosCellFactory.createBlock(BlockInterFunction.SPLIT_f);
+        /*
+         * Select the right split accordingly to the link klass
+         */
+        BlockInterFunction f;
+        if (link instanceof CommandControlLink) {
+            f = BlockInterFunction.CLKSPLIT_f;
+        } else if (link instanceof ImplicitLink) {
+            f = BlockInterFunction.IMPSPLIT_f;
+        } else {
+            f = BlockInterFunction.SPLIT_f;
+        }
+
+        final SplitBlock splitBlock = (SplitBlock) XcosCellFactory.createBlock(f);
 
         getModel().beginUpdate();
         try {
@@ -862,8 +874,6 @@ public class XcosDiagram extends ScilabGraph {
                 orig = new mxPoint();
             }
 
-            // FIXME create the port is needed
-            // splitBlock.addConnection(linkSource);
 
             addCell(splitBlock);
             // force resize and align on the grid
index be612b0..d6b4ab8 100644 (file)
@@ -183,43 +183,30 @@ public class XcosCell extends ScilabGraphUniqueObject {
         }
     }
 
-    /* (non-Javadoc)
-     * @see com.mxgraph.model.mxCell#setSource(com.mxgraph.model.mxICell)
-     */
     @Override
-    public void setSource(mxICell source) {
-        super.setSource(source);
+    public mxICell setTerminal(mxICell terminal, boolean isSource) {
+        mxICell cell = super.setTerminal(terminal, isSource);
 
-        // a source of an XcosCell is always another XcosCell
-        XcosCell s = (XcosCell) source;
-        JavaController controller = new JavaController();
-        switch (getKind()) {
-            case LINK:
-                controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SOURCE_PORT, s.getUID());
-                break;
-            default:
-                break;
+        if (cell == null) {
+            return cell;
         }
 
-    }
-
-    /* (non-Javadoc)
-     * @see com.mxgraph.model.mxCell#setTarget(com.mxgraph.model.mxICell)
-     */
-    @Override
-    public void setTarget(mxICell target) {
-        super.setTarget(target);
-
-        // a target of an XcosCell is always another XcosCell
-        XcosCell t = (XcosCell) target;
+        // a terminal of an XcosCell is always another XcosCell
+        XcosCell t = (XcosCell) cell;
         JavaController controller = new JavaController();
         switch (getKind()) {
             case LINK:
-                controller.setObjectProperty(getUID(), getKind(), ObjectProperties.DESTINATION_PORT, t.getUID());
+                if (isSource) {
+                    controller.setObjectProperty(getUID(), getKind(), ObjectProperties.SOURCE_PORT, t.getUID());
+                } else {
+                    controller.setObjectProperty(getUID(), getKind(), ObjectProperties.DESTINATION_PORT, t.getUID());
+                }
                 break;
             default:
                 break;
         }
+
+        return cell;
     }
 
     /* (non-Javadoc)
index c521c92..adcf43a 100644 (file)
 
 package org.scilab.modules.xcos.graph.model;
 
+import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
+import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.synchronousScilabExec;
+
 import java.lang.reflect.InvocationTargetException;
 import java.util.Arrays;
 import java.util.EnumSet;
+import java.util.Optional;
 
+import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
 import org.scilab.modules.xcos.JavaController;
 import org.scilab.modules.xcos.Kind;
 import org.scilab.modules.xcos.ObjectProperties;
+import org.scilab.modules.xcos.UpdateStatus;
 import org.scilab.modules.xcos.VectorOfScicosID;
+import org.scilab.modules.xcos.Xcos;
+import org.scilab.modules.xcos.XcosView;
+import org.scilab.modules.xcos.XcosViewListener;
 import org.scilab.modules.xcos.block.AfficheBlock;
 import org.scilab.modules.xcos.block.BasicBlock;
 import org.scilab.modules.xcos.block.SplitBlock;
@@ -38,6 +47,7 @@ import org.scilab.modules.xcos.block.positionning.RoundBlock;
 import org.scilab.modules.xcos.block.positionning.Summation;
 import org.scilab.modules.xcos.block.positionning.VoltageSensorBlock;
 import org.scilab.modules.xcos.graph.XcosDiagram;
+import org.scilab.modules.xcos.io.scicos.ScilabDirectHandler;
 import org.scilab.modules.xcos.port.BasicPort;
 import org.scilab.modules.xcos.port.command.CommandPort;
 import org.scilab.modules.xcos.port.control.ControlPort;
@@ -145,33 +155,71 @@ public final class XcosCellFactory {
      * Block and Annotation management
      */
 
+
+
     /**
-     * Instantiate a new block with the specified UID value.
+     * Instantiate a new block with the specified UID value and interface function
      *
      * @param uid
      *            The associated UID value
+     * @param interfaceFunction the interface function
      * @return A new instance of a block.
      */
-    public static BasicBlock createBlock(long uid) {
-        BasicBlock block = null;
+    public static BasicBlock createBlock(String interfaceFunction) {
+        Optional<BlockInterFunction> func = EnumSet.allOf(BlockInterFunction.class).stream()
+                                            .filter(f -> f.name().equals(interfaceFunction))
+                                            .findFirst();
+
+        final BasicBlock block;
+        if (func.isPresent()) {
+            block = createBlock(func.get());
+        } else {
+            block = createBlock(BlockInterFunction.BASIC_BLOCK);
+        }
+        block.setStyle(interfaceFunction);
 
-        JavaController controller = new JavaController();
-        String[] interfaceFunction = new String[1];
-        controller.getObjectProperty(uid, Kind.BLOCK, ObjectProperties.INTERFACE_FUNCTION, interfaceFunction);
+        return block;
+    }
 
-        for (BlockInterFunction func : BlockInterFunction.values()) {
-            if (func.name().equals(interfaceFunction)) {
-                block = createBlock(controller, func, uid);
-                break;
-            }
+
+
+    private static class BlockLoadedListener extends XcosViewListener {
+        private long uid;
+
+        public BlockLoadedListener() {
+            uid = 0;
         }
 
-        if (block == null) {
-            block = createBlock(controller, BlockInterFunction.BASIC_BLOCK, uid);
-            block.setStyle(interfaceFunction[0]);
+        public long getUID() {
+            return uid;
         }
 
-        return block;
+        /**
+         * When a unique block is created then store it for later use.
+         */
+        @Override
+        public void objectCreated(long uid, Kind kind) {
+            if (!EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(kind)) {
+                return;
+            }
+
+            this.uid = uid;
+        }
+
+        /**
+         * When a composite block is created we track the PARENT_BLOCK / CHILDREN association to store the parent.
+         */
+        @Override
+        public void propertyUpdated(long uid, Kind kind, ObjectProperties property, UpdateStatus status) {
+            if (status != UpdateStatus.SUCCESS || property != ObjectProperties.CHILDREN) {
+                return;
+            }
+            if (!EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(kind)) {
+                return;
+            }
+
+            this.uid = uid;
+        }
     }
 
     /**
@@ -182,10 +230,33 @@ public final class XcosCellFactory {
      * @return A new instance of a block.
      */
     public static BasicBlock createBlock(BlockInterFunction func) {
-        JavaController controller = new JavaController();
+        return createBlock(new JavaController(), func);
+    }
 
-        long uid = controller.createObject(Kind.BLOCK);
-        return createBlock(controller, func, uid);
+    /**
+     * Instantiate a new block with the specified interface function.
+     *
+     * @param func
+     *            the interface function
+     * @return A new instance of a block.
+     */
+    public static BasicBlock createBlock(final JavaController controller, BlockInterFunction func) {
+        XcosView view = (XcosView) JavaController.lookup_view(Xcos.class.getSimpleName());
+
+        BlockLoadedListener blockLoaded = new BlockLoadedListener();
+        view.addXcosViewListener(blockLoaded, EnumSet.allOf(Kind.class), true, EnumSet.of(ObjectProperties.CHILDREN));
+
+        BasicBlock block;
+        try {
+            synchronousScilabExec(ScilabDirectHandler.BLK + " = " + buildCall(func.name(), "define"));
+            block = XcosCellFactory.createBlock(controller, func, blockLoaded.getUID());
+        } catch (InterpreterException e1) {
+            block = null;
+        } finally {
+            view.removeXcosViewListener(blockLoaded);
+        }
+
+        return block;
     }
 
     /**
@@ -196,7 +267,7 @@ public final class XcosCellFactory {
      * @param uid the allocated uid
      * @return A new instance of a block.
      */
-    public static BasicBlock createBlock(final JavaController controller, BlockInterFunction func, long uid) {
+    private static BasicBlock createBlock(final JavaController controller, BlockInterFunction func, long uid) {
         BasicBlock block = null;
         try {
             block = func.getKlass().getConstructor(Long.TYPE).newInstance(uid);
index b0b2c61..4d46bf2 100644 (file)
@@ -138,8 +138,7 @@ public class GraphHandler extends mxGraphHandler {
      */
     private void createTextBlock(MouseEvent e) {
         // allocate
-        JavaController controller = new JavaController();
-        final TextBlock textBlock = (TextBlock) XcosCellFactory.createBlock(controller, BlockInterFunction.TEXT_f, controller.createObject(Kind.ANNOTATION));
+        final TextBlock textBlock = (TextBlock) XcosCellFactory.createBlock(BlockInterFunction.TEXT_f);
 
         // set the position of the block
         final mxPoint pt = graphComponent.getPointForEvent(e);
index 8721196..c172c18 100644 (file)
@@ -12,9 +12,6 @@
 
 package org.scilab.modules.xcos.palette;
 
-import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.buildCall;
-import static org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.synchronousScilabExec;
-
 import java.awt.Point;
 import java.awt.datatransfer.Transferable;
 import java.awt.dnd.DnDConstants;
@@ -24,26 +21,18 @@ import java.awt.dnd.DragSource;
 import java.awt.dnd.InvalidDnDOperationException;
 import java.awt.event.MouseListener;
 import java.lang.ref.WeakReference;
-import java.util.EnumSet;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
-import org.scilab.modules.action_binding.highlevel.ScilabInterpreterManagement.InterpreterException;
 import org.scilab.modules.gui.messagebox.ScilabModalDialog;
 import org.scilab.modules.gui.messagebox.ScilabModalDialog.IconType;
 import org.scilab.modules.localization.Messages;
 import org.scilab.modules.xcos.JavaController;
 import org.scilab.modules.xcos.Kind;
-import org.scilab.modules.xcos.ObjectProperties;
-import org.scilab.modules.xcos.UpdateStatus;
-import org.scilab.modules.xcos.Xcos;
-import org.scilab.modules.xcos.XcosView;
-import org.scilab.modules.xcos.XcosViewListener;
 import org.scilab.modules.xcos.block.BasicBlock;
 import org.scilab.modules.xcos.graph.XcosDiagram;
 import org.scilab.modules.xcos.graph.model.XcosCellFactory;
 import org.scilab.modules.xcos.io.scicos.ScicosFormatException;
-import org.scilab.modules.xcos.io.scicos.ScilabDirectHandler;
 import org.scilab.modules.xcos.palette.listener.PaletteBlockMouseListener;
 import org.scilab.modules.xcos.palette.model.PaletteBlock;
 import org.scilab.modules.xcos.palette.view.PaletteBlockView;
@@ -128,13 +117,7 @@ public final class PaletteBlockCtrl {
     public synchronized Transferable getTransferable() throws ScicosFormatException {
         Transferable transfer = transferable.get();
         if (transfer == null) {
-            BasicBlock block;
-            try {
-                block = loadBlock();
-            } catch (ScicosFormatException ex) {
-                getView().setEnabled(false);
-                throw ex;
-            }
+            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()));
@@ -163,71 +146,6 @@ public final class PaletteBlockCtrl {
         return transfer;
     }
 
-    private static class BlockLoadedListener extends XcosViewListener {
-        private long uid;
-
-        public BlockLoadedListener() {
-            uid = 0;
-        }
-
-        public long getUID() {
-            return uid;
-        }
-
-        /**
-         * When a unique block is created then store it for later use.
-         */
-        @Override
-        public void objectCreated(long uid, Kind kind) {
-            if (!EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(kind)) {
-                return;
-            }
-
-            this.uid = uid;
-        }
-
-        /**
-         * When a composite block is created we track the PARENT_BLOCK / CHILDREN association to store the parent.
-         */
-        @Override
-        public void propertyUpdated(long uid, Kind kind, ObjectProperties property, UpdateStatus status) {
-            if (status != UpdateStatus.SUCCESS || property != ObjectProperties.CHILDREN) {
-                return;
-            }
-            if (!EnumSet.of(Kind.BLOCK, Kind.ANNOTATION).contains(kind)) {
-                return;
-            }
-
-            this.uid = uid;
-        }
-    }
-
-
-    /**
-     * @return the loaded block.
-     * @throws ScicosFormatException
-     *             on error
-     */
-    private BasicBlock loadBlock() throws ScicosFormatException {
-        XcosView view = (XcosView) JavaController.lookup_view(Xcos.class.getSimpleName());
-
-        BlockLoadedListener blockLoaded = new BlockLoadedListener();
-        view.addXcosViewListener(blockLoaded, EnumSet.allOf(Kind.class), true, EnumSet.of(ObjectProperties.CHILDREN));
-
-        BasicBlock block;
-        try {
-            synchronousScilabExec(ScilabDirectHandler.BLK + " = " + buildCall(model.getName(), "define"));
-            block = XcosCellFactory.createBlock(blockLoaded.getUID());
-        } catch (InterpreterException e1) {
-            LOG.severe(e1.toString());
-            block = null;
-        } finally {
-            view.removeXcosViewListener(blockLoaded);
-        }
-
-        return block;
-    }
-
     /**
      * This function load the block and render it on the hidden diagram. This
      * can be time-consuming and each block should be cached on the caller when
index a654844..37e7ceb 100644 (file)
@@ -117,10 +117,13 @@ public abstract class BasicPort extends XcosCell {
      * @param style
      *            Value to be set as a Style and as TypeName
      */
-    public BasicPort(long uid) {
+    public BasicPort(long uid, String style, Orientation orientation) {
         super(uid, Kind.PORT);
+
         setVertex(true);
+        setStyle(style);
         setGeometry(new mxGeometry(0, 0, DEFAULT_PORTSIZE, DEFAULT_PORTSIZE));
+        setOrientation(orientation);
     }
 
     /**
index 4dcf42f..ad2ab87 100644 (file)
@@ -29,10 +29,7 @@ public class CommandPort extends BasicPort {
 
     /** Default constructor */
     public CommandPort(long uid) {
-        super(uid);
-
-        setStyle("CommandPort");
-        setOrientation(Orientation.SOUTH);
+        super(uid, "CommandPort", Orientation.SOUTH);
     }
 
     @Override
index 7908c37..0141025 100644 (file)
@@ -29,10 +29,7 @@ public class ControlPort extends BasicPort {
 
     /** Default constructor */
     public ControlPort(long uid) {
-        super(uid);
-
-        setStyle("ControlPort");
-        setOrientation(Orientation.NORTH);
+        super(uid, "ControlPort", Orientation.NORTH);
     }
 
     /**
index 2164675..6e87c58 100644 (file)
@@ -22,9 +22,7 @@ public class ExplicitInputPort extends InputPort {
 
     /** Default constructor */
     public ExplicitInputPort(long uid) {
-        super(uid);
-
-        setStyle("ExplicitInputPort");
+        super(uid, "ExplicitInputPort");
     }
 
     /** @return always Type.EXPLICIT */
index 1bd226f..4f2e5f9 100644 (file)
@@ -27,9 +27,7 @@ public class ImplicitInputPort extends InputPort {
 
     /** Default constructor */
     public ImplicitInputPort(long uid) {
-        super(uid);
-
-        setStyle("ImplicitInputPort");
+        super(uid, "ImplicitInputPort");
     }
 
     /** @return always Type.IMPLICIT */
index 2b22366..ef2fb64 100644 (file)
@@ -32,8 +32,7 @@ public abstract class InputPort extends BasicPort {
      *            The string port name ("ExplicitInputPort" or
      *            "ImplicitInputPort")
      */
-    protected InputPort(long uid) {
-        super(uid);
-        setOrientation(Orientation.WEST);
+    protected InputPort(long uid, String style) {
+        super(uid, style, Orientation.WEST);
     }
 }
index 482a5ef..a55a970 100644 (file)
@@ -22,9 +22,7 @@ public class ExplicitOutputPort extends OutputPort {
 
     /** Default constructor */
     public ExplicitOutputPort(long uid) {
-        super(uid);
-
-        setStyle("ExplicitOutputPort");
+        super(uid, "ExplicitOutputPort");
     }
 
     /**
index 582ba43..ac04afa 100644 (file)
@@ -27,9 +27,7 @@ public class ImplicitOutputPort extends OutputPort {
 
     /** Default constructor */
     public ImplicitOutputPort(long uid) {
-        super(uid);
-
-        setStyle("ExplicitOutputPort");
+        super(uid, "ImplicitOutputPort");
     }
 
     /** @return always Type.IMPLICIT */
index dbdfb4b..89960b4 100644 (file)
@@ -32,8 +32,7 @@ public abstract class OutputPort extends BasicPort {
      *            The string port name ("ExplicitOutputPort" or
      *            "ImplicitOutputPort")
      */
-    public OutputPort(long uid) {
-        super(uid);
-        setOrientation(Orientation.EAST);
+    public OutputPort(long uid, String style) {
+        super(uid, style, Orientation.EAST);
     }
 }