KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layout > BorderAndPadding


1 /*
2  * $Id: BorderAndPadding.java,v 1.5.2.3 2003/02/25 14:07:02 jeremias Exp $
3  * ============================================================================
4  * The Apache Software License, Version 1.1
5  * ============================================================================
6  *
7  * Copyright (C) 1999-2003 The Apache Software Foundation. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without modifica-
10  * tion, are permitted provided that the following conditions are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright notice,
13  * this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  * this list of conditions and the following disclaimer in the documentation
17  * and/or other materials provided with the distribution.
18  *
19  * 3. The end-user documentation included with the redistribution, if any, must
20  * include the following acknowledgment: "This product includes software
21  * developed by the Apache Software Foundation (http://www.apache.org/)."
22  * Alternately, this acknowledgment may appear in the software itself, if
23  * and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. The names "FOP" and "Apache Software Foundation" must not be used to
26  * endorse or promote products derived from this software without prior
27  * written permission. For written permission, please contact
28  * apache@apache.org.
29  *
30  * 5. Products derived from this software may not be called "Apache", nor may
31  * "Apache" appear in their name, without prior written permission of the
32  * Apache Software Foundation.
33  *
34  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
35  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
36  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
37  * APACHE SOFTWARE FOUNDATION OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
38  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLU-
39  * DING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
40  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
41  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
42  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
43  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44  * ============================================================================
45  *
46  * This software consists of voluntary contributions made by many individuals
47  * on behalf of the Apache Software Foundation and was originally created by
48  * James Tauber <jtauber@jtauber.com>. For more information on the Apache
49  * Software Foundation, please see <http://www.apache.org/>.
50  */

51 package org.apache.fop.layout;
52
53 import org.apache.fop.datatypes.ColorType;
54 import org.apache.fop.datatypes.CondLength;
55
56 public class BorderAndPadding implements Cloneable JavaDoc {
57
58     public static final int TOP = 0;
59     public static final int RIGHT = 1;
60     public static final int BOTTOM = 2;
61     public static final int LEFT = 3;
62     //ResolvedCondLength is long, mask wiht 0x100000000 != 0 is bDiscard
63
//mask wiht 0xFFFFFFFF is iLength
64
static final long bDiscard_MASK = 0x100000000L;
65     static final long iLength_MASK = 0x0FFFFFFFFL;
66     private static final long new_ResolvedCondLength(CondLength length) {
67         return (length.isDiscard()?bDiscard_MASK:0) + length.mvalue();
68     }
69
70     /**
71         * Return a full copy of the BorderAndPadding information. This clones all
72         * padding and border information.
73         * @return The copy.
74         */

75     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
76         BorderAndPadding bp = (BorderAndPadding) super.clone();
77         bp.padding = new long[ padding.length]; //
78
System.arraycopy( padding, 0, bp.padding, 0, padding.length);
79         bp.borderInfo = (BorderInfo[])borderInfo.clone();
80         for (int i=0; i<borderInfo.length; i++) {
81             if (borderInfo[i] != null) {
82                 bp.borderInfo[i]=(BorderInfo)borderInfo[i].clone();
83             }
84         }
85         return bp;
86     }
87
88     public static class BorderInfo implements Cloneable JavaDoc {
89         private int mStyle; // Enum for border style
90
private ColorType mColor; // Border color
91
private long mWidth;
92
93         BorderInfo(int style, CondLength width, ColorType color) {
94             mStyle = style;
95             mWidth = new_ResolvedCondLength(width);
96             mColor = color;
97         }
98         
99         public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
100             BorderInfo bi = (BorderInfo) super.clone();
101             bi.mWidth = mWidth;
102             // do we need to clone the Color too???
103
return bi;
104         }
105     }
106
107     private BorderInfo[] borderInfo = new BorderInfo[4];
108     private long[] padding = new long[4];//
109

110     public BorderAndPadding() {}
111
112     public void setBorder(int side, int style, CondLength width,
113                           ColorType color) {
114         borderInfo[side] = new BorderInfo(style, width, color);
115     }
116
117     public void setPadding(int side, CondLength width) {
118         padding[ side] = new_ResolvedCondLength(width);
119     }
120
121     public void setPaddingLength(int side, int iLength) {
122         padding[side] = iLength + (padding[side] & bDiscard_MASK);
123     }
124
125     public void setBorderLength(int side, int iLength) {
126         borderInfo[side].mWidth = iLength + (borderInfo[side].mWidth & bDiscard_MASK);
127     }
128
129     public int getBorderLeftWidth(boolean bDiscard) {
130         return getBorderWidth(LEFT, bDiscard);
131     }
132
133     public int getBorderRightWidth(boolean bDiscard) {
134         return getBorderWidth(RIGHT, bDiscard);
135     }
136
137     public int getBorderTopWidth(boolean bDiscard) {
138         return getBorderWidth(TOP, bDiscard);
139     }
140
141     public int getBorderBottomWidth(boolean bDiscard) {
142         return getBorderWidth(BOTTOM, bDiscard);
143     }
144
145     public int getPaddingLeft(boolean bDiscard) {
146         return getPadding(LEFT, bDiscard);
147     }
148
149     public int getPaddingRight(boolean bDiscard) {
150         return getPadding(RIGHT, bDiscard);
151     }
152
153     public int getPaddingBottom(boolean bDiscard) {
154         return getPadding(BOTTOM, bDiscard);
155     }
156
157     public int getPaddingTop(boolean bDiscard) {
158         return getPadding(TOP, bDiscard);
159     }
160
161
162     private int getBorderWidth(int side, boolean bDiscard) {
163         if ((borderInfo[side] == null)
164                 || (bDiscard &&
165                     ((borderInfo[side].mWidth&bDiscard_MASK) != 0L)
166                    )) {
167             return 0;
168         } else {
169             return (int) (borderInfo[side].mWidth&iLength_MASK);
170         }
171     }
172
173     public ColorType getBorderColor(int side) {
174         if (borderInfo[side] != null) {
175             return borderInfo[side].mColor;
176         } else
177             return null;
178     }
179
180     public int getBorderStyle(int side) {
181         if (borderInfo[side] != null) {
182             return borderInfo[side].mStyle;
183         } else
184             return 0;
185     }
186
187     private int getPadding(int side, boolean bDiscard) {
188         return (int)(padding[side]&iLength_MASK);
189     }
190
191 }
192
Popular Tags