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;
}
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()));
}
Aggregations