How to detect if a Popup is showing

356 views
Skip to first unread message

Craig Mitchell

unread,
Jul 11, 2011, 9:00:00 PM7/11/11
to google-we...@googlegroups.com
Hi,

I wanted to check if a popup is showing, so I wrote this method:

public static int getNumPopupsShowing() {
int result = 0;
NodeList<Element> divs = Document.get().getElementsByTagName("div");
for (int i=0; i<divs.getLength(); i++) {
if ("gwt-PopupPanel".equals(divs.getItem(i).getClassName())) {
result++;
}
}
return result;
}

However, is there a better way?

Thanks.

ashwin....@gmail.com

unread,
Jul 11, 2011, 9:52:46 PM7/11/11
to google-we...@googlegroups.com
Popup Panels have a isShowing() method, use that to determine if its currently displayed

~Ashwin

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/Jwf77I4AUsoJ.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

Craig Mitchell

unread,
Jul 11, 2011, 10:19:14 PM7/11/11
to google-we...@googlegroups.com
Thanks Ashwin.  Yes, sorry, should have been a little more clear, I wanted to detect if a popup is showing, "which I didn't already have the reference to".

Apologies for not being more clear.

dreamer

unread,
Jul 12, 2011, 12:21:43 AM7/12/11
to Google Web Toolkit
Not sure, if there is a automatic way of detecting all child windows
to a parent window on demand,
but certainly you can main maintain, global array list and every popup
window that you open, and
call close on each of those. You can lot of examples in javascript -
"closing child windows to idea".

Thomas Broyer

unread,
Jul 12, 2011, 4:19:01 AM7/12/11
to google-we...@googlegroups.com
PopupPanels append themselves to RootPanel.get(), so you can loop over RootPanel.get().getChildren() and whenever you see a PopupPanel you can ask if it isShowing().
Message has been deleted

Craig Mitchell

unread,
Jul 12, 2011, 11:34:08 PM7/12/11
to google-we...@googlegroups.com
Thanks Thomas.  Much cleaner that way!

New code:

/**
 * @return the number of modal popups currently showing
 */
public static int getNumPopupsShowing() {
int result = 0;

for (int i=0; i<RootPanel.get().getWidgetCount(); i++) {
Widget widget = RootPanel.get().getWidget(i);
if (widget instanceof PopupPanel
&& ((PopupPanel)widget).isShowing()
&& ((PopupPanel)widget).isModal()) {
result++;
}
}

return result;
}

I actually thought this wouldn't work when compiled, because I didn't know how it would tell which div was a popup panel, but somehow it does!  Go GWT!

Thomas Broyer

unread,
Jul 13, 2011, 5:06:53 AM7/13/11
to google-we...@googlegroups.com
Any reason you don't use an "enhanced for loop"? for (Widget widget : RootPanel.get().getChildren()) { ... }

It works because it's looping on widgets, not elements (look at the code, there's an ArrayList<Widget> to store the child widgets; there's no magic)

Craig Mitchell

unread,
Jul 13, 2011, 9:08:53 PM7/13/11
to google-we...@googlegroups.com
getChildren() doesn't exist (in GWT 2.2.0).  However, I could have used RootPanel.get().iterator().

I would have thought when GWT compiled the widgets, they would turn into a <div> or something similar, and their type would be lost, however, I'm clearly wrong, as it works a treat!

Craig Mitchell

unread,
Jul 13, 2011, 9:11:41 PM7/13/11
to google-we...@googlegroups.com
Sorry, getChildren() does exist, however, it's a protected method.

Thomas Broyer

unread,
Jul 13, 2011, 9:18:58 PM7/13/11
to google-we...@googlegroups.com
Oh right, sorry, missed the 'protected' keyword in the javadoc. Note however that HasWidgets extends Iterable<Widget> (hence the existence of RootPanel.get().iterator()) so you could write "for (Widget w : RootPanel.get()) { ... }" (not sure I like it though)
Reply all
Reply to author
Forward
0 new messages