Search in sources :

Example 86 with NlComponent

use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.

the class MockupFileHelper method writePositionToXML.

/**
   * Write the attribute {@link SdkConstants#ATTR_MOCKUP_CROP} and its value using the provided mockup
   * @param mockup The mockup to retrieve the position string from
   * @see #getPositionString(Mockup)
   */
public static void writePositionToXML(@NotNull Mockup mockup) {
    NlComponent component = mockup.getComponent();
    if (component == null) {
        return;
    }
    final NlModel model = component.getModel();
    final WriteCommandAction action = new WriteCommandAction(model.getProject(), "Edit Mockup Crop", model.getFile()) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            if (mockup.isFullScreen()) {
                component.removeAttribute(SdkConstants.TOOLS_URI, SdkConstants.ATTR_MOCKUP_CROP);
            } else {
                final String cropping = getPositionString(mockup);
                component.setAttribute(SdkConstants.TOOLS_URI, SdkConstants.ATTR_MOCKUP_CROP, cropping);
            }
        }
    };
    action.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) Result(com.intellij.openapi.application.Result)

Example 87 with NlComponent

use of com.android.tools.idea.uibuilder.model.NlComponent 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 88 with NlComponent

use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.

the class BlueprintLayer method paint.

/**
   * Base buildDisplayList method. Draw the blueprint background.
   * TODO: We might want to simplify the stack of layers and not keep this one.
   *
   * @param gc The Graphics object to draw into
   */
@Override
public void paint(@NotNull Graphics2D gc) {
    myScreenView.getSize(myScreenViewSize);
    mySizeRectangle.setBounds(myScreenView.getX(), myScreenView.getY(), myScreenViewSize.width, myScreenViewSize.height);
    Rectangle2D.intersect(mySizeRectangle, gc.getClipBounds(), mySizeRectangle);
    if (mySizeRectangle.isEmpty()) {
        return;
    }
    // Draw the background
    Graphics2D g = (Graphics2D) gc.create();
    Shape prevClip = null;
    Shape screenShape = myScreenView.getScreenShape();
    if (screenShape != null) {
        prevClip = g.getClip();
        g.clip(screenShape);
    }
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.setColor(BLUEPRINT_BG_COLOR);
    g.fillRect(mySizeRectangle.x, mySizeRectangle.y, mySizeRectangle.width, mySizeRectangle.height);
    if (prevClip != null) {
        g.setClip(prevClip);
    }
    // Draw the components
    NlModel model = myScreenView.getModel();
    if (model.getComponents().size() == 0) {
        return;
    }
    NlComponent component = model.getComponents().get(0);
    component = component.getRoot();
    ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(model.getFacet());
    // Draw the components
    if (drawComponent(g, component, viewHandlerManager, false)) {
        Dimension size = myScreenView.getSize();
        DesignSurface surface = myScreenView.getSurface();
        if (size.width != 0 && size.height != 0) {
            surface.repaint(myScreenView.getX(), myScreenView.getY(), size.width, size.height);
        } else {
            surface.repaint();
        }
    }
    g.dispose();
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewHandlerManager(com.android.tools.idea.uibuilder.handlers.ViewHandlerManager) NlModel(com.android.tools.idea.uibuilder.model.NlModel)

Example 89 with NlComponent

use of com.android.tools.idea.uibuilder.model.NlComponent 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 90 with NlComponent

use of com.android.tools.idea.uibuilder.model.NlComponent in project android by JetBrains.

the class WarningLayer method getTooltip.

@Nullable
@Override
public String getTooltip(@SwingCoordinate int mx, @SwingCoordinate int my) {
    LintAnnotationsModel lintModel = myScreenView.getModel().getLintAnnotationsModel();
    if (lintModel == null) {
        return null;
    }
    // linear search through all the components with annotations
    for (NlComponent component : myAnnotatedComponents) {
        Icon icon = lintModel.getIssueIcon(component);
        if (icon == null) {
            continue;
        }
        int x = Coordinates.getSwingX(myScreenView, component.x);
        int y = Coordinates.getSwingY(myScreenView, component.y);
        int w = Coordinates.getSwingDimension(myScreenView, component.w);
        if (mx > (x + w - icon.getIconWidth() - PADDING) && mx < (x + w - PADDING)) {
            if (my > (y + PADDING) && my < (y + PADDING + icon.getIconHeight())) {
                // TODO: show all messages?
                return lintModel.getIssueMessage(component);
            }
        }
    }
    return null;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) LintAnnotationsModel(com.android.tools.idea.uibuilder.lint.LintAnnotationsModel) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)184 NlModel (com.android.tools.idea.uibuilder.model.NlModel)34 NotNull (org.jetbrains.annotations.NotNull)34 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)18 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)17 Nullable (org.jetbrains.annotations.Nullable)14 Test (org.junit.Test)12 Matchers.anyString (org.mockito.Matchers.anyString)11 ViewGroupHandler (com.android.tools.idea.uibuilder.api.ViewGroupHandler)9 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)9 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)8 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)7 XmlFile (com.intellij.psi.xml.XmlFile)7 XmlTag (com.intellij.psi.xml.XmlTag)7 ArrayList (java.util.ArrayList)7 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)6 NlGraphics (com.android.tools.idea.uibuilder.graphics.NlGraphics)6 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)6 Project (com.intellij.openapi.project.Project)6 List (java.util.List)6