Search in sources :

Example 1 with ViewEditorImpl

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

the class DragDropInteraction method moveTo.

private void moveTo(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask final int modifiers, boolean commit) {
    myScreenView = myDesignSurface.getScreenView(x, y);
    if (myScreenView == null) {
        return;
    }
    myDesignSurface.getLayeredPane().scrollRectToVisible(new Rectangle(x - NlConstants.DEFAULT_SCREEN_OFFSET_X, y - NlConstants.DEFAULT_SCREEN_OFFSET_Y, 2 * NlConstants.DEFAULT_SCREEN_OFFSET_X, 2 * NlConstants.DEFAULT_SCREEN_OFFSET_Y));
    final int ax = Coordinates.getAndroidX(myScreenView, x);
    final int ay = Coordinates.getAndroidY(myScreenView, y);
    Project project = myScreenView.getModel().getProject();
    ViewGroupHandler handler = findViewGroupHandlerAt(ax, ay);
    if (handler != myCurrentHandler) {
        if (myDragHandler != null) {
            myDragHandler.cancel();
            myDragHandler = null;
            myScreenView.getSurface().repaint();
        }
        myCurrentHandler = handler;
        if (myCurrentHandler != null) {
            assert myDragReceiver != null;
            String error = null;
            ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(project);
            for (NlComponent component : myDraggedComponents) {
                if (!myCurrentHandler.acceptsChild(myDragReceiver, component, ax, ay)) {
                    error = String.format("<%1$s> does not accept <%2$s> as a child", myDragReceiver.getTagName(), component.getTagName());
                    break;
                }
                ViewHandler viewHandler = viewHandlerManager.getHandler(component);
                if (viewHandler != null && !viewHandler.acceptsParent(myDragReceiver, component)) {
                    error = String.format("<%1$s> does not accept <%2$s> as a parent", component.getTagName(), myDragReceiver.getTagName());
                    break;
                }
            }
            if (error == null) {
                myDragHandler = myCurrentHandler.createDragHandler(new ViewEditorImpl(myScreenView), myDragReceiver, myDraggedComponents, myType);
                if (myDragHandler != null) {
                    myDragHandler.start(Coordinates.getAndroidX(myScreenView, myStartX), Coordinates.getAndroidY(myScreenView, myStartY), myStartMask);
                }
            } else {
                myCurrentHandler = null;
            }
        }
    }
    if (myDragHandler != null && myCurrentHandler != null) {
        String error = myDragHandler.update(ax, ay, modifiers);
        final List<NlComponent> added = Lists.newArrayList();
        if (commit && error == null) {
            added.addAll(myDraggedComponents);
            final NlModel model = myScreenView.getModel();
            XmlFile file = model.getFile();
            String label = myType.getDescription();
            WriteCommandAction action = new WriteCommandAction(project, label, file) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    InsertType insertType = model.determineInsertType(myType, myTransferItem, false);
                    // TODO: Run this *after* making a copy
                    myDragHandler.commit(ax, ay, modifiers, insertType);
                }
            };
            action.execute();
            model.notifyModified(NlModel.ChangeType.DND_COMMIT);
            // Select newly dropped components
            model.getSelectionModel().setSelection(added);
        }
        myScreenView.getSurface().repaint();
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) XmlFile(com.intellij.psi.xml.XmlFile) ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result) Project(com.intellij.openapi.project.Project) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)

Example 2 with ViewEditorImpl

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

the class ScrollInteraction method createScrollInteraction.

/**
   * Creates a new {@link ScrollInteraction} if any of the components in the component hierarchy can handle scrolling.
   * @return the {@link ScrollInteraction} or null if none of the components handle the scrolling
   */
@Nullable
public static ScrollInteraction createScrollInteraction(@NonNull ScreenView screenView, @NonNull NlComponent component) {
    NlComponent currentComponent = component;
    ScrollHandler scrollHandler = null;
    ViewEditor editor = new ViewEditorImpl(screenView);
    // Find the component that is the lowest in the hierarchy and can take the scrolling events
    while (currentComponent != null) {
        ViewGroupHandler viewGroupHandler = currentComponent.getViewGroupHandler();
        scrollHandler = viewGroupHandler != null ? viewGroupHandler.createScrollHandler(editor, currentComponent) : null;
        if (scrollHandler != null) {
            break;
        }
        currentComponent = currentComponent.getParent();
    }
    if (scrollHandler == null) {
        return null;
    }
    return new ScrollInteraction(screenView, scrollHandler);
}
Also used : ScrollHandler(com.android.tools.idea.uibuilder.api.ScrollHandler) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewEditor(com.android.tools.idea.uibuilder.api.ViewEditor) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl) Nullable(org.jetbrains.annotations.Nullable)

Example 3 with ViewEditorImpl

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

the class ResizeInteraction method begin.

@Override
public void begin(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask int startMask) {
    super.begin(x, y, startMask);
    NlComponent parent = myComponent.getParent();
    if (parent != null) {
        ViewGroupHandler viewGroupHandler = ViewHandlerManager.get(myScreenView.getModel().getFacet()).findLayoutHandler(parent, false);
        if (viewGroupHandler != null) {
            ViewEditor editor = new ViewEditorImpl(myScreenView);
            myResizeHandler = viewGroupHandler.createResizeHandler(editor, myComponent, myHorizontalEdge, myVerticalEdge);
            if (myResizeHandler != null) {
                int androidX = Coordinates.getAndroidX(myScreenView, myStartX);
                int androidY = Coordinates.getAndroidY(myScreenView, myStartY);
                myResizeHandler.start(androidX, androidY, startMask);
            }
        }
    }
}
Also used : ViewEditor(com.android.tools.idea.uibuilder.api.ViewEditor) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)

Example 4 with ViewEditorImpl

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

Example 5 with ViewEditorImpl

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

the class NlActionManager method addViewActions.

public void addViewActions(@NotNull DefaultActionGroup group, @Nullable NlComponent component, @Nullable NlComponent parent, @NotNull List<NlComponent> newSelection, boolean toolbar) {
    ScreenView screenView = mySurface.getCurrentScreenView();
    if (screenView == null || (parent == null && component == null)) {
        return;
    }
    ViewEditor editor = new ViewEditorImpl(screenView);
    // TODO: Perform caching
    if (component != null) {
        ViewHandler handler = ViewHandlerManager.get(mySurface.getProject()).getHandler(component);
        addViewActionsForHandler(group, component, newSelection, editor, handler, toolbar);
    }
    if (parent != null) {
        ViewHandler handler = ViewHandlerManager.get(mySurface.getProject()).getHandler(parent);
        List<NlComponent> selectedChildren = Lists.newArrayListWithCapacity(newSelection.size());
        for (NlComponent selected : newSelection) {
            if (selected.getParent() == parent) {
                selectedChildren.add(selected);
            }
        }
        addViewActionsForHandler(group, parent, selectedChildren, editor, handler, toolbar);
    }
}
Also used : ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ViewEditor(com.android.tools.idea.uibuilder.api.ViewEditor) ViewEditorImpl(com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)

Aggregations

ViewEditorImpl (com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)6 ViewEditor (com.android.tools.idea.uibuilder.api.ViewEditor)3 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)2 ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)2 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)2 ScrollHandler (com.android.tools.idea.uibuilder.api.ScrollHandler)1 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 Result (com.intellij.openapi.application.Result)1 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)1 Project (com.intellij.openapi.project.Project)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlTag (com.intellij.psi.xml.XmlTag)1 NotNull (org.jetbrains.annotations.NotNull)1 Nullable (org.jetbrains.annotations.Nullable)1