在打开窗口之前检查窗口是否已打开

时间:2012-11-23 08:18:10

标签: java gwt jsni

我有gwt java代码程序,用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);

由于某种原因,它被调用了两次代码的问题(但IE和chrome处理它但不是firefox),这就是为什么窗口只在Firefox中弹出两次。我需要知道如何防止这种情况。我试图修改窗口的JSNI代码,但我没有背景,当我做研究时,一个简单的警报不会出现,所以我无法真正调试/看看发生了什么。

/**
 * 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) {
    }         
}-*/;

我尝试通过JUNIT测试调用open()方法,但没有...

任何人都可以告诉我如何弹出警报,因为即使我删除了整个代码而只是离开了$ wnd.alert(“test2”),上面的代码也无法正常工作;如何检查JSNI中是否存在winref然后我不打开窗口?请帮助。

或者有没有办法在打开的窗口中插入类似javascript的代码?解决这个问题的最佳方法是什么? 感谢

1 个答案:

答案 0 :(得分:0)

您必须保持对打开的窗口的引用,以便您可以询问它是否已关闭。这里有一个有效的例子:

  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);
      }
    });

  }