Search in sources :

Example 41 with NlModel

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

the class ViewEditorImpl method measureChildren.

@Nullable
@Override
public Map<NlComponent, Dimension> measureChildren(@NotNull NlComponent parent, @Nullable RenderTask.AttributeFilter filter) {
    // TODO: Reuse snapshot!
    Map<NlComponent, Dimension> unweightedSizes = Maps.newHashMap();
    XmlTag parentTag = parent.getTag();
    if (parentTag.isValid()) {
        if (parent.getChildCount() == 0) {
            return Collections.emptyMap();
        }
        Map<XmlTag, NlComponent> tagToComponent = Maps.newHashMapWithExpectedSize(parent.getChildCount());
        for (NlComponent child : parent.getChildren()) {
            tagToComponent.put(child.getTag(), child);
        }
        NlModel model = myScreen.getModel();
        XmlFile xmlFile = model.getFile();
        AndroidFacet facet = model.getFacet();
        RenderService renderService = RenderService.get(facet);
        RenderLogger logger = renderService.createLogger();
        final RenderTask task = renderService.createTask(xmlFile, getConfiguration(), logger, null);
        if (task == null) {
            return null;
        }
        // Measure unweighted bounds
        Map<XmlTag, ViewInfo> map = task.measureChildren(parentTag, filter);
        task.dispose();
        if (map != null) {
            for (Map.Entry<XmlTag, ViewInfo> entry : map.entrySet()) {
                ViewInfo viewInfo = entry.getValue();
                viewInfo = RenderService.getSafeBounds(viewInfo);
                Dimension size = new Dimension(viewInfo.getRight() - viewInfo.getLeft(), viewInfo.getBottom() - viewInfo.getTop());
                NlComponent child = tagToComponent.get(entry.getKey());
                if (child != null) {
                    unweightedSizes.put(child, size);
                }
            }
        }
    }
    return unweightedSizes;
}
Also used : RenderLogger(com.android.tools.idea.rendering.RenderLogger) XmlFile(com.intellij.psi.xml.XmlFile) RenderTask(com.android.tools.idea.rendering.RenderTask) NlModel(com.android.tools.idea.uibuilder.model.NlModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) RenderService(com.android.tools.idea.rendering.RenderService) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Example 42 with NlModel

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

the class NlPreviewForm method setFile.

public boolean setFile(@Nullable PsiFile file) {
    if (!isActive) {
        // The form is not active so we just save the file to show once activate() is called
        myInactiveFile = (XmlFile) file;
        if (file != null) {
            return false;
        }
    }
    if (myPendingFile != null) {
        if (file == myPendingFile.file) {
            return false;
        }
        myPendingFile.invalidate();
        // Set the model to null so the progressbar is displayed
        mySurface.setModel(null);
    } else if (file == myFile) {
        return false;
    }
    AndroidFacet facet = file instanceof XmlFile ? AndroidFacet.getInstance(file) : null;
    if (facet == null || file.getVirtualFile() == null) {
        myPendingFile = null;
        myFile = null;
        setActiveModel(null);
    } else {
        XmlFile xmlFile = (XmlFile) file;
        NlModel model = NlModel.create(mySurface, null, facet, xmlFile);
        myPendingFile = new Pending(xmlFile, model);
    }
    return true;
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) NlModel(com.android.tools.idea.uibuilder.model.NlModel) AndroidFacet(org.jetbrains.android.facet.AndroidFacet)

Example 43 with NlModel

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

the class ResizeTarget method mouseRelease.

@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    NlComponent component = myComponent.getNlComponent();
    AttributesTransaction attributes = component.startAttributeTransaction();
    updateAttributes(attributes, x, y);
    attributes.apply();
    NlModel nlModel = component.getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    String label = "Constraint";
    WriteCommandAction action = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            attributes.commit();
        }
    };
    action.execute();
    myComponent.getScene().needsLayout(Scene.IMMEDIATE_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 44 with NlModel

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

the class DragTarget method mouseRelease.

@Override
public void mouseRelease(int x, int y, @Nullable Target closestTarget) {
    if (myComponent.getParent() != null) {
        boolean commitChanges = true;
        if (Math.abs(x - myFirstMouseX) <= 1 && Math.abs(y - myFirstMouseY) <= 1) {
            commitChanges = false;
        }
        NlComponent component = myComponent.getNlComponent();
        AttributesTransaction attributes = component.startAttributeTransaction();
        int dx = x - myOffsetX;
        int dy = y - myOffsetY;
        if (myCurrentNotchX != null) {
            dx = myCurrentNotchX.apply(dx);
            if (myComponent.allowsAutoConnect()) {
                myCurrentNotchX.apply(attributes);
            }
            myCurrentNotchX = null;
        }
        if (myCurrentNotchY != null) {
            dy = myCurrentNotchY.apply(dy);
            if (myComponent.allowsAutoConnect()) {
                myCurrentNotchY.apply(attributes);
            }
            myCurrentNotchY = null;
        }
        updateAttributes(attributes, dx, dy);
        cleanup(attributes);
        attributes.apply();
        if (commitChanges) {
            NlModel nlModel = component.getModel();
            Project project = nlModel.getProject();
            XmlFile file = nlModel.getFile();
            String label = "Component dragged";
            WriteCommandAction action = new WriteCommandAction(project, label, file) {

                @Override
                protected void run(@NotNull Result result) throws Throwable {
                    attributes.commit();
                }
            };
            action.execute();
        }
    }
    if (myChangedComponent) {
        myComponent.getScene().needsLayout(Scene.IMMEDIATE_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 45 with NlModel

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

the class AnchorTarget method disconnectMe.

/**
   * Disconnect the anchor
   *
   * @param component
   */
private void disconnectMe(NlComponent component) {
    String label = "Constraint Disconnected";
    NlModel nlModel = component.getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    AttributesTransaction attributes = component.startAttributeTransaction();
    clearMe(attributes);
    cleanup(attributes);
    attributes.apply();
    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) 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)

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