use of com.vaadin.shared.ui.dd.VerticalDropLocation in project VaadinUtils by rlsutton1.
the class BaseCrudView method enableDragAndDropOrdering.
/**
* allows the user to sort the items in the list via drag and drop
*
* @param ordinalField
*/
public void enableDragAndDropOrdering(final SingularAttribute<E, Long> ordinalField) {
dragAndDropOrderingEnabled = true;
this.ordinalField = ordinalField;
container.sort(new Object[] { ordinalField.getName() }, new boolean[] { true });
this.entityTable.setDragMode(TableDragMode.ROW);
this.entityTable.setDropHandler(new DropHandler() {
private static final long serialVersionUID = -6024948983201516170L;
@Override
public AcceptCriterion getAcceptCriterion() {
return SourceIsTarget.get();
}
@SuppressWarnings("unchecked")
@Override
public void drop(DragAndDropEvent event) {
if (isDirty()) {
Notification.show("You must save first", Type.WARNING_MESSAGE);
return;
}
Object draggedItemId = event.getTransferable().getData("itemId");
EntityItem<E> dragged = container.getItem(draggedItemId);
if (dragged != null) {
EntityItemProperty draggedOrdinalProp = dragged.getItemProperty(ordinalField.getName());
if (draggedOrdinalProp != null) {
AbstractSelectTargetDetails td = (AbstractSelectTargetDetails) event.getTargetDetails();
VerticalDropLocation dl = td.getDropLocation();
Object targetId = ((AbstractSelectTargetDetails) event.getTargetDetails()).getItemIdOver();
int idx = container.indexOfId(targetId);
if (dl == VerticalDropLocation.BOTTOM) {
// drop below so move the idx down one
idx++;
}
if (idx > -1) {
targetId = container.getIdByIndex(idx);
}
boolean added = false;
Long ctr = 1l;
for (Object id : container.getItemIds()) {
if (id.equals(targetId)) {
draggedOrdinalProp.setValue(ctr++);
added = true;
}
if (!id.equals(draggedItemId)) {
container.getItem(id).getItemProperty(ordinalField.getName()).setValue(ctr++);
}
}
if (!added) {
draggedOrdinalProp.setValue(ctr++);
}
container.commit();
if (dragAndDropListener != null) {
dragAndDropListener.dropped();
}
// to save.
try {
invokeTopLevelCrudSave();
} catch (Exception e) {
ErrorWindow.showErrorWindow(e);
}
} else {
logger.error("draggedOrdinalProp is null");
}
} else {
logger.error("dragged is null");
}
}
});
}
use of com.vaadin.shared.ui.dd.VerticalDropLocation in project cuba by cuba-platform.
the class VDDGridLayout method getVerticalDropLocation.
/**
* Returns the vertical drop location
*
* @param cell
* The cell details
* @param event
* The drag event
* @return
*/
protected VerticalDropLocation getVerticalDropLocation(CellDetails cell, VDragEvent event) {
// Get the vertical location
VerticalDropLocation vdetail;
int y = Util.getTouchOrMouseClientY(event.getCurrentGwtEvent()) - getAbsoluteTop() - cell.y;
assert (y >= 0 && y <= cell.height);
if (y < cell.height * cellTopBottomDropRatio) {
vdetail = VerticalDropLocation.TOP;
} else if (y < cell.height * (1.0 - cellTopBottomDropRatio)) {
vdetail = VerticalDropLocation.MIDDLE;
} else {
vdetail = VerticalDropLocation.BOTTOM;
}
return vdetail;
}
use of com.vaadin.shared.ui.dd.VerticalDropLocation in project cuba by cuba-platform.
the class VDDFormLayout method emphasis.
/**
* Empasises the drop location of the component when hovering over a
* ĆhildComponentContainer. Passing null as the container removes any
* previous emphasis.
*
* @param widget
* The container which we are hovering over
* @param event
* The drag event
*/
protected void emphasis(Widget widget, VDragEvent event) {
// Remove emphasis from previous hovers
deEmphasis();
// Validate
if (widget == null || !getElement().isOrHasChild(widget.getElement())) {
return;
}
/*
* Get row for widget
*/
Element rowElement = getRowFromChildElement(widget.getElement(), VDDFormLayout.this.getElement());
currentlyEmphasised = rowElement;
if (rowElement != this.getElement()) {
VerticalDropLocation vl = getVerticalDropLocation(rowElement, event);
setStyleName(rowElement, OVER + "-" + vl.toString().toLowerCase(), true);
} else {
setStyleName(rowElement, OVER, true);
}
}
use of com.vaadin.shared.ui.dd.VerticalDropLocation in project cuba by cuba-platform.
the class VDDVerticalSplitPanel method updateDragDetails.
/**
* Updates the drop details while dragging. This is needed to ensure client
* side criterias can validate the drop location.
*
* @param event
* The drag event
*/
protected void updateDragDetails(VDragEvent event) {
Element over = event.getElementOver();
// Resolve where the drop was made
VerticalDropLocation location = null;
Widget content = null;
if (firstContainer.isOrHasChild(over)) {
location = VerticalDropLocation.TOP;
content = Util.findWidget(firstContainer, null);
} else if (splitter.isOrHasChild(over)) {
location = VerticalDropLocation.MIDDLE;
content = this;
} else if (secondContainer.isOrHasChild(over)) {
location = VerticalDropLocation.BOTTOM;
content = Util.findWidget(secondContainer, null);
}
event.getDropDetails().put(Constants.DROP_DETAIL_VERTICAL_DROP_LOCATION, location);
if (content != null) {
event.getDropDetails().put(Constants.DROP_DETAIL_OVER_CLASS, content.getClass().getName());
} else {
event.getDropDetails().put(Constants.DROP_DETAIL_OVER_CLASS, this.getClass().getName());
}
// Add mouse event details
MouseEventDetails details = MouseEventDetailsBuilder.buildMouseEventDetails(event.getCurrentGwtEvent(), getElement());
event.getDropDetails().put(Constants.DROP_DETAIL_MOUSE_EVENT, details.serialize());
}
use of com.vaadin.shared.ui.dd.VerticalDropLocation in project cuba by cuba-platform.
the class VDDVerticalLayout method emphasis.
/**
* Empasises the drop location of the component when hovering over a
* ĆhildComponentContainer. Passing null as the container removes any
* previous emphasis.
*
* @param container
* The container which we are hovering over
* @param event
* The drag event
*/
protected void emphasis(Widget container, VDragEvent event) {
// Remove emphasis from previous hovers
deEmphasis();
// validate container
if (container == null || !getElement().isOrHasChild(container.getElement())) {
return;
}
currentlyEmphasised = container;
VerticalDropLocation location = null;
// Add drop location specific style
if (currentlyEmphasised != this) {
location = getVerticalDropLocation(currentlyEmphasised, event);
} else {
location = VerticalDropLocation.MIDDLE;
}
UIObject.setStyleName(currentlyEmphasised.getElement(), OVER, true);
UIObject.setStyleName(currentlyEmphasised.getElement(), OVER + "-" + location.toString().toLowerCase(), true);
}
Aggregations