use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.
the class DragDropInteraction method acceptsDrop.
private boolean acceptsDrop(@NotNull NlComponent parent, @NotNull ViewGroupHandler parentHandler, @AndroidCoordinate int x, @AndroidCoordinate int y) {
ScreenView view = myDesignSurface.getScreenView(x, y);
assert view != null;
ViewHandlerManager manager = ViewHandlerManager.get(view.getModel().getFacet());
Predicate<NlComponent> acceptsChild = child -> parentHandler.acceptsChild(parent, child, x, y);
Predicate<NlComponent> acceptsParent = child -> {
ViewHandler childHandler = manager.getHandler(child);
return childHandler != null && childHandler.acceptsParent(parent, child);
};
return myDraggedComponents.stream().allMatch(acceptsChild.and(acceptsParent));
}
use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.
the class DragDropInteraction method findViewGroupHandlerAt.
@Nullable
private ViewGroupHandler findViewGroupHandlerAt(@AndroidCoordinate int x, @AndroidCoordinate int y) {
final ScreenView screenView = myDesignSurface.getScreenView(x, y);
if (screenView == null) {
return null;
}
NlModel model = screenView.getModel();
NlComponent component = model.findLeafAt(x, y, true);
component = excludeDraggedComponents(component);
if (component == myCachedComponent && myCachedHandler != null) {
return myCachedHandler;
}
myCachedComponent = component;
myCachedHandler = null;
ViewHandlerManager handlerManager = ViewHandlerManager.get(model.getFacet());
while (component != null) {
Object handler = handlerManager.getHandler(component);
if (handler instanceof ViewGroupHandler && acceptsDrop(component, (ViewGroupHandler) handler, x, y)) {
myCachedHandler = (ViewGroupHandler) handlerManager.getHandler(component);
// HACK: This method should not side-effect set this; instead the method should compute it!
myDragReceiver = component;
return myCachedHandler;
}
component = component.getParent();
}
return null;
}
use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.
the class DesignSurfaceActionHandler method pasteOperation.
private boolean pasteOperation(boolean checkOnly) {
ScreenView screenView = mySurface.getCurrentScreenView();
if (screenView == null) {
return false;
}
List<NlComponent> selection = screenView.getSelectionModel().getSelection();
NlComponent receiver = !selection.isEmpty() ? selection.get(0) : null;
if (receiver == null) {
// In the case where there is no selection but we only have a root component, use that one
List<NlComponent> components = screenView.getModel().getComponents();
if (components.size() == 1) {
receiver = components.get(0);
}
}
if (receiver == null) {
return false;
}
NlComponent before;
NlModel model = screenView.getModel();
ViewHandlerManager handlerManager = ViewHandlerManager.get(model.getProject());
ViewHandler handler = handlerManager.getHandler(receiver);
if (handler instanceof ViewGroupHandler) {
before = receiver.getChild(0);
} else {
before = receiver.getNextSibling();
receiver = receiver.getParent();
if (receiver == null) {
return false;
}
}
DnDTransferItem item = getClipboardData();
if (item == null) {
return false;
}
InsertType insertType = model.determineInsertType(DragType.PASTE, item, checkOnly);
List<NlComponent> pasted = model.createComponents(screenView, item, insertType);
if (!model.canAddComponents(pasted, receiver, before)) {
return false;
}
if (checkOnly) {
return true;
}
model.addComponents(pasted, receiver, before, insertType);
return true;
}
use of com.android.tools.idea.uibuilder.handlers.ViewHandlerManager in project android by JetBrains.
the class NlModel method createComponent.
public NlComponent createComponent(@Nullable ScreenView screenView, @NotNull XmlTag tag, @Nullable NlComponent parent, @Nullable NlComponent before, @NotNull InsertType insertType) {
if (parent != null) {
// Creating a component intended to be inserted into an existing layout
XmlTag parentTag = parent.getTag();
if (before != null) {
tag = (XmlTag) parentTag.addBefore(tag, before.getTag());
} else {
tag = parentTag.addSubTag(tag, false);
}
// Required attribute for all views; drop handlers can adjust as necessary
if (tag.getAttribute(ATTR_LAYOUT_WIDTH, ANDROID_URI) == null) {
tag.setAttribute(ATTR_LAYOUT_WIDTH, ANDROID_URI, VALUE_WRAP_CONTENT);
}
if (tag.getAttribute(ATTR_LAYOUT_HEIGHT, ANDROID_URI) == null) {
tag.setAttribute(ATTR_LAYOUT_HEIGHT, ANDROID_URI, VALUE_WRAP_CONTENT);
}
} else {
// No namespace yet: use the default prefix instead
if (tag.getAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_WIDTH) == null) {
tag.setAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_WIDTH, VALUE_WRAP_CONTENT);
}
if (tag.getAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_HEIGHT) == null) {
tag.setAttribute(ANDROID_NS_NAME_PREFIX + ATTR_LAYOUT_HEIGHT, VALUE_WRAP_CONTENT);
}
}
NlComponent child = new NlComponent(this, tag);
if (parent != null) {
parent.addChild(child, before);
}
// Notify view handlers
ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(getProject());
ViewHandler childHandler = viewHandlerManager.getHandler(child);
if (childHandler != null && screenView != null) {
ViewEditor editor = new ViewEditorImpl(screenView);
boolean ok = childHandler.onCreate(editor, parent, child, insertType);
if (!ok) {
if (parent != null) {
parent.removeChild(child);
}
tag.delete();
return null;
}
}
if (parent != null) {
ViewHandler parentHandler = viewHandlerManager.getHandler(parent);
if (parentHandler instanceof ViewGroupHandler) {
((ViewGroupHandler) parentHandler).onChildInserted(parent, child, insertType);
}
}
return child;
}
Aggregations