Example usage for com.google.gwt.user.client Window getScrollTop

List of usage examples for com.google.gwt.user.client Window getScrollTop

Introduction

In this page you can find the example usage for com.google.gwt.user.client Window getScrollTop.

Prototype

public static int getScrollTop() 

Source Link

Usage

From source file:com.threerings.gwt.util.WindowUtil.java

License:Open Source License

/**
 * Returns the vertical scroll position needed to place the specified target widget in view,
 * while trying to minimize scrolling.//from   www  .j  av a  2 s  .com
 */
public static int getScrollIntoView(Widget target) {
    int top = Window.getScrollTop(), height = Window.getClientHeight();
    int ttop = target.getAbsoluteTop(), theight = target.getOffsetHeight();
    // if the target widget is taller than the browser window, or is above the current scroll
    // position of the browser window, scroll the top of the widget to the top of the window
    if (theight > height || ttop < top) {
        return ttop;
        // otherwise scroll the bottom of the widget to the bottom of the window
    } else if (ttop + theight > top + height) {
        return ttop - (height - theight);
    } else {
        return top; // no scrolling needed
    }
}

From source file:com.threerings.gwt.util.WindowUtil.java

License:Open Source License

/**
 * Returns true if the target widget is vertically scrolled into view.
 *
 * @param minPixels the minimum number of pixels that must be visible to count as "in view".
 *///from  www  .  j a v a2  s. c  o m
public static boolean isScrolledIntoView(Widget target, int minPixels) {
    int wtop = Window.getScrollTop(), wheight = Window.getClientHeight();
    int ttop = target.getAbsoluteTop();
    if (ttop > wtop) {
        return (wtop + wheight - ttop > minPixels);
    } else {
        return (ttop + target.getOffsetHeight() - wtop > minPixels);
    }
}

From source file:com.tractionsoftware.gwt.user.client.util.Geometry.java

License:Apache License

/**
 * Determines if a mouse event is inside a box.
 *///  ww w. j  a v a 2s.c o  m
public static final boolean isMouseInside(NativeEvent event, Element element) {
    return isInside(event.getClientX() + Window.getScrollLeft(), event.getClientY() + Window.getScrollTop(),
            getBounds(element));
}

From source file:com.tractionsoftware.gwt.user.client.util.Geometry.java

License:Apache License

/**
 * This takes into account scrolling and will be in absolute
 * coordinates where the top left corner of the page is 0,0 but
 * the viewport may be scrolled to something else.
 *//*w ww .ja v a2 s .com*/
public static final Rect getViewportBounds() {
    return new Rect(Window.getScrollLeft(), Window.getScrollTop(), Window.getClientWidth(),
            Window.getClientHeight());
}

From source file:com.vaadin.addon.calendar.gwt.client.ui.VCalendarPaintable.java

/**
 * Registers listeners on the calendar so server can be notified of the
 * events/*w  w  w  .ja  v a 2 s . co m*/
 */
