Search in sources :

Example 1 with NlModel

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

the class AnchorTarget method mouseRelease.

/**
   * On mouseRelease, we can either disconnect the current anchor (if the mouse release is on ourselve)
   * or connect the anchor to a given target. Modifications are applied first in memory then commited
   * to the XML model.
   *
   * @param x
   * @param y
   * @param closestTarget
   */
@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    myLastX = -1;
    myLastY = -1;
    if (myComponent.getParent() != null) {
        myComponent.getParent().setExpandTargetArea(false);
    }
    if (closestTarget != null && closestTarget instanceof AnchorTarget && !(((AnchorTarget) closestTarget).isConnected(this))) {
        NlComponent component = myComponent.getNlComponent();
        if (closestTarget == this) {
            disconnectMe(component);
        } else {
            String attribute = getAttribute(closestTarget);
            if (attribute != null) {
                AnchorTarget targetAnchor = (AnchorTarget) closestTarget;
                NlComponent targetComponent = targetAnchor.myComponent.getNlComponent();
                AttributesTransaction attributes = connectMe(component, attribute, targetComponent);
                NlModel nlModel = component.getModel();
                Project project = nlModel.getProject();
                XmlFile file = nlModel.getFile();
                String label = "Constraint Connected";
                WriteCommandAction action = new WriteCommandAction(project, label, file) {

                    @Override
                    protected void run(@NotNull Result result) throws Throwable {
                        attributes.commit();
                    }
                };
                action.execute();
                myComponent.getScene().needsLayout(Scene.ANIMATED_LAYOUT);
            }
        }
    }
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) XmlFile(com.intellij.psi.xml.XmlFile) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Example 2 with NlModel

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

the class GuidelineCycleTarget method mouseRelease.

@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    AttributesTransaction attributes = myComponent.getNlComponent().startAttributeTransaction();
    String begin = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_BEGIN);
    String end = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_END);
    String percent = attributes.getAttribute(SdkConstants.SHERPA_URI, SdkConstants.LAYOUT_CONSTRAINT_GUIDE_PERCENT);
    SceneComponent parent = myComponent.getParent();
    int value = myComponent.getDrawY() - parent.getDrawY();
    int dimension = parent.getDrawHeight();
    if (!myIsHorizontal) {
        value = myComponent.getDrawX() - parent.getDrawX();
        dimension = parent.getDrawWidth();
    }
    if (begin != null) {
        setEnd(attributes, dimension - value);
    } else if (end != null) {
        setPercent(attributes, value / (float) dimension);
    } else if (percent != null) {
        setBegin(attributes, value);
    }
    attributes.apply();
    NlModel nlModel = myComponent.getNlComponent().getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    String label = "Cycle Guideline";
    WriteCommandAction action = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            attributes.commit();
        }
    };
    action.execute();
}
Also used : WriteCommandAction(com.intellij.openapi.command.WriteCommandAction) Project(com.intellij.openapi.project.Project) XmlFile(com.intellij.psi.xml.XmlFile) SceneComponent(com.android.tools.idea.uibuilder.scene.SceneComponent) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) Result(com.intellij.openapi.application.Result)

Example 3 with NlModel

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

the class IconPreviewFactoryTest method setUp.

@Override
public void setUp() throws Exception {
    super.setUp();
    Palette palette = loadPalette();
    List<Palette.Item> items = new ArrayList<>();
    palette.accept(items::add);
    myItem = items.get(0);
    NlModel model = createModel();
    myScreenView = surface().screen(model).getScreen();
    myFactory = new IconPreviewFactory();
    myFacet.setRenderService(new MyRenderService(myFacet));
    myFactory.myRenderTimeoutSeconds = Long.MAX_VALUE;
}
Also used : ArrayList(java.util.ArrayList) NlModel(com.android.tools.idea.uibuilder.model.NlModel)

Example 4 with NlModel

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

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

Aggregations

NlModel (com.android.tools.idea.uibuilder.model.NlModel)71 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)33 ModelBuilder (com.android.tools.idea.uibuilder.fixtures.ModelBuilder)19 NotNull (org.jetbrains.annotations.NotNull)18 XmlFile (com.intellij.psi.xml.XmlFile)14 Project (com.intellij.openapi.project.Project)12 AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)11 Result (com.intellij.openapi.application.Result)11 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)11 ScreenView (com.android.tools.idea.uibuilder.surface.ScreenView)6 ComponentDescriptor (com.android.tools.idea.uibuilder.fixtures.ComponentDescriptor)5 NlProperty (com.android.tools.idea.uibuilder.property.NlProperty)5 NlPropertyItem (com.android.tools.idea.uibuilder.property.NlPropertyItem)5 AttributeDefinition (org.jetbrains.android.dom.attrs.AttributeDefinition)5 Configuration (com.android.tools.idea.configurations.Configuration)4 DesignSurface (com.android.tools.idea.uibuilder.surface.DesignSurface)3 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)3 SelectionModel (com.android.tools.idea.uibuilder.model.SelectionModel)2 XmlTag (com.intellij.psi.xml.XmlTag)2 BufferedImage (java.awt.image.BufferedImage)2