Search in sources :

Example 11 with XmlElementFactory

use of com.intellij.psi.XmlElementFactory in project android by JetBrains.

the class NlModel method createTag.

@NotNull
@VisibleForTesting
public static XmlTag createTag(@NotNull Project project, @NotNull String text) {
    XmlElementFactory elementFactory = XmlElementFactory.getInstance(project);
    XmlTag tag = null;
    if (XmlUtils.parseDocumentSilently(text, false) != null) {
        try {
            tag = elementFactory.createTagFromText(text);
            setNamespaceUri(tag, ANDROID_NS_NAME, ANDROID_URI);
            setNamespaceUri(tag, APP_PREFIX, AUTO_URI);
        } catch (IncorrectOperationException ignore) {
        // Thrown by XmlElementFactory if you try to parse non-valid XML. User might have tried
        // to drop something like plain text -- insert this as a text view instead.
        // However, createTagFromText may not always throw this for invalid XML, so we perform the above parseDocument
        // check first instead.
        }
    }
    if (tag == null) {
        tag = elementFactory.createTagFromText("<TextView xmlns:android=\"http://schemas.android.com/apk/res/android\" " + " android:text=\"" + XmlUtils.toXmlAttributeValue(text) + "\"" + " android:layout_width=\"wrap_content\"" + " android:layout_height=\"wrap_content\"" + "/>");
    }
    return tag;
}
Also used : XmlElementFactory(com.intellij.psi.XmlElementFactory) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag) VisibleForTesting(com.google.common.annotations.VisibleForTesting) NotNull(org.jetbrains.annotations.NotNull)

Example 12 with XmlElementFactory

use of com.intellij.psi.XmlElementFactory in project android by JetBrains.

the class IconPreviewFactory method renderDragImage.

/**
   * Return a component image to display while dragging a component from the palette.
   * Return null if such an image cannot be rendered. The palette must provide a fallback in this case.
   */
@Nullable
public BufferedImage renderDragImage(@NotNull Palette.Item item, @NotNull ScreenView screenView) {
    XmlElementFactory elementFactory = XmlElementFactory.getInstance(screenView.getModel().getProject());
    String xml = item.getDragPreviewXml();
    if (xml.equals(NO_PREVIEW)) {
        return null;
    }
    XmlTag tag;
    try {
        tag = elementFactory.createTagFromText(xml);
    } catch (IncorrectOperationException exception) {
        return null;
    }
    NlModel model = screenView.getModel();
    NlComponent component = ApplicationManager.getApplication().runWriteAction((Computable<NlComponent>) () -> model.createComponent(screenView, tag, null, null, InsertType.CREATE_PREVIEW));
    if (component == null) {
        return null;
    }
    // Some components require a parent to render correctly.
    xml = String.format(LINEAR_LAYOUT, CONTAINER_ID, component.getTag().getText());
    RenderResult result = renderImage(myExecutorService, myRenderTimeoutSeconds, getRenderTask(model.getConfiguration()), xml);
    if (result == null || !result.hasImage()) {
        return null;
    }
    ImagePool.Image image = result.getRenderedImage();
    List<ViewInfo> infos = result.getRootViews();
    if (infos.isEmpty()) {
        return null;
    }
    infos = infos.get(0).getChildren();
    if (infos == null || infos.isEmpty()) {
        return null;
    }
    ViewInfo view = infos.get(0);
    if (image.getHeight() < view.getBottom() || image.getWidth() < view.getRight() || view.getBottom() <= view.getTop() || view.getRight() <= view.getLeft()) {
        return null;
    }
    @AndroidCoordinate int shadowWitdh = SHADOW_SIZE * screenView.getConfiguration().getDensity().getDpiValue() / Density.DEFAULT_DENSITY;
    @SwingCoordinate int shadowIncrement = 1 + Coordinates.getSwingDimension(screenView, shadowWitdh);
    BufferedImage imageCopy = image.getCopy();
    if (imageCopy == null) {
        return null;
    }
    return imageCopy.getSubimage(view.getLeft(), view.getTop(), Math.min(view.getRight() + shadowIncrement, image.getWidth()), Math.min(view.getBottom() + shadowIncrement, image.getHeight()));
}
Also used : XmlElementFactory(com.intellij.psi.XmlElementFactory) BufferedImage(java.awt.image.BufferedImage) ViewInfo(com.android.ide.common.rendering.api.ViewInfo) IncorrectOperationException(com.intellij.util.IncorrectOperationException) XmlTag(com.intellij.psi.xml.XmlTag) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

XmlElementFactory (com.intellij.psi.XmlElementFactory)12 XmlTag (com.intellij.psi.xml.XmlTag)9 XmlAttribute (com.intellij.psi.xml.XmlAttribute)3 IncorrectOperationException (com.intellij.util.IncorrectOperationException)2 HashMap (com.intellij.util.containers.HashMap)2 Map (java.util.Map)2 NotNull (org.jetbrains.annotations.NotNull)2 ViewInfo (com.android.ide.common.rendering.api.ViewInfo)1 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)1 Project (com.intellij.openapi.project.Project)1 Computable (com.intellij.openapi.util.Computable)1 TextRange (com.intellij.openapi.util.TextRange)1 PsiElement (com.intellij.psi.PsiElement)1 XmlFile (com.intellij.psi.xml.XmlFile)1 XmlText (com.intellij.psi.xml.XmlText)1 Task (com.intellij.tasks.Task)1 BufferedImage (java.awt.image.BufferedImage)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1