我正在处理单页wicket应用程序。当用户点击导航项时,会打开一个新选项卡。要求是有两种类型的面板,一种是普通面板,另一种是自动刷新面板。
我有一个TabPanel类,它从Panel扩展,实际上包含了管理标签的所有选项卡和方法,如添加,删除,设置当前。
以下是我添加到TabPanel的客户计时器。
private class AutoRefreshTimer extends AbstractAjaxTimerBehavior {
public AutoRefreshTimer(Duration updateInterval) {
super(updateInterval);
}
@Override
protected void onTimer(AjaxRequestTarget target) {
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel) currentTab.getPanel();
ajaxRefreshPanel.onRefresh(target);
}
}
}
}
以下是添加计时器'autoRefreshTimer'的方法,它是TabPanel类中的实例变量。
private void addNewAutoRefreshTimer(final Duration autoRefreshInterval) {
autoRefreshTimer = new AutoRefreshTimer(autoRefreshInterval);
add(autoRefreshTimer);
}
以下是控制计时器的方法:
public void startAutoRefreshTimer(final AjaxRequestTarget target, final Duration autoRefreshInterval) {
if(!autoRefreshTimer.isStopped())
addNewAutoRefreshTimer(autoRefreshInterval);
autoRefreshTimer.restart(target);
}
public void stopAutoRefreshTimer(final AjaxRequestTarget target, final Duration autoRefreshInterval) {
if(autoRefreshTimer.isStopped())
addNewAutoRefreshTimer(autoRefreshInterval);
autoRefreshTimer.stop(target);
}
public void toggleAutoRefreshTimer(final AjaxRequestTarget target){
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel)currentTab.getPanel();
this.autoRefreshInterval = ajaxRefreshPanel.getAutoRefreshInterval();
startAutoRefreshTimer(target, this.autoRefreshInterval);
return;
}
}
stopAutoRefreshTimer(target, this.autoRefreshInterval);
}
当用户在选项卡之间切换时,计时器在本地服务器中启动和停止,无论是打开新的,还是切换到已打开,关闭当前选择上一个选项卡的电流,或关闭另一个选项卡而不切换当前。 但是当我在位于远程位置的登台服务器中测试相同时,计时器在以下情况下失败。
以下是在远程中创建问题的步骤:
1. Open two Normal tabs.
2. Open a tab containing AutoRefreshPanel.
3. Close 1st or 2nd normal tab by keeping the tab containing AutoRefreshPanel active/current. This expect the timer to be
fired/running.
4. Switch to the remaining Normal tab. This stops the timer in local server but not in Remote.
除了上述所有其他方案中的步骤外,计时器在本地和远程都按预期启动和停止。
//更新:我找到了解决此问题的方法。但不确定它是最好的。以下是我所做的更改:
private class AutoRefreshTimer extends AbstractAjaxTimerBehavior {
public AutoRefreshTimer(Duration updateInterval) {
super(updateInterval);
}
@Override
protected void onTimer(AjaxRequestTarget target) {
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel) currentTab.getPanel();
ajaxRefreshPanel.onRefresh(target);
return; // added return
}
}
stop(target); // added a call to stop method.
}
}
// Replaced startAutoRefreshTimer, stopAutoRefreshTimer and toggleAutoRefreshTimer with the following method:
public void manageAutoRefreshTimer(final AjaxRequestTarget target){
Tab currentTab = getCurrentTab();
if(currentTab != null) {
if(currentTab.getPanel() instanceof AutoRefreshPanel) {
AutoRefreshPanel ajaxRefreshPanel = (AutoRefreshPanel)currentTab.getPanel();
this.autoRefreshInterval = ajaxRefreshPanel.getAutoRefreshInterval();
if(!autoRefreshTimer.isStopped())
addNewAutoRefreshTimer(autoRefreshInterval);
autoRefreshTimer.restart(target);
}
}
}