diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java index cd32dde461..f124b7d105 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/AdempiereWebUI.java @@ -17,6 +17,7 @@ package org.adempiere.webui; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -53,6 +54,7 @@ import org.zkoss.web.Attributes; import org.zkoss.web.servlet.Servlets; import org.zkoss.zk.au.out.AuScript; import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.Session; @@ -238,10 +240,14 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb keyListener.setCtrlKeys("@a@c@d@e@f@h@n@o@p@r@s@t@z@x@#left@#right@#up@#down@#home@#end#enter^u@u"); keyListener.setAutoBlur(false); - IDesktop d = (IDesktop) currSess.getAttribute(APPLICATION_DESKTOP_KEY); + @SuppressWarnings("unchecked") + WeakReference desktopRef = (WeakReference) currSess.getAttribute(APPLICATION_DESKTOP_KEY); + IDesktop d = desktopRef != null ? desktopRef.get() : null; if (d != null && d instanceof IDesktop) { - ExecutionCarryOver eco = (ExecutionCarryOver) currSess.getAttribute(EXECUTION_CARRYOVER_SESSION_KEY); + @SuppressWarnings("unchecked") + WeakReference ecoRef = (WeakReference) currSess.getAttribute(EXECUTION_CARRYOVER_SESSION_KEY);; + ExecutionCarryOver eco = ecoRef != null ? ecoRef.get() : null; if (eco != null) { //try restore try { @@ -299,11 +305,11 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb } appDesktop.setPage(this.getPage()); Clients.response(new AuScript("$('.slimScroll .z-anchorlayout-body').slimScroll({height: '100%',railVisible: true, alwaysVisible: false});")); - currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, current); + currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, new WeakReference(current)); } - currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop()); - ctx.put(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop()); + currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, new WeakReference(this.getPage().getDesktop())); + ctx.put(ZK_DESKTOP_SESSION_KEY, new WeakReference(this.getPage().getDesktop())); ClientInfo sessionClientInfo = (ClientInfo) currSess.getAttribute(CLIENT_INFO); if (sessionClientInfo != null) { clientInfo = sessionClientInfo; @@ -329,11 +335,11 @@ public class AdempiereWebUI extends Window implements EventListener, IWeb createDesktop(); appDesktop.setClientInfo(clientInfo); appDesktop.createPart(this.getPage()); - currSess.setAttribute(APPLICATION_DESKTOP_KEY, appDesktop); + currSess.setAttribute(APPLICATION_DESKTOP_KEY, new WeakReference(appDesktop)); ExecutionCarryOver eco = new ExecutionCarryOver(this.getPage().getDesktop()); - currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, eco); - currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop()); - ctx.put(ZK_DESKTOP_SESSION_KEY, this.getPage().getDesktop()); + currSess.setAttribute(EXECUTION_CARRYOVER_SESSION_KEY, new WeakReference(eco)); + currSess.setAttribute(ZK_DESKTOP_SESSION_KEY, new WeakReference(this.getPage().getDesktop())); + ctx.put(ZK_DESKTOP_SESSION_KEY, new WeakReference(this.getPage().getDesktop())); } //ensure server push is on diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/AEnv.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/AEnv.java index d1253899b2..da460d60b6 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/AEnv.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/AEnv.java @@ -21,6 +21,7 @@ import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; +import java.lang.ref.WeakReference; import java.net.URI; import java.net.URISyntaxException; import java.sql.PreparedStatement; @@ -811,8 +812,13 @@ public final class AEnv */ public static Desktop getDesktop() { boolean inUIThread = Executions.getCurrent() != null; - return inUIThread ? Executions.getCurrent().getDesktop() - : (Desktop) Env.getCtx().get(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY); + if (inUIThread) { + return Executions.getCurrent().getDesktop(); + } else { + @SuppressWarnings("unchecked") + WeakReference ref = (WeakReference) Env.getCtx().get(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY); + return ref != null ? ref.get() : null; + } } /** diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessDialog.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessDialog.java index 236f7e3809..1c2f2a1861 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessDialog.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessDialog.java @@ -6,6 +6,7 @@ import static org.compiere.model.SystemIDs.PROCESS_M_INOUT_GENERATE; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; +import java.lang.ref.WeakReference; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -347,7 +348,7 @@ public class ProcessDialog extends Window implements EventListener, IProc Properties context = ServerContext.getCurrentInstance(); if (context.get(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY) == null) { Desktop desktop = this.getDesktop(); - context.put(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY, desktop); + context.put(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY, new WeakReference(desktop)); } processDialogRunnable = new ProcessDialogRunnable(); diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessModalDialog.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessModalDialog.java index 7ea413b0a6..0566bb62fd 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessModalDialog.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/apps/ProcessModalDialog.java @@ -17,6 +17,7 @@ package org.adempiere.webui.apps; import java.io.File; +import java.lang.ref.WeakReference; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; @@ -382,7 +383,7 @@ public class ProcessModalDialog extends Window implements EventListener, Properties context = ServerContext.getCurrentInstance(); if (context.get(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY) == null) { Desktop desktop = this.getDesktop(); - context.put(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY, desktop); + context.put(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY, new WeakReference(desktop)); } processDialogRunnable = new ProcessDialogRunnable(); diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPCalendar.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPCalendar.java index 96a8fdebad..4152c9ab61 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPCalendar.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPCalendar.java @@ -13,6 +13,7 @@ *****************************************************************************/ package org.adempiere.webui.dashboard; +import java.lang.ref.WeakReference; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.Timestamp; @@ -56,6 +57,7 @@ import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; +import org.zkoss.zk.ui.util.DesktopCleanup; import org.zkoss.zul.Label; import org.zkoss.zul.impl.LabelImageElement; @@ -84,8 +86,9 @@ public class DPCalendar extends DashboardPanel implements EventListener, private EventWindow eventWin; private Properties ctx; - private Desktop desktop; + private WeakReference desktop; private ArrayList events; + private DesktopCleanup listener; private static RequestEventHandler eventHandler; private static TopicSubscriber subscriber; @@ -126,6 +129,13 @@ public class DPCalendar extends DashboardPanel implements EventListener, calendars.addEventListener("onEventEdit", this); createStaticListeners(); + + listener = new DesktopCleanup() { + @Override + public void cleanup(Desktop desktop) throws Exception { + DPCalendar.this.cleanup(); + } + }; } private synchronized void createStaticListeners() { @@ -348,7 +358,17 @@ public class DPCalendar extends DashboardPanel implements EventListener, public void refresh(ServerPushTemplate template) { refreshModel(); template.executeAsync(this); - desktop = getDesktop(); + if (desktop != null && desktop.get() != null) { + if (desktop.get() != getDesktop()) { + desktop.get().removeListener(listener); + desktop = new WeakReference(getDesktop()); + desktop.get().addListener(listener); + } + } else { + desktop = new WeakReference(getDesktop()); + desktop.get().addListener(listener); + } + } @Override @@ -405,8 +425,8 @@ public class DPCalendar extends DashboardPanel implements EventListener, if (clientId.equals(AD_Client_ID) && !"0".equals(AD_User_ID)) { if (salesRepId.equals(AD_User_ID) || userId.equals(AD_User_ID) || createdBy.equals(AD_User_ID)) { try { - if (desktop != null && desktop.isAlive()) { - ServerPushTemplate template = new ServerPushTemplate(desktop); + if (desktop != null && desktop.get() != null && desktop.get().isAlive()) { + ServerPushTemplate template = new ServerPushTemplate(desktop.get()); refresh(template); } else { EventManager.getInstance().unregister(this); @@ -424,13 +444,24 @@ public class DPCalendar extends DashboardPanel implements EventListener, super.onPageAttached(newpage, oldpage); if (newpage != null) { EventManager.getInstance().register(ON_REQUEST_CHANGED_TOPIC, this); - desktop = getDesktop(); + if (desktop != null && desktop.get() != null) { + desktop.get().removeListener(listener); + } + desktop = new WeakReference(getDesktop()); + desktop.get().addListener(listener); } } @Override public void onPageDetached(Page page) { super.onPageDetached(page); + cleanup(); + } + + /** + * + */ + protected void cleanup() { EventManager.getInstance().unregister(this); desktop = null; } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPRecentItems.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPRecentItems.java index 8166928945..7d34098857 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPRecentItems.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DPRecentItems.java @@ -13,6 +13,7 @@ *****************************************************************************/ package org.adempiere.webui.dashboard; +import java.lang.ref.WeakReference; import java.util.List; import java.util.Properties; @@ -39,6 +40,7 @@ import org.zkoss.zk.ui.event.DropEvent; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; +import org.zkoss.zk.ui.util.DesktopCleanup; import org.zkoss.zul.A; import org.zkoss.zul.Box; import org.zkoss.zul.Image; @@ -71,7 +73,9 @@ public class DPRecentItems extends DashboardPanel implements EventListener desktop; + + private DesktopCleanup listener; public DPRecentItems() { @@ -109,6 +113,18 @@ public class DPRecentItems extends DashboardPanel implements EventListener(getDesktop()); + desktop.get().addListener(listener); + } } @Override @@ -267,8 +297,8 @@ public class DPRecentItems extends DashboardPanel implements EventListener { diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DashboardRunnable.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DashboardRunnable.java index 648c6b8914..47524de587 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DashboardRunnable.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/dashboard/DashboardRunnable.java @@ -14,6 +14,7 @@ package org.adempiere.webui.dashboard; import java.io.Serializable; +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.Locale; @@ -26,6 +27,7 @@ import org.compiere.util.CLogger; import org.zkoss.util.Locales; import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.event.Events; +import org.zkoss.zk.ui.util.DesktopCleanup; /** * @@ -38,10 +40,12 @@ public class DashboardRunnable implements Runnable, Serializable private static final long serialVersionUID = 5995227773511788894L; - private Desktop desktop; + private WeakReference desktop; private List dashboardPanels; private Locale locale; + private DesktopCleanup listener; + @SuppressWarnings("unused") private static final CLogger logger = CLogger.getCLogger(DashboardRunnable.class); @@ -51,25 +55,43 @@ public class DashboardRunnable implements Runnable, Serializable * @param appDesktop adempiere desktop interface */ public DashboardRunnable(Desktop desktop) { - this.desktop = desktop; + this.desktop = new WeakReference(desktop); dashboardPanels = new ArrayList(); locale = Locales.getCurrent(); + + listener = new DesktopCleanup() { + @Override + public void cleanup(Desktop desktop) throws Exception { + DashboardRunnable.this.cleanup(); + } + }; + this.desktop.get().addListener(listener); + } + + protected void cleanup() { + dashboardPanels = null; + if (desktop != null && desktop.get() != null) + desktop.get().removeListener(listener); + desktop = null; } public DashboardRunnable(DashboardRunnable tmp, Desktop desktop) { this(desktop); this.dashboardPanels = tmp.dashboardPanels; + tmp.cleanup(); } public void run() - { - Locales.setThreadLocal(locale); - try { - refreshDashboard(true); - } catch (Exception e) { -// logger.log(Level.INFO, e.getLocalizedMessage(), (e.getCause() != null ? e.getCause() : e)); - throw new RuntimeException(e); + { + if (dashboardPanels != null && desktop != null && desktop.get() != null) + { + Locales.setThreadLocal(locale); + try { + refreshDashboard(true); + } catch (Exception e) { + throw new RuntimeException(e); + } } } @@ -79,7 +101,7 @@ public class DashboardRunnable implements Runnable, Serializable public void refreshDashboard(boolean pooling) { - ServerPushTemplate template = new ServerPushTemplate(desktop); + ServerPushTemplate template = new ServerPushTemplate(desktop.get()); //set thread local context if not in event thread Properties ctx = null; boolean isEventThread = Events.inEventListener(); @@ -122,7 +144,8 @@ public class DashboardRunnable implements Runnable, Serializable * @param dashboardPanel */ public void add(DashboardPanel dashboardPanel) { - dashboardPanels.add(dashboardPanel); + if (dashboardPanels != null) + dashboardPanels.add(dashboardPanel); } /** diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DashboardController.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DashboardController.java index 6c705441e8..5d5ba96013 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DashboardController.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DashboardController.java @@ -384,12 +384,12 @@ public class DashboardController implements EventListener { { dashboardRunnable.refreshDashboard(false); - // default Update every one minutes - int interval = MSysConfig.getIntValue(MSysConfig.ZK_DASHBOARD_REFRESH_INTERVAL, 60000); - dashboardFuture = Adempiere.getThreadPoolExecutor().scheduleWithFixedDelay(dashboardRunnable, interval, interval, TimeUnit.MILLISECONDS); - } + // default Update every one minutes + int interval = MSysConfig.getIntValue(MSysConfig.ZK_DASHBOARD_REFRESH_INTERVAL, 60000); + dashboardFuture = Adempiere.getThreadPoolExecutor().scheduleWithFixedDelay(dashboardRunnable, interval, interval, TimeUnit.MILLISECONDS); + } } - + public void onEvent(Event event) throws Exception { Component comp = event.getTarget(); String eventName = event.getName(); @@ -598,8 +598,9 @@ public class DashboardController implements EventListener { public void onSetPage(Page page, Desktop desktop) { if (dashboardFuture != null && !dashboardFuture.isDone()) { dashboardFuture.cancel(true); - - DashboardRunnable tmp = dashboardRunnable; + Adempiere.getThreadPoolExecutor().remove((Runnable) dashboardFuture); + + DashboardRunnable tmp = dashboardRunnable; dashboardRunnable = new DashboardRunnable(tmp, desktop); // default Update every one minutes int interval = MSysConfig.getIntValue(MSysConfig.ZK_DASHBOARD_REFRESH_INTERVAL, 60000); @@ -613,7 +614,14 @@ public class DashboardController implements EventListener { public void onLogOut() { if (dashboardFuture != null && !dashboardFuture.isDone()) { dashboardFuture.cancel(true); + Adempiere.getThreadPoolExecutor().remove((Runnable) dashboardFuture); + dashboardFuture = null; } + if (dashboardRunnable != null) { + dashboardRunnable = null; + } + dashboardLayout.detach(); + dashboardLayout = null; } public void addDashboardPanel(DashboardPanel dashboardPanel) { diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DefaultDesktop.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DefaultDesktop.java index b2d3ecfb9f..253624c85f 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DefaultDesktop.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/desktop/DefaultDesktop.java @@ -108,8 +108,6 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria @SuppressWarnings("unused") private static final CLogger logger = CLogger.getCLogger(DefaultDesktop.class); - private Center windowArea; - private Borderlayout layout; private int noOfNotice; @@ -124,12 +122,9 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria private DashboardController dashboardController, sideController; - private BroadcastMessageWindow messageWindow; - private BroadcastMessageWindow testMessageWindow; private HeaderPanel pnlHead; private Desktop m_desktop = null; - private TimeoutPanel panel = null; private HelpController helpController; @@ -217,7 +212,7 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria helpController.render(e, this); - windowArea = layout.getCenter(); + Center windowArea = layout.getCenter(); windowContainer.createPart(windowArea); @@ -229,7 +224,7 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria busyDialog.setShadow(false); homeTab.appendChild(busyDialog); - messageWindow = new BroadcastMessageWindow(pnlHead); + BroadcastMessageWindow messageWindow = new BroadcastMessageWindow(pnlHead); BroadcastMsgUtil.showPendingMessage(Env.getAD_User_ID(Env.getCtx()), messageWindow); if (!layout.getDesktop().isServerPushEnabled()) @@ -422,11 +417,18 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria unbindEventManager(); if (dashboardController != null) { dashboardController.onLogOut(); + dashboardController = null; } if (sideController != null) { sideController.onLogOut(); + sideController = null; } + layout.detach(); + layout = null; + pnlHead = null; + max = null; + m_desktop = null; } public void updateUI() { @@ -499,8 +501,7 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria .toString(Env.getContextAsInt(Env.getCtx(), "AD_Session_ID")); if (currSession.equals(msg.getTarget())) { - if (testMessageWindow == null) - testMessageWindow = new BroadcastMessageWindow( + BroadcastMessageWindow testMessageWindow = new BroadcastMessageWindow( pnlHead); testMessageWindow.appendMessage(mbMessage, true); testMessageWindow = null; @@ -512,8 +513,7 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria Env.getCtx(), msg.getIntData()); if (mbMessage.isValidUserforMessage()) { - if (messageWindow == null) - messageWindow = new BroadcastMessageWindow( + BroadcastMessageWindow messageWindow = new BroadcastMessageWindow( pnlHead); messageWindow.appendMessage(mbMessage, false); } @@ -522,12 +522,8 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria currSession = Integer.toString(Env.getContextAsInt( Env.getCtx(), "AD_Session_ID")); - System.out.println("Current Session" + currSession); if (currSession.equalsIgnoreCase(msg.getTarget())) { - if (panel == null) { - panel = new TimeoutPanel(pnlHead, - msg.getIntData()); - } + new TimeoutPanel(pnlHead, msg.getIntData()); } break; @@ -536,10 +532,7 @@ public class DefaultDesktop extends TabbedDesktop implements MenuListener, Seria currSession = WebUtil.getServerName(); if (currSession.equalsIgnoreCase(msg.getTarget())) { - if (panel == null) { - panel = new TimeoutPanel(pnlHead, - msg.getIntData()); - } + new TimeoutPanel(pnlHead, msg.getIntData()); } } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java index 91f5df4ef0..f9cc4659b6 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/editor/WTableDirEditor.java @@ -53,11 +53,13 @@ import org.compiere.util.KeyNamePair; import org.compiere.util.Msg; import org.compiere.util.NamePair; import org.compiere.util.ValueNamePair; +import org.zkoss.zk.ui.Desktop; import org.zkoss.zk.ui.Executions; import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; import org.zkoss.zk.ui.event.Events; +import org.zkoss.zk.ui.util.DesktopCleanup; import org.zkoss.zul.Comboitem; import org.zkoss.zul.Menuitem; @@ -656,18 +658,37 @@ ContextMenuListener, IZoomableEditor */ private static final long serialVersionUID = 4540856986889452983L; protected WTableDirEditor editor; + private DesktopCleanup listener = null; @Override public void onPageAttached(Page newpage, Page oldpage) { super.onPageAttached(newpage, oldpage); if (editor.tableCacheListener == null) { editor.createCacheListener(); + if (listener == null) { + listener = new DesktopCleanup() { + @Override + public void cleanup(Desktop desktop) throws Exception { + EditorCombobox.this.cleanup(); + } + }; + newpage.getDesktop().addListener(listener); + } } } @Override public void onPageDetached(Page page) { super.onPageDetached(page); + if (listener != null && page.getDesktop() != null) + page.getDesktop().removeListener(listener); + cleanup(); + } + + /** + * + */ + protected void cleanup() { if (editor.tableCacheListener != null) { CacheMgt.get().unregister(editor.tableCacheListener); editor.tableCacheListener = null; diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/AbstractMenuPanel.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/AbstractMenuPanel.java index 72821dca98..b823bcd363 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/AbstractMenuPanel.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/AbstractMenuPanel.java @@ -33,8 +33,10 @@ import org.compiere.model.MTreeNode; import org.compiere.util.DB; import org.compiere.util.Env; import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zk.ui.event.EventQueue; import org.zkoss.zk.ui.event.EventQueues; import org.zkoss.zk.ui.event.Events; import org.zkoss.zul.A; @@ -65,57 +67,14 @@ public abstract class AbstractMenuPanel extends Panel implements EventListener listener; public AbstractMenuPanel(Component parent) { if (parent != null) this.setParent(parent); - init(); - - EventQueues.lookup(MENU_ITEM_SELECTED_QUEUE, EventQueues.DESKTOP, true).subscribe(new EventListener() { - public void onEvent(Event event) throws Exception { - if (event.getName() == Events.ON_SELECT) - { - Treeitem selectedItem = (Treeitem) event.getData(); - - if (selectedItem != null) - { - Object value = selectedItem.getValue(); - if (value != null) - { - if (menuTree.getSelectedItem() != null && menuTree.getSelectedItem().getValue() != null && menuTree.getSelectedItem().getValue().equals(value)) - return; - - Collection items = menuTree.getItems(); - for (Treeitem item : items) - { - if (item != null && item.getValue() != null && item.getValue().equals(value)) - { - TreeSearchPanel.select(item); - return; - } - } - } - else - { - String label = selectedItem.getLabel(); - if (menuTree.getSelectedItem() != null && menuTree.getSelectedItem().getLabel() != null && menuTree.getSelectedItem().getLabel().equals(label)) - return; - - Collection items = menuTree.getItems(); - for (Treeitem item : items) - { - if (item != null && item.getLabel() != null && item.getLabel().equals(label)) - { - TreeSearchPanel.select(item); - return; - } - } - } - } - } - } - }); + init(); } protected void init() { @@ -350,4 +309,75 @@ public abstract class AbstractMenuPanel extends Panel implements EventListener queue = EventQueues.lookup(MENU_ITEM_SELECTED_QUEUE, EventQueues.DESKTOP, true); + if (queue != null) + queue.unsubscribe(listener); + } finally { + listener = null; + } + + } + } + + @Override + public void onPageAttached(Page newpage, Page oldpage) { + super.onPageAttached(newpage, oldpage); + if (listener == null) { + listener = new EventListener() { + public void onEvent(Event event) throws Exception { + if (event.getName() == Events.ON_SELECT) + { + Treeitem selectedItem = (Treeitem) event.getData(); + + if (selectedItem != null) + { + Object value = selectedItem.getValue(); + if (value != null) + { + if (menuTree.getSelectedItem() != null && menuTree.getSelectedItem().getValue() != null && menuTree.getSelectedItem().getValue().equals(value)) + return; + + Collection items = menuTree.getItems(); + for (Treeitem item : items) + { + if (item != null && item.getValue() != null && item.getValue().equals(value)) + { + TreeSearchPanel.select(item); + return; + } + } + } + else + { + String label = selectedItem.getLabel(); + if (menuTree.getSelectedItem() != null && menuTree.getSelectedItem().getLabel() != null && menuTree.getSelectedItem().getLabel().equals(label)) + return; + + Collection items = menuTree.getItems(); + for (Treeitem item : items) + { + if (item != null && item.getLabel() != null && item.getLabel().equals(label)) + { + TreeSearchPanel.select(item); + return; + } + } + } + } + } + } + }; + } + EventQueues.lookup(MENU_ITEM_SELECTED_QUEUE, EventQueues.DESKTOP, true).subscribe(listener); + } + } diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/MenuSearchPanel.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/MenuSearchPanel.java index 0d6b64067d..ae151584a9 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/MenuSearchPanel.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/panel/MenuSearchPanel.java @@ -15,8 +15,10 @@ package org.adempiere.webui.panel; import org.zkoss.zk.ui.Component; +import org.zkoss.zk.ui.Page; import org.zkoss.zk.ui.event.Event; import org.zkoss.zk.ui.event.EventListener; +import org.zkoss.zk.ui.event.EventQueue; import org.zkoss.zk.ui.event.EventQueues; import org.zkoss.zk.ui.event.Events; import org.zkoss.zul.Checkbox; @@ -35,6 +37,8 @@ public class MenuSearchPanel extends AbstractMenuPanel private static final long serialVersionUID = 5308522340852904168L; protected MenuTreeSearchPanel pnlSearch; + + private EventListener listener; public MenuSearchPanel(Component parent) { @@ -45,26 +49,7 @@ public class MenuSearchPanel extends AbstractMenuPanel protected void init() { super.init(); - pnlSearch.initialise(); - - EventQueues.lookup(MenuTreeFilterPanel.MENU_TREE_FILTER_CHECKED_QUEUE, EventQueues.DESKTOP, true).subscribe(new EventListener() { - public void onEvent(Event event) throws Exception { - if (event.getName() == Events.ON_CHECK) - { - Checkbox chk = (Checkbox) event.getData(); - if (chk != null) - { - if ("flatView".equals(chk.getId())) - MenuTreeFilterPanel.toggleFlatView(getMenuTree(), chk); - else - MenuTreeFilterPanel.toggle(getMenuTree(), chk); - if (pnlSearch != null) - pnlSearch.refreshSearchList(); - getMenuTree().invalidate(); - } - } - } - }); + pnlSearch.initialise(); } protected void initComponents() @@ -83,4 +68,50 @@ public class MenuSearchPanel extends AbstractMenuPanel pnlSearch.setSclass("menu-search-panel"); toolbar.appendChild(pnlSearch); } + + /* (non-Javadoc) + * @see org.zkoss.zk.ui.AbstractComponent#onPageDetached(org.zkoss.zk.ui.Page) + */ + @Override + public void onPageDetached(Page page) { + super.onPageDetached(page); + if (listener != null) { + try { + EventQueue queue = EventQueues.lookup(MenuTreeFilterPanel.MENU_TREE_FILTER_CHECKED_QUEUE, EventQueues.DESKTOP, true); + if (queue != null) + queue.unsubscribe(listener); + } finally { + listener = null; + } + + } + } + + @Override + public void onPageAttached(Page newpage, Page oldpage) { + super.onPageAttached(newpage, oldpage); + if (listener == null) { + listener = new EventListener() { + public void onEvent(Event event) throws Exception { + if (event.getName() == Events.ON_CHECK) + { + Checkbox chk = (Checkbox) event.getData(); + if (chk != null) + { + if ("flatView".equals(chk.getId())) + MenuTreeFilterPanel.toggleFlatView(getMenuTree(), chk); + else + MenuTreeFilterPanel.toggle(getMenuTree(), chk); + if (pnlSearch != null) + pnlSearch.refreshSearchList(); + getMenuTree().invalidate(); + } + } + } + }; + } + EventQueues.lookup(MenuTreeFilterPanel.MENU_TREE_FILTER_CHECKED_QUEUE, EventQueues.DESKTOP, true).subscribe(listener); + } + + } \ No newline at end of file diff --git a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java index 9c02b50455..3ef9670f0d 100644 --- a/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java +++ b/org.adempiere.ui.zk/WEB-INF/src/org/adempiere/webui/session/SessionManager.java @@ -17,6 +17,7 @@ package org.adempiere.webui.session; +import java.lang.ref.WeakReference; import java.util.Properties; import org.adempiere.webui.AdempiereWebUI; @@ -53,8 +54,17 @@ public class SessionManager private static Session getSession() { Execution execution = Executions.getCurrent(); - Desktop desktop = execution != null ? execution.getDesktop() - : (Desktop) Env.getCtx().get(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY); + Desktop desktop = null; + if (execution != null) + { + desktop = execution.getDesktop(); + } + else + { + @SuppressWarnings("unchecked") + WeakReference ref = (WeakReference) Env.getCtx().get(AdempiereWebUI.ZK_DESKTOP_SESSION_KEY); + desktop = ref != null ? ref.get() : null; + } return desktop != null ? desktop.getSession() : null; } @@ -62,7 +72,7 @@ public class SessionManager { Session session = getSession(); if (session != null) - session.setAttribute(SESSION_APPLICATION, app); + session.setAttribute(SESSION_APPLICATION, new WeakReference(app)); } public static IDesktop getAppDesktop() @@ -74,7 +84,13 @@ public class SessionManager public static IWebClient getSessionApplication() { Session session = getSession(); - IWebClient app = session != null ? (IWebClient)session.getAttribute(SESSION_APPLICATION) : null; + IWebClient app = null; + if (session != null) + { + @SuppressWarnings("unchecked") + WeakReference wref = (WeakReference) session.getAttribute(SESSION_APPLICATION); + app = wref != null ? wref.get() : null; + } return app; }