Search in sources :

Example 26 with NlComponent

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

the class DesignSurfaceFixture method findView.

/**
   * Searches for the nth occurrence of a given view in the layout. The ordering of widgets of the same
   * type is by visual order, first vertically, then horizontally (and finally by XML source offset, if they exactly overlap
   * as for example would happen in a {@code <merge>}
   *
   * @param tag the view tag to search for, e.g. "Button" or "TextView"
   * @param occurrence the index of the occurrence of the tag, e.g. 0 for the first TextView in the layout
   */
@NotNull
public NlComponentFixture findView(@NotNull final String tag, int occurrence) {
    waitForRenderToFinish();
    ScreenView screenView = target().getCurrentScreenView();
    assertNotNull(screenView);
    final NlModel model = screenView.getModel();
    final java.util.List<NlComponent> components = Lists.newArrayList();
    model.getComponents().forEach(component -> addComponents(tag, component, components));
    // Sort by visual order
    components.sort((component1, component2) -> {
        int delta = component1.y - component2.y;
        if (delta != -1) {
            return delta;
        }
        delta = component1.x - component2.x;
        if (delta != -1) {
            return delta;
        }
        // Unlikely
        return component1.getTag().getTextOffset() - component2.getTag().getTextOffset();
    });
    assertTrue("Only " + components.size() + " found, not enough for occurrence #" + occurrence, components.size() > occurrence);
    NlComponent component = components.get(occurrence);
    return createComponentFixture(component);
}
Also used : ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull)

Example 27 with NlComponent

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

the class DragHandler method insertComponents.

/**
   * Insert the components being dragged into this layout.
   *
   * @param insertIndex the position to drop the dragged components at, or -1 to append them at the end.
   *                    The index refers to the position of the children <b>before</b> the drag, which
   *                    matters if some of the existing children in the layout are being dragged.
   * @param insertType  the type of move/insert
   */
protected final void insertComponents(int insertIndex, @NotNull InsertType insertType) {
    NlComponent before = null;
    if (insertIndex != -1 && insertIndex < layout.getChildCount()) {
        before = layout.getChild(insertIndex);
    }
    editor.getModel().addComponents(components, layout, before, insertType);
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Example 28 with NlComponent

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

the class ConvertToConstraintLayoutAction method actionPerformed.

@Override
public void actionPerformed(AnActionEvent e) {
    ScreenView screenView = mySurface.getCurrentScreenView();
    if (screenView == null) {
        return;
    }
    NlComponent target = findTarget(screenView);
    if (target == null) {
        // Shouldn't happen, enforced by update(AnActionEvent)
        return;
    }
    // Step #1: UI
    Project project = mySurface.getProject();
    ConvertToConstraintLayoutForm dialog = new ConvertToConstraintLayoutForm(project);
    if (!dialog.showAndGet()) {
        return;
    }
    boolean flatten = dialog.getFlattenHierarchy();
    boolean includeIds = dialog.getFlattenReferenced();
    // Step #2: Ensure ConstraintLayout is available in the project
    GradleDependencyManager manager = GradleDependencyManager.getInstance(project);
    GradleCoordinate coordinate = GradleCoordinate.parseCoordinateString(SdkConstants.CONSTRAINT_LAYOUT_LIB_ARTIFACT + ":+");
    if (!manager.ensureLibraryIsIncluded(screenView.getModel().getModule(), Collections.singletonList(coordinate), null)) {
        return;
    }
    // Step #3: Migrate
    NlModel model = screenView.getModel();
    @SuppressWarnings("ConstantConditions") ConstraintLayoutConverter converter = new ConstraintLayoutConverter(screenView, target, flatten, includeIds);
    converter.execute();
    // Finally we need to apply the infer constraints action; we can't do that from the above action
    // since we're holding the write lock
    inferConstraints(model);
}
Also used : Project(com.intellij.openapi.project.Project) ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) GradleCoordinate(com.android.ide.common.repository.GradleCoordinate) NlModel(com.android.tools.idea.uibuilder.model.NlModel) GradleDependencyManager(com.android.tools.idea.gradle.dependencies.GradleDependencyManager)

Example 29 with NlComponent

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

the class ConvertToConstraintLayoutAction method update.

@Override
public void update(AnActionEvent e) {
    Presentation presentation = e.getPresentation();
    ScreenView screenView = mySurface.getCurrentScreenView();
    NlComponent target = findTarget(screenView);
    if (target != null) {
        String tagName = target.getTagName();
        // Don't show action if it's already a ConstraintLayout
        if (target.isOrHasSuperclass(CONSTRAINT_LAYOUT)) {
            presentation.setVisible(false);
            return;
        }
        presentation.setVisible(true);
        tagName = tagName.substring(tagName.lastIndexOf('.') + 1);
        presentation.setText("Convert " + tagName + " to ConstraintLayout");
        presentation.setEnabled(true);
    } else {
        presentation.setText(TITLE);
        presentation.setEnabled(false);
        presentation.setVisible(true);
    }
}
Also used : ScreenView(com.android.tools.idea.uibuilder.surface.ScreenView) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) Presentation(com.intellij.openapi.actionSystem.Presentation)

Example 30 with NlComponent

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

the class GenerateLayoutTestSkeletonAction method getBounds.

@NotNull
private static Rectangle getBounds(@NotNull NlComponent component) {
    NlComponent parent = component.getParent();
    Rectangle parentBounds = parent != null ? getBounds(parent) : new Rectangle(0, 0, 1000, 1000);
    ViewInfo viewInfo = component.viewInfo;
    if (viewInfo == null) {
        return new Rectangle(parentBounds.x, parentBounds.y, 100, 100);
    }
    return new Rectangle(viewInfo.getLeft() + parentBounds.x, viewInfo.getTop() + parentBounds.y, viewInfo.getRight() - viewInfo.getLeft(), viewInfo.getBottom() - viewInfo.getTop());
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) NotNull(org.jetbrains.annotations.NotNull)

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