/*
 * #%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.elements;

import org.apache.commons.lang.StringUtils;
import org.fujion.component.Pane;
import org.fujion.event.ResizeEvent;

/**
 * A child of the ElementSplitterView.
 */
public class ElementSplitterPane extends ElementUI {

    static {
        registerAllowedParentClass(ElementSplitterPane.class, ElementSplitterView.class);
        registerAllowedChildClass(ElementSplitterPane.class, ElementUI.class, 1);
    }

    private final Pane pane = new Pane();

    private double size = 100.0;

    private boolean relative = true;

    private boolean resizable = true;

    public ElementSplitterPane() {
        super();
        setResizable(resizable);
        setOuterComponent(pane);
        pane.addEventListener("resize", (event) -> {
            resize((ResizeEvent) event);
        });
        updateSize(true);
    }

    public double getSize() {
        return size;
    }

    public void setSize(double size) {
        this.size = size;
        updateSize();
    }

    @Override
    public String getInstanceName() {
        return "Pane #" + (getParent().indexOfChild(this) + 1);
    }

    public void setRelative(boolean relative) {
        this.relative = relative;
        updateSize();
    }

    public boolean isRelative() {
        return relative;
    }

    public String getCaption() {
        return pane.getTitle();
    }

    public void setCaption(String caption) {
        pane.setTitle(caption);
    }

    /*package*/ void updateSize(boolean isHorizontal) {
        String sz = StringUtils.removeEnd(Double.toString(size), ".0");
        pane.setFlex(!isDesignMode() && relative ? sz : null);
        pane.setHeight(isHorizontal ? null : sz + "px");
        pane.setWidth(!isHorizontal ? null : sz + "px");
    }

    private void updateSize() {
        if (getParent() != null) {
            updateSize(isHorizontal());
        }
    }

    private void resize(ResizeEvent event) {
        if (getParent() != null) {
            size = isHorizontal() ? event.getWidth() : event.getHeight();
            updateSize();
        }
    }
    
    private boolean isHorizontal() {
        return getParent() != null && ((ElementSplitterView) getParent()).isHorizontal();
    }
    
    public boolean isResizable() {
        return resizable;
    }

    public void setResizable(boolean resizable) {
        this.resizable = resizable;
        pane.setSplittable(resizable || isDesignMode());
    }

    @Override
    public void setDesignMode(boolean designMode) {
        super.setDesignMode(designMode);
        setResizable(resizable);
        pane.invoke("reportSize");
    }

}

@@ -1146,17 +1146,21 @@
 entUI %7B%0A
+    
 %0A
-
     stat
@@ -1334,33 +1334,37 @@
 lass, 1);%0A    %7D%0A
+    
 %0A
-
     private fina
@@ -1389,17 +1389,21 @@
 Pane();%0A
+    
 %0A
-
     priv
@@ -1427,17 +1427,21 @@
  100.0;%0A
+    
 %0A
-
     priv
@@ -1465,25 +1465,29 @@
 ive = true;%0A
+    
 %0A
-
     private 
@@ -1512,17 +1512,21 @@
 = true;%0A
+    
 %0A
-
     publ
@@ -1765,33 +1765,37 @@
 ze(true);%0A    %7D%0A
+    
 %0A
-
     public doubl
@@ -1827,33 +1827,37 @@
 urn size;%0A    %7D%0A
+    
 %0A
-
     public void 
@@ -1925,33 +1925,37 @@
 teSize();%0A    %7D%0A
+    
 %0A
-
     @Override%0A  
@@ -2052,33 +2052,37 @@
 is) + 1);%0A    %7D%0A
+    
 %0A
-
     public void 
@@ -2167,33 +2167,37 @@
 teSize();%0A    %7D%0A
+    
 %0A
-
     public boole
@@ -2237,33 +2237,37 @@
 relative;%0A    %7D%0A
+    
 %0A
-
     public Strin
@@ -2313,33 +2313,37 @@
 tTitle();%0A    %7D%0A
+    
 %0A
-
     public void 
@@ -2405,25 +2405,29 @@
 ion);%0A    %7D%0A
+    
 %0A
-
     /*packag
@@ -2554,21 +2554,25 @@
     
-pane.set
+boolean is
 Flex
-(
+ = 
 !isD
@@ -2594,16 +2594,45 @@
 relative
+;%0A        pane.setFlex(isFlex
  ? sz : 
@@ -2661,16 +2661,26 @@
 tHeight(
+isFlex %7C%7C 
 isHorizo
@@ -2727,16 +2727,26 @@
 etWidth(
+isFlex %7C%7C 
 !isHoriz
@@ -2774,25 +2774,29 @@
 px%22);%0A    %7D%0A
+    
 %0A
-
     private 
@@ -2902,25 +2902,29 @@
     %7D%0A    %7D%0A
+    
 %0A
-
     private 
@@ -3099,37 +3099,33 @@
         %7D%0A    %7D%0A
-    
 %0A
+
     private bool
@@ -3233,37 +3233,33 @@
 zontal();%0A    %7D%0A
-    
 %0A
+
     public boole
@@ -3301,33 +3301,37 @@
 esizable;%0A    %7D%0A
+    
 %0A
-
     public void 
@@ -3459,25 +3459,29 @@
 e());%0A    %7D%0A
+    
 %0A
-
     @Overrid
@@ -3649,11 +3649,15 @@
 ;%0A    %7D%0A
+    
 %0A%7D%0A

Improve splitter behavior.