KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lowagie > text > Anchor


1 /*
2  * $Id: Anchor.java 2748 2007-05-12 15:11:48Z blowagie $
3  * $Name$
4  *
5  * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
6  *
7  * The contents of this file are subject to the Mozilla Public License Version 1.1
8  * (the "License"); you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the License.
14  *
15  * The Original Code is 'iText, a free JAVA-PDF library'.
16  *
17  * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
18  * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
19  * All Rights Reserved.
20  * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
21  * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
22  *
23  * Contributor(s): all the names of the contributors are added in the source code
24  * where applicable.
25  *
26  * Alternatively, the contents of this file may be used under the terms of the
27  * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
28  * provisions of LGPL are applicable instead of those above. If you wish to
29  * allow use of your version of this file only under the terms of the LGPL
30  * License and not to allow others to use your version of this file under
31  * the MPL, indicate your decision by deleting the provisions above and
32  * replace them with the notice and other provisions required by the LGPL.
33  * If you do not delete the provisions above, a recipient may use your version
34  * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
35  *
36  * This library is free software; you can redistribute it and/or modify it
37  * under the terms of the MPL as stated above or under the terms of the GNU
38  * Library General Public License as published by the Free Software Foundation;
39  * either version 2 of the License, or any later version.
40  *
41  * This library is distributed in the hope that it will be useful, but WITHOUT
42  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
43  * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
44  * details.
45  *
46  * If you didn't download this code from the following link, you should check if
47  * you aren't using an obsolete version:
48  * http://www.lowagie.com/iText/
49  */

50
51 package com.lowagie.text;
52
53 import java.net.MalformedURLException JavaDoc;
54 import java.net.URL JavaDoc;
55 import java.util.ArrayList JavaDoc;
56 import java.util.Iterator JavaDoc;
57
58 /**
59  * An <CODE>Anchor</CODE> can be a reference or a destination of a reference.
60  * <P>
61  * An <CODE>Anchor</CODE> is a special kind of <CODE>Phrase</CODE>.
62  * It is constructed in the same way.
63  * <P>
64  * Example:
65  * <BLOCKQUOTE><PRE>
66  * <STRONG>Anchor anchor = new Anchor("this is a link");</STRONG>
67  * <STRONG>anchor.setName("LINK");</STRONG>
68  * <STRONG>anchor.setReference("http://www.lowagie.com");</STRONG>
69  * </PRE></BLOCKQUOTE>
70  *
71  * @see Element
72  * @see Phrase
73  */

