Skip to content

Commit 022567d

Browse files
DiegoCardosomanolo
andauthored
feat: add table support for Spreadsheet (#3303)
Co-authored-by: Manuel Carrasco Moñino <manolo@vaadin.com>
1 parent a91ccff commit 022567d

16 files changed

Lines changed: 429 additions & 74 deletions

File tree

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-client/src/main/java/com/vaadin/addon/spreadsheet/client/PopupButtonConnector.java‎

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.vaadin.client.ConnectorHierarchyChangeEvent;
2929
import com.vaadin.client.communication.StateChangeEvent;
3030
import com.vaadin.client.ui.AbstractHasComponentsConnector;
31+
import com.vaadin.shared.communication.ServerRpc;
3132

3233
@SuppressWarnings("serial")
3334
public class PopupButtonConnector extends AbstractHasComponentsConnector
@@ -46,6 +47,7 @@ public void closePopup() {
4647
}
4748

4849
};
50+
private PopupButtonServerRpc serverRpc;
4951

5052
@Override
5153
public void init() {
@@ -55,6 +57,15 @@ public void init() {
5557
getWidget().addCloseHandler(this);
5658
}
5759

60+
public void setPopupButtonServerRpc(PopupButtonServerRpc rpc) {
61+
this.serverRpc = rpc;
62+
}
63+
64+
@Override
65+
protected <T extends ServerRpc> T getRpcProxy(Class<T> rpcInterface) {
66+
return (T) serverRpc;
67+
}
68+
5869
@Override
5970
protected Widget createWidget() {
6071
return GWT.create(PopupButtonWidget.class);
@@ -122,13 +133,18 @@ public void onConnectorHierarchyChange(
122133

123134
@Override
124135
public void onClose(CloseEvent<PopupPanel> event) {
125-
if (getWidget().isAttached()) {
126-
getRpcProxy(PopupButtonServerRpc.class).onPopupClose();
136+
final PopupButtonWidget widget = getWidget();
137+
if (widget.isAttached()) {
138+
getRpcProxy(PopupButtonServerRpc.class)
139+
.onPopupClose(widget.getRow(), widget.getCol());
127140
}
128141
}
129142

130143
@Override
131144
public void onClick(ClickEvent event) {
132-
getRpcProxy(PopupButtonServerRpc.class).onPopupButtonClick();
145+
final PopupButtonWidget widget = getWidget();
146+
getRpcProxy(PopupButtonServerRpc.class)
147+
.onPopupButtonClick(widget.getRow(), widget.getCol());
133148
}
149+
134150
}

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-client/src/main/java/com/vaadin/addon/spreadsheet/client/PopupButtonServerRpc.java‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,18 @@
1313
* #L%
1414
*/
1515

16+
import com.google.gwt.event.dom.client.ClickEvent;
1617
import com.vaadin.shared.communication.ServerRpc;
1718

1819
public interface PopupButtonServerRpc extends ServerRpc {
1920

2021
/**
2122
* Called when the button has been clicked, and the pop-up has been opened.
2223
*/
23-
public void onPopupButtonClick();
24+
public void onPopupButtonClick(int row, int column);
2425

2526
/**
2627
* Called after the pop-up has been closed.
2728
*/
28-
public void onPopupClose();
29+
public void onPopupClose(int row, int column);
2930
}

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-client/src/main/java/com/vaadin/addon/spreadsheet/client/PopupButtonWidget.java‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ public void setPosition(int offsetWidth, int offsetHeight) {
7474
private int col;
7575
private int row;
7676
private SheetWidget owner;
77+
private Widget content;
7778

7879
public PopupButtonWidget() {
7980
root.setClassName(BUTTON_CLASSNAME);
@@ -197,6 +198,18 @@ public void removePopupComponent(Widget widget) {
197198
popupLayout.remove(widget);
198199
}
199200

201+
public void setPopupContent(Widget widget) {
202+
this.content = widget;
203+
addPopupComponent(widget);
204+
}
205+
206+
public void clearPopupContent() {
207+
if (this.content != null) {
208+
removePopupComponent(this.content);
209+
this.content = null;
210+
}
211+
}
212+
200213
public void setPopupHeight(String popupHeight) {
201214
if (popupHeight != null) {
202215
((PopupPanel) popup).setHeight(popupHeight);
@@ -260,6 +273,7 @@ public boolean isPopupOpen() {
260273

261274
public void closePopup() {
262275
popup.hide();
276+
clearPopupContent();
263277
}
264278

265279
public void openPopup() {

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-client/src/main/java/com/vaadin/component/spreadsheet/client/js/Parser.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ public static List<PopupButtonState> parseListOfPopupButtons(String raw) {
2626
return parseArrayJstype(raw, PopupButtonState::new);
2727
}
2828

29+
public static PopupButtonState parsePopupButtonState(String raw) {
30+
return parseJsType(raw, PopupButtonState::new);
31+
}
32+
2933
public static List<GroupingData> parseListOfGroupingData(String raw) {
3034
return parseArrayJstype(raw, GroupingData::new);
3135
}
@@ -104,6 +108,17 @@ private static <T> Set<T> parseSet(String raw,
104108
return new HashSet<T>(parseArray(raw, jsToJava));
105109
}
106110

111+
private static <T> T parseJsType(String raw, Supplier<T> constructor) {
112+
if (raw == null || raw.isEmpty() || "null".equals(raw)) {
113+
return null;
114+
}
115+
116+
JsonValue jsBean = JsonUtil.parse(raw);
117+
T javaBean = constructor.get();
118+
copyJsToJava(jsBean, javaBean);
119+
return javaBean;
120+
}
121+
107122
private static <T> ArrayList<T> parseArrayJstype(String raw,
108123
Supplier<T> constructor) {
109124
return parseArray(raw, jsBean -> {

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-client/src/main/java/com/vaadin/component/spreadsheet/client/js/SpreadsheetJsApi.java‎

Lines changed: 94 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22

33
import java.util.ArrayList;
44
import java.util.HashMap;
5-
import java.util.List;
65
import java.util.Map;
76

87
import com.google.gwt.core.client.Scheduler;
98
import com.google.gwt.dom.client.DivElement;
109
import com.google.gwt.dom.client.Element;
1110
import com.google.gwt.user.client.ui.RootPanel;
11+
import com.google.gwt.user.client.ui.Widget;
1212
import com.vaadin.addon.spreadsheet.client.PopupButtonConnector;
1313
import com.vaadin.addon.spreadsheet.client.PopupButtonState;
1414
import com.vaadin.addon.spreadsheet.client.PopupButtonWidget;
@@ -35,6 +35,7 @@ public class SpreadsheetJsApi {
3535

3636
private SpreadsheetWidget spreadsheetWidget;
3737
protected SpreadsheetConnector spreadsheetConnector;
38+
3839
private Map<String, PopupButtonWidget> popupButtonWidgets = new HashMap<>();
3940
private Map<String, PopupButtonConnector> popupButtonConnectors = new HashMap<>();
4041
private Map<String, PopupButtonState> popupButtonStates = new HashMap<>();
@@ -463,26 +464,91 @@ public void setClass(String classNames) {
463464
}
464465
}
465466

466-
public void setPopups(String raw) {
467-
List<PopupButtonState> l = Parser.parseListOfPopupButtons(raw);
468-
l.forEach(state -> {
469-
String k = state.row + "_" + state.col;
470-
if (!popupButtonWidgets.containsKey(k)) {
471-
PopupButtonWidget widget;
472-
popupButtonWidgets.put(k, widget = new PopupButtonWidget());
473-
popupButtonConnectors.put(k, new PopupButtonConnector());
474-
popupButtonStates.put(k, state);
475-
widget.setCol(state.col);
476-
widget.setRow(state.row);
477-
widget.setPopupHeaderHidden(state.headerHidden);
478-
widget.setSheetWidget(spreadsheetWidget.getSheetWidget(),
479-
DivElement.as(spreadsheetWidget.getSheetWidget()
480-
.getElement()));
481-
widget.setPopupWidth(state.popupWidth);
482-
widget.setPopupHeight(state.popupHeight);
483-
spreadsheetWidget.addPopupButton(widget);
467+
private static String getPopupKey(String sheet, int row, int column) {
468+
return sheet + "_" + row + "_" + column;
469+
}
470+
471+
public void addPopupButton(String rawState) {
472+
PopupButtonState state = Parser.parsePopupButtonState(rawState);
473+
String key = getPopupKey(state.sheet, state.row, state.col);
474+
475+
PopupButtonWidget widget;
476+
477+
if (!popupButtonWidgets.containsKey(key)) {
478+
PopupButtonConnector connector;
479+
popupButtonConnectors.put(key,
480+
connector = new PopupButtonConnector());
481+
popupButtonWidgets.put(key, widget = connector.getWidget());
482+
483+
popupButtonStates.put(key, state);
484+
connector.setPopupButtonServerRpc(getServerRpcInstance());
485+
connector.init();
486+
widget.setCol(state.col);
487+
widget.setRow(state.row);
488+
widget.setPopupHeaderHidden(state.headerHidden);
489+
widget.setSheetWidget(spreadsheetWidget.getSheetWidget(), DivElement
490+
.as(spreadsheetWidget.getSheetWidget().getElement()));
491+
widget.setPopupWidth(state.popupWidth);
492+
widget.setPopupHeight(state.popupHeight);
493+
} else {
494+
widget = popupButtonWidgets.get(key);
495+
}
496+
spreadsheetWidget.addPopupButton(widget);
497+
}
498+
499+
public void removePopupButton(String rawState) {
500+
PopupButtonState state = Parser.parsePopupButtonState(rawState);
501+
String key = getPopupKey(state.sheet, state.row, state.col);
502+
PopupButtonWidget popupButton = popupButtonWidgets.get(key);
503+
504+
if (popupButton != null) {
505+
spreadsheetWidget.removePopupButton(popupButton);
506+
popupButtonWidgets.remove(key);
507+
popupButtonConnectors.remove(key);
508+
popupButtonStates.remove(key);
509+
}
510+
}
511+
512+
public static class ContentWidget extends Widget {
513+
public ContentWidget(Element contentElement) {
514+
setElement(contentElement);
515+
}
516+
}
517+
518+
private static native Element getPopupContentContainer(
519+
String contentParentId) /*-{
520+
return $wnd.Vaadin
521+
&& $wnd.Vaadin.Flow
522+
&& $wnd.Vaadin.Flow.clients.ROOT
523+
&& $wnd.Vaadin.Flow.clients.ROOT.getByNodeId(contentParentId);
524+
}-*/;
525+
526+
public void onPopupButtonOpened(int row, int column,
527+
String contentParentId) {
528+
Element container = getPopupContentContainer(contentParentId);
529+
530+
if (container == null) {
531+
return;
532+
}
533+
ContentWidget contentWidget = new ContentWidget(
534+
container.getFirstChildElement());
535+
536+
if (contentWidget.getElement() != null) {
537+
PopupButtonWidget widget = popupButtonWidgets.get(getPopupKey(
538+
spreadsheetWidget.getActiveSheetName(), row, column));
539+
if (!widget.isPopupOpen()) {
540+
widget.openPopup();
484541
}
485-
});
542+
widget.setPopupContent(contentWidget);
543+
}
544+
}
545+
546+
public void closePopup(int row, int column) {
547+
PopupButtonWidget widget = popupButtonWidgets.get(getPopupKey(
548+
spreadsheetWidget.getActiveSheetName(), row, column));
549+
if (widget != null) {
550+
widget.closePopup();
551+
}
486552
}
487553

488554
public void setResources(Element element, String resources) {
@@ -740,6 +806,14 @@ public void setActionOnColumnHeaderCallback(JsConsumer<String> callback) {
740806
getServerRpcInstance().setActionOnColumnHeaderCallback(callback);
741807
}
742808

809+
public void setPopupButtonClickCallback(JsConsumer<Void> callback) {
810+
getServerRpcInstance().setPopupButtonClickCallback(callback);
811+
}
812+
813+
public void setPopupCloseCallback(JsConsumer<Void> callback) {
814+
getServerRpcInstance().setPopupCloseCallback(callback);
815+
}
816+
743817
public void load() {
744818
spreadsheetWidget.load();
745819
}

‎vaadin-spreadsheet-flow-parent/vaadin-spreadsheet-flow-client/src/main/java/com/vaadin/component/spreadsheet/client/js/SpreadsheetServerRpcImpl.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
import java.util.HashMap;
44
import java.util.Map;
55

6+
import com.vaadin.addon.spreadsheet.client.PopupButtonServerRpc;
67
import com.vaadin.addon.spreadsheet.client.SpreadsheetServerRpc;
78

89
import elemental.json.Json;
910
import elemental.json.JsonArray;
1011
import jsinterop.annotations.JsFunction;
1112

1213
@SuppressWarnings("serial")
13-
public class SpreadsheetServerRpcImpl implements SpreadsheetServerRpc {
14+
public class SpreadsheetServerRpcImpl
15+
implements SpreadsheetServerRpc, PopupButtonServerRpc {
1416

1517
@FunctionalInterface
1618
@JsFunction
@@ -57,6 +59,8 @@ public interface JsConsumer<T> {
5759
private JsConsumer<String> actionOnRowHeaderCallback;
5860
private JsConsumer<Integer> columnHeaderContextMenuOpenCallback;
5961
private JsConsumer<String> actionOnColumnHeaderCallback;
62+
private JsConsumer<Void> popupButtonClickCallback;
63+
private JsConsumer<Void> popupCloseCallback;
6064

6165
public SpreadsheetServerRpcImpl() {
6266
}
@@ -228,6 +232,15 @@ public void setActionOnColumnHeaderCallback(JsConsumer<String> callback) {
228232
actionOnColumnHeaderCallback = callback;
229233
}
230234

235+
public void setPopupCloseCallback(JsConsumer<Void> popupCloseCallback) {
236+
this.popupCloseCallback = popupCloseCallback;
237+
}
238+
239+
public void setPopupButtonClickCallback(
240+
JsConsumer<Void> popupButtonClickCallback) {
241+
this.popupButtonClickCallback = popupButtonClickCallback;
242+
}
243+
231244
private native void call(JsConsumer<?> fnc, Object... args) /*-{
232245
if (!fnc) {
233246
return;
@@ -463,4 +476,14 @@ public void columnHeaderContextMenuOpen(int columnIndex) {
463476
public void actionOnColumnHeader(String actionKey) {
464477
call(actionOnColumnHeaderCallback, actionKey);
465478
}
479+
480+
@Override
481+
public void onPopupButtonClick(int row, int column) {
482+
call(popupButtonClickCallback, row, column);
483+
}
484+
485+
@Override
486+
public void onPopupClose(int row, int column) {
487+
call(popupCloseCallback, row, column);
488+
}
466489
}

0 commit comments

Comments
 (0)