Search in sources :

Example 1 with InsertType

use of com.android.tools.idea.uibuilder.api.InsertType in project android by JetBrains.

the class NlDropListener method captureDraggedComponents.

@Nullable
private InsertType captureDraggedComponents(@NotNull NlDropEvent event, boolean isPreview) {
    clearDraggedComponents();
    ScreenView screenView = myTree.getScreenView();
    if (screenView == null) {
        return null;
    }
    NlModel model = screenView.getModel();
    if (event.isDataFlavorSupported(ItemTransferable.DESIGNER_FLAVOR)) {
        try {
            myTransferItem = (DnDTransferItem) event.getTransferable().getTransferData(ItemTransferable.DESIGNER_FLAVOR);
            InsertType insertType = determineInsertType(event, isPreview);
            if (insertType.isMove()) {
                myDragged.addAll(keepOnlyAncestors(model.getSelectionModel().getSelection()));
            } else {
                Collection<NlComponent> captured = ApplicationManager.getApplication().runWriteAction((Computable<Collection<NlComponent>>) () -> model.createComponents(screenView, myTransferItem, insertType));
                if (captured != null) {
                    myDragged.addAll(keepOnlyAncestors(captured));
                }
            }
            return insertType;
        } catch (IOException | UnsupportedFlavorException exception) {
            Logger.getInstance(NlDropListener.class).warn(exception);
        }
    }
    return null;
}
Also used : ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) InsertType(com.android.tools.idea.uibuilder.api.InsertType) IOException(java.io.IOException) UnsupportedFlavorException(java.awt.datatransfer.UnsupportedFlavorException) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with InsertType

use of com.android.tools.idea.uibuilder.api.InsertType in project android by JetBrains.

the class NlDropListener method drop.

@Override
public void drop(@NotNull DropTargetDropEvent dropEvent) {
    NlDropEvent event = new NlDropEvent(dropEvent);
    InsertType insertType = captureDraggedComponents(event, false);
    if (findInsertionPoint(event) != null) {
        performDrop(dropEvent, insertType);
    }
    clearInsertionPoint();
    clearDraggedComponents();
}
Also used : InsertType(com.android.tools.idea.uibuilder.api.InsertType)

Example 3 with InsertType

use of com.android.tools.idea.uibuilder.api.InsertType in project android by JetBrains.

the class DesignSurfaceActionHandler method pasteOperation.

private boolean pasteOperation(boolean checkOnly) {
    ScreenView screenView = mySurface.getCurrentScreenView();
    if (screenView == null) {
        return false;
    }
    List<NlComponent> selection = screenView.getSelectionModel().getSelection();
    NlComponent receiver = !selection.isEmpty() ? selection.get(0) : null;
    if (receiver == null) {
        // In the case where there is no selection but we only have a root component, use that one
        List<NlComponent> components = screenView.getModel().getComponents();
        if (components.size() == 1) {
            receiver = components.get(0);
        }
    }
    if (receiver == null) {
        return false;
    }
    NlComponent before;
    NlModel model = screenView.getModel();
    ViewHandlerManager handlerManager = ViewHandlerManager.get(model.getProject());
    ViewHandler handler = handlerManager.getHandler(receiver);
    if (handler instanceof ViewGroupHandler) {
        before = receiver.getChild(0);
    } else {
        before = receiver.getNextSibling();
        receiver = receiver.getParent();
        if (receiver == null) {
            return false;
        }
    }
    DnDTransferItem item = getClipboardData();
    if (item == null) {
        return false;
    }
    InsertType insertType = model.determineInsertType(DragType.PASTE, item, checkOnly);
    List<NlComponent> pasted = model.createComponents(screenView, item, insertType);
    if (!model.canAddComponents(pasted, receiver, before)) {
        return false;
    }
    if (checkOnly) {
        return true;
    }
    model.addComponents(pasted, receiver, before, insertType);
    return true;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) InsertType(com.android.tools.idea.uibuilder.api.InsertType)

Aggregations

InsertType (com.android.tools.idea.uibuilder.api.InsertType)3 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)1 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)1 ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 UnsupportedFlavorException (java.awt.datatransfer.UnsupportedFlavorException)1 IOException (java.io.IOException)1 Nullable (org.jetbrains.annotations.Nullable)1