KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: Annotation.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.URL JavaDoc;
54 import java.util.ArrayList JavaDoc;
55 import java.util.HashMap JavaDoc;
56
57 /**
58  * An <CODE>Annotation</CODE> is a little note that can be added to a page on
59  * a document.
60  *
61  * @see Element
62  * @see Anchor
63  */

64
65 public class Annotation implements Element {
66
67     // membervariables
68

69     /** This is a possible annotation type. */
70     public static final int TEXT = 0;
71
72     /** This is a possible annotation type. */
73     public static final int URL_NET = 1;
74
75     /** This is a possible annotation type. */
76     public static final int URL_AS_STRING = 2;
77
78     /** This is a possible annotation type. */
79     public static final int FILE_DEST = 3;
80
81     /** This is a possible annotation type. */
82     public static final int FILE_PAGE = 4;
83
84     /** This is a possible annotation type. */
85     public static final int NAMED_DEST = 5;
86
87     /** This is a possible annotation type. */
88     public static final int LAUNCH = 6;
89
90     /** This is a possible annotation type. */
91     public static final int SCREEN = 7;
92
93     /** This is a possible attribute. */
94     public static final String JavaDoc TITLE = "title";
95
96     /** This is a possible attribute. */
97     public static final String JavaDoc CONTENT = "content";
98
99     /** This is a possible attribute. */
100     public static final String JavaDoc URL = "url";
101
102     /** This is a possible attribute. */
103     public static final String JavaDoc FILE = "file";
104
105     /** This is a possible attribute. */
106     public static final String JavaDoc DESTINATION = "destination";
107
108     /** This is a possible attribute. */
109     public static final String JavaDoc PAGE = "page";
110
111     /** This is a possible attribute. */
112     public static final String JavaDoc NAMED = "named";
113
114     /** This is a possible attribute. */
115     public static final String JavaDoc APPLICATION = "application";
116
117     /** This is a possible attribute. */
118     public static final String JavaDoc PARAMETERS = "parameters";
119
120     /** This is a possible attribute. */
121     public static final String JavaDoc OPERATION = "operation";
122
123     /** This is a possible attribute. */
124     public static final String JavaDoc DEFAULTDIR = "defaultdir";
125
126     /** This is a possible attribute. */
127     public static final String JavaDoc LLX = "llx";
128
129     /** This is a possible attribute. */
130     public static final String JavaDoc LLY = "lly";
131
132     /** This is a possible attribute. */
133     public static final String JavaDoc URX = "urx";
134
135     /** This is a possible attribute. */
136     public static final String JavaDoc URY = "ury";
137
138     /** This is a possible attribute. */
139     public static final String JavaDoc MIMETYPE = "mime";
140
141     /** This is the type of annotation. */
142     protected int annotationtype;
143
144     /** This is the title of the <CODE>Annotation</CODE>. */
145     protected HashMap JavaDoc annotationAttributes = new HashMap JavaDoc();
146
147     /** This is the lower left x-value */
148     protected float llx = Float.NaN;
149
150     /** This is the lower left y-value */
151     protected float lly = Float.NaN;
152
153     /** This is the upper right x-value */
154     protected float urx = Float.NaN;
155
156     /** This is the upper right y-value */
157     protected float ury = Float.NaN;
158
159     // constructors
160

161     /**
162      * Constructs an <CODE>Annotation</CODE> with a certain title and some
163      * text.
164      *
165      * @param llx
166      * lower left x coordinate
167      * @param lly
168      * lower left y coordinate
169      * @param urx
170      * upper right x coordinate
171      * @param ury
172      * upper right y coordinate
173      */

174     private Annotation(float llx, float lly, float urx, float ury) {
175         this.llx = llx;
176         this.lly = lly;
177         this.urx = urx;
178         this.ury = ury;
179     }
180
181     /**
182      * Copy constructor.
183      */

184     public Annotation(Annotation an) {
185         annotationtype = an.annotationtype;
186         annotationAttributes = an.annotationAttributes;
187         llx = an.llx;
188         lly = an.lly;
189         urx = an.urx;
190         ury = an.ury;
191     }
192     
193     /**
194      * Constructs an <CODE>Annotation</CODE> with a certain title and some
195      * text.
196      *
197      * @param title
198      * the title of the annotation
199      * @param text
200      * the content of the annotation
201      */

202     public Annotation(String JavaDoc title, String JavaDoc text) {
203         annotationtype = TEXT;
204         annotationAttributes.put(TITLE, title);
205         annotationAttributes.put(CONTENT, text);
206     }
207
208     /**
209      * Constructs an <CODE>Annotation</CODE> with a certain title and some
210      * text.
211      *
212      * @param title
213      * the title of the annotation
214      * @param text
215      * the content of the annotation
216      * @param llx
217      * the lower left x-value
218      * @param lly
219      * the lower left y-value
220      * @param urx
221      * the upper right x-value
222      * @param ury
223      * the upper right y-value
224      */

225     public Annotation(String JavaDoc title, String JavaDoc text, float llx, float lly,
226             float urx, float ury) {
227         this(llx, lly, urx, ury);
228         annotationtype = TEXT;
229         annotationAttributes.put(TITLE, title);
230         annotationAttributes.put(CONTENT, text);
231     }
232
233     /**
234      * Constructs an <CODE>Annotation</CODE>.
235      *
236      * @param llx
237      * the lower left x-value
238      * @param lly
239      * the lower left y-value
240      * @param urx
241      * the upper right x-value
242      * @param ury
243      * the upper right y-value
244      * @param url
245      * the external reference
246      */

247     public Annotation(float llx, float lly, float urx, float ury, URL JavaDoc url) {
248         this(llx, lly, urx, ury);
249         annotationtype = URL_NET;
250         annotationAttributes.put(URL, url);
251     }
252
253     /**
254      * Constructs an <CODE>Annotation</CODE>.
255      *
256      * @param llx
257      * the lower left x-value
258      * @param lly
259      * the lower left y-value
260      * @param urx
261      * the upper right x-value
262      * @param ury
263      * the upper right y-value
264      * @param url
265      * the external reference
266      */

267     public Annotation(float llx, float lly, float urx, float ury, String JavaDoc url) {
268         this(llx, lly, urx, ury);
269         annotationtype = URL_AS_STRING;
270         annotationAttributes.put(FILE, url);
271     }
272
273     /**
274      * Constructs an <CODE>Annotation</CODE>.
275      *
276      * @param llx
277      * the lower left x-value
278      * @param lly
279      * the lower left y-value
280      * @param urx
281      * the upper right x-value
282      * @param ury
283      * the upper right y-value
284      * @param file
285      * an external PDF file
286      * @param dest
287      * the destination in this file
288      */

289     public Annotation(float llx, float lly, float urx, float ury, String JavaDoc file,
290             String JavaDoc dest) {
291         this(llx, lly, urx, ury);
292         annotationtype = FILE_DEST;
293         annotationAttributes.put(FILE, file);
294         annotationAttributes.put(DESTINATION, dest);
295     }
296
297     /**
298      * Creates a Screen anotation to embed media clips
299      *
300      * @param llx
301      * @param lly
302      * @param urx
303      * @param ury
304      * @param moviePath
305      * path to the media clip file
306      * @param mimeType
307      * mime type of the media
308      * @param showOnDisplay
309      * if true play on display of the page
310      */

311     public Annotation(float llx, float lly, float urx, float ury,
312             String JavaDoc moviePath, String JavaDoc mimeType, boolean showOnDisplay) {
313         this(llx, lly, urx, ury);
314         annotationtype = SCREEN;
315         annotationAttributes.put(FILE, moviePath);
316         annotationAttributes.put(MIMETYPE, mimeType);
317         annotationAttributes.put(PARAMETERS, new boolean[] {
318                 false /* embedded */, showOnDisplay });
319     }
320
321     /**
322      * Constructs an <CODE>Annotation</CODE>.
323      *
324      * @param llx
325      * the lower left x-value
326      * @param lly
327      * the lower left y-value
328      * @param urx
329      * the upper right x-value
330      * @param ury
331      * the upper right y-value
332      * @param file
333      * an external PDF file
334      * @param page
335      * a page number in this file
336      */

337     public Annotation(float llx, float lly, float urx, float ury, String JavaDoc file,
338             int page) {
339         this(llx, lly, urx, ury);
340         annotationtype = FILE_PAGE;
341         annotationAttributes.put(FILE, file);
342         annotationAttributes.put(PAGE, new Integer JavaDoc(page));
343     }
344
345     /**
346      * Constructs an <CODE>Annotation</CODE>.
347      *
348      * @param llx
349      * the lower left x-value
350      * @param lly
351      * the lower left y-value
352      * @param urx
353      * the upper right x-value
354      * @param ury
355      * the upper right y-value
356      * @param named
357      * a named destination in this file
358      */

359     public Annotation(float llx, float lly, float urx, float ury, int named) {
360         this(llx, lly, urx, ury);
361         annotationtype = NAMED_DEST;
362         annotationAttributes.put(NAMED, new Integer JavaDoc(named));
363     }
364
365     /**
366      * Constructs an <CODE>Annotation</CODE>.
367      *
368      * @param llx
369      * the lower left x-value
370      * @param lly
371      * the lower left y-value
372      * @param urx
373      * the upper right x-value
374      * @param ury
375      * the upper right y-value
376      * @param application
377      * an external application
378      * @param parameters
379      * parameters to pass to this application
380      * @param operation
381      * the operation to pass to this application
382      * @param defaultdir
383      * the default directory to run this application in
384      */

385     public Annotation(float llx, float lly, float urx, float ury,
386             String JavaDoc application, String JavaDoc parameters, String JavaDoc operation,
387             String JavaDoc defaultdir) {
388         this(llx, lly, urx, ury);
389         annotationtype = LAUNCH;
390         annotationAttributes.put(APPLICATION, application);
391         annotationAttributes.put(PARAMETERS, parameters);
392         annotationAttributes.put(OPERATION, operation);
393         annotationAttributes.put(DEFAULTDIR, defaultdir);
394     }
395
396     // implementation of the Element-methods
397

398     /**
399      * Gets the type of the text element.
400      *
401      * @return a type
402      */

403     public int type() {
404         return Element.ANNOTATION;
405     }
406
407     /**
408      * Processes the element by adding it (or the different parts) to an <CODE>
409      * ElementListener</CODE>.
410      *
411      * @param listener
412      * an <CODE>ElementListener</CODE>
413      * @return <CODE>true</CODE> if the element was processed successfully
414      */

415     public boolean process(ElementListener listener) {
416         try {
417             return listener.add(this);
418         } catch (DocumentException de) {
419             return false;
420         }
421     }
422
423     /**
424      * Gets all the chunks in this element.
425      *
426      * @return an <CODE>ArrayList</CODE>
427      */

428
429     public ArrayList JavaDoc getChunks() {
430         return new ArrayList JavaDoc();
431     }
432
433     // methods
434

435     /**
436      * Sets the dimensions of this annotation.
437      *
438      * @param llx
439      * the lower left x-value
440      * @param lly
441      * the lower left y-value
442      * @param urx
443      * the upper right x-value
444      * @param ury
445      * the upper right y-value
446      */

447     public void setDimensions(float llx, float lly, float urx, float ury) {
448         this.llx = llx;
449         this.lly = lly;
450         this.urx = urx;
451         this.ury = ury;
452     }
453
454     // methods to retrieve information
455

456     /**
457      * Returns the lower left x-value.
458      *
459      * @return a value
460      */

461     public float llx() {
462         return llx;
463     }
464
465     /**
466      * Returns the lower left y-value.
467      *
468      * @return a value
469      */

470     public float lly() {
471         return lly;
472     }
473
474     /**
475      * Returns the uppper right x-value.
476      *
477      * @return a value
478      */

479     public float urx() {
480         return urx;
481     }
482
483     /**
484      * Returns the uppper right y-value.
485      *
486      * @return a value
487      */

488     public float ury() {
489         return ury;
490     }
491
492     /**
493      * Returns the lower left x-value.
494      *
495      * @param def
496      * the default value
497      * @return a value
498      */

499     public float llx(float def) {
500         if (Float.isNaN(llx))
501             return def;
502         return llx;
503     }
504
505     /**
506      * Returns the lower left y-value.
507      *
508      * @param def
509      * the default value
510      * @return a value
511      */

512     public float lly(float def) {
513         if (Float.isNaN(lly))
514             return def;
515         return lly;
516     }
517
518     /**
519      * Returns the upper right x-value.
520      *
521      * @param def
522      * the default value
523      * @return a value
524      */

525     public float urx(float def) {
526         if (Float.isNaN(urx))
527             return def;
528         return urx;
529     }
530
531     /**
532      * Returns the upper right y-value.
533      *
534      * @param def
535      * the default value
536      * @return a value
537      */

538     public float ury(float def) {
539         if (Float.isNaN(ury))
540             return def;
541         return ury;
542     }
543
544     /**
545      * Returns the type of this <CODE>Annotation</CODE>.
546      *
547      * @return a type
548      */

549     public int annotationType() {
550         return annotationtype;
551     }
552
553     /**
554      * Returns the title of this <CODE>Annotation</CODE>.
555      *
556      * @return a name
557      */

558     public String JavaDoc title() {
559         String JavaDoc s = (String JavaDoc) annotationAttributes.get(TITLE);
560         if (s == null)
561             s = "";
562         return s;
563     }
564
565     /**
566      * Gets the content of this <CODE>Annotation</CODE>.
567      *
568      * @return a reference
569      */

570     public String JavaDoc content() {
571         String JavaDoc s = (String JavaDoc) annotationAttributes.get(CONTENT);
572         if (s == null)
573             s = "";
574         return s;
575     }
576
577     /**
578      * Gets the content of this <CODE>Annotation</CODE>.
579      *
580      * @return a reference
581      */

582     public HashMap JavaDoc attributes() {
583         return annotationAttributes;
584     }
585
586     /**
587      * Returns an <CODE>Annotation</CODE> that has been constructed taking in
588      * account the value of some <VAR>attributes </VAR>.
589      *
590      * @param attributes
591      * Some attributes
592      * @deprecated Use ElementFactory.getAnnotation(attributes)
593      */

594     public Annotation(java.util.Properties JavaDoc attributes) {
595         this(com.lowagie.text.factories.ElementFactory.getAnnotation(attributes));
596     }
597 }
Popular Tags