Introduction
This web page tells you a couple of functions you might find
useful for managing dates and popup window coordinates.
Dates
To process the GWT date (in order to extract year, date and
month) the following functions may be useful. Note these functions are deprecated,
but there are no other simple solutions available in GWT currently.
Buried with the GWT documentation is the fact that dates are stored relative to
1/1/1900 (note they can also be set before this date). Also months are from 0
to 11 but days are from 1 to 31. Also to get the day you must use the function getDate()
and not getDay().
To convert a date to a managed string:
|
@SuppressWarnings(“deprecation”)
public static String FormatDate(Date date)
{
Date dt = null;
if (date == null)
dt = new Date();
else
dt = date;
int year = dt.getYear() + 1900;
int month = dt.getMonth() + 1;
int day = dt.getDate();
String dateStr = day + "-" + month + "-" + year;
return dateStr;
}
|
To convert a managed string to a date:
|
@SuppressWarnings("deprecation")
public static Date ParseDate(String dateStr, Date
defaultDate)
{
try
{
// TODO Auto-generated method stub
if (dateStr == null)
{
if (defaultDate == null)
return new Date();
else
return defaultDate;
}
int sep1 = dateStr.indexOf("-");
if (sep1 < 0)
return new Date();
int sep2 = dateStr.indexOf("-", sep1 + 1);
if (sep2 < 0)
return new Date();
int day = Integer.parseInt(dateStr.substring(0,
sep1));
int month = Integer.parseInt(dateStr.substring(sep1
+ 1, sep2)) - 1;
int year = Integer.parseInt(dateStr.substring(sep2
+ 1)) - 1900;
return new Date(year,
month, day, 0, 0, 0);
}
catch (Exception ex)
{
if (defaultDate == null)
return new Date();
else
return defaultDate;
}
}
|
Coordinates
If you want to make a popup box appear then you may find the
following example is given, for a click event.
|
final int final_x = e.getClientX();
final int final_y = e.getClientY();
|
However this does not take account of the active scroll
position of the current window in the browser. To do this properly use this
code, (also I move the popup window slightly to the right of the cursor by 20
pixels).
|
final int final_x = e.getClientX() + Window.getScrollLeft()
+ 20;
final int final_y = e.getClientY() +
Window.getScrollTop();
|
Once the popup appears you may find it falls below the
current browser window. It is not easily possible to calculate the popup window
dimensions before it is rendered in the browser. Therefore the following
solution, to move the window up, should be acceptable.
|
_popup.setPopupPosition(x, y);
_popup.show();
Boolean moveWindow = false;
if (y > Window.getClientHeight() +
Window.getScrollTop() - _popup.getOffsetHeight())
{
y = Window.getClientHeight() + Window.getScrollTop()
- _popup.getOffsetHeight();
if (y < 0)
y = 0;
moveWindow = true;
}
if (x > Window.getClientWidth() +
Window.getScrollLeft() - _popup.getOffsetWidth())
{
x = Window.getClientWidth() + Window.getScrollLeft()
- _popup.getOffsetWidth();
if (x < 0)
x = 0;
moveWindow = true;
}
if (moveWindow)
{
_popup.setPopupPosition(x, y);
}
|
This formula can be derived by studying the following
diagram. The goal, in this case, is to ensure that the y-coordinate is equal to
the top left corner of the popup.

Here is an example of this popup positioning action. The
popup Window with the text “Set this filter …” has been moved up from its normal
position, which would usually be level with the yellow label saying “Filter
Words”. You can see this happening on this website.
