Search in sources :

Example 1 with ViewGroupHandler

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

the class BlueprintLayer method drawComponent.

/**
   * Draw the given component and its children
   *
   * @param gc                 the graphics context
   * @param component          the component we want to draw
   * @param viewHandlerManager the view handler
   */
private boolean drawComponent(@NotNull Graphics2D gc, @NotNull NlComponent component, @NotNull ViewHandlerManager viewHandlerManager, boolean parentHandlesPainting) {
    boolean needsRepaint = false;
    boolean handlesPainting = false;
    if (component.viewInfo != null) {
        ViewHandler handler = component.getViewHandler();
        // Check if the view handler handles the painting
        if (handler != null && handler instanceof ViewGroupHandler) {
            ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
            if (viewGroupHandler.handlesPainting()) {
                if (handler.paintConstraints(myScreenView, gc, component)) {
                    return needsRepaint;
                }
                needsRepaint |= viewGroupHandler.drawGroup(gc, myScreenView, component);
                handlesPainting = true;
            }
        }
        if (!handlesPainting && !parentHandlesPainting) {
            // If not, layout the component ourselves
            Graphics2D g = (Graphics2D) gc.create();
            int x = getSwingX(myScreenView, component.x);
            int y = getSwingY(myScreenView, component.y);
            int w = getSwingDimension(myScreenView, component.w);
            int h = getSwingDimension(myScreenView, component.h);
            drawComponentBackground(g, component);
            String name = component.getTagName();
            name = name.substring(name.lastIndexOf('.') + 1);
            Font font = BLUEPRINT_TEXT_FONT;
            g.setFont(font);
            g.setColor(BLUEPRINT_FG_COLOR);
            String id = component.getId();
            int lineHeight = g.getFontMetrics().getHeight();
            FontRenderContext fontRenderContext = g.getFontRenderContext();
            if (id != null && h > lineHeight * 2) {
                // Can fit both
                Rectangle2D classBounds = font.getStringBounds(name, fontRenderContext);
                Rectangle2D idBounds = font.getStringBounds(id, fontRenderContext);
                int textY = y + h / 2;
                int textX = x + w / 2 - ((int) classBounds.getWidth()) / 2;
                if (component.isRoot()) {
                    textX = x + lineHeight;
                    textY = y - (int) (classBounds.getHeight() + idBounds.getHeight());
                }
                g.drawString(name, textX, textY);
                if (component.isRoot()) {
                    textX = x + lineHeight;
                    textY = y - (int) (idBounds.getHeight());
                } else {
                    textX = x + w / 2 - ((int) idBounds.getWidth()) / 2;
                    textY += (int) (idBounds.getHeight());
                }
                g.drawString(id, textX, textY);
            } else {
                // Only room for a single line: prioritize the id if it's available, otherwise the class name
                String text = id != null ? id : name;
                Rectangle2D stringBounds = font.getStringBounds(text, fontRenderContext);
                int textX = x + w / 2 - ((int) stringBounds.getWidth()) / 2;
                int textY = y + h / 2 + ((int) stringBounds.getHeight()) / 2;
                g.drawString(text, textX, textY);
            }
            g.dispose();
        }
    }
    // Draw the children of the component...
    for (NlComponent child : component.getChildren()) {
        needsRepaint |= drawComponent(gc, child, viewHandlerManager, handlesPainting);
    }
    return needsRepaint;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) Rectangle2D(java.awt.geom.Rectangle2D) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) FontRenderContext(java.awt.font.FontRenderContext)

Example 2 with ViewGroupHandler

use of com.android.tools.idea.uibuilder.api.ViewGroupHandler 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 ViewGroupHandler

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

the class ViewHandlerManager method findLayoutHandler.

/**
   * Finds the nearest layout/view group handler for the given component.
   *
   * @param component the component to search from
   * @param strict    if true, only consider parents of the component, not the component itself
   */
@Nullable
public ViewGroupHandler findLayoutHandler(@NotNull NlComponent component, boolean strict) {
    NlComponent curr = component;
    if (strict) {
        curr = curr.getParent();
    }
    while (curr != null) {
        ViewHandler handler = getHandler(curr);
        if (handler instanceof ViewGroupHandler) {
            return (ViewGroupHandler) handler;
        }
        curr = curr.getParent();
    }
    return null;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) AdViewHandler(com.android.tools.idea.uibuilder.handlers.google.AdViewHandler) MapViewHandler(com.android.tools.idea.uibuilder.handlers.google.MapViewHandler) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ViewGroupHandler

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

