Search code examples
javagwtjsni

check if window is already open before it opens it


I have gwt java code program that opens up a window with url.

 Map<String, Object> ids = new HashMap<String, Object>();
                    ids.put("employeeId", UserToken.getCurrentUserToken().getEmployeeId());
                    createCaseUrl = ICMIntakeConstants.buildGrailsUrl(ICMIntakeConstants.CLIENT_APPLICATION_CONTROLLER, "", ids);
                    ICMWindow.open(createCaseUrl, "intake", "height=600,width=800,scrollbars=yes,resizable=yes,toolbar=no,directories=no,status=no,menubar=no", true);

The problem with the code it is being called twice for some reason (but IE and chrome handles it but not firefox), that is why the window pops up twice only in Firefox.I need to know how to prevent this. I was trying to modify the window's JSNI code but I have zero background and when I did research a simple alert would not come up so I couldn't really debug/see what is happening.

/**
 * Defines all variable in JavaScript accessible by Java code and opens a new browser window
 * with the specified url, name and features.
 */
public static native void open(String url, String name, String features, boolean force) 
/*-{
  $wnd.alert("test2");
    var winRef;
    try {

        if (force)
        {


                 winRef = $wnd.open(url,name, features);

                  $wnd.alert("test1 force");
                  Alert.alert("clicked!");

        }
        else
        {
            $wnd.alert("test2");
             winRef = $wnd.open("",name, features);
             if(winRef.location == 'about:blank')
             {
                  winRef.location=url
             }
             else 
             {
                  if(!/Chrome[\/\s]/.test(navigator.userAgent))
                  {
                      winRef.alert("Please click Ok To Continue....");
                  }
             }
        }

        // Set focus to the opened window
        winRef.focus();

    } catch (err) {
    }         
}-*/;

I tried calling the open() method through JUNIT test but nothing...

Can anyone tell me how can I make the alert pop up coz the above code won't work even if I erased the whole code and just left the $wnd.alert("test2");? and how can I check if winref exists in JSNI then I shall not open the window? Pls help.

Or is there a way I can insert a like a javascript code in the window being opened? What is the best way to resolve this? thanks


Solution

  • You have to maintain the reference to the opened window so as you can ask whether it is closed. Here you have a working example:

      private native Element open(String url, String name, String features) /*-{
        return $wnd.open(url, name, features);
      }-*/;
    
      private native boolean isOpen(Element win) /*-{
        return !! (win && !win.closed);
      }-*/;
    
      private native void close(Element win) /*-{
        if (win) win.close();
      }-*/;
    
      private native void focus(Element win) /*-{
        if (win) win.focus();
      }-*/;
    
      private Element win = null;
    
      public void onModuleLoad() {
    
        Button open = new Button("Open Win");
        Button close = new Button("Close Win");
        RootPanel.get().add(open);
        RootPanel.get().add(close);
    
    
        open.addClickHandler(new ClickHandler() {
          public void onClick(ClickEvent event) {
            if (!isOpen(win)) {
              win = open("http://www.google.com", "myWin", "height=600,width=800,scrollbars=yes,resizable=yes,toolbar=no,directories=no,status=no,menubar=no");
            }
            focus(win);
          }
        });
    
        close.addClickHandler(new ClickHandler() {
          public void onClick(ClickEvent event) {
            close(win);
          }
        });
    
      }