74
75 public class Anchor extends Phrase {
76
77     // constant
78
private static final long serialVersionUID = -852278536049236911L;
79     
80     // membervariables
81

82     /** This is the name of the <CODE>Anchor</CODE>. */
83     protected String JavaDoc name = null;
84     
85     /** This is the reference of the <CODE>Anchor</CODE>. */
86     protected String JavaDoc reference = null;
87     
88     // constructors
89

90     /**
91      * Constructs an <CODE>Anchor</CODE> without specifying a leading.
92      */

93     public Anchor() {
94         super(16);
95     }
96     
97     /**
98      * Constructs an <CODE>Anchor</CODE> with a certain leading.
99      *
100      * @param leading the leading
101      */

102     
103     public Anchor(float leading) {
104         super(leading);
105     }
106     
107     /**
108      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>Chunk</CODE>.
109      *
110      * @param chunk a <CODE>Chunk</CODE>
111      */

112     public Anchor(Chunk chunk) {
113         super(chunk);
114     }
115     
116     /**
117      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>String</CODE>.
118      *
119      * @param string a <CODE>String</CODE>
120      */

121     public Anchor(String JavaDoc string) {
122         super(string);
123     }
124     
125     /**
126      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>String</CODE>
127      * and a certain <CODE>Font</CODE>.
128      *
129      * @param string a <CODE>String</CODE>
130      * @param font a <CODE>Font</CODE>
131      */

132     public Anchor(String JavaDoc string, Font font) {
133         super(string, font);
134     }
135     
136     /**
137      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>Chunk</CODE>
138      * and a certain leading.
139      *
140      * @param leading the leading
141      * @param chunk a <CODE>Chunk</CODE>
142      */

143     public Anchor(float leading, Chunk chunk) {
144         super(leading, chunk);
145     }
146     
147     /**
148      * Constructs an <CODE>Anchor</CODE> with a certain leading
149      * and a certain <CODE>String</CODE>.
150      *
151      * @param leading the leading
152      * @param string a <CODE>String</CODE>
153      */

154     public Anchor(float leading, String JavaDoc string) {
155         super(leading, string);
156     }
157     
158     /**
159      * Constructs an <CODE>Anchor</CODE> with a certain leading,
160      * a certain <CODE>String</CODE> and a certain <CODE>Font</CODE>.
161      *
162      * @param leading the leading
163      * @param string a <CODE>String</CODE>
164      * @param font a <CODE>Font</CODE>
165      */

166     public Anchor(float leading, String JavaDoc string, Font font) {
167         super(leading, string, font);
168     }
169     
170     /**
171      * Constructs an <CODE>Anchor</CODE> with a certain <CODE>Phrase</CODE>.
172      *
173      * @param phrase a <CODE>Phrase</CODE>
174      */

175     public Anchor(Phrase phrase) {
176         super(phrase);
177         if (phrase instanceof Anchor) {
178             Anchor a = (Anchor) phrase;
179             setName(a.name);
180             setReference(a.reference);
181         }
182     }
183     
184     // implementation of the Element-methods
185

186     /**
187      * Processes the element by adding it (or the different parts) to an
188      * <CODE>ElementListener</CODE>.
189      *
190      * @param listener an <CODE>ElementListener</CODE>
191      * @return <CODE>true</CODE> if the element was processed successfully
192      */

193     public boolean process(ElementListener listener) {
194         try {
195             Chunk chunk;
196             Iterator JavaDoc i = getChunks().iterator();
197             boolean localDestination = (reference != null && reference.startsWith("#"));
198             boolean notGotoOK = true;
199             while (i.hasNext()) {
200                 chunk = (Chunk) i.next();
201                 if (name != null && notGotoOK && !chunk.isEmpty()) {
202                     chunk.setLocalDestination(name);
203                     notGotoOK = false;
204                 }
205                 if (localDestination) {
206                     chunk.setLocalGoto(reference.substring(1));
207                 }
208                 listener.add(chunk);
209             }
210             return true;
211         }
212         catch(DocumentException de) {
213             return false;
214         }
215     }
216     
217     /**
218      * Gets all the chunks in this element.
219      *
220      * @return an <CODE>ArrayList</CODE>
221      */

222     public ArrayList JavaDoc getChunks() {
223         ArrayList JavaDoc tmp = new ArrayList JavaDoc();
224         Chunk chunk;
225         Iterator JavaDoc i = iterator();
226         boolean localDestination = (reference != null && reference.startsWith("#"));
227         boolean notGotoOK = true;
228         while (i.hasNext()) {
229             chunk = (Chunk) i.next();
230             if (name != null && notGotoOK && !chunk.isEmpty()) {
231                 chunk.setLocalDestination(name);
232                 notGotoOK = false;
233             }
234             if (localDestination) {
235                 chunk.setLocalGoto(reference.substring(1));
236             }
237             else if (reference != null)
238                 chunk.setAnchor(reference);
239             tmp.add(chunk);
240         }
241         return tmp;
242     }
243     
244     /**
245      * Gets the type of the text element.
246      *
247      * @return a type
248      */

249     public int type() {
250         return Element.ANCHOR;
251     }
252     
253     // methods
254

255     /**
256      * Sets the name of this <CODE>Anchor</CODE>.
257      *
258      * @param name a new name
259      */

260     public void setName(String JavaDoc name) {
261         this.name = name;
262     }
263     
264     /**
265      * Sets the reference of this <CODE>Anchor</CODE>.
266      *
267      * @param reference a new reference
268      */

269     public void setReference(String JavaDoc reference) {
270         this.reference = reference;
271     }
272     
273     // methods to retrieve information
274

275     /**
276      * Returns the name of this <CODE>Anchor</CODE>.
277      *
278      * @return a name
279      */

280     public String JavaDoc getName() {
281         return name;
282     }
283
284     /**
285      * Gets the reference of this <CODE>Anchor</CODE>.
286      *
287      * @return a reference
288      */

289     public String JavaDoc getReference() {
290         return reference;
291     }
292
293     /**
294      * Gets the reference of this <CODE>Anchor</CODE>.
295      *
296      * @return an <CODE>URL</CODE>
297      */

298     public URL JavaDoc getUrl() {
299         try {
300             return new URL JavaDoc(reference);
301         }
302         catch(MalformedURLException JavaDoc mue) {
303             return null;
304         }
305     }
306     
307     // deprecated stuff
308

309     /**
310      * Returns an <CODE>Anchor</CODE> that has been constructed taking in account
311      * the value of some <VAR>attributes</VAR>.
312      *
313      * @param attributes Some attributes
314      * @deprecated use ElementFactory.getAnchor(attributes)
315      */

316     public Anchor(java.util.Properties JavaDoc attributes) {
317         this(com.lowagie.text.factories.ElementFactory.getAnchor(attributes));
318     }
319     
320     /**
321      * Returns the name of this <CODE>Anchor</CODE>.
322      *
323      * @return a name
324      * @deprecated Use {@link #getName()} instead
325      */

326     public String JavaDoc name() {
327         return getName();
328     }
329     
330     /**
331      * Gets the reference of this <CODE>Anchor</CODE>.
332      *
333      * @return a reference
334      * @deprecated Use {@link #getReference()} instead
335      */

336     public String JavaDoc reference() {
337         return getReference();
338     }
339     
340     /**
341      * Gets the reference of this <CODE>Anchor</CODE>.
342      *
343      * @return an <CODE>URL</CODE>
344      * @deprecated Use {@link #getUrl()} instead
345      */

346     public URL JavaDoc url() {
347         return getUrl();
348     }
349 }
350
Popular Tags