2012-11-23 155 views
0

我有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.I兩次彈出需要知道如何防止這一點。我試圖修改窗口的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測試,但沒有開放的()方法...

誰能告訴我怎樣才能使警報彈出怎麼把上面的代碼,如果我甚至不會工作抹去了整個代碼,只剩下$ wnd.alert(「test2」);?以及如何檢查JSNI中是否存在winref,那麼我不會打開窗口?請幫助。

或者有沒有一種方法可以在打開的窗口中插入一個類似於JavaScript代碼的方法?解決這個問題的最好方法是什麼? 謝謝

回答

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

    }