Search in sources :

Example 1 with ViewHandler

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

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

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

the class ViewHandlerManager method getHandler.

/**
   * Gets the {@link ViewHandler} associated with the given XML tag, if any
   *
   * @param viewTag the tag to look up
   * @return the corresponding view handler, if any
   */
@Nullable
public ViewHandler getHandler(@NotNull String viewTag) {
    ViewHandler handler = myHandlers.get(viewTag);
    if (handler == null) {
        if (viewTag.indexOf('.') != -1) {
            String tag = NlComponent.viewClassToTag(viewTag);
            if (!tag.equals(viewTag)) {
                handler = getHandler(tag);
                if (handler != null) {
                    // Alias fully qualified widget name to tag
                    myHandlers.put(viewTag, handler);
                    return handler;
                }
            }
        }
        handler = createHandler(viewTag);
        myHandlers.put(viewTag, handler);
    }
    return handler != NONE ? handler : null;
}
Also used : 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) Nullable(org.jetbrains.annotations.Nullable)

Example 4 with ViewHandler

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

the class PaletteTestCase method checkComponent.

private void checkComponent(@NotNull NlComponent component, @NotNull String expectedTitle, @NotNull Icon expectedIcon) {
    ViewHandlerManager handlerManager = ViewHandlerManager.get(getProject());
    ViewHandler handler = handlerManager.getHandlerOrDefault(component);
    String title = handler.getTitle(component);
    String attrs = handler.getTitleAttributes(component);
    if (!StringUtil.isEmpty(attrs)) {
        title += " " + attrs;
    }
    assertEquals(component.getTagName() + ".Component.Title", expectedTitle, title);
    assertEquals(component.getTagName() + ".Component.Icon", expectedIcon, handler.getIcon(component));
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) TextViewHandler(com.android.tools.idea.uibuilder.handlers.TextViewHandler) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler)

Example 5 with ViewHandler

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

the class StyleFilter method getWidgetStyleNames.

@NotNull
private static List<String> getWidgetStyleNames(@NotNull Project project, @NotNull ResourceResolver resolver, @NotNull String tagName) {
    ViewHandlerManager manager = ViewHandlerManager.get(project);
    ViewHandler handler = manager.getHandler(tagName);
    if (handler == null) {
        return Collections.emptyList();
    }
    List<String> possibleNames = handler.getBaseStyles(tagName);
    ResourceValueMap frameworkStyles = resolver.getFrameworkResources().get(ResourceType.STYLE);
    ResourceValueMap projectStyles = resolver.getProjectResources().get(ResourceType.STYLE);
    // This is usually a small list
    List<String> names = new ArrayList<>(4);
    for (String styleName : possibleNames) {
        if (frameworkStyles.containsKey(styleName)) {
            names.add(SdkConstants.PREFIX_ANDROID + styleName);
        }
        if (projectStyles.containsKey(styleName)) {
            names.add(styleName);
        }
    }
    return names;
}
Also used : ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) ViewHandler(com.android.tools.idea.uibuilder.api.ViewHandler) ResourceValueMap(com.android.ide.common.resources.ResourceValueMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ViewHandler (com.android.tools.idea.uibuilder.api.ViewHandler)13 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)6 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)4 ViewHandlerManager (com.android.tools.idea.uibuilder.handlers.ViewHandlerManager)3 AdViewHandler (com.android.tools.idea.uibuilder.handlers.google.AdViewHandler)2 MapViewHandler (com.android.tools.idea.uibuilder.handlers.google.MapViewHandler)2 Nullable (org.jetbrains.annotations.Nullable)2 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 ResourceValueMap (com.android.ide.common.resources.ResourceValueMap)1 InsertType (com.android.tools.idea.uibuilder.api.InsertType)1 ViewEditor (com.android.tools.idea.uibuilder.api.ViewEditor)1 TextViewHandler (com.android.tools.idea.uibuilder.handlers.TextViewHandler)1 ViewEditorImpl (com.android.tools.idea.uibuilder.handlers.ViewEditorImpl)1 ConstraintLayoutHandler (com.android.tools.idea.uibuilder.handlers.constraint.ConstraintLayoutHandler)1 ConstraintLayoutDecorator (com.android.tools.idea.uibuilder.scene.decorator.ConstraintLayoutDecorator)1 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)1 Project (com.intellij.openapi.project.Project)1 FontRenderContext (java.awt.font.FontRenderContext)1 Rectangle2D (java.awt.geom.Rectangle2D)1 NotNull (org.jetbrains.annotations.NotNull)1