Search in sources :

Example 6 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class DragDropInteraction method acceptsDrop.

private boolean acceptsDrop(@NotNull NlComponent parent, @NotNull ViewGroupHandler parentHandler, @AndroidCoordinate int x, @AndroidCoordinate int y) {
    ScreenView view = myDesignSurface.getScreenView(x, y);
    assert view != null;
    ViewHandlerManager manager = ViewHandlerManager.get(view.getModel().getFacet());
    Predicate<NlComponent> acceptsChild = child -> parentHandler.acceptsChild(parent, child, x, y);
    Predicate<NlComponent> acceptsParent = child -> {
        ViewHandler childHandler = manager.getHandler(child);
        return childHandler != null && childHandler.acceptsParent(parent, child);
    };
    return myDraggedComponents.stream().allMatch(acceptsChild.and(acceptsParent));
}
Also used : com.android.tools.idea.uibuilder.api(com.android.tools.idea.uibuilder.api) XmlFile(com.intellij.psi.xml.XmlFile) Predicate(java.util.function.Predicate) NlGraphics(com.android.tools.idea.uibuilder.graphics.NlGraphics) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl) java.awt(java.awt) Nullable(org.jetbrains.annotations.Nullable) List(java.util.List) Lists(com.google.common.collect.Lists) com.android.tools.idea.uibuilder.model(com.android.tools.idea.uibuilder.model) ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) InputEventMask(org.intellij.lang.annotations.JdkConstants.InputEventMask) NotNull(org.jetbrains.annotations.NotNull) Collections(java.util.Collections) NlConstants(com.android.tools.idea.uibuilder.graphics.NlConstants) ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)

Example 7 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class DragDropInteraction method findViewGroupHandlerAt.

@Nullable
private ViewGroupHandler findViewGroupHandlerAt(@AndroidCoordinate int x, @AndroidCoordinate int y) {
    final ScreenView screenView = myDesignSurface.getScreenView(x, y);
    if (screenView == null) {
        return null;
    }
    NlModel model = screenView.getModel();
    NlComponent component = model.findLeafAt(x, y, true);
    component = excludeDraggedComponents(component);
    if (component == myCachedComponent && myCachedHandler != null) {
        return myCachedHandler;
    }
    myCachedComponent = component;
    myCachedHandler = null;
    ViewHandlerManager handlerManager = ViewHandlerManager.get(model.getFacet());
    while (component != null) {
        Object handler = handlerManager.getHandler(component);
        if (handler instanceof ViewGroupHandler && acceptsDrop(component, (ViewGroupHandler) handler, x, y)) {
            myCachedHandler = (ViewGroupHandler) handlerManager.getHandler(component);
            // HACK: This method should not side-effect set this; instead the method should compute it!
            myDragReceiver = component;
            return myCachedHandler;
        }
        component = component.getParent();
    }
    return null;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) Nullable(org.jetbrains.annotations.Nullable)

Example 8 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager 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)

Example 9 with ViewHandlerManager

use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.

the class NlModel method createComponent.

public NlComponent createComponent(@Nullable ScreenView screenView, @NotNull XmlTag tag, @Nullable NlComponent parent, @Nullable NlComponent before, @NotNull InsertType insertType) {
    if (parent != null) {
        // Creating a component intended to be inserted into an existing layout
        XmlTag parentTag = parent.getTag();
        if (before != null) {
            tag = (XmlTag) parentTag.addBefore(tag, before.getTag());
        } else {
            tag = parentTag.addSubTag(tag, false);
        }
        // Required attribute for all views; drop handlers can adjust as necessary
        if (tag.getAttribute(ATTR_LAYOUT_WIDTH, ANDROID_URI) == null) {
            tag.setAttribute(ATTR_LAYOUT_WIDTH, ANDROID_URI, VALUE_WRAP_CONTENT);
        }
        if (tag.getAttribute(ATTR_LAYOUT_HEIGHT, ANDROID_URI) == null) {
            tag.setAttribute(ATTR_LAYOUT_HEIGHT, ANDROID_URI, VALUE_WRAP_CONTENT);
        }
    } else {
        // No namespace yet: use the default prefix instead
        if (tag.getAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_WIDTH) == null) {
            tag.setAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_WIDTH, VALUE_WRAP_CONTENT);
        }
        if (tag.getAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_HEIGHT) == null) {
            tag.setAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_HEIGHT, VALUE_WRAP_CONTENT);
        }
    }
    NlComponent child = new NlComponent(this, tag);
    if (parent != null) {
        parent.addChild(child, before);
    }
    // Notify view handlers
    ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(getProject());
    ViewHandler childHandler = viewHandlerManager.getHandler(child);
    if (childHandler != null && screenView != null) {
        ViewEditor editor = new ViewEditorImpl(screenView);
        boolean ok = childHandler.onCreate(editor, parent, child, insertType);
        if (!ok) {
            if (parent != null) {
                parent.removeChild(child);
            }
            tag.delete();
            return null;
        }
    }
    if (parent != null) {
        ViewHandler parentHandler = viewHandlerManager.getHandler(parent);
        if (parentHandler instanceof ViewGroupHandler) {
            ((ViewGroupHandler) parentHandler).onChildInserted(parent, child, insertType);
        }
    }
    return child;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) XmlTag(com.intellij.psi.xml.XmlTag) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)

Aggregations

ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)9 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)3 ViewEditorImpl (com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)3 NotNull (org.jetbrains.annotations.NotNull)3 Result (com.intellij.openapi.application.Result)2 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)2 Project (com.intellij.openapi.project.Project)2 XmlFile (com.intellij.psi.xml.XmlFile)2 List (java.util.List)2 Nullable (org.jetbrains.annotations.Nullable)2 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 com.android.tools.idea.uibuilder.api (com.android.tools.idea.uibuilder.api)1 InsertType (com.android.tools.idea.uibuilder.api.InsertType)1 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)1 NlConstants (com.android.tools.idea.uibuilder.graphics.NlConstants)1 NlGraphics (com.android.tools.idea.uibuilder.graphics.NlGraphics)1 TextViewHandler (com.android.tools.idea.uibuilder.handlers.TextViewHandler)1 com.android.tools.idea.uibuilder.model (com.android.tools.idea.uibuilder.model)1 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1