Search in sources :

Example 16 with AttributesTransaction

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

the class ConstraintTarget method cycleChainStyle.

protected void cycleChainStyle(SceneComponent chainHeadComponent, String orientationStyle) {
    NlComponent chainHead = chainHeadComponent.getNlComponent();
    String chainStyle = chainHead.getLiveAttribute(SdkConstants.SHERPA_URI, orientationStyle);
    if (chainStyle != null) {
        if (chainStyle.equalsIgnoreCase(SdkConstants.ATTR_LAYOUT_CHAIN_SPREAD)) {
            chainStyle = SdkConstants.ATTR_LAYOUT_CHAIN_SPREAD_INSIDE;
        } else if (chainStyle.equalsIgnoreCase(SdkConstants.ATTR_LAYOUT_CHAIN_SPREAD_INSIDE)) {
            chainStyle = SdkConstants.ATTR_LAYOUT_CHAIN_PACKED;
        } else if (chainStyle.equalsIgnoreCase(SdkConstants.ATTR_LAYOUT_CHAIN_PACKED)) {
            chainStyle = SdkConstants.ATTR_LAYOUT_CHAIN_SPREAD;
        }
    } else {
        chainStyle = SdkConstants.ATTR_LAYOUT_CHAIN_SPREAD;
    }
    AttributesTransaction transaction = chainHead.startAttributeTransaction();
    transaction.setAttribute(SdkConstants.SHERPA_URI, orientationStyle, chainStyle);
    transaction.apply();
    NlModel nlModel = chainHead.getModel();
    Project project = nlModel.getProject();
    XmlFile file = nlModel.getFile();
    String label = "Cycle chain style";
    WriteCommandAction action = new WriteCommandAction(project, label, file) {

        @Override
        protected void run(@NotNull Result result) throws Throwable {
            transaction.commit();
        }
    };
    action.execute();
    myComponent.getScene().needsRebuildList();
}
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 17 with AttributesTransaction

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

the class IncludeTagCreator method createNewIncludedLayout.

/**
   * Create a new layout that will be included as a child of the mockup component
   */
private String createNewIncludedLayout() {
    AndroidFacet facet = getMockup().getComponent().getModel().getFacet();
    ResourceFolderType folderType = AndroidResourceUtil.XML_FILE_RESOURCE_TYPES.get(ResourceType.LAYOUT);
    XmlFile newFile = CreateResourceFileAction.createFileResource(facet, folderType, null, null, null, true, null, null, null, false);
    if (newFile == null) {
        return null;
    }
    XmlTag rootTag = newFile.getRootTag();
    if (rootTag == null) {
        return null;
    }
    NlModel nlModel = NlModel.create(getScreenView().getSurface(), newFile.getProject(), facet, newFile);
    ModelListener listener = new ModelListener() {

        @Override
        public void modelChanged(@NotNull NlModel model) {
        }

        @Override
        public void modelRendered(@NotNull NlModel model) {
            model.removeListener(this);
            if (model.getComponents().isEmpty()) {
                return;
            }
            NlComponent component = model.getComponents().get(0);
            final AttributesTransaction transaction = component.startAttributeTransaction();
            addShowInAttribute(transaction);
            addSizeAttributes(transaction, getAndroidBounds());
            addMockupAttributes(transaction, getSelectionBounds());
            WriteCommandAction.runWriteCommandAction(model.getProject(), (Computable) transaction::commit);
        }

        @Override
        public void modelChangedOnLayout(@NotNull NlModel model, boolean animate) {
        // Do nothing
        }
    };
    nlModel.addListener(listener);
    nlModel.requestRender();
    return getResourceName(newFile);
}
Also used : XmlFile(com.intellij.psi.xml.XmlFile) NlComponent(com.android.tools.idea.uibuilder.model.NlComponent) ResourceFolderType(com.android.resources.ResourceFolderType) ModelListener(com.android.tools.idea.uibuilder.model.ModelListener) NlModel(com.android.tools.idea.uibuilder.model.NlModel) NotNull(org.jetbrains.annotations.NotNull) AndroidFacet(org.jetbrains.android.facet.AndroidFacet) AttributesTransaction(com.android.tools.idea.uibuilder.model.AttributesTransaction) XmlTag(com.intellij.psi.xml.XmlTag)

Aggregations

AttributesTransaction (com.android.tools.idea.uibuilder.model.AttributesTransaction)17 NlModel (com.android.tools.idea.uibuilder.model.NlModel)11 XmlFile (com.intellij.psi.xml.XmlFile)11 Result (com.intellij.openapi.application.Result)10 WriteCommandAction (com.intellij.openapi.command.WriteCommandAction)10 Project (com.intellij.openapi.project.Project)10 NotNull (org.jetbrains.annotations.NotNull)10 NlComponent (com.android.tools.idea.uibuilder.model.NlComponent)9 ResourceFolderType (com.android.resources.ResourceFolderType)1 ModelListener (com.android.tools.idea.uibuilder.model.ModelListener)1 Scene (com.android.tools.idea.uibuilder.scene.Scene)1 SceneComponent (com.android.tools.idea.uibuilder.scene.SceneComponent)1 XmlTag (com.intellij.psi.xml.XmlTag)1 AndroidFacet (org.jetbrains.android.facet.AndroidFacet)1