001: /*
002: * $Id: Anchor.java 2748 2007-05-12 15:11:48Z blowagie $
003: * $Name$
004: *
005: * Copyright 1999, 2000, 2001, 2002 by Bruno Lowagie.
006: *
007: * The contents of this file are subject to the Mozilla Public License Version 1.1
008: * (the "License"); you may not use this file except in compliance with the License.
009: * You may obtain a copy of the License at http://www.mozilla.org/MPL/
010: *
011: * Software distributed under the License is distributed on an "AS IS" basis,
012: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
013: * for the specific language governing rights and limitations under the License.
014: *
015: * The Original Code is 'iText, a free JAVA-PDF library'.
016: *
017: * The Initial Developer of the Original Code is Bruno Lowagie. Portions created by
018: * the Initial Developer are Copyright (C) 1999, 2000, 2001, 2002 by Bruno Lowagie.
019: * All Rights Reserved.
020: * Co-Developer of the code is Paulo Soares. Portions created by the Co-Developer
021: * are Copyright (C) 2000, 2001, 2002 by Paulo Soares. All Rights Reserved.
022: *
023: * Contributor(s): all the names of the contributors are added in the source code
024: * where applicable.
025: *
026: * Alternatively, the contents of this file may be used under the terms of the
027: * LGPL license (the "GNU LIBRARY GENERAL PUBLIC LICENSE"), in which case the
028: * provisions of LGPL are applicable instead of those above. If you wish to
029: * allow use of your version of this file only under the terms of the LGPL
030: * License and not to allow others to use your version of this file under
031: * the MPL, indicate your decision by deleting the provisions above and
032: * replace them with the notice and other provisions required by the LGPL.
033: * If you do not delete the provisions above, a recipient may use your version
034: * of this file under either the MPL or the GNU LIBRARY GENERAL PUBLIC LICENSE.
035: *
036: * This library is free software; you can redistribute it and/or modify it
037: * under the terms of the MPL as stated above or under the terms of the GNU
038: * Library General Public License as published by the Free Software Foundation;
039: * either version 2 of the License, or any later version.
040: *
041: * This library is distributed in the hope that it will be useful, but WITHOUT
042: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
043: * FOR A PARTICULAR PURPOSE. See the GNU Library general Public License for more
044: * details.
045: *
046: * If you didn't download this code from the following link, you should check if
047: * you aren't using an obsolete version:
048: * http://www.lowagie.com/iText/
049: */
050:
051: package com.lowagie.text;
052:
053: import java.net.MalformedURLException;
054: import java.net.URL;
055: import java.util.ArrayList;
056: import java.util.Iterator;
057:
058: /**
059: * An <CODE>Anchor</CODE> can be a reference or a destination of a reference.
060: * <P>
061: * An <CODE>Anchor</CODE> is a special kind of <CODE>Phrase</CODE>.
062: * It is constructed in the same way.
063: * <P>
064: * Example:
065: * <BLOCKQUOTE><PRE>
066: * <STRONG>Anchor anchor = new Anchor("this is a link");</STRONG>
067: * <STRONG>anchor.setName("LINK");</STRONG>
068: * <STRONG>anchor.setReference("http://www.lowagie.com");</STRONG>
069: * </PRE></BLOCKQUOTE>
070: *
071: * @see Element
072: * @see Phrase
073: */
074:
075: public class Anchor extends Phrase {
076:
077: // constant
078: private static final long serialVersionUID = -852278536049236911L;
079:
080: // membervariables
081:
082: /** This is the name of the <CODE>Anchor</CODE>. */
083: protected String name = null;
084:
085: /** This is the reference of the <CODE>Anchor</CODE>. */
086: protected String reference = null;
087:
088: // constructors
089:
090: /**
091: * Constructs an <CODE>Anchor</CODE> without specifying a leading.
092: */
093: public Anchor() {
094: super (16);
095: }
096:
097: /**
098: * Constructs an <CODE>Anchor</CODE> with a certain leading.
099: *
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 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 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 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 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 i = getChunks().iterator();
197: boolean localDestination = (reference != null && reference
198: .startsWith("#"));
199: boolean notGotoOK = true;
200: while (i.hasNext()) {
201: chunk = (Chunk) i.next();
202: if (name != null && notGotoOK && !chunk.isEmpty()) {
203: chunk.setLocalDestination(name);
204: notGotoOK = false;
205: }
206: if (localDestination) {
207: chunk.setLocalGoto(reference.substring(1));
208: }
209: listener.add(chunk);
210: }
211: return true;
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 getChunks() {
223: ArrayList tmp = new ArrayList();
224: Chunk chunk;
225: Iterator i = iterator();
226: boolean localDestination = (reference != null && reference
227: .startsWith("#"));
228: boolean notGotoOK = true;
229: while (i.hasNext()) {
230: chunk = (Chunk) i.next();
231: if (name != null && notGotoOK && !chunk.isEmpty()) {
232: chunk.setLocalDestination(name);
233: notGotoOK = false;
234: }
235: if (localDestination) {
236: chunk.setLocalGoto(reference.substring(1));
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 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 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 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 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 getUrl() {
299: try {
300: return new URL(reference);
301: } catch (MalformedURLException mue) {
302: return null;
303: }
304: }
305:
306: // deprecated stuff
307:
308: /**
309: * Returns an <CODE>Anchor</CODE> that has been constructed taking in account
310: * the value of some <VAR>attributes</VAR>.
311: *
312: * @param attributes Some attributes
313: * @deprecated use ElementFactory.getAnchor(attributes)
314: */
315: public Anchor(java.util.Properties attributes) {
316: this (com.lowagie.text.factories.ElementFactory
317: .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 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 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 url() {
347: return getUrl();
348: }
349: }
|