/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.layout;

import org.apache.fop.datatypes.ColorType;
import org.apache.fop.datatypes.CondLength;

public class BorderAndPadding {

    public static final int TOP = 0;
    public static final int RIGHT = 1;
    public static final int BOTTOM = 2;
    public static final int LEFT = 3;

    private static class ResolvedCondLength {
        int iLength;    // Resolved length value
        boolean bDiscard;

        ResolvedCondLength(CondLength length) {
            bDiscard = length.isDiscard();
            iLength = length.mvalue();
        }

    }

    public static class BorderInfo {
        private int mStyle;          // Enum for border style
        private ColorType mColor;    // Border color
        private ResolvedCondLength mWidth;

        BorderInfo(int style, CondLength width, ColorType color) {
            mStyle = style;
            mWidth = new ResolvedCondLength(width);
            mColor = color;
        }

    }

    private BorderInfo[] borderInfo = new BorderInfo[4];
    private ResolvedCondLength[] padding = new ResolvedCondLength[4];

    public BorderAndPadding() {}

    public void setBorder(int side, int style, CondLength width,
                          ColorType color) {
        borderInfo[side] = new BorderInfo(style, width, color);
    }

    public void setPadding(int side, CondLength width) {
        padding[side] = new ResolvedCondLength(width);
    }

    public void setPaddingLength(int side, int iLength) {
        padding[side].iLength = iLength;
    }

    public void setBorderLength(int side, int iLength) {
        borderInfo[side].mWidth.iLength = iLength;
    }

    public int getBorderLeftWidth(boolean bDiscard) {
        return getBorderWidth(LEFT, bDiscard);
    }

    public int getBorderRightWidth(boolean bDiscard) {
        return getBorderWidth(RIGHT, bDiscard);
    }

    public int getBorderTopWidth(boolean bDiscard) {
        return getBorderWidth(TOP, bDiscard);
    }

    public int getBorderBottomWidth(boolean bDiscard) {
        return getBorderWidth(BOTTOM, bDiscard);
    }

    public int getPaddingLeft(boolean bDiscard) {
        return getPadding(LEFT, bDiscard);
    }

    public int getPaddingRight(boolean bDiscard) {
        return getPadding(RIGHT, bDiscard);
    }

    public int getPaddingBottom(boolean bDiscard) {
        return getPadding(BOTTOM, bDiscard);
    }

    public int getPaddingTop(boolean bDiscard) {
        return getPadding(TOP, bDiscard);
    }


    private int getBorderWidth(int side, boolean bDiscard) {
        if ((borderInfo[side] == null)
                || (bDiscard && borderInfo[side].mWidth.bDiscard)) {
            return 0;
        } else
            return borderInfo[side].mWidth.iLength;
    }

    public ColorType getBorderColor(int side) {
        if (borderInfo[side] != null) {
            return borderInfo[side].mColor;
        } else
            return null;
    }

    public int getBorderStyle(int side) {
        if (borderInfo[side] != null) {
            return borderInfo[side].mStyle;
        } else
            return 0;
    }

    private int getPadding(int side, boolean bDiscard) {
        if ((padding[side] == null) || (bDiscard && padding[side].bDiscard)) {
            return 0;
        } else
            return padding[side].iLength;
    }

}

@@ -340,16 +340,37 @@
 Padding 
+implements Cloneable 
 %7B%0A%0A    p
@@ -563,16 +563,37 @@
 dLength 
+implements Cloneable 
 %7B%0A      
@@ -795,32 +795,755 @@
 e();%0A        %7D%0A%0A
+%09public Object clone() throws CloneNotSupportedException %7B%0A%09    return super.clone();%0A%09%7D%0A%0A    %7D%0A%0A    /**%0A     * Return a full copy of the BorderAndPadding information. This clones all%0A     * padding and border information.%0A     * @return The copy.%0A     */%0A    public Object clone() throws CloneNotSupportedException %7B%0A%09BorderAndPadding bp = (BorderAndPadding) super.clone();%0A%09bp.padding = (ResolvedCondLength%5B%5D)padding.clone();%0A%09bp.borderInfo = (BorderInfo%5B%5D)borderInfo.clone();%0A%09for (int i=0; i%3Cpadding.length; i++) %7B%0A%09    if (padding%5Bi%5D != null) %7B%0A%09%09bp.padding%5Bi%5D=(ResolvedCondLength)padding%5Bi%5D.clone();%0A%09    %7D%0A%09    if (borderInfo%5Bi%5D != null) %7B%0A%09%09bp.borderInfo%5Bi%5D=(BorderInfo)borderInfo%5Bi%5D.clone();%0A%09    %7D%0A%09%7D%0A%09return bp;%0A
     %7D%0A%0A    publi
@@ -1568,16 +1568,37 @@
 derInfo 
+implements Cloneable 
 %7B%0A      
@@ -1932,24 +1932,249 @@
 %0A        %7D%0A%0A
+%09public Object clone() throws CloneNotSupportedException %7B%0A%09    BorderInfo bi = (BorderInfo) super.clone();%0A%09    bi.mWidth = (ResolvedCondLength)mWidth.clone();%0A%09    // do we need to clone the Color too???%0A%09    return bi;%0A%09%7D%0A
     %7D%0A%0A    p

Add clone method