Skip to content

Commit

Permalink
Add GridContextClickEvent with item ids, properties and section (#16855)
Browse files Browse the repository at this point in the history
Change-Id: I03091a3a7a22a921c127ed0b37fe792871ba5edd
  • Loading branch information
Teemu Suo-Anttila authored and Henri Sara committed Oct 5, 2015
1 parent a97534f commit dd02932
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 14 deletions.
1 change: 1 addition & 0 deletions WebContent/release-notes.html
Expand Up @@ -121,6 +121,7 @@ <h3 id="incompatible">Incompatible or Behavior-altering Changes in @version-mino
GridConnector, GridState, GridServerRpc and GridClientRpc</li>
<li>StringToEnumConverter now explicitly supports Enum types with custom toString() implementations.
This may affect applications that relied on the undefined behavior in previous versions.</li>
<li>The Section enumeration from client-side of the Grid has been moved to GridConstants</li>
</ul>
<h3 id="knownissues">Known Issues and Limitations</h3>
<ul>
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.google.web.bindery.event.shared.HandlerRegistration;
import com.vaadin.client.MouseEventDetailsBuilder;
import com.vaadin.client.renderers.ClickableRenderer;
import com.vaadin.client.renderers.ClickableRenderer.RendererClickEvent;
import com.vaadin.client.renderers.ClickableRenderer.RendererClickHandler;
import com.vaadin.shared.ui.grid.renderers.RendererClickRpc;
Expand Down
25 changes: 25 additions & 0 deletions client/src/com/vaadin/client/connectors/GridConnector.java
Expand Up @@ -32,6 +32,7 @@
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NativeEvent;
import com.google.gwt.event.dom.client.ContextMenuEvent;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Timer;
Expand All @@ -54,6 +55,7 @@
import com.vaadin.client.widget.grid.CellStyleGenerator;
import com.vaadin.client.widget.grid.DetailsGenerator;
import com.vaadin.client.widget.grid.EditorHandler;
import com.vaadin.client.widget.grid.EventCellReference;
import com.vaadin.client.widget.grid.RowReference;
import com.vaadin.client.widget.grid.RowStyleGenerator;
import com.vaadin.client.widget.grid.events.BodyClickHandler;
Expand All @@ -77,13 +79,15 @@
import com.vaadin.client.widgets.Grid.FooterRow;
import com.vaadin.client.widgets.Grid.HeaderCell;
import com.vaadin.client.widgets.Grid.HeaderRow;
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.Connect;
import com.vaadin.shared.ui.grid.EditorClientRpc;
import com.vaadin.shared.ui.grid.EditorServerRpc;
import com.vaadin.shared.ui.grid.GridClientRpc;
import com.vaadin.shared.ui.grid.GridColumnState;
import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.shared.ui.grid.GridConstants.Section;
import com.vaadin.shared.ui.grid.GridServerRpc;
import com.vaadin.shared.ui.grid.GridState;
import com.vaadin.shared.ui.grid.GridStaticSectionState;
Expand Down Expand Up @@ -1188,6 +1192,27 @@ public TooltipInfo getTooltipInfo(Element element) {
return super.getTooltipInfo(element);
}

@Override
protected void sendContextClickEvent(ContextMenuEvent event) {
EventCellReference<JsonObject> eventCell = getWidget().getEventCell();

Section section = eventCell.getSection();
String rowKey = null;
if (eventCell.isBody()) {
rowKey = getRowKey(eventCell.getRow());
}

String columnId = getColumnId(eventCell.getColumn());
MouseEventDetails details = MouseEventDetailsBuilder
.buildMouseEventDetails(event.getNativeEvent());

getRpcProxy(GridServerRpc.class).contextClick(eventCell.getRowIndex(),
rowKey, columnId, section, details);

event.preventDefault();
event.stopPropagation();
}

/**
* Creates a concatenation of all columns errors for Editor.
*
Expand Down
Expand Up @@ -24,6 +24,7 @@
import com.google.gwt.event.dom.client.MouseEvent;
import com.google.gwt.event.shared.EventHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.web.bindery.event.shared.HandlerRegistration;
import com.vaadin.client.WidgetUtil;
Expand All @@ -33,7 +34,7 @@
import com.vaadin.client.widget.grid.EventCellReference;
import com.vaadin.client.widgets.Escalator;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* An abstract superclass for renderers that render clickable widgets. Click
Expand Down
Expand Up @@ -19,7 +19,7 @@
import com.vaadin.client.widget.escalator.Cell;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.Column;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* A data class which contains information which identifies a cell being the
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridClickHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.AbstractGridMouseEvent;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* Represents native mouse click event in Grid.
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.vaadin.client.widget.grid.events.AbstractGridMouseEventHandler.GridDoubleClickHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.AbstractGridMouseEvent;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* Represents native mouse double click event in Grid.
Expand Down
Expand Up @@ -21,7 +21,7 @@
import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyDownHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* Represents native key down event in Grid.
Expand Down
Expand Up @@ -20,7 +20,7 @@
import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyPressHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* Represents native key press event in Grid.
Expand Down
Expand Up @@ -21,7 +21,7 @@
import com.vaadin.client.widget.grid.events.AbstractGridKeyEventHandler.GridKeyUpHandler;
import com.vaadin.client.widgets.Grid;
import com.vaadin.client.widgets.Grid.AbstractGridKeyEvent;
import com.vaadin.client.widgets.Grid.Section;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* Represents native key up event in Grid.
Expand Down
8 changes: 1 addition & 7 deletions client/src/com/vaadin/client/widgets/Grid.java
Expand Up @@ -165,6 +165,7 @@
import com.vaadin.client.widgets.Grid.StaticSection.StaticRow;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.shared.ui.grid.GridConstants.Section;
import com.vaadin.shared.ui.grid.GridStaticCellType;
import com.vaadin.shared.ui.grid.HeightMode;
import com.vaadin.shared.ui.grid.Range;
Expand Down Expand Up @@ -217,13 +218,6 @@ public class Grid<T> extends ResizeComposite implements

private static final String SELECT_ALL_CHECKBOX_CLASSNAME = "-select-all-checkbox";

/**
* Enum describing different sections of Grid.
*/
public enum Section {
HEADER, BODY, FOOTER
}

/**
* Abstract base class for Grid header and footer sections.
*
Expand Down
82 changes: 82 additions & 0 deletions server/src/com/vaadin/ui/Grid.java
Expand Up @@ -68,6 +68,7 @@
import com.vaadin.data.util.IndexedContainer;
import com.vaadin.data.util.converter.Converter;
import com.vaadin.data.util.converter.ConverterUtil;
import com.vaadin.event.ContextClickEvent;
import com.vaadin.event.ItemClickEvent;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.event.ItemClickEvent.ItemClickNotifier;
Expand All @@ -92,6 +93,7 @@
import com.vaadin.shared.ui.grid.GridClientRpc;
import com.vaadin.shared.ui.grid.GridColumnState;
import com.vaadin.shared.ui.grid.GridConstants;
import com.vaadin.shared.ui.grid.GridConstants.Section;
import com.vaadin.shared.ui.grid.GridServerRpc;
import com.vaadin.shared.ui.grid.GridState;
import com.vaadin.shared.ui.grid.GridStaticCellType;
Expand Down Expand Up @@ -385,6 +387,73 @@ public interface EditorErrorHandler extends Serializable {
void commitError(CommitErrorEvent event);
}

/**
* ContextClickEvent for the Grid Component.
*
* @since
*/
public static class GridContextClickEvent extends ContextClickEvent {

private final Object itemId;
private final int rowIndex;
private final Object propertyId;
private final Section section;

public GridContextClickEvent(Grid source,
MouseEventDetails mouseEventDetails, Section section,
int rowIndex, Object itemId, Object propertyId) {
super(source, mouseEventDetails);
this.itemId = itemId;
this.propertyId = propertyId;
this.section = section;
this.rowIndex = rowIndex;
}

/**
* Returns the item id of context clicked row.
*
* @return item id of clicked row; <code>null</code> if header or footer
*/
public Object getItemId() {
return itemId;
}

/**
* Returns property id of clicked column.
*
* @return property id
*/
public Object getPropertyId() {
return propertyId;
}

/**
* Return the clicked section of Grid.
*
* @return section of grid
*/
public Section getSection() {
return section;
}

/**
* Returns the clicked row index relative to Grid section. In the body
* of the Grid the index is the item index in the Container. Header and
* Footer rows for index can be fetched with
* {@link Grid#getHeaderRow(int)} and {@link Grid#getFooterRow(int)}.
*
* @return row index in section
*/
public int getRowIndex() {
return rowIndex;
}

@Override
public Grid getComponent() {
return (Grid) super.getComponent();
}
}

/**
* An event which is fired when saving the editor fails
*/
Expand Down Expand Up @@ -4239,6 +4308,19 @@ public void editorClose(String rowKey) {
fireEvent(new EditorCloseEvent(Grid.this, getKeyMapper().get(
rowKey)));
}

@Override
public void contextClick(int rowIndex, String rowKey,
String columnId, Section section, MouseEventDetails details) {
Object itemId = null;
if (rowKey != null) {
itemId = getKeyMapper().get(rowKey);
}

fireEvent(new GridContextClickEvent(Grid.this, details,
section, rowIndex, itemId,
getPropertyIdByColumnId(columnId)));
}
});

