diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2017-06-28 10:29:57 +0200 |
---|---|---|
committer | Henri Sara <henri.sara@gmail.com> | 2017-06-28 11:29:57 +0300 |
commit | 6ed5f2a0df068dba713d8e24dc9f78e2a0829a73 (patch) | |
tree | 0cc6c37ba43e5d1371b9283b18042289f2e14db0 /client | |
parent | e4e2328a3a78d652cd09ef8293f233d31d899415 (diff) | |
download | vaadin-framework-6ed5f2a0df068dba713d8e24dc9f78e2a0829a73.tar.gz vaadin-framework-6ed5f2a0df068dba713d8e24dc9f78e2a0829a73.zip |
Create drag source and drop target extensions for tree grid (#9463)
Resolves #9372
Diffstat (limited to 'client')
4 files changed, 213 insertions, 5 deletions
diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java index 5b409e3174..9954134821 100644 --- a/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java +++ b/client/src/main/java/com/vaadin/client/connectors/grid/GridDropTargetConnector.java @@ -124,7 +124,14 @@ public class GridDropTargetConnector extends DropTargetExtensionConnector { rowKey, dropLocation, mouseEventDetails); } - private JsonObject getRowData(TableRowElement row) { + /** + * Get the row data as json object for the given row. + * + * @param row + * table row element + * @return row data as json object for the given row + */ + protected JsonObject getRowData(TableRowElement row) { int rowIndex = ((Escalator.AbstractRowContainer) getGridBody()) .getLogicalRowIndex(row); return gridConnector.getDataSource().getRow(rowIndex); @@ -132,8 +139,13 @@ public class GridDropTargetConnector extends DropTargetExtensionConnector { /** * Returns the location of the event within the row. + * + * @param target + * drop target element + * @param event + * drop event */ - private DropLocation getDropLocation(Element target, NativeEvent event) { + protected DropLocation getDropLocation(Element target, NativeEvent event) { if (TableRowElement.is(target)) { if (getState().dropMode == DropMode.BETWEEN) { if (getRelativeY(target, @@ -241,7 +253,14 @@ public class GridDropTargetConnector extends DropTargetExtensionConnector { element.removeClassName(styleDragEmpty); } - private Element getTargetElement(Element source) { + /** + * Gets the target element for a dragover or drop event. + * + * @param source + * the event target of the event + * @return the element that should be handled as the target of the event + */ + protected Element getTargetElement(Element source) { final Element tableWrapper = getDropTargetElement(); final BodyRowContainer gridBody = getGridBody(); final int rowCount = gridBody.getRowCount(); diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/TreeGridDragSourceConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/TreeGridDragSourceConnector.java new file mode 100644 index 0000000000..c2450bb9db --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/grid/TreeGridDragSourceConnector.java @@ -0,0 +1,37 @@ +/* + * 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 com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.treegrid.TreeGridDragSourceState; +import com.vaadin.ui.components.grid.TreeGridDragSource; + +/** + * Adds HTML5 drag and drop functionality to a TreeGrid's rows. This is the + * client side counterpart of {@link TreeGridDragSource}. + * + * @author Vaadin Ltd + * @since 8.1 + */ +@Connect(TreeGridDragSource.class) +public class TreeGridDragSourceConnector extends GridDragSourceConnector { + + @Override + public TreeGridDragSourceState getState() { + return (TreeGridDragSourceState) super.getState(); + } + +} diff --git a/client/src/main/java/com/vaadin/client/connectors/grid/TreeGridDropTargetConnector.java b/client/src/main/java/com/vaadin/client/connectors/grid/TreeGridDropTargetConnector.java new file mode 100644 index 0000000000..fd13426514 --- /dev/null +++ b/client/src/main/java/com/vaadin/client/connectors/grid/TreeGridDropTargetConnector.java @@ -0,0 +1,86 @@ +/* + * 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.List; +import java.util.Map; + +import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.NativeEvent; +import com.google.gwt.dom.client.TableRowElement; +import com.vaadin.client.MouseEventDetailsBuilder; +import com.vaadin.shared.MouseEventDetails; +import com.vaadin.shared.data.HierarchicalDataCommunicatorConstants; +import com.vaadin.shared.ui.Connect; +import com.vaadin.shared.ui.grid.DropLocation; +import com.vaadin.shared.ui.grid.GridState; +import com.vaadin.shared.ui.treegrid.TreeGridDropTargetRpc; +import com.vaadin.shared.ui.treegrid.TreeGridDropTargetState; +import com.vaadin.ui.components.grid.TreeGridDropTarget; + +import elemental.json.JsonObject; + +/** + * Makes TreeGrid an HTML5 drop target. This is the client side counterpart of + * {@link TreeGridDropTarget}. + * + * @author Vaadin Ltd + * @since 8.1 + */ +@Connect(TreeGridDropTarget.class) +public class TreeGridDropTargetConnector extends GridDropTargetConnector { + + @Override + protected void sendDropEventToServer(List<String> types, + Map<String, String> data, String dropEffect, + NativeEvent dropEvent) { + String rowKey = null; + DropLocation dropLocation = null; + Integer rowDepth = null; + Boolean rowCollapsed = null; + + Element targetElement = getTargetElement( + (Element) dropEvent.getEventTarget().cast()); + // the target element is either the tablewrapper or one of the body rows + if (TableRowElement.is(targetElement)) { + JsonObject rowData = getRowData(targetElement.cast()); + rowKey = rowData.getString(GridState.JSONKEY_ROWKEY); + dropLocation = getDropLocation(targetElement, dropEvent); + + // Collect hierarchy information + JsonObject hierarchyDescription = rowData.getObject( + HierarchicalDataCommunicatorConstants.ROW_HIERARCHY_DESCRIPTION); + rowDepth = (int) hierarchyDescription + .getNumber(HierarchicalDataCommunicatorConstants.ROW_DEPTH); + rowCollapsed = hierarchyDescription.getBoolean( + HierarchicalDataCommunicatorConstants.ROW_COLLAPSED); + } else { + dropLocation = DropLocation.EMPTY; + } + + MouseEventDetails mouseEventDetails = MouseEventDetailsBuilder + .buildMouseEventDetails(dropEvent, targetElement); + + getRpcProxy(TreeGridDropTargetRpc.class).drop(types, data, dropEffect, + rowKey, rowDepth, rowCollapsed, dropLocation, + mouseEventDetails); + } + + @Override + public TreeGridDropTargetState getState() { + return (TreeGridDropTargetState) super.getState(); + } +} diff --git a/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java b/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java index fb66bebb7d..328d594fe1 100644 --- a/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java +++ b/client/src/main/java/com/vaadin/client/widget/treegrid/TreeGrid.java @@ -16,10 +16,14 @@ package com.vaadin.client.widget.treegrid; import com.google.gwt.dom.client.Element; +import com.google.gwt.dom.client.TableRowElement; import com.google.gwt.event.shared.HandlerRegistration; +import com.vaadin.client.widget.escalator.EscalatorUpdater; +import com.vaadin.client.widget.escalator.Row; import com.vaadin.client.widget.grid.events.BodyClickHandler; import com.vaadin.client.widget.treegrid.events.TreeGridClickEvent; import com.vaadin.client.widgets.Grid; +import com.vaadin.shared.data.HierarchicalDataCommunicatorConstants; import elemental.json.JsonObject; @@ -27,14 +31,63 @@ import elemental.json.JsonObject; * An extension of the Grid widget, which supports displaying of hierarchical * data. * - * @see Grid - * * @author Vaadin Ltd + * @see Grid * @since 8.1 */ public class TreeGrid extends Grid<JsonObject> { /** + * Style name prefix for the row's depth in the hierarchy + */ + private String depthStyleNamePrefix; + + /** + * Body updater that adds additional style to each row containing depth + * information inside the hierarchy. + */ + protected class BodyUpdater extends Grid.BodyUpdater { + @Override + public void update(Row row, Iterable cellsToUpdate) { + super.update(row, cellsToUpdate); + + int rowIndex = row.getRow(); + TableRowElement rowElement = row.getElement(); + + JsonObject rowData = getDataSource().getRow(rowIndex); + if (rowData != null) { + int depth = (int) rowData.getObject( + HierarchicalDataCommunicatorConstants.ROW_HIERARCHY_DESCRIPTION) + .getNumber( + HierarchicalDataCommunicatorConstants.ROW_DEPTH); + + // Add or replace style name containing depth information + String styleToBeReplaced = getFullClassName( + depthStyleNamePrefix, rowElement.getClassName()); + if (styleToBeReplaced == null) { + rowElement.addClassName(depthStyleNamePrefix + depth); + } else { + rowElement.replaceClassName(styleToBeReplaced, + depthStyleNamePrefix + depth); + } + } + } + + private String getFullClassName(String prefix, String classNameList) { + int start = classNameList.indexOf(prefix); + int end = start + prefix.length(); + if (start > -1) { + while (end < classNameList.length() + && classNameList.charAt(end) != ' ') { + end++; + } + return classNameList.substring(start, end); + } + return null; + } + } + + /** * Method for accessing the private {@link Grid#focusCell(int, int)} method * from this package */ @@ -50,8 +103,21 @@ public class TreeGrid extends Grid<JsonObject> { return this.@com.vaadin.client.widgets.Grid::isElementInChildWidget(*)(e); }-*/; + @Override public HandlerRegistration addBodyClickHandler(BodyClickHandler handler) { return addHandler(handler, TreeGridClickEvent.TYPE); } + + @Override + protected EscalatorUpdater createBodyUpdater() { + return new BodyUpdater(); + } + + @Override + public void setStylePrimaryName(String style) { + super.setStylePrimaryName(style); + + depthStyleNamePrefix = getStylePrimaryName() + "-row-depth-"; + } } |