KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > fop > layoutmgr > table > CollapsingBorderModelEyeCatching


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 /* $Id: CollapsingBorderModelEyeCatching.java 478928 2006-11-24 17:32:48Z vhennebert $ */
19
20 package org.apache.fop.layoutmgr.table;
21
22 import org.apache.fop.fo.Constants;
23 import org.apache.fop.fo.flow.Table;
24 import org.apache.fop.fo.flow.TableBody;
25 import org.apache.fop.fo.flow.TableCell;
26 import org.apache.fop.fo.flow.TableColumn;
27 import org.apache.fop.fo.flow.TableRow;
28 import org.apache.fop.fo.properties.CommonBorderPaddingBackground.BorderInfo;
29
30 /**
31  * Implements the normal "collapse" border model defined in 6.7.10 in XSL 1.0.
32  *
33  * TODO Column groups are not yet checked in this algorithm!
34  */

35 public class CollapsingBorderModelEyeCatching extends CollapsingBorderModel {
36
37     public BorderInfo determineWinner(GridUnit currentGridUnit,
38             GridUnit otherGridUnit, int side, int flags) {
39         final boolean vertical = isVerticalRelation(side);
40         final int otherSide = getOtherSide(side);
41         
42         //Get cells
43
TableCell currentCell = currentGridUnit.getCell();
44         TableCell otherCell = null;
45         if (otherGridUnit != null) {
46             otherCell = otherGridUnit.getCell();
47         }
48         
49         //Get rows
50
TableRow currentRow = currentGridUnit.getRow();
51         TableRow otherRow = null;
52         if (vertical && otherCell != null) {
53             otherRow = otherGridUnit.getRow();
54         }
55         
56         //get bodies
57
TableBody currentBody = currentGridUnit.getBody();
58         TableBody otherBody = null;
59         if (otherRow != null) {
60             otherBody = otherGridUnit.getBody();
61         }
62
63         //get columns
64
TableColumn currentColumn = currentGridUnit.getColumn();
65         TableColumn otherColumn = null;
66         if (otherGridUnit != null) {
67             otherColumn = otherGridUnit.getColumn();
68         }
69         
70         //TODO get column groups
71

72         //Get table
73
Table table = currentGridUnit.getTable();
74         
75         //----------------------------------------------------------------------
76
//We're creating two arrays containing the applicable BorderInfos for
77
//each cell in question.
78
//0 = cell, 1 = row, 2 = row group (body), 3 = column,
79
//4 = col group (spanned column, see 6.7.3), 5 = table
80

81         BorderInfo[] current = new BorderInfo[6];
82         BorderInfo[] other = new BorderInfo[6];
83         //cell
84
current[0] = currentGridUnit.getOriginalBorderInfoForCell(side);
85         if (otherGridUnit != null) {
86             other[0] = otherGridUnit.getOriginalBorderInfoForCell(otherSide);
87         }
88         if ((currentRow != null)
89                 && (side == BEFORE
90                     || side == AFTER
91                     || (currentGridUnit.getFlag(GridUnit.IN_FIRST_COLUMN) && side == START)
92                     || (currentGridUnit.getFlag(GridUnit.IN_LAST_COLUMN) && side == END))) {
93             //row
94
current[1] = currentRow.getCommonBorderPaddingBackground().getBorderInfo(side);
95         }
96         if (otherRow != null) {
97             //row
98
other[1] = otherRow.getCommonBorderPaddingBackground().getBorderInfo(otherSide);
99         }
100         if (currentBody != null
101                 && ((side == BEFORE && currentGridUnit.getFlag(GridUnit.FIRST_IN_PART))
102                 || (side == AFTER && currentGridUnit.getFlag(GridUnit.LAST_IN_PART))
103                 || (currentGridUnit.getFlag(GridUnit.IN_FIRST_COLUMN) && side == START)
104                 || (currentGridUnit.getFlag(GridUnit.IN_LAST_COLUMN) && side == END))) {
105             //row group (=body, table-header or table-footer)
106
current[2] = currentBody.getCommonBorderPaddingBackground().getBorderInfo(side);
107         }
108         if (otherGridUnit != null
109                 && otherBody != null
110                 && ((otherSide == BEFORE && otherGridUnit.getFlag(GridUnit.FIRST_IN_PART))
111                     || (otherSide == AFTER && otherGridUnit.getFlag(GridUnit.LAST_IN_PART)))) {
112             //row group (=body, table-header or table-footer)
113
other[2] = otherBody.getCommonBorderPaddingBackground().getBorderInfo(otherSide);
114         }
115         if ((side == BEFORE && otherGridUnit == null)
116                 || (side == AFTER && otherGridUnit == null)
117                 || (side == START)
118                 || (side == END)) {
119             //column
120
current[3] = currentColumn.getCommonBorderPaddingBackground().getBorderInfo(side);
121         }
122         if (otherColumn != null) {
123             //column
124
other[3] = otherColumn.getCommonBorderPaddingBackground().getBorderInfo(otherSide);
125         }
126         //TODO current[4] and other[4] for column groups
127
if (otherGridUnit == null
128             && ((side == BEFORE && (flags & VERTICAL_START_END_OF_TABLE) > 0)
129                     || (side == AFTER && (flags & VERTICAL_START_END_OF_TABLE) > 0)
130                     || (side == START)
131                     || (side == END))) {
132             //table
133
current[5] = table.getCommonBorderPaddingBackground().getBorderInfo(side);
134         }
135         //other[6] is always null, since it's always the same table
136

137         BorderInfo resolved = null;
138         
139         // *** Rule 1 ***
140
resolved = doRule1(current, other);
141         if (resolved != null) {
142             return resolved;
143         }
144         
145         // *** Rule 2 ***
146
if (!doRule2(current, other)) {
147         }
148         
149         // *** Rule 3 ***
150
resolved = doRule3(current, other);
151         if (resolved != null) {
152             return resolved;
153         }
154         
155         // *** Rule 4 ***
156
resolved = doRule4(current, other);
157         if (resolved != null) {
158             return resolved;
159         }
160         
161         // *** Rule 5 ***
162
resolved = doRule5(current, other);
163         if (resolved != null) {
164             return resolved;
165         }
166         
167         return null; //no winner, no border
168
}
169
170     private BorderInfo doRule1(BorderInfo[] current, BorderInfo[] other) {
171         for (int i = 0; i < current.length; i++) {
172             if ((current[i] != null) && (current[i].getStyle() == Constants.EN_HIDDEN)) {
173                 return current[i];
174             }
175             if ((other[i] != null) && (other[i].getStyle() == Constants.EN_HIDDEN)) {
176                 return other[i];
177             }
178         }
179         return null;
180     }
181     
182     private boolean doRule2(BorderInfo[] current, BorderInfo[] other) {
183         boolean found = false;
184         for (int i = 0; i < current.length; i++) {
185             if ((current[i] != null) && (current[i].getStyle() != Constants.EN_NONE)) {
186                 found = true;
187                 break;
188             }
189             if ((other[i] != null) && (other[i].getStyle() != Constants.EN_NONE)) {
190                 found = true;
191                 break;
192             }
193         }
194         return found;
195     }
196
197     private BorderInfo doRule3(BorderInfo[] current, BorderInfo[] other) {
198         int width = 0;
199         //Find max border width
200
for (int i = 0; i < current.length; i++) {
201             if ((current[i] != null) && (current[i].getRetainedWidth() > width)) {
202                 width = current[i].getRetainedWidth();
203             }
204             if ((other[i] != null) && (other[i].getRetainedWidth() > width)) {
205                 width = other[i].getRetainedWidth();
206             }
207         }
208         BorderInfo widest = null;
209         int count = 0;
210         //See if there's only one with the widest border
211
for (int i = 0; i < current.length; i++) {
212             if ((current[i] != null) && (current[i].getRetainedWidth() == width)) {
213                 count++;
214                 if (widest == null) {
215                     widest = current[i];
216                 }
217             } else {
218                 current[i] = null; //Discard the narrower ones
219
}
220             if ((other[i] != null) && (other[i].getRetainedWidth() == width)) {
221                 count++;
222                 if (widest == null) {
223                     widest = other[i];
224                 }
225             } else {
226                 other[i] = null; //Discard the narrower ones
227
}
228         }
229         if (count == 1) {
230             return widest;
231         } else {
232             return null;
233         }
234     }
235
236     private BorderInfo doRule4(BorderInfo[] current, BorderInfo[] other) {
237         int pref = getPreferenceValue(Constants.EN_INSET); //Lowest preference
238
//Find highest preference value
239
for (int i = 0; i < current.length; i++) {
240             if (current[i] != null) {
241                 int currPref = getPreferenceValue(current[i].getStyle());
242                 if (currPref > pref) {
243                     pref = currPref;
244                 }
245             }
246             if (other[i] != null) {
247                 int currPref = getPreferenceValue(other[i].getStyle());
248                 if (currPref > pref) {
249                     pref = currPref;
250                 }
251             }
252         }
253         BorderInfo preferred = null;
254         int count = 0;
255         //See if there's only one with the preferred border style
256
for (int i = 0; i < current.length; i++) {
257             if (current[i] != null) {
258                 int currPref = getPreferenceValue(current[i].getStyle());
259                 if (currPref == pref) {
260                     count++;
261                     if (preferred == null) {
262                         preferred = current[i];
263                     }
264                     break;
265                 }
266             } else {
267                 current[i] = null; //Discard the ones that are not preferred
268
}
269             if (other[i] != null) {
270                 int currPref = getPreferenceValue(other[i].getStyle());
271                 if (currPref == pref) {
272                     count++;
273                     if (preferred == null) {
274                         preferred = other[i];
275                     }
276                     break;
277                 }
278             } else {
279                 other[i] = null; //Discard the ones that are not preferred
280
}
281         }
282         if (count == 1) {
283             return preferred;
284         } else {
285             return null;
286         }
287     }
288
289     private BorderInfo doRule5(BorderInfo[] current, BorderInfo[] other) {
290         for (int i = 0; i < current.length; i++) {
291             if (current[i] != null) {
292                 return current[i];
293             }
294             if (other[i] != null) {
295                 return other[i];
296             }
297         }
298         return null;
299     }
300
301 }
302
Popular Tags