Palette browser - improving DnD 98/18398/2
Marcos CARDINOT [Sun, 31 Jul 2016 01:59:31 +0000 (02:59 +0100)]
Change-Id: I3069757d6abcf7d73db2e4e98fd5368323281819

scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/PaletteBlockCtrl.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/PaletteCtrl.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/listener/PaletteBlockKeyListener.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/palette/listener/PaletteBlockMouseListener.java
scilab/modules/xcos/src/java/org/scilab/modules/xcos/utils/BlockPositioning.java

index 6a2b718..3d4c8a0 100644 (file)
@@ -33,7 +33,6 @@ import org.scilab.modules.xcos.palette.view.PaletteBlockView.StatusUI;
 import org.scilab.modules.xcos.palette.view.PaletteManagerView;
 
 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.
@@ -74,7 +73,7 @@ public final class PaletteBlockCtrl {
 
         DragSource dragSource = DragSource.getDefaultDragSource();
         dragSource.createDefaultDragGestureRecognizer(this.getView(),
-                DnDConstants.ACTION_COPY_OR_MOVE, DRAG_LISTENER);
+                DnDConstants.ACTION_COPY, DRAG_LISTENER);
     }
 
     /**
index 403d236..28ef9c0 100644 (file)
@@ -33,6 +33,7 @@ import org.scilab.modules.xcos.utils.XcosConstants;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
 import com.mxgraph.swing.handler.mxGraphTransferHandler;
+import com.mxgraph.swing.util.mxGraphTransferable;
 
 /**
  * All the operations which are used to render, load and put
@@ -104,14 +105,13 @@ public final class PaletteCtrl {
     }
 
     /**
-     * Add all the selected blocks to a XcosDiagram.
-     * @param diagram XcosDiagram
-     * @return true if some block was added
+     * Get the selected palette blocks
+     * @return blocks that have been added to the XcosDiagram
      */
-    public boolean addSelectedBlocks(final XcosDiagram diagram) {
+    public Object[] getSelectedBlocks() {
         int row = 0;
         int column = 0;
-        boolean hasNewBlocks = false;
+        List<BasicBlock> basicBlocks = new ArrayList<BasicBlock>();
         for (PaletteBlockCtrl blockCtrl : BLOCKS) {
             if (!blockCtrl.isSelected()) {
                 continue;
@@ -135,7 +135,7 @@ public final class PaletteCtrl {
             // add to the 'recently used blocks' panel
             PaletteManagerView.get().getPanel().addRecentltyUsedBlock(blockCtrl.getModel());
 
-            // Render it and export it
+            // render and export it
             final double margin = 15.0;
             basicBlock.getGeometry().setX(margin + BLOCK_POSITION.width * column);
             basicBlock.getGeometry().setY(margin + BLOCK_POSITION.height * row);
@@ -146,12 +146,20 @@ public final class PaletteCtrl {
                 ++row;
             }
 
-            diagram.addCell(basicBlock);
-            diagram.addSelectionCell(basicBlock);
-            BlockPositioning.updateBlockView(internalGraph, basicBlock);
-            hasNewBlocks = true;
+            basicBlocks.add(basicBlock);
         }
-        return hasNewBlocks;
+
+        int size = basicBlocks.size();
+        if (size == 0) {
+            return null;
+        }
+
+        // convert to Object[]
+        Object[] ret = new Object[size];
+        for (int i = 0; i < size; i++) {
+            ret[i] = basicBlocks.get(i);
+        }
+        return ret;
     }
 
     /**
@@ -160,12 +168,18 @@ public final class PaletteCtrl {
      */
     public synchronized Transferable getTransferable() {
         Transferable transfer = null;
-        internalGraph.removeCells();
-        if (addSelectedBlocks(internalGraph)) {
-            mxGraphTransferHandler handler = (mxGraphTransferHandler) internalGraph.getAsComponent().getTransferHandler();
-            transfer = handler.createTransferable(internalGraph.getAsComponent());
-            internalGraph.removeCells();
+        Object[] cells = getSelectedBlocks();
+        if (cells == null) {
+            return null;
         }
+
+        BlockPositioning.updatePortsPosition(internalGraph, cells);
+        internalGraph.addCells(cells);
+
+        mxGraphTransferHandler handler = (mxGraphTransferHandler) internalGraph.getAsComponent().getTransferHandler();
+        transfer = new mxGraphTransferable(cells, internalGraph.getPaintBounds(cells),
+                handler.createTransferableImage(internalGraph.getAsComponent(), cells));
+
         return transfer;
     }
 }
index 4605512..21338b7 100644 (file)
@@ -24,6 +24,7 @@ import org.scilab.modules.xcos.palette.view.PaletteBlockView;
 import org.scilab.modules.xcos.palette.view.PaletteBlockView.StatusUI;
 import org.scilab.modules.xcos.palette.view.PaletteManagerPanel;
 import org.scilab.modules.xcos.palette.view.PaletteView;
+import org.scilab.modules.xcos.utils.BlockPositioning;
 import org.scilab.modules.xcos.utils.XcosConstants;
 import org.scilab.modules.xcos.utils.XcosConstants.PaletteBlockSize;
 
@@ -52,7 +53,11 @@ public final class PaletteBlockKeyListener implements KeyListener {
             assert size != 0;
 
             final XcosDiagram theDiagram = allDiagrams.get(size - 1);
-            control.getPaletteCtrl().addSelectedBlocks(theDiagram);
+            Object[] cells = control.getPaletteCtrl().getSelectedBlocks();
+            if (cells != null) {
+                BlockPositioning.updatePortsPosition(theDiagram, cells);
+                theDiagram.addCells(cells);
+            }
 
         /** deselects blocks and loses focus **/
         } else if (e.getKeyCode() == KeyEvent.VK_ESCAPE || e.getKeyCode() == KeyEvent.VK_TAB) {
index 3d4d980..705a723 100644 (file)
@@ -41,6 +41,7 @@ import org.scilab.modules.xcos.palette.PaletteBlockCtrl;
 import org.scilab.modules.xcos.palette.PaletteCtrl;
 import org.scilab.modules.xcos.palette.view.PaletteBlockView;
 import org.scilab.modules.xcos.palette.view.PaletteBlockView.StatusUI;
+import org.scilab.modules.xcos.utils.BlockPositioning;
 import org.scilab.modules.xcos.utils.XcosMessages;
 
 /**
@@ -87,7 +88,11 @@ public final class PaletteBlockMouseListener implements MouseListener {
 
                 @Override
                 public void callBack() {
-                    ctrl.getPaletteCtrl().addSelectedBlocks(theDiagram);
+                    Object[] cells = ctrl.getPaletteCtrl().getSelectedBlocks();
+                    if (cells != null) {
+                        BlockPositioning.updatePortsPosition(theDiagram, cells);
+                        theDiagram.addCells(cells);
+                    }
                 }
             });
             menu.add(addTo);
@@ -105,7 +110,11 @@ public final class PaletteBlockMouseListener implements MouseListener {
 
                     @Override
                     public void callBack() {
-                        ctrl.getPaletteCtrl().addSelectedBlocks(theDiagram);
+                        Object[] cells = ctrl.getPaletteCtrl().getSelectedBlocks();
+                        if (cells != null) {
+                            BlockPositioning.updatePortsPosition(theDiagram, cells);
+                            theDiagram.addCells(cells);
+                        }
                     }
                 });
                 addTo.add(diagram);
@@ -168,10 +177,14 @@ public final class PaletteBlockMouseListener implements MouseListener {
                 blockCtrl.setSelected(!isSelected); // toggle
             } else if (e.getClickCount() == 2) {
                 blockCtrl.setSelected(true);
-                // add the current block to the most recent diagram
+                // add the selected blocks into the most recent diagram
                 final List<XcosDiagram> allDiagrams = getOpenedDiagrams();
                 XcosDiagram theDiagram = allDiagrams.get(allDiagrams.size() - 1);
-                blockCtrl.getPaletteCtrl().addSelectedBlocks(theDiagram);
+                Object[] cells = blockCtrl.getPaletteCtrl().getSelectedBlocks();
+                if (cells != null) {
+                    BlockPositioning.updatePortsPosition(theDiagram, cells);
+                    theDiagram.addCells(cells);
+                }
             }
         } else {
             return;
index 9e6aa86..94133aa 100644 (file)
@@ -423,6 +423,20 @@ public final class BlockPositioning {
     }
 
     /**
+     * Update the geometry of the block's ports.
+     * @param diag XcosDiagram
+     * @param blocks array with several blocks
+     */
+    public static void updatePortsPosition(final XcosDiagram diag, Object[] blocks) {
+        diag.getModel().beginUpdate();
+        for (Object b : blocks) {
+            BasicBlock block = (BasicBlock) b;
+            updatePortsPosition(diag, block);
+        }
+        diag.getModel().endUpdate();
+    }
+
+    /**
      * Flip a block (horizontal inversion).
      *
      * @param block