protected void registerListeners() {
    setListener(new DateClickListener() {
        public void dateClick(String date) {
            if (!isDisabledOrReadOnly()
                    && getClient().hasEventListeners(VCalendarPaintable.this, CalendarEventId.DATECLICK)) {
                client.updateVariable(getPaintableId(), CalendarEventId.DATECLICK, date, true);
            }
        }
    });
    setListener(new ForwardListener() {
        public void forward() {
            if (client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.FORWARD)) {
                client.updateVariable(getPaintableId(), ATTR_NAVIGATION, true, true);
            }
        }
    });
    setListener(new BackwardListener() {
        public void backward() {
            if (client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.BACKWARD)) {
                client.updateVariable(getPaintableId(), ATTR_NAVIGATION, false, true);
            }
        }
    });
    setListener(new RangeSelectListener() {
        public void rangeSelected(String value) {
            if (client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.RANGESELECT)) {
                client.updateVariable(getPaintableId(), CalendarEventId.RANGESELECT, value, true);
            }
        }
    });
    setListener(new WeekClickListener() {
        public void weekClick(String event) {
            if (!isDisabledOrReadOnly()
                    && client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.WEEKCLICK)) {
                client.updateVariable(getPaintableId(), CalendarEventId.WEEKCLICK, event, true);
            }
        }
    });
    setListener(new EventMovedListener() {
        public void eventMoved(CalendarEvent event) {
            if (client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.EVENTMOVE)) {
                StringBuilder sb = new StringBuilder();
                sb.append(event.getIndex());
                sb.append(":");
                sb.append(DateUtil.formatClientSideDate(event.getStart()));
                sb.append("-");
                sb.append(DateUtil.formatClientSideTime(event.getStartTime()));
                client.updateVariable(getPaintableId(), CalendarEventId.EVENTMOVE, sb.toString(), true);
            }
        }
    });
    setListener(new EventResizeListener() {
        public void eventResized(CalendarEvent event) {
            if (client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.EVENTRESIZE)) {
                StringBuilder buffer = new StringBuilder();
                buffer.append(event.getIndex());
                buffer.append(",");

                buffer.append(DateUtil.formatClientSideDate(event.getStart()));
                buffer.append("-");
                buffer.append(DateUtil.formatClientSideTime(event.getStartTime()));

                buffer.append(",");

                buffer.append(DateUtil.formatClientSideDate(event.getEnd()));
                buffer.append("-");
                buffer.append(DateUtil.formatClientSideTime(event.getEndTime()));

                client.updateVariable(getPaintableId(), CalendarEventId.EVENTRESIZE, buffer.toString(), true);
            }
        }
    });
    setListener(new ScrollListener() {
        public void scroll(int scrollPosition) {
            client.updateVariable(getPaintableId(), ATTR_SCROLL, scrollPosition, false);
        }
    });
    setListener(new EventClickListener() {
        public void eventClick(CalendarEvent event) {
            if (client.hasEventListeners(VCalendarPaintable.this, CalendarEventId.EVENTCLICK)) {
                client.updateVariable(getPaintableId(), CalendarEventId.EVENTCLICK, event.getIndex(), true);
            }
        }
    });
    setListener(new MouseEventListener() {
        public void contextMenu(ContextMenuEvent event, final Widget widget) {
            final NativeEvent ne = event.getNativeEvent();
            int left = ne.getClientX();
            int top = ne.getClientY();
            top += Window.getScrollTop();
            left += Window.getScrollLeft();
            client.getContextMenu().showAt(new ActionOwner() {
                public String getPaintableId() {
                    return VCalendarPaintable.this.getPaintableId();
                }

                public ApplicationConnection getClient() {
                    return VCalendarPaintable.this.getClient();
                }

                @SuppressWarnings("deprecation")
                public Action[] getActions() {
                    if (widget instanceof SimpleDayCell) {
                        /*
                         * Month view
                         */
                        SimpleDayCell cell = (SimpleDayCell) widget;
                        Date start = new Date(cell.getDate().getYear(), cell.getDate().getMonth(),
                                cell.getDate().getDate(), 0, 0, 0);

                        Date end = new Date(cell.getDate().getYear(), cell.getDate().getMonth(),
                                cell.getDate().getDate(), 23, 59, 59);

                        return VCalendarPaintable.this.getActionsBetween(start, end);
                    } else if (widget instanceof DateCell) {
                        /*
                         * Week and Day view
                         */
                        DateCell cell = (DateCell) widget;
                        int slotIndex = DOM.getChildIndex(cell.getElement(),
                                (Element) ne.getEventTarget().cast());
                        DateCellSlot slot = cell.getSlot(slotIndex);
                        return VCalendarPaintable.this.getActionsBetween(slot.getFrom(), slot.getTo());
                    } else if (widget instanceof DayEvent) {
                        /*
                         * Context menu on event
                         */
                        DayEvent dayEvent = (DayEvent) widget;
                        CalendarEvent event = dayEvent.getCalendarEvent();
                        Action[] actions = VCalendarPaintable.this.getActionsBetween(event.getStartTime(),
                                event.getEndTime());
                        for (Action action : actions) {
                            ((VCalendarAction) action).setEvent(event);
                        }
                        return actions;

                    } else if (widget instanceof MonthEventLabel) {
                        MonthEventLabel mel = (MonthEventLabel) widget;
                        CalendarEvent event = mel.getCalendarEvent();
                        Action[] actions = VCalendarPaintable.this.getActionsBetween(event.getStartTime(),
                                event.getEndTime());
                        for (Action action : actions) {
                            ((VCalendarAction) action).setEvent(event);
                        }
                        return actions;
                    }
                    return null;
                }
            }, left, top);
        }
    });
}