registerRpc(new EditorServerRpc() {
Expand Down
7 changes: 7 additions & 0 deletions shared/src/com/vaadin/shared/ui/grid/GridConstants.java
Expand Up @@ -26,6 +26,13 @@
*/
public final class GridConstants implements Serializable {

/**
* Enum describing different sections of Grid.
*/
public enum Section {
HEADER, BODY, FOOTER
}

/**
* Default padding in pixels when scrolling programmatically, without an
* explicitly defined padding value.
Expand Down
19 changes: 19 additions & 0 deletions shared/src/com/vaadin/shared/ui/grid/GridServerRpc.java
Expand Up @@ -20,6 +20,7 @@
import com.vaadin.shared.MouseEventDetails;
import com.vaadin.shared.communication.ServerRpc;
import com.vaadin.shared.data.sort.SortDirection;
import com.vaadin.shared.ui.grid.GridConstants.Section;

/**
* Client-to-server RPC interface for the Grid component
Expand Down Expand Up @@ -69,6 +70,24 @@ void sort(String[] columnIds, SortDirection[] directions,
*/
void itemClick(String rowKey, String columnId, MouseEventDetails details);

/**
* Informs the server that a context click has happened inside of Grid.
*
* @since
* @param rowIndex
* index of clicked row in Grid section
* @param rowKey
* a key identifying the clicked item
* @param columnId
* column id identifying the clicked property
* @param section
* grid section (header, footer, body)
* @param details
* mouse event details
*/
void contextClick(int rowIndex, String rowKey, String columnId,
Section section, MouseEventDetails details);

/**
* Informs the server that the columns of the Grid have been reordered.
*
Expand Down
61 changes: 61 additions & 0 deletions uitest/src/com/vaadin/tests/contextclick/GridContextClick.java
@@ -0,0 +1,61 @@
/*
* Copyright 2000-2014 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.tests.contextclick;

import com.vaadin.data.Item;
import com.vaadin.shared.ui.grid.GridConstants.Section;
import com.vaadin.tests.util.PersonContainer;
import com.vaadin.ui.Grid;
import com.vaadin.ui.Grid.GridContextClickEvent;

public class GridContextClick extends
AbstractContextClickUI<Grid, GridContextClickEvent> {

@Override
protected Grid createTestComponent() {
Grid grid = new Grid(PersonContainer.createWithTestData());
grid.setFooterVisible(true);
grid.appendFooterRow();

grid.setColumnOrder("address", "email", "firstName", "lastName",
"phoneNumber", "address.streetAddress", "address.postalCode",
"address.city");

grid.setSizeFull();

return grid;
}

@Override
protected void handleContextClickEvent(GridContextClickEvent event) {
String value = "";
Object propertyId = event.getPropertyId();
if (event.getItemId() != null) {
Item item = event.getComponent().getContainerDataSource()
.getItem(event.getItemId());
value += item.getItemProperty("firstName").getValue();
value += " " + item.getItemProperty("lastName").getValue();
} else if (event.getSection() == Section.HEADER) {
value = event.getComponent().getHeaderRow(event.getRowIndex())
.getCell(propertyId).getText();
} else if (event.getSection() == Section.FOOTER) {
value = event.getComponent().getFooterRow(event.getRowIndex())
.getCell(propertyId).getText();
}
log("ContextClickEvent value: " + value + ", propertyId: " + propertyId
+ ", section: " + event.getSection());
}
}

0 comments on commit dd02932

Please sign in to comment.