KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: LinkSet.java,v 1.11.2.4 2003/02/25 14:07:04 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 // Java
54
import java.util.List JavaDoc;
55 import java.awt.Rectangle JavaDoc;
56
57 // FOP
58
import org.apache.fop.layout.inline.InlineArea;
59
60 /**
61  * a set of rectangles on a page that are linked to a common
62  * destination
63  */

64 public class LinkSet {
65
66     /**
67      * the destination of the links
68      */

69     private String JavaDoc destination;
70
71     /**
72      * the set of rectangles
73      */

74     private List JavaDoc rects = new java.util.ArrayList JavaDoc();
75
76     private int xoffset = 0;
77     private int yoffset = 0;
78
79     /* the maximum Y offset value encountered for this LinkSet */
80     private int maxY = 0;
81
82     protected int startIndent;
83     protected int endIndent;
84
85     private int linkType;
86
87     private Area area;
88
89     public static final int INTERNAL = 0; // represents internal link
90
public static final int EXTERNAL = 1; // represents external link
91

92     // property required for alignment adjustments
93
private int contentRectangleWidth = 0;
94
95     public LinkSet(String JavaDoc destination, Area area, int linkType) {
96         this.destination = destination;
97         this.area = area;
98         this.linkType = linkType;
99     }
100
101     public void addRect(Rectangle JavaDoc r, LineArea lineArea,
102                         InlineArea inlineArea) {
103         LinkedRectangle linkedRectangle = new LinkedRectangle(r, lineArea,
104                 inlineArea);
105         linkedRectangle.setY(this.yoffset);
106         if (this.yoffset > maxY) {
107             maxY = this.yoffset;
108         }
109         rects.add(linkedRectangle);
110     }
111
112     public void setYOffset(int y) {
113         this.yoffset = y;
114     }
115
116     public void setXOffset(int x) {
117         this.xoffset = x;
118     }
119
120     public void setContentRectangleWidth(int contentRectangleWidth) {
121         this.contentRectangleWidth = contentRectangleWidth;
122     }
123
124     public void applyAreaContainerOffsets(AreaContainer ac, Area area) {
125         int height = area.getAbsoluteHeight();
126         BlockArea ba = (BlockArea)area;
127         for (int i = 0; i < rects.size(); i++ ) {
128             LinkedRectangle r = (LinkedRectangle)rects.get(i);
129             r.setX(r.getX() + ac.getXPosition() + area.getTableCellXOffset());
130             r.setY(ac.getYPosition() - height + (maxY - r.getY())
131                    - ba.getHalfLeading());
132         }
133     }
134
135     // intermediate implementation for joining all sublinks on same line
136
public void mergeLinks() {
137         int numRects = rects.size();
138         if (numRects <= 1) return;
139
140         LinkedRectangle curRect =
141             new LinkedRectangle((LinkedRectangle)rects.get(0));
142         List JavaDoc nv = new java.util.ArrayList JavaDoc();
143
144         for (int ri = 1; ri < numRects; ri++) {
145             LinkedRectangle r = (LinkedRectangle)rects.get(ri);
146
147             // yes, I'm really happy with comparing refs...
148
if (r.getLineArea() == curRect.getLineArea()) {
149                 curRect.setWidth(r.getX() + r.getWidth() - curRect.getX());
150             } else {
151                 nv.add(curRect);
152                 curRect = new LinkedRectangle(r);
153             }
154
155             if (ri == numRects - 1) {
156                 nv.add(curRect);
157             }
158         }
159
160         rects = nv;
161     }
162
163     public void align() {
164         for (int i = 0; i < rects.size(); i++ ) {
165             LinkedRectangle r = (LinkedRectangle)rects.get(i);
166             r.setX(r.getX() + r.getLineArea().getStartIndent()
167                    + r.getInlineArea().getXOffset());
168         }
169     }
170
171     public String JavaDoc getDest() {
172         return this.destination;
173     }
174
175     public List JavaDoc getRects() {
176         return this.rects;
177     }
178
179     public int getEndIndent() {
180         return endIndent;
181     }
182
183     public int getStartIndent() {
184         return startIndent;
185     }
186
187     public Area getArea() {
188         return area;
189     }
190
191     public int getLinkType() {
192         return linkType;
193     }
194
195 }
196
Popular Tags