From source file:com.vaadin.addon.spreadsheet.client.SheetWidget.java

private void setHyperlinkTooltipPosition(int offsetWidth, int offsetHeight, DivElement element) {
    // Calculate left position for the popup. The computation for
    // the left position is bidi-sensitive.

    int textBoxOffsetWidth = element.getOffsetWidth();

    // Compute the difference between the popup's width and the
    // textbox's width
    int offsetWidthDiff = offsetWidth - textBoxOffsetWidth;

    int left;/*from  w ww  .  j  ava  2 s  . c o  m*/

    if (LocaleInfo.getCurrentLocale().isRTL()) { // RTL case

        int textBoxAbsoluteLeft = element.getAbsoluteLeft();

        // Right-align the popup. Note that this computation is
        // valid in the case where offsetWidthDiff is negative.
        left = textBoxAbsoluteLeft - offsetWidthDiff;

        // If the suggestion popup is not as wide as the text box, always
        // align to the right edge of the text box. Otherwise, figure out
        // whether
        // to right-align or left-align the popup.
        if (offsetWidthDiff > 0) {

            // Make sure scrolling is taken into account, since
            // box.getAbsoluteLeft() takes scrolling into account.
            int windowRight = Window.getClientWidth() + Window.getScrollLeft();
            int windowLeft = Window.getScrollLeft();

            // Compute the left value for the right edge of the textbox
            int textBoxLeftValForRightEdge = textBoxAbsoluteLeft + textBoxOffsetWidth;

            // Distance from the right edge of the text box to the right
            // edge
            // of the window
            int distanceToWindowRight = windowRight - textBoxLeftValForRightEdge;

            // Distance from the right edge of the text box to the left edge
            // of the
            // window
            int distanceFromWindowLeft = textBoxLeftValForRightEdge - windowLeft;

            // If there is not enough space for the overflow of the popup's
            // width to the right of the text box and there IS enough space
            // for the
            // overflow to the right of the text box, then left-align the
            // popup.
            // However, if there is not enough space on either side, stick
            // with
            // right-alignment.
            if (distanceFromWindowLeft < offsetWidth && distanceToWindowRight >= offsetWidthDiff) {
                // Align with the left edge of the text box.
                left = textBoxAbsoluteLeft;
            }
        }
    } else { // LTR case

        // Left-align the popup.
        left = element.getAbsoluteLeft();

        // If the suggestion popup is not as wide as the text box, always
        // align to
        // the left edge of the text box. Otherwise, figure out whether to
        // left-align or right-align the popup.
        if (offsetWidthDiff > 0) {
            // Make sure scrolling is taken into account, since
            // box.getAbsoluteLeft() takes scrolling into account.
            int windowRight = Window.getClientWidth() + Window.getScrollLeft();
            int windowLeft = Window.getScrollLeft();

            // Distance from the left edge of the text box to the right edge
            // of the window
            int distanceToWindowRight = windowRight - left;

            // Distance from the left edge of the text box to the left edge
            // of the
            // window
            int distanceFromWindowLeft = left - windowLeft;

            // If there is not enough space for the overflow of the popup's
            // width to the right of hte text box, and there IS enough space
            // for the
            // overflow to the left of the text box, then right-align the
            // popup.
            // However, if there is not enough space on either side, then
            // stick with
            // left-alignment.
            if (distanceToWindowRight < offsetWidth && distanceFromWindowLeft >= offsetWidthDiff) {
                // Align with the right edge of the text box.
                left -= offsetWidthDiff;
            }
        }
    }

    // Calculate top position for the popup

    int top = element.getAbsoluteTop();

    // Make sure scrolling is taken into account, since
    // box.getAbsoluteTop() takes scrolling into account.
    int windowTop = Window.getScrollTop();
    int windowBottom = Window.getScrollTop() + Window.getClientHeight();

    // Distance from the top edge of the window to the top edge of the
    // text box
    int distanceFromWindowTop = top - windowTop;

    // Distance from the bottom edge of the window to the bottom edge of
    // the text box
    int distanceToWindowBottom = windowBottom - (top + element.getOffsetHeight());

    // If there is not enough space for the popup's height below the text
    // box and there IS enough space for the popup's height above the text
    // box, then then position the popup above the text box. However, if
    // there
    // is not enough space on either side, then stick with displaying the
    // popup below the text box.
    if (distanceToWindowBottom < offsetHeight && distanceFromWindowTop >= offsetHeight) {
        top -= offsetHeight;
    } else {
        // Position above the text box
        top += element.getOffsetHeight();
    }
    hyperlinkTooltip.setPopupPosition(left, top);
}