the class GridDragHandlerTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    GridLayout viewObject = new GridLayout();
    viewObject.setVerticalAxis(new Axis(new int[] { 0, 1024 }));
    viewObject.setHorizontalAxis(new Axis(new int[] { 0, 248, 248, 248, 248, 248, 248, 248, 248, 520, 768 }));
    viewObject.setRowCount(1);
    viewObject.setColumnCount(10);
    // @formatter:off
    ComponentDescriptor button1 = component(SdkConstants.BUTTON).withBounds(0, 160, 248, 96).withAttribute("android:layout_row", "0").withAttribute("android:layout_column", "0").layoutParamsObject(new LayoutParams(new Spec(new Interval(0, 1)), new Spec(new Interval(0, 1))));
    ComponentDescriptor button2 = component(SdkConstants.BUTTON).withBounds(520, 160, 248, 96).withAttribute("android:layout_row", "0").withAttribute("android:layout_column", "9").layoutParamsObject(new LayoutParams(new Spec(new Interval(0, 1)), new Spec(new Interval(9, 10))));
    ComponentDescriptor layout = component(SdkConstants.GRID_LAYOUT).withBounds(0, 160, 768, 1024).viewObject(viewObject).children(button1, button2);
    NlModel model = model("grid_layout.xml", layout).build();
    // @formatter:on
    ViewEditor editor = Mockito.mock(ViewEditor.class);
    Mockito.when(editor.getModel()).thenReturn(model);
    List<NlComponent> components = Collections.emptyList();
    handler = new GridDragHandler(editor, new ViewGroupHandler(), model.getComponents().get(0), components, DragType.CREATE);
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ComponentDescriptor(com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor) ViewEditor(com.android.tools.idea.uibuilder.api.ViewEditor) NlModel(com.android.tools.idea.uibuilder.model.NlModel) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler)

Example 5 with ViewGroupHandler

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

the class Scene method addTargets.

/**
   * Add targets to the given component (by asking the associated
   * {@linkplain ViewGroupHandler} to do it)
   *
   * @param component
   */
void addTargets(@NotNull SceneComponent component) {
    SceneComponent parent = component.getParent();
    if (parent != null) {
        component = parent;
    } else {
        component = myRoot;
    }
    ViewHandler handler = component.getNlComponent().getViewHandler();
    if (handler instanceof ViewGroupHandler) {
        ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
        if (component.getViewGroupHandler() != viewGroupHandler) {
            component.setViewGroupHandler(viewGroupHandler, true);
        }
        int childCount = component.getChildCount();
        for (int i = 0; i < childCount; i++) {
            SceneComponent child = component.getChild(i);
            if (child.getViewGroupHandler() != viewGroupHandler) {
                child.setViewGroupHandler(viewGroupHandler, false);
            }
        }
    }
    needsRebuildList();
}
Also used : ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ViewGroupHandler(com.android.tools.idea.uibuilder.api.ViewGroupHandler)

Aggregations

ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)14 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)9 ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)6 ViewEditor (com.android.tools.idea.uibuilder.api.ViewEditor)5 ViewEditorImpl (com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)2 ArrayList (java.util.ArrayList)2 NotNull (org.jetbrains.annotations.NotNull)2 Nullable (org.jetbrains.annotations.Nullable)2 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 InsertType (com.android.tools.idea.uibuilder.api.InsertType)1 ScrollHandler (com.android.tools.idea.uibuilder.api.ScrollHandler)1 ComponentDescriptor (com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)1 ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)1 AdViewHandler (com.android.tools.idea.uibuilder.handlers.google.AdViewHandler)1 MapViewHandler (com.android.tools.idea.uibuilder.handlers.google.MapViewHandler)1 NlModel (com.android.tools.idea.uibuilder.model.NlModel)1 FontRenderContext (java.awt.font.FontRenderContext)1 Rectangle2D (java.awt.geom.Rectangle2D)1