Skip to content

Commit

Permalink
Grid editor save/cancel buttons can now be rendered on top (#16534)
Browse files Browse the repository at this point in the history
Change-Id: Ie70588885f0c876757df96c0deffe7e020cc29e5
  • Loading branch information
Henrik Paul authored and Vaadin Code Review committed Jan 30, 2015
1 parent a33e96d commit 0533c6d
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 11 deletions.
41 changes: 41 additions & 0 deletions client/src/com/vaadin/client/WidgetUtil.java
Expand Up @@ -21,6 +21,7 @@
import java.util.Map;
import java.util.logging.Logger;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.core.client.Scheduler.ScheduledCommand;
import com.google.gwt.dom.client.AnchorElement;
Expand Down Expand Up @@ -1243,6 +1244,46 @@ public static boolean pixelValuesEqual(final double num1, final double num2) {
return Math.abs(num1 - num2) <= PIXEL_EPSILON;
}

public static native TextRectangle getBoundingClientRect(Element e)
/*-{
return e.getBoundingClientRect();
}-*/;

public static final class TextRectangle extends JavaScriptObject {
protected TextRectangle() {
}

public native double getBottom()
/*-{
return this.bottom;
}-*/;

public native double getHeight()
/*-{
return this.height;
}-*/;

public native double getLeft()
/*-{
return this.left;
}-*/;

public native double getRight()
/*-{
return this.right;
}-*/;

public native double getTop()
/*-{
return this.top;
}-*/;

public native double getWidth()
/*-{
return this.width;
}-*/;
}

/**
* Wrap a css size value and its unit and translate back and forth to the
* string representation.<br/>
Expand Down
Expand Up @@ -18,7 +18,7 @@

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.TableRowElement;
import com.vaadin.client.widgets.Escalator;
import com.google.gwt.dom.client.TableSectionElement;

/**
* A representation of the rows in each of the sections (header, body and
Expand Down Expand Up @@ -192,5 +192,5 @@ public TableRowElement getRowElement(int index)
*
* @return RowContainer root element
*/
public Element getElement();
public TableSectionElement getElement();
}
2 changes: 1 addition & 1 deletion client/src/com/vaadin/client/widgets/Escalator.java
Expand Up @@ -1245,7 +1245,7 @@ public AbstractRowContainer(
}

@Override
public Element getElement() {
public TableSectionElement getElement() {
return root;
}

Expand Down
47 changes: 41 additions & 6 deletions client/src/com/vaadin/client/widgets/Grid.java
Expand Up @@ -39,6 +39,7 @@
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.dom.client.TableSectionElement;
import com.google.gwt.dom.client.Touch;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
Expand Down Expand Up @@ -941,6 +942,9 @@ protected FooterCell createCell() {
*/
protected static class Editor<T> {

private static final double BUTTON_HEIGHT = 25;
private static final double BUTTON_WIDTH = 50;
private static final double BUTTON_MARGIN = 5;
public static final int KEYCODE_SHOW = KeyCodes.KEY_ENTER;
public static final int KEYCODE_HIDE = KeyCodes.KEY_ESCAPE;

Expand Down Expand Up @@ -1256,8 +1260,9 @@ protected void showOverlay(TableRowElement tr) {
.getRequiredWidthBoundingClientRectDouble(tr);
double height = WidgetUtil
.getRequiredHeightBoundingClientRectDouble(tr);
setBounds(editorOverlay, tr.getOffsetLeft(), rowTop + bodyTop
- wrapperTop, width, height);
double overlayTop = rowTop + bodyTop - wrapperTop;
setBounds(editorOverlay, tr.getOffsetLeft(), overlayTop, width,
height);

updateHorizontalScrollPosition();

Expand Down Expand Up @@ -1297,8 +1302,6 @@ public void onClick(ClickEvent event) {
save();
}
});
setBounds(saveButton.getElement(), 0, tr.getOffsetHeight() + 5, 50,
25);
attachWidget(saveButton, editorOverlay);

cancelButton = new Button();
Expand All @@ -1311,9 +1314,41 @@ public void onClick(ClickEvent event) {
cancel();
}
});
setBounds(cancelButton.getElement(), 55, tr.getOffsetHeight() + 5,
50, 25);
attachWidget(cancelButton, editorOverlay);

/*
* We can't use BUTTON_HEIGHT here, becuase it's ignored by at least
* Chrome and Firefox. So we measure it.
*/
double buttonTop = getButtonTop(tr, saveButton.getOffsetHeight());
setBounds(saveButton.getElement(), 0, buttonTop, BUTTON_WIDTH,
BUTTON_HEIGHT);
setBounds(cancelButton.getElement(), BUTTON_WIDTH + BUTTON_MARGIN,
buttonTop, BUTTON_WIDTH, BUTTON_HEIGHT);
}

private double getButtonTop(TableRowElement tr, int buttonHeight) {
boolean buttonsShouldBeRenderedBelow = buttonsShouldBeRenderedBelow(
tr, buttonHeight);
final double buttonTop;
if (buttonsShouldBeRenderedBelow) {
buttonTop = tr.getOffsetHeight() + BUTTON_MARGIN;
} else {
buttonTop = -(buttonHeight + BUTTON_MARGIN);
}
return buttonTop;
}

private boolean buttonsShouldBeRenderedBelow(TableRowElement tr,
int buttonHeight) {
TableSectionElement tfoot = grid.escalator.getFooter().getElement();
double tfootPageTop = WidgetUtil.getBoundingClientRect(tfoot)
.getTop();
double trPageBottom = WidgetUtil.getBoundingClientRect(tr)
.getBottom();
double bottomOfButtons = trPageBottom + buttonHeight
+ BUTTON_MARGIN;
return bottomOfButtons < tfootPageTop;
}

protected void hideOverlay() {
Expand Down
Expand Up @@ -17,6 +17,7 @@

import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.dom.client.TableSectionElement;
import com.vaadin.client.widget.escalator.Cell;
import com.vaadin.client.widget.escalator.ColumnConfiguration;
import com.vaadin.client.widget.escalator.EscalatorUpdater;
Expand Down Expand Up @@ -138,7 +139,8 @@ public int getRowCount() {
}

@Override
public void setDefaultRowHeight(double px) throws IllegalArgumentException {
public void setDefaultRowHeight(double px)
throws IllegalArgumentException {
rowContainer.setDefaultRowHeight(px);
}

Expand All @@ -159,7 +161,7 @@ public TableRowElement getRowElement(int index)
}

@Override
public Element getElement() {
public TableSectionElement getElement() {
return rowContainer.getElement();
}

Expand Down

0 comments on commit 0533c6d

Please sign in to comment.