From source file:com.vaadin.client.connectors.grid.GridDragSourceConnector.java

License:Apache License

private int getRelativeY(Element element, NativeEvent event) {
    int relativeTop = element.getAbsoluteTop() - Window.getScrollTop();
    return WidgetUtil.getTouchOrMouseClientY(event) - relativeTop;
}

From source file:com.vaadin.client.ui.calendar.CalendarConnector.java

License:Apache License

/**
 * Registers listeners on the calendar so server can be notified of the
 * events/*from w  ww  .  j a v  a2s  .  co  m*/
 */
protected void registerListeners() {
    getWidget().setListener(new DateClickListener() {
        @Override
        public void dateClick(String date) {
            if (!getWidget().isDisabled() && hasEventListener(CalendarEventId.DATECLICK)) {
                rpc.dateClick(date);
            }
        }
    });
    getWidget().setListener(new ForwardListener() {
        @Override
        public void forward() {
            if (hasEventListener(CalendarEventId.FORWARD)) {
                rpc.forward();
            }
        }
    });
    getWidget().setListener(new BackwardListener() {
        @Override
        public void backward() {
            if (hasEventListener(CalendarEventId.BACKWARD)) {
                rpc.backward();
            }
        }
    });
    getWidget().setListener(new RangeSelectListener() {
        @Override
        public void rangeSelected(String value) {
            if (hasEventListener(CalendarEventId.RANGESELECT)) {
                rpc.rangeSelect(value);
            }
        }
    });
    getWidget().setListener(new WeekClickListener() {
        @Override
        public void weekClick(String event) {
            if (!getWidget().isDisabled() && hasEventListener(CalendarEventId.WEEKCLICK)) {
                rpc.weekClick(event);
            }
        }
    });
    getWidget().setListener(new EventMovedListener() {
        @Override
        public void eventMoved(CalendarEvent event) {
            if (hasEventListener(CalendarEventId.EVENTMOVE)) {
                StringBuilder sb = new StringBuilder();
                sb.append(DateUtil.formatClientSideDate(event.getStart()));
                sb.append("-");
                sb.append(DateUtil.formatClientSideTime(event.getStartTime()));
                rpc.eventMove(event.getIndex(), sb.toString());
            }
        }
    });
    getWidget().setListener(new EventResizeListener() {
        @Override
        public void eventResized(CalendarEvent event) {
            if (hasEventListener(CalendarEventId.EVENTRESIZE)) {
                StringBuilder buffer = new StringBuilder();

                buffer.append(DateUtil.formatClientSideDate(event.getStart()));
                buffer.append("-");
                buffer.append(DateUtil.formatClientSideTime(event.getStartTime()));

                String newStartDate = buffer.toString();

                buffer = new StringBuilder();
                buffer.append(DateUtil.formatClientSideDate(event.getEnd()));
                buffer.append("-");
                buffer.append(DateUtil.formatClientSideTime(event.getEndTime()));

                String newEndDate = buffer.toString();

                rpc.eventResize(event.getIndex(), newStartDate, newEndDate);
            }
        }
    });
    getWidget().setListener(new VCalendar.ScrollListener() {
        @Override
        public void scroll(int scrollPosition) {
            // This call is @Delayed (== non-immediate)
            rpc.scroll(scrollPosition);
        }
    });
    getWidget().setListener(new EventClickListener() {
        @Override
        public void eventClick(CalendarEvent event) {
            if (hasEventListener(CalendarEventId.EVENTCLICK)) {
                rpc.eventClick(event.getIndex());
            }
        }
    });
    getWidget().setListener(new MouseEventListener() {
        @Override
        public void contextMenu(ContextMenuEvent event, final Widget widget) {
            final NativeEvent ne = event.getNativeEvent();
            int left = ne.getClientX();
            int top = ne.getClientY();
            top += Window.getScrollTop();
            left += Window.getScrollLeft();
            getClient().getContextMenu().showAt(new ActionOwner() {
                @Override
                public String getPaintableId() {
                    return CalendarConnector.this.getPaintableId();
                }

                @Override
                public ApplicationConnection getClient() {
                    return CalendarConnector.this.getClient();
                }

                @Override
                @SuppressWarnings("deprecation")
                public Action[] getActions() {
                    if (widget instanceof SimpleDayCell) {
                        /*
                         * Month view
                         */
                        SimpleDayCell cell = (SimpleDayCell) widget;
                        Date start = new Date(cell.getDate().getYear(), cell.getDate().getMonth(),
                                cell.getDate().getDate(), 0, 0, 0);

                        Date end = new Date(cell.getDate().getYear(), cell.getDate().getMonth(),
                                cell.getDate().getDate(), 23, 59, 59);

                        return CalendarConnector.this.getActionsBetween(start, end);

                    } else if (widget instanceof MonthEventLabel) {
                        MonthEventLabel mel = (MonthEventLabel) widget;
                        CalendarEvent event = mel.getCalendarEvent();
                        Action[] actions = CalendarConnector.this.getActionsBetween(event.getStartTime(),
                                event.getEndTime());
                        for (Action action : actions) {
                            ((VCalendarAction) action).setEvent(event);
                        }
                        return actions;

                    } else if (widget instanceof DateCell) {
                        /*
                         * Week and Day view
                         */
                        DateCell cell = (DateCell) widget;
                        int slotIndex = DOM.getChildIndex(cell.getElement(),
                                (Element) ne.getEventTarget().cast());
                        DateCellSlot slot = cell.getSlot(slotIndex);
                        return CalendarConnector.this.getActionsBetween(slot.getFrom(), slot.getTo());
                    } else if (widget instanceof DateCellDayEvent) {
                        /*
                         * Context menu on event
                         */
                        DateCellDayEvent dayEvent = (DateCellDayEvent) widget;
                        CalendarEvent event = dayEvent.getCalendarEvent();

                        Action[] actions = CalendarConnector.this.getActionsBetween(event.getStartTime(),
                                event.getEndTime());

                        for (Action action : actions) {
                            ((VCalendarAction) action).setEvent(event);
                        }

                        return actions;
                    }
                    return null;
                }
            }, left, top);
        }
    });
}

