123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216 |
- /*
- * Copyright 2000-2016 Vaadin Ltd.
- *
- * Licensed under the Apache License, Version 2.0 (the "License"); you may not
- * use this file except in compliance with the License. You may obtain a copy of
- * the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
- * License for the specific language governing permissions and limitations under
- * the License.
- */
- package com.vaadin.client.connectors.grid;
-
- import java.util.Objects;
- import java.util.Optional;
-
- import com.google.gwt.dom.client.Element;
- import com.google.gwt.dom.client.NativeEvent;
- import com.google.gwt.dom.client.TableRowElement;
- import com.google.gwt.user.client.Window;
- import com.vaadin.client.ServerConnector;
- import com.vaadin.client.WidgetUtil;
- import com.vaadin.client.extensions.DropTargetExtensionConnector;
- import com.vaadin.client.widget.escalator.RowContainer;
- import com.vaadin.client.widgets.Escalator;
- import com.vaadin.shared.ui.Connect;
- import com.vaadin.shared.ui.grid.DropLocation;
- import com.vaadin.shared.ui.grid.DropMode;
- import com.vaadin.shared.ui.grid.GridDropTargetRpc;
- import com.vaadin.shared.ui.grid.GridDropTargetState;
- import com.vaadin.shared.ui.grid.GridState;
- import com.vaadin.ui.GridDropTarget;
-
- import elemental.events.Event;
- import elemental.json.JsonObject;
-
- /**
- * Makes Grid an HTML5 drop target. This is the client side counterpart of
- * {@link GridDropTarget}.
- *
- * @author Vaadin Ltd
- * @since
- */
- @Connect(GridDropTarget.class)
- public class GridDropTargetConnector extends
- DropTargetExtensionConnector {
-
- /**
- * Current style name
- */
- private String currentStyleName;
-
- private GridConnector gridConnector;
-
- /**
- * Class name to apply when an element is dragged over a row and the
- * location is {@link DropLocation#ON_TOP}.
- */
- private String styleDragCenter;
-
- /**
- * Class name to apply when an element is dragged over a row and the
- * location is {@link DropLocation#ABOVE}.
- */
- private String styleDragTop;
-
- /**
- * Class name to apply when an element is dragged over a row and the
- * location is {@link DropLocation#BELOW}.
- */
- private String styleDragBottom;
-
- @Override
- protected void extend(ServerConnector target) {
- gridConnector = (GridConnector) target;
-
- super.extend(target);
- }
-
- @Override
- protected void sendDropEventToServer(String dataTransferText,
- Event dropEvent) {
-
- String rowKey = null;
- DropLocation dropLocation = null;
-
- Optional<TableRowElement> targetRow = getTargetRow(
- (Element) dropEvent.getTarget());
- if (targetRow.isPresent()) {
- rowKey = getRowData(targetRow.get())
- .getString(GridState.JSONKEY_ROWKEY);
- dropLocation = getDropLocation(targetRow.get(),
- (NativeEvent) dropEvent);
- }
-
- getRpcProxy(GridDropTargetRpc.class)
- .drop(dataTransferText, rowKey, dropLocation);
- }
-
- private JsonObject getRowData(TableRowElement row) {
- int rowIndex = ((Escalator.AbstractRowContainer) getGridBody())
- .getLogicalRowIndex(row);
- return gridConnector.getDataSource().getRow(rowIndex);
- }
-
- /**
- * Returns the location of the event within the row.
- */
- private DropLocation getDropLocation(Element target, NativeEvent event) {
- if (getState().dropMode == DropMode.BETWEEN) {
- if (getRelativeY(target, event) < (target.getOffsetHeight() / 2)) {
- return DropLocation.ABOVE;
- } else {
- return DropLocation.BELOW;
- }
- }
- return DropLocation.ON_TOP;
- }
-
- private int getRelativeY(Element element, NativeEvent event) {
- int relativeTop = element.getAbsoluteTop() - Window.getScrollTop();
- return WidgetUtil.getTouchOrMouseClientY(event) - relativeTop;
- }
-
- @Override
- protected void onDragEnter(Event event) {
- // Generate style names for the drop target
- String styleRow =
- gridConnector.getWidget().getStylePrimaryName() + "-row";
- styleDragCenter = styleRow + STYLE_SUFFIX_DRAG_CENTER;
- styleDragTop = styleRow + STYLE_SUFFIX_DRAG_TOP;
- styleDragBottom = styleRow + STYLE_SUFFIX_DRAG_BOTTOM;
-
- super.onDragEnter(event);
- }
-
- @Override
- protected void setTargetIndicator(Event event) {
- getTargetRow(((Element) event.getTarget())).ifPresent(target -> {
-
- // Get required class name
- String className = getTargetClassName(target, (NativeEvent) event);
-
- // Add or replace class name if changed
- if (!target.hasClassName(className)) {
- if (currentStyleName != null) {
- target.removeClassName(currentStyleName);
- }
- target.addClassName(className);
- currentStyleName = className;
- }
- });
- }
-
- private String getTargetClassName(Element target, NativeEvent event) {
- String className;
-
- switch (getDropLocation(target, event)) {
- case ABOVE:
- className = styleDragTop;
- break;
- case BELOW:
- className = styleDragBottom;
- break;
- case ON_TOP:
- default:
- className = styleDragCenter;
- break;
- }
-
- return className;
- }
-
- @Override
- protected void removeTargetIndicator(Event event) {
-
- // Remove all possible style names
- getTargetRow((Element) event.getTarget()).ifPresent(e -> {
- e.removeClassName(styleDragCenter);
- e.removeClassName(styleDragTop);
- e.removeClassName(styleDragBottom);
- });
- }
-
- private Optional<TableRowElement> getTargetRow(Element source) {
- while (!Objects.equals(source, getGridBody().getElement())) {
- if (TableRowElement.is(source)) {
- return Optional.of(source.cast());
- }
- source = source.getParentElement();
- }
- return Optional.empty();
- }
-
- @Override
- protected Element getDropTargetElement() {
- return getGridBody().getElement();
- }
-
- private Escalator getEscalator() {
- return gridConnector.getWidget().getEscalator();
- }
-
- private RowContainer.BodyRowContainer getGridBody() {
- return getEscalator().getBody();
- }
-
- @Override
- public GridDropTargetState getState() {
- return (GridDropTargetState) super.getState();
- }
- }
|