Search in sources :

Example 1 with Position

use of com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position in project intellij-community by JetBrains.

the class RunConfigurable method getAvailableDropPosition.

@Nullable
Trinity<Integer, Integer, RowsDnDSupport.RefinedDropSupport.Position> getAvailableDropPosition(int direction) {
    int[] rows = myTree.getSelectionRows();
    if (rows == null || rows.length != 1) {
        return null;
    }
    int oldIndex = rows[0];
    int newIndex = oldIndex + direction;
    if (!getKind((DefaultMutableTreeNode) myTree.getPathForRow(oldIndex).getLastPathComponent()).supportsDnD())
        return null;
    while (newIndex > 0 && newIndex < myTree.getRowCount()) {
        TreePath targetPath = myTree.getPathForRow(newIndex);
        boolean allowInto = getKind((DefaultMutableTreeNode) targetPath.getLastPathComponent()) == FOLDER && !myTree.isExpanded(targetPath);
        RowsDnDSupport.RefinedDropSupport.Position position = allowInto && myTreeModel.isDropInto(myTree, oldIndex, newIndex) ? INTO : direction > 0 ? BELOW : ABOVE;
        DefaultMutableTreeNode oldNode = getNode(oldIndex);
        DefaultMutableTreeNode newNode = getNode(newIndex);
        if (oldNode.getParent() != newNode.getParent() && getKind(newNode) != FOLDER) {
            RowsDnDSupport.RefinedDropSupport.Position copy = position;
            if (position == BELOW) {
                copy = ABOVE;
            } else if (position == ABOVE) {
                copy = BELOW;
            }
            if (myTreeModel.canDrop(oldIndex, newIndex, copy)) {
                return Trinity.create(oldIndex, newIndex, copy);
            }
        }
        if (myTreeModel.canDrop(oldIndex, newIndex, position)) {
            return Trinity.create(oldIndex, newIndex, position);
        }
        if (position == BELOW && newIndex < myTree.getRowCount() - 1 && myTreeModel.canDrop(oldIndex, newIndex + 1, ABOVE)) {
            return Trinity.create(oldIndex, newIndex + 1, ABOVE);
        }
        if (position == ABOVE && newIndex > 1 && myTreeModel.canDrop(oldIndex, newIndex - 1, BELOW)) {
            return Trinity.create(oldIndex, newIndex - 1, BELOW);
        }
        if (position == BELOW && myTreeModel.canDrop(oldIndex, newIndex, ABOVE)) {
            return Trinity.create(oldIndex, newIndex, ABOVE);
        }
        if (position == ABOVE && myTreeModel.canDrop(oldIndex, newIndex, BELOW)) {
            return Trinity.create(oldIndex, newIndex, BELOW);
        }
        newIndex += direction;
    }
    return null;
}
Also used : Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with Position

use of com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position in project intellij-community by JetBrains.

the class RowsDnDSupport method installImpl.

private static void installImpl(@NotNull final JComponent component, @NotNull final EditableModel model) {
    component.setTransferHandler(new TransferHandler(null));
    DnDSupport.createBuilder(component).setBeanProvider(info -> {
        final Point p = info.getPoint();
        return new DnDDragStartBean(new RowDragInfo(component, Integer.valueOf(getRow(component, p))));
    }).setTargetChecker(new DnDTargetChecker() {

        @Override
        public boolean update(DnDEvent event) {
            final Object o = event.getAttachedObject();
            if (!(o instanceof RowDragInfo) || ((RowDragInfo) o).component != component) {
                event.setDropPossible(false, "");
                return true;
            }
            event.setDropPossible(true);
            int oldIndex = ((RowDragInfo) o).row;
            int newIndex = getRow(component, event.getPoint());
            if (newIndex == -1) {
                event.setDropPossible(false, "");
                return true;
            }
            Rectangle cellBounds = getCellBounds(component, newIndex);
            if (model instanceof RefinedDropSupport) {
                RefinedDropSupport.Position position = ((RefinedDropSupport) model).isDropInto(component, oldIndex, newIndex) ? INTO : (event.getPoint().y < cellBounds.y + cellBounds.height / 2) ? ABOVE : BELOW;
                boolean canDrop = ((RefinedDropSupport) model).canDrop(oldIndex, newIndex, position);
                event.setDropPossible(canDrop);
                if (canDrop && oldIndex != newIndex) {
                    if (position == BELOW) {
                        cellBounds.y += cellBounds.height - 2;
                    }
                    RelativeRectangle rectangle = new RelativeRectangle(component, cellBounds);
                    switch(position) {
                        case INTO:
                            event.setHighlighting(rectangle, DnDEvent.DropTargetHighlightingType.RECTANGLE);
                            break;
                        case ABOVE:
                        case BELOW:
                            rectangle.getDimension().height = 2;
                            event.setHighlighting(rectangle, DnDEvent.DropTargetHighlightingType.FILLED_RECTANGLE);
                            break;
                    }
                    return true;
                } else {
                    event.hideHighlighter();
                    return true;
                }
            } else {
                if (oldIndex == newIndex) {
                    // Drag&Drop always starts with new==old and we shouldn't display 'rejecting' cursor in this case
                    return true;
                }
                boolean canExchange = model.canExchangeRows(oldIndex, newIndex);
                if (canExchange) {
                    if (oldIndex < newIndex) {
                        cellBounds.y += cellBounds.height - 2;
                    }
                    RelativeRectangle rectangle = new RelativeRectangle(component, cellBounds);
                    rectangle.getDimension().height = 2;
                    event.setDropPossible(true);
                    event.setHighlighting(rectangle, DnDEvent.DropTargetHighlightingType.FILLED_RECTANGLE);
                } else {
                    event.setDropPossible(false);
                }
                return true;
            }
        }
    }).setDropHandler(new DnDDropHandler() {

        @Override
        public void drop(DnDEvent event) {
            final Object o = event.getAttachedObject();
            final Point p = event.getPoint();
            if (o instanceof RowDragInfo && ((RowDragInfo) o).component == component) {
                int oldIndex = ((RowDragInfo) o).row;
                if (oldIndex == -1)
                    return;
                int newIndex = getRow(component, p);
                if (newIndex == -1) {
                    newIndex = getRowCount(component) - 1;
                }
                if (oldIndex != newIndex) {
                    if (model instanceof RefinedDropSupport) {
                        Rectangle cellBounds = getCellBounds(component, newIndex);
                        RefinedDropSupport.Position position = ((RefinedDropSupport) model).isDropInto(component, oldIndex, newIndex) ? INTO : (event.getPoint().y < cellBounds.y + cellBounds.height / 2) ? ABOVE : BELOW;
                        if (((RefinedDropSupport) model).canDrop(oldIndex, newIndex, position)) {
                            ((RefinedDropSupport) model).drop(oldIndex, newIndex, position);
                        }
                    } else {
                        if (model.canExchangeRows(oldIndex, newIndex)) {
                            model.exchangeRows(oldIndex, newIndex);
                            setSelectedRow(component, newIndex);
                        }
                    }
                }
            }
            event.hideHighlighter();
        }
    }).install();
}
Also used : java.awt(java.awt) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) Function(com.intellij.util.Function) EditableModel(com.intellij.util.ui.EditableModel) com.intellij.ide.dnd(com.intellij.ide.dnd) NotNull(org.jetbrains.annotations.NotNull) javax.swing(javax.swing) Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle) Position(com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position) RelativeRectangle(com.intellij.ui.awt.RelativeRectangle)

Aggregations

Position (com.intellij.ui.RowsDnDSupport.RefinedDropSupport.Position)2 com.intellij.ide.dnd (com.intellij.ide.dnd)1 RelativeRectangle (com.intellij.ui.awt.RelativeRectangle)1 Function (com.intellij.util.Function)1 EditableModel (com.intellij.util.ui.EditableModel)1 java.awt (java.awt)1 javax.swing (javax.swing)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1