From source file:com.vaadin.client.ui.dd.DDUtil.java

License:Apache License

public static VerticalDropLocation getVerticalDropLocation(Element element, int offsetHeight, int clientY,
        double topBottomRatio) {

    // Event coordinates are relative to the viewport, element absolute
    // position is relative to the document. Make element position relative
    // to viewport by adjusting for viewport scrolling. See #6021
    int elementTop = element.getAbsoluteTop() - Window.getScrollTop();
    int fromTop = clientY - elementTop;

    float percentageFromTop = (fromTop / (float) offsetHeight);
    if (percentageFromTop < topBottomRatio) {
        return VerticalDropLocation.TOP;
    } else if (percentageFromTop > 1 - topBottomRatio) {
        return VerticalDropLocation.BOTTOM;
    } else {// w ww  . ja  v  a  2 s .  co  m
        return VerticalDropLocation.MIDDLE;
    }
}

From source file:com.vaadin.client.ui.SuperTreeWidget.java

License:Apache License

private void handleBodyContextMenu(ContextMenuEvent event) {
    if (!readonly && !disabled) {
        if (bodyActionKeys != null) {
            int left = event.getNativeEvent().getClientX();
            int top = event.getNativeEvent().getClientY();
            top += Window.getScrollTop();
            left += Window.getScrollLeft();
            client.getContextMenu().showAt(this, left, top);
        }//  ww  w.  ja v a2s .c  o  m
        event.stopPropagation();
        event.preventDefault();
    }
}