use of com.android.tools.idea.uibuilder.handlers.ViewEditorImpl in project android by JetBrains.
the class DragDropInteraction method moveTo.
private void moveTo(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask final int modifiers, boolean commit) {
myScreenView = myDesignSurface.getScreenView(x, y);
if (myScreenView == null) {
return;
}
myDesignSurface.getLayeredPane().scrollRectToVisible(new Rectangle(x - NlConstants.DEFAULT_SCREEN_OFFSET_X, y - NlConstants.DEFAULT_SCREEN_OFFSET_Y, 2 * NlConstants.DEFAULT_SCREEN_OFFSET_X, 2 * NlConstants.DEFAULT_SCREEN_OFFSET_Y));
final int ax = Coordinates.getAndroidX(myScreenView, x);
final int ay = Coordinates.getAndroidY(myScreenView, y);
Project project = myScreenView.getModel().getProject();
ViewGroupHandler handler = findViewGroupHandlerAt(ax, ay);
if (handler != myCurrentHandler) {
if (myDragHandler != null) {
myDragHandler.cancel();
myDragHandler = null;
myScreenView.getSurface().repaint();
}
myCurrentHandler = handler;
if (myCurrentHandler != null) {
assert myDragReceiver != null;
String error = null;
ViewHandlerManager viewHandlerManager = ViewHandlerManager.get(project);
for (NlComponent component : myDraggedComponents) {
if (!myCurrentHandler.acceptsChild(myDragReceiver, component, ax, ay)) {
error = String.format("<%1$s> does not accept <%2$s> as a child", myDragReceiver.getTagName(), component.getTagName());
break;
}
ViewHandler viewHandler = viewHandlerManager.getHandler(component);
if (viewHandler != null && !viewHandler.acceptsParent(myDragReceiver, component)) {
error = String.format("<%1$s> does not accept <%2$s> as a parent", component.getTagName(), myDragReceiver.getTagName());
break;
}
}
if (error == null) {
myDragHandler = myCurrentHandler.createDragHandler(new ViewEditorImpl(myScreenView), myDragReceiver, myDraggedComponents, myType);
if (myDragHandler != null) {
myDragHandler.start(Coordinates.getAndroidX(myScreenView, myStartX), Coordinates.getAndroidY(myScreenView, myStartY), myStartMask);
}
} else {
myCurrentHandler = null;
}
}
}
if (myDragHandler != null && myCurrentHandler != null) {
String error = myDragHandler.update(ax, ay, modifiers);
final List<NlComponent> added = Lists.newArrayList();
if (commit && error == null) {
added.addAll(myDraggedComponents);
final NlModel model = myScreenView.getModel();
XmlFile file = model.getFile();
String label = myType.getDescription();
WriteCommandAction action = new WriteCommandAction(project, label, file) {
@Override
protected void run(@NotNull Result result) throws Throwable {
InsertType insertType = model.determineInsertType(myType, myTransferItem, false);
// TODO: Run this *after* making a copy
myDragHandler.commit(ax, ay, modifiers, insertType);
}
};
action.execute();
model.notifyModified(NlModel.ChangeType.DND_COMMIT);
// Select newly dropped components
model.getSelectionModel().setSelection(added);
}
myScreenView.getSurface().repaint();
}
}
use of com.android.tools.idea.uibuilder.handlers.ViewEditorImpl in project android by JetBrains.
the class ScrollInteraction method createScrollInteraction.
/**
* Creates a new {@link ScrollInteraction} if any of the components in the component hierarchy can handle scrolling.
* @return the {@link ScrollInteraction} or null if none of the components handle the scrolling
*/
@Nullable
public static ScrollInteraction createScrollInteraction(@NonNull ScreenView screenView, @NonNull NlComponent component) {
NlComponent currentComponent = component;
ScrollHandler scrollHandler = null;
ViewEditor editor = new ViewEditorImpl(screenView);
// Find the component that is the lowest in the hierarchy and can take the scrolling events
while (currentComponent != null) {
ViewGroupHandler viewGroupHandler = currentComponent.getViewGroupHandler();
scrollHandler = viewGroupHandler != null ? viewGroupHandler.createScrollHandler(editor, currentComponent) : null;
if (scrollHandler != null) {
break;
}
currentComponent = currentComponent.getParent();
}
if (scrollHandler == null) {
return null;
}
return new ScrollInteraction(screenView, scrollHandler);
}
use of com.android.tools.idea.uibuilder.handlers.ViewEditorImpl in project android by JetBrains.
the class ResizeInteraction method begin.
@Override
public void begin(@SwingCoordinate int x, @SwingCoordinate int y, @InputEventMask int startMask) {
super.begin(x, y, startMask);
NlComponent parent = myComponent.getParent();
if (parent != null) {
ViewGroupHandler viewGroupHandler = ViewHandlerManager.get(myScreenView.getModel().getFacet()).findLayoutHandler(parent, false);
if (viewGroupHandler != null) {
ViewEditor editor = new ViewEditorImpl(myScreenView);
myResizeHandler = viewGroupHandler.createResizeHandler(editor, myComponent, myHorizontalEdge, myVerticalEdge);
if (myResizeHandler != null) {
int androidX = Coordinates.getAndroidX(myScreenView, myStartX);
int androidY = Coordinates.getAndroidY(myScreenView, myStartY);
myResizeHandler.start(androidX, androidY, startMask);
}
}
}
}
use of com.android.tools.idea.uibuilder.handlers.ViewEditorImpl 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;
}
use of com.android.tools.idea.uibuilder.handlers.ViewEditorImpl in project android by JetBrains.
the class NlActionManager method addViewActions.
public void addViewActions(@NotNull DefaultActionGroup group, @Nullable NlComponent component, @Nullable NlComponent parent, @NotNull List<NlComponent> newSelection, boolean toolbar) {
ScreenView screenView = mySurface.getCurrentScreenView();
if (screenView == null || (parent == null && component == null)) {
return;
}
ViewEditor editor = new ViewEditorImpl(screenView);
// TODO: Perform caching
if (component != null) {
ViewHandler handler = ViewHandlerManager.get(mySurface.getProject()).getHandler(component);
addViewActionsForHandler(group, component, newSelection, editor, handler, toolbar);
}
if (parent != null) {
ViewHandler handler = ViewHandlerManager.get(mySurface.getProject()).getHandler(parent);
List<NlComponent> selectedChildren = Lists.newArrayListWithCapacity(newSelection.size());
for (NlComponent selected : newSelection) {
if (selected.getParent() == parent) {
selectedChildren.add(selected);
}
}
addViewActionsForHandler(group, parent, selectedChildren, editor, handler, toolbar);
}
}
Aggregations