use of com.android.tools.idea.uibuilder.api.ViewHandler in project android by JetBrains.
the class BlueprintLayer method drawComponent.
/**
* Draw the given component and its children
*
* @param gc the graphics context
* @param component the component we want to draw
* @param viewHandlerManager the view handler
*/
private boolean drawComponent(@NotNull Graphics2D gc, @NotNull NlComponent component, @NotNull ViewHandlerManager viewHandlerManager, boolean parentHandlesPainting) {
boolean needsRepaint = false;
boolean handlesPainting = false;
if (component.viewInfo != null) {
ViewHandler handler = component.getViewHandler();
// Check if the view handler handles the painting
if (handler != null && handler instanceof ViewGroupHandler) {
ViewGroupHandler viewGroupHandler = (ViewGroupHandler) handler;
if (viewGroupHandler.handlesPainting()) {
if (handler.paintConstraints(myScreenView, gc, component)) {
return needsRepaint;
}
needsRepaint |= viewGroupHandler.drawGroup(gc, myScreenView, component);
handlesPainting = true;
}
}
if (!handlesPainting && !parentHandlesPainting) {
// If not, layout the component ourselves
Graphics2D g = (Graphics2D) gc.create();
int x = getSwingX(myScreenView, component.x);
int y = getSwingY(myScreenView, component.y);
int w = getSwingDimension(myScreenView, component.w);
int h = getSwingDimension(myScreenView, component.h);
drawComponentBackground(g, component);
String name = component.getTagName();
name = name.substring(name.lastIndexOf('.') + 1);
Font font = BLUEPRINT_TEXT_FONT;
g.setFont(font);
g.setColor(BLUEPRINT_FG_COLOR);
String id = component.getId();
int lineHeight = g.getFontMetrics().getHeight();
FontRenderContext fontRenderContext = g.getFontRenderContext();
if (id != null && h > lineHeight * 2) {
// Can fit both
Rectangle2D classBounds = font.getStringBounds(name, fontRenderContext);
Rectangle2D idBounds = font.getStringBounds(id, fontRenderContext);
int textY = y + h / 2;
int textX = x + w / 2 - ((int) classBounds.getWidth()) / 2;
if (component.isRoot()) {
textX = x + lineHeight;
textY = y - (int) (classBounds.getHeight() + idBounds.getHeight());
}
g.drawString(name, textX, textY);
if (component.isRoot()) {
textX = x + lineHeight;
textY = y - (int) (idBounds.getHeight());
} else {
textX = x + w / 2 - ((int) idBounds.getWidth()) / 2;
textY += (int) (idBounds.getHeight());
}
g.drawString(id, textX, textY);
} else {
// Only room for a single line: prioritize the id if it's available, otherwise the class name
String text = id != null ? id : name;
Rectangle2D stringBounds = font.getStringBounds(text, fontRenderContext);
int textX = x + w / 2 - ((int) stringBounds.getWidth()) / 2;
int textY = y + h / 2 + ((int) stringBounds.getHeight()) / 2;
g.drawString(text, textX, textY);
}
g.dispose();
}
}
// Draw the children of the component...
for (NlComponent child : component.getChildren()) {
needsRepaint |= drawComponent(gc, child, viewHandlerManager, handlesPainting);
}
return needsRepaint;
}
use of com.android.tools.idea.uibuilder.api.ViewHandler in project android by JetBrains.
the class ViewHandlerManager method findLayoutHandler.
/**
* Finds the nearest layout/view group handler for the given component.
*
* @param component the component to search from
* @param strict if true, only consider parents of the component, not the component itself
*/
@Nullable
public ViewGroupHandler findLayoutHandler(@NotNull NlComponent component, boolean strict) {
NlComponent curr = component;
if (strict) {
curr = curr.getParent();
}
while (curr != null) {
ViewHandler handler = getHandler(curr);
if (handler instanceof ViewGroupHandler) {
return (ViewGroupHandler) handler;
}
curr = curr.getParent();
}
return null;
}
use of com.android.tools.idea.uibuilder.api.ViewHandler in project android by JetBrains.
the class ViewHandlerManager method getHandler.
/**
* Gets the {@link ViewHandler} associated with the given XML tag, if any
*
* @param viewTag the tag to look up
* @return the corresponding view handler, if any
*/
@Nullable
public ViewHandler getHandler(@NotNull String viewTag) {
ViewHandler handler = myHandlers.get(viewTag);
if (handler == null) {
if (viewTag.indexOf('.') != -1) {
String tag = NlComponent.viewClassToTag(viewTag);
if (!tag.equals(viewTag)) {
handler = getHandler(tag);
if (handler != null) {
// Alias fully qualified widget name to tag
myHandlers.put(viewTag, handler);
return handler;
}
}
}
handler = createHandler(viewTag);
myHandlers.put(viewTag, handler);
}
return handler != NONE ? handler : null;
}
use of com.android.tools.idea.uibuilder.api.ViewHandler in project android by JetBrains.
the class PaletteTestCase method checkComponent.
private void checkComponent(@NotNull NlComponent component, @NotNull String expectedTitle, @NotNull Icon expectedIcon) {
ViewHandlerManager handlerManager = ViewHandlerManager.get(getProject());
ViewHandler handler = handlerManager.getHandlerOrDefault(component);
String title = handler.getTitle(component);
String attrs = handler.getTitleAttributes(component);
if (!StringUtil.isEmpty(attrs)) {
title += " " + attrs;
}
assertEquals(component.getTagName() + ".Component.Title", expectedTitle, title);
assertEquals(component.getTagName() + ".Component.Icon", expectedIcon, handler.getIcon(component));
}
use of com.android.tools.idea.uibuilder.api.ViewHandler in project android by JetBrains.
the class StyleFilter method getWidgetStyleNames.
@NotNull
private static List<String> getWidgetStyleNames(@NotNull Project project, @NotNull ResourceResolver resolver, @NotNull String tagName) {
ViewHandlerManager manager = ViewHandlerManager.get(project);
ViewHandler handler = manager.getHandler(tagName);
if (handler == null) {
return Collections.emptyList();
}
List<String> possibleNames = handler.getBaseStyles(tagName);
ResourceValueMap frameworkStyles = resolver.getFrameworkResources().get(ResourceType.STYLE);
ResourceValueMap projectStyles = resolver.getProjectResources().get(ResourceType.STYLE);
// This is usually a small list
List<String> names = new ArrayList<>(4);
for (String styleName : possibleNames) {
if (frameworkStyles.containsKey(styleName)) {
names.add(SdkConstants.PREFIX_ANDROID + styleName);
}
if (projectStyles.containsKey(styleName)) {
names.add(styleName);
}
}
return names;
}
Aggregations