/**
 * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
 * If a copy of the MPL was not distributed with this file, You can obtain one at
 * http://mozilla.org/MPL/2.0/.
 * 
 * This Source Code Form is also subject to the terms of the Health-Related Additional
 * Disclaimer of Warranty and Limitation of Liability available at
 * http://www.carewebframework.org/licensing/disclaimer.
 */
package org.carewebframework.rpms.ui.common;

import java.util.Iterator;

import org.carewebframework.rpms.api.common.Params;
import org.carewebframework.ui.FrameworkController;
import org.carewebframework.vista.api.util.VistAUtil;
import org.carewebframework.vista.mbroker.BrokerSession;
import org.carewebframework.vista.ui.mbroker.AsyncRPCAbortEvent;
import org.carewebframework.vista.ui.mbroker.AsyncRPCCompleteEvent;
import org.carewebframework.vista.ui.mbroker.AsyncRPCErrorEvent;
import org.carewebframework.vista.ui.mbroker.AsyncRPCEventDispatcher;

import org.zkoss.zk.ui.Component;

public class BgoBaseController<T> extends FrameworkController {
    
    private static final long serialVersionUID = 1L;
    
    private BrokerSession broker;
    
    protected T result;
    
    private boolean canceled;
    
    private AsyncRPCEventDispatcher asyncDispatcher;
    
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
    }
    
    public Component getRoot() {
        return root;
    }
    
    public Iterator<Object> getParameters() {
        return arg instanceof Params ? ((Params) arg).iterator() : new Params().iterator();
    }
    
    public T getResult() {
        return result;
    }
    
    public boolean canceled() {
        return canceled;
    }
    
    public void close(boolean canceled) {
        this.canceled = canceled;
        root.detach();
    }
    
    /**
     * Called in main event thread when async RPC has completed. Override to handle the event.
     * 
     * @param event The async completion event.
     */
    public void onAsyncRPCComplete(AsyncRPCCompleteEvent event) {
    }
    
    /**
     * Called in main event thread when async RPC has encountered an error. Override to handle the
     * event.
     * 
     * @param event The async error event.
     */
    public void onAsyncRPCError(AsyncRPCErrorEvent event) {
    }
    
    /**
     * Called in main event thread when async RPC has been aborted. Override to handle the event.
     * 
     * @param event The async abort event.
     */
    public void onAsyncRPCAbort(AsyncRPCAbortEvent event) {
    }
    
    public AsyncRPCEventDispatcher getAsyncDispatcher() {
        if (asyncDispatcher == null) {
            asyncDispatcher = new AsyncRPCEventDispatcher(getBroker(), root);
        }
        
        return asyncDispatcher;
    }
    
    public BrokerSession getBroker() {
        if (broker == null) {
            broker = VistAUtil.getBrokerSession();
        }
        
        return broker;
    }
    
    public void setBroker(BrokerSession broker) {
        this.broker = broker;
    }
    
}

@@ -1009,16 +1009,96 @@
 mponent;
+%0Aimport org.zkoss.zk.ui.HtmlBasedComponent;%0Aimport org.zkoss.zk.ui.event.Events;
 %0A%0Apublic
@@ -1921,24 +1921,269 @@
 = canceled;%0A
+        Events.echoEvent(%22onDeferredClose%22, root, null);%0A        %0A        if (root instanceof HtmlBasedComponent) %7B%0A            ((HtmlBasedComponent) root).setStyle(%22visibility: hidden%22);%0A        %7D%0A    %7D%0A    %0A    public void onDeferredClose() %7B%0A
         root

Fix phantom dialog issue.