Search in sources :

Example 16 with NlComponent

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

the class AnchorTarget method mouseDrag.

/**
   * On mouse drag, we can connect (in memory) to existing targets, or revert to the
   * original state that we capatured on mouseDown.
   *
   * @param x
   * @param y
   * @param closestTarget
   */
@Override
public void mouseDrag(int x, int y, @Nullable Target closestTarget) {
    myLastX = x;
    myLastY = y;
    if (closestTarget != null && closestTarget instanceof AnchorTarget) {
        NlComponent component = myComponent.getNlComponent();
        String attribute = getAttribute(closestTarget);
        if (attribute != null) {
            AnchorTarget targetAnchor = (AnchorTarget) closestTarget;
            if (targetAnchor.myComponent != myComponent && !targetAnchor.isConnected(this)) {
                NlComponent targetComponent = targetAnchor.myComponent.getNlComponent();
                connectMe(component, attribute, targetComponent);
                return;
            }
        }
    }
    revertToPreviousState();
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Example 17 with NlComponent

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

the class AnchorTarget method mouseDown.

@Override
public void mouseDown(int x, int y) {
    myLastX = -1;
    myLastY = -1;
    myConnectedX = -1;
    myConnectedY = -1;
    NlComponent component = myComponent.getNlComponent();
    mPreviousAttributes.clear();
    mPreviousAttributes.put(SdkConstants.ATTR_LAYOUT_EDITOR_ABSOLUTE_X, component.getLiveAttribute(SdkConstants.TOOLS_URI, SdkConstants.ATTR_LAYOUT_EDITOR_ABSOLUTE_X));
    mPreviousAttributes.put(SdkConstants.ATTR_LAYOUT_EDITOR_ABSOLUTE_Y, component.getLiveAttribute(SdkConstants.TOOLS_URI, SdkConstants.ATTR_LAYOUT_EDITOR_ABSOLUTE_Y));
    mPreviousAttributes.put(SdkConstants.ATTR_LAYOUT_BASELINE_TO_BASELINE_OF, component.getLiveAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_BASELINE_TO_BASELINE_OF));
    if (myComponent.getParent() != null) {
        myComponent.getParent().setExpandTargetArea(true);
    }
    switch(myType) {
        case LEFT:
            {
                rememberPreviousAttribute(SdkConstants.SHERPA_URI, ourLeftAttributes);
            }
            break;
        case RIGHT:
            {
                rememberPreviousAttribute(SdkConstants.SHERPA_URI, ourRightAttributes);
            }
            break;
        case TOP:
            {
                rememberPreviousAttribute(SdkConstants.SHERPA_URI, ourTopAttributes);
            }
            break;
        case BOTTOM:
            {
                rememberPreviousAttribute(SdkConstants.SHERPA_URI, ourBottomAttributes);
            }
            break;
        case BASELINE:
            {
                rememberPreviousAttribute(SdkConstants.SHERPA_URI, ourTopAttributes);
                rememberPreviousAttribute(SdkConstants.SHERPA_URI, ourBottomAttributes);
                mPreviousAttributes.put(SdkConstants.ATTR_LAYOUT_MARGIN_TOP, component.getLiveAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_MARGIN_TOP));
                mPreviousAttributes.put(SdkConstants.ATTR_LAYOUT_MARGIN_BOTTOM, component.getLiveAttribute(SdkConstants.ANDROID_URI, SdkConstants.ATTR_LAYOUT_MARGIN_BOTTOM));
                mPreviousAttributes.put(SdkConstants.ATTR_LAYOUT_VERTICAL_BIAS, component.getLiveAttribute(SdkConstants.SHERPA_URI, SdkConstants.ATTR_LAYOUT_VERTICAL_BIAS));
            }
            break;
    }
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent)

Example 18 with NlComponent

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

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

the class DragTarget method getTargetComponent.

//endregion
/////////////////////////////////////////////////////////////////////////////
//region Utilities
/////////////////////////////////////////////////////////////////////////////
@Nullable
private SceneComponent getTargetComponent(@NotNull String uri, @NotNull ArrayList<String> attributes) {
    NlComponent nlComponent = myComponent.getNlComponent();
    String target = null;
    int count = attributes.size();
    for (int i = 0; i < count; i++) {
        target = nlComponent.getAttribute(uri, attributes.get(i));
        if (target != null) {
            if (target.equalsIgnoreCase(SdkConstants.ATTR_PARENT)) {
                return myComponent.getParent();
            }
            target = NlComponent.extractId(target);
            if (target != null) {
                return myComponent.getScene().getSceneComponent(target);
            }
        }
    }
    return null;
}
Also used : NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with NlComponent

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

the class NlPropertyOrderingTest method testGrouping.

public void testGrouping() {
    @Language("XML") String source = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" + "<RelativeLayout xmlns:android=\"http://schemas.android.com/apk/res/android\" >" + "  <TextView />" + "</RelativeLayout>";
    NlComponent component = getFirstComponent(source);
    Table<String, String, NlPropertyItem> properties = NlProperties.getInstance().getProperties(ImmutableList.of(component));
    List<NlPropertyItem> propertyList = new ArrayList<>(properties.values());
    List<PTableItem> items = new NlPropertiesGrouper().group(propertyList, ImmutableList.of(component));
    // assert that all padding related attributes are grouped together
    PTableItem padding = findItemByName(items, "Padding");
    assertNotNull(padding);
    assertNotNull("Attribute paddingStart missing inside padding attributes", findItemByName(padding.getChildren(), "paddingStart"));
    assertNotNull("Attribute paddingEnd missing inside padding attributes", findItemByName(padding.getChildren(), "paddingEnd"));
    // assert that textview attributes are grouped together (disabled for now)
    //PTableItem textView = findItemByName(items, "TextView");
    //assertNotNull("Missing group for TextView attributes", textView);
    //assertNotNull("Attribute capitalize missing inside textview attributes", findItemByName(textView.getChildren(), "capitalize"));
    //assertNotNull("Attribute password missing inside textview attributes", findItemByName(textView.getChildren(), "password"));
    // certain special attrs should be at the top level
    assertNotNull("Missing attribute id at the top level after grouping", findItemByName(items, "id"));
}
Also used : Language(org.intellij.lang.annotations.Language) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ArrayList(java.util.ArrayList) PTableItem(com.android.tools.idea.uibuilder.property.ptable.PTableItem)

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