/*
 * #%L
 * carewebframework
 * %%
 * Copyright (C) 2008 - 2016 Regenstrief Institute, Inc.
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * 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.
 *
 * #L%
 */
package org.carewebframework.shell.layout;

import org.carewebframework.shell.designer.PropertyEditorTabView;
import org.carewebframework.shell.property.PropertyTypeRegistry;

import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.Events;
import org.zkoss.zk.ui.event.SelectEvent;
import org.zkoss.zul.Tabbox;

/**
 * Wraps the ZK Tabbox component. This UI element can only accept UIElementTabPane elements as
 * children and only one of those can be active at a time.
 */
public class UIElementTabView extends UIElementZKBase {
    
    static {
        registerAllowedParentClass(UIElementTabView.class, UIElementBase.class);
        registerAllowedChildClass(UIElementTabView.class, UIElementTabPane.class);
        PropertyTypeRegistry.register("tabs", PropertyEditorTabView.class);
    }
    
    private final Tabbox tabBox;
    
    private UIElementTabPane activePane;
    
    public UIElementTabView() throws Exception {
        super();
        maxChildren = Integer.MAX_VALUE;
        tabBox = (Tabbox) createFromTemplate();
        setOuterComponent(tabBox);
        tabBox.setSclass("cwf-tabbox");
        tabBox.addEventListener(Events.ON_SELECT, new EventListener<SelectEvent<?, ?>>() {
            
            @Override
            public void onEvent(SelectEvent<?, ?> event) throws Exception {
                setActivePane((UIElementTabPane) getAssociatedUIElement(event.getTarget()));
            }
            
        });
    }
    
    /**
     * Sets the orientation which can be horizontal or vertical.
     * 
     * @param orientation Orientation setting.
     */
    public void setOrientation(String orientation) {
        if ("accordion".equals(orientation)) {
            tabBox.setOrient("top");
            tabBox.setMold("accordion");
        } else {
            tabBox.setMold("default");
            tabBox.setOrient(orientation);
        }
    }
    
    /**
     * Returns the orientation (horizontal, vertical or accordion).
     * 
     * @return Orientation setting.
     */
    public String getOrientation() {
        return "accordion".equals(tabBox.getMold()) ? "accordion" : tabBox.getOrient();
    }
    
    /**
     * Need to detach both the tab and the tab panel of the child component.
     */
    @Override
    protected void beforeRemoveChild(UIElementBase child) {
        if (child == activePane) {
            setActivePane(null);
        }
    }
    
    /**
     * Sets the active (visible) pane.
     * 
     * @param pane The pane to become active.
     */
    protected void setActivePane(UIElementTabPane pane) {
        if (pane == activePane) {
            return;
        }
        
        if (activePane != null) {
            activePane.activate(false);
        }
        
        activePane = pane;
        
        if (activePane != null) {
            activePane.activate(true);
        }
    }
    
    /**
     * Overrides activateChildren to ensure that only the active pane is affected.
     */
    @Override
    public void activateChildren(boolean activate) {
        if (activePane == null) {
            activePane = (UIElementTabPane) getAssociatedUIElement(tabBox.getSelectedTab());
        }
        
        if (activePane != null) {
            activePane.activate(activate);
        }
    }
    
}

@@ -272,18 +272,17 @@
 se at%0A *
- 
 %0A
+
  *      
@@ -326,17 +326,16 @@
 E-2.0%0A *
- 
 %0A * Unle
@@ -650,17 +650,16 @@
 ense.%0A *
- 
 %0A * This
@@ -1059,17 +1059,16 @@
 gistry;%0A
-%0A
 import o
@@ -1430,21 +1430,17 @@
 KBase %7B%0A
-    
 %0A
+
     stat
@@ -1686,29 +1686,25 @@
 ass);%0A    %7D%0A
-    
 %0A
+
     private 
@@ -1724,21 +1724,17 @@
 tabBox;%0A
-    
 %0A
+
     priv
@@ -1766,21 +1766,17 @@
 vePane;%0A
-    
 %0A
+
     publ
@@ -2088,29 +2088,17 @@
 ?%3E%3E() %7B%0A
-            
 %0A
+
         
@@ -2298,21 +2298,9 @@
   %7D%0A
-            
 %0A
+
     
@@ -2305,37 +2305,33 @@
       %7D);%0A    %7D%0A
-    
 %0A
+
     /**%0A     * S
@@ -2385,33 +2385,32 @@
 vertical.%0A     *
- 
 %0A     * @param o
@@ -2734,37 +2734,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     /**%0A     * R
@@ -2817,33 +2817,32 @@
 cordion).%0A     *
- 
 %0A     * @return 
@@ -2997,29 +2997,25 @@
 nt();%0A    %7D%0A
-    
 %0A
+
     /**%0A    
@@ -3245,37 +3245,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     /**%0A     * S
@@ -3307,17 +3307,16 @@
 .%0A     *
- 
 %0A     * 
@@ -3480,33 +3480,25 @@
 ;%0A        %7D%0A
-        
 %0A
+
         if (
@@ -3565,33 +3565,25 @@
 ;%0A        %7D%0A
-        
 %0A
+
         acti
@@ -3597,25 +3597,17 @@
 = pane;%0A
-        
 %0A
+
         
@@ -3687,21 +3687,17 @@
 %7D%0A    %7D%0A
-    
 %0A
+
     /**%0A
@@ -3991,24 +3991,25 @@
       %7D%0A
+%0A
         
 %0A       
@@ -4000,16 +4000,152 @@
         
+if (activePane == null %7C%7C !activePane.isVisible()) %7B%0A            activePane = (UIElementTabPane) this.getFirstVisibleChild();%0A        %7D%0A
 %0A       
@@ -4230,15 +4230,11 @@
 %7D%0A    %7D%0A
-    
 %0A%7D%0A

Fix default tab selection logic.