KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > faceless > pdf > Form


1 // $Id: Form.java,v 1.3 2003/10/06 12:40:06 mike Exp $
2

3 package org.faceless.pdf;
4
5 import java.util.*;
6 import java.io.*;
7 import java.awt.Color JavaDoc;
8
9 /**
10  * <p>
11  * The Form class represents the interactive Form that may be included as part
12  * of a PDF document. This form can be filled out by users and then eventually
13  * submitted for processing in the same way as an HTML form. Unlike HTML, a
14  * PDF document may only have one form.
15  * </p><p>
16  * A form contains zero or more {@link FormElement} objects, each one of which
17  * usually has one or more visual representation on the page, in the form of
18  * a {@link PDFAnnotation} (the exception is the {@link FormSignature}
19  * class, which currently cannot have a visual appearance). Each element in the
20  * form is stored under a name, which is used to reference the element and must
21  * be unique.
22  * </p><p>
23  * The name may be a simple string, like <code>"Element1"</code>, or it may be
24  * a <i>compound</i> name, with fields separated with dots, for example
25  * <code>"Employee.Address.City"</code>. Simple and Compound names must not
26  * collide - for example, it would be illegal to have elements called
27  * "Country.Capital" and "Country" in the same document.
28  * </p>
29  * <p>
30  * <b>Note that using interactive forms requires the "Extended Edition"
31  * of the library</b> - although the classes are supplied with the package an
32  * "Extended Edition" license must be purchased to activate this functionality.
33  * </p>
34  *
35  * @see PDF#getForm
36  * @see FormElement
37  * @since 1.1.13
38  */

39 public final class Form extends PeeredObject
40 {
41     private final org.faceless.pdf2.Form form;
42     private PDFStyle backstyle;
43     private int radio=FormElement.STYLE_CIRCLE, check=FormElement.STYLE_CHECK;
44
45     Form(org.faceless.pdf2.Form form)
46     {
47         this.form=form;
48     }
49
50     Object JavaDoc getPeer()
51     {
52         return form;
53     }
54
55     /**
56      * <p>
57      * Add an element to the form. Although a form can contain as many elements
58      * as you like, currently only a single signature with a state of
59      * {@link FormSignature#STATE_PENDING} can be added to each document.
60      * </p>
61      * @param name the name of the form element
62      * @param element the element to add to the form
63      * @throws IllegalStateException if the element already exists in the form
64      */

65     public void addElement(String JavaDoc name, FormElement element)
66     {
67         form.addElement(name, element.element);
68     }
69
70     /**
71      * Return the specified element from the form.
72      * @param name the name of the form element
73      * @return the specified element, or <code>null</code> if it doesn't exist
74      */

75     public FormElement getElement(String JavaDoc name)
76     {
77     return (FormElement)PeeredObject.getPeer(form.getElement(name));
78     }
79
80     /**
81      * Remove the specified element from the form, if it exists.
82      * @param name the name of the form element
83      */

84     public void removeElement(String JavaDoc name)
85     {
86     form.removeElement(name);
87     }
88
89     /**
90      * Rename an element in the form. If the specified element
91      * name does not exist, an <code>IllegalArgumentException</code>
92      * is thrown
93      * @param fromname the original name of the form element
94      * @param toname the new name of the form element
95      * @since 1.1.23
96      */

97     public void renameElement(String JavaDoc fromname, String JavaDoc toname)
98     {
99     form.renameElement(fromname,toname);
100     }
101
102     /**
103      * Remove all the elements from the form
104      * @since 1.2.1
105      */

106     public void clear()
107     {
108     form.clear();
109     }
110
111     /**
112      * Return a map of all the elements in the form. Each key
113      * in the Map is a {@link java.lang.String} representing the name of the
114      * element, and the corresponding value is the {@link FormElement}. The
115      * returned map is unmodifiable - changes are not allowed.
116      * @return an unmodifiable <code>Map</code> containing all the form elements
117      */

118     public Map getElements()
119     {
120     Map out = new org.faceless.util.OrderedMap();
121     Map in = form.getElements();
122     for (Iterator i = in.entrySet().iterator();i.hasNext();) {
123         Map.Entry e = (Map.Entry)i.next();
124         out.put(e.getKey(), PeeredObject.getPeer(e.getValue()));
125     }
126     return Collections.unmodifiableMap(out);
127     }
128
129     /**
130      * Given a <code>FormElement</code>, return the name by which
131      * this element is stored in the form, or <code>null</code> if
132      * it doesn't exist.
133      * @return the name of this element or <code>null</code> if it's not in the Form
134      */

135     public String JavaDoc getName(FormElement element)
136     {
137     return form.getName(element.element);
138     }
139
140     /**
141      * Set the default background style for all new elements
142      * added to the form. This can be overridden by the <code>setStyle</code>
143      * method of each <code>FormElement</code>. The default is a white
144      * background with a plain black border
145      * @since 1.1.23
146      * @param style the default background style for new form elements
147      * @see PDFStyle#setFormStyle
148      */

149     public void setBackgroundStyle(PDFStyle style)
150     {
151     org.faceless.pdf2.PDFStyle newstyle = style.style;
152
153     char newcheck, newradio;
154     if (check==FormElement.STYLE_CIRCLE) newcheck=newstyle.FORMRADIOBUTTONSTYLE_CIRCLE;
155     else if (check==FormElement.STYLE_CROSS) newcheck=newstyle.FORMRADIOBUTTONSTYLE_CROSS;
156     else if (check==FormElement.STYLE_DIAMOND) newcheck=newstyle.FORMRADIOBUTTONSTYLE_DIAMOND;
157     else if (check==FormElement.STYLE_SQUARE) newcheck=newstyle.FORMRADIOBUTTONSTYLE_SQUARE;
158     else if (check==FormElement.STYLE_STAR) newcheck=newstyle.FORMRADIOBUTTONSTYLE_STAR;
159     else newcheck=newstyle.FORMRADIOBUTTONSTYLE_CHECK;
160
161     if (radio==FormElement.STYLE_CIRCLE) newradio=newstyle.FORMRADIOBUTTONSTYLE_CIRCLE;
162     else if (radio==FormElement.STYLE_CROSS) newradio=newstyle.FORMRADIOBUTTONSTYLE_CROSS;
163     else if (radio==FormElement.STYLE_DIAMOND) newradio=newstyle.FORMRADIOBUTTONSTYLE_DIAMOND;
164     else if (radio==FormElement.STYLE_SQUARE) newradio=newstyle.FORMRADIOBUTTONSTYLE_SQUARE;
165     else if (radio==FormElement.STYLE_STAR) newradio=newstyle.FORMRADIOBUTTONSTYLE_STAR;
166     else newradio=newstyle.FORMRADIOBUTTONSTYLE_CHECK;
167
168     newstyle.setFormRadioButtonStyle(newradio);
169     newstyle.setFormCheckboxStyle(newcheck);
170
171     form.setBackgroundStyle(newstyle);
172
173     this.backstyle=style;
174     }
175
176     /**
177      * Set the default text style for all new elements added
178      * to the form that contain text (the {@link FormText}, {@link FormChoice}
179      * and {@link FormButton} classes). The style must include a font, which
180      * is an instance of {@link StandardFont} or {@link TrueTypeFont}, and fill
181      * color to draw the text in. Note that some TrueType fonts caused problems
182      * when used in text boxes in Acrobat 4.0, so for maximum compatibility we
183      * recommend using a Standard font. The default is Black 11pt Helvetica.
184      * @param style the default text style for new form elements
185      * @since 1.1.23
186      */

187     public void setTextStyle(PDFStyle style)
188     {
189     form.setTextStyle(style.style);
190     }
191
192     /**
193      * Set the default style for any {@link FormRadioButton} and
194      * {@link FormCheckbox} elements added to the form. The values
195      * are one of {@link FormElement#STYLE_CIRCLE}, {@link FormElement#STYLE_CHECK}
196      * {@link FormElement#STYLE_SQUARE}, {@link FormElement#STYLE_CROSS},
197      * {@link FormElement#STYLE_DIAMOND} or {@link FormElement#STYLE_STAR}.
198      * The defaults are <code>STYLE_CHECK</code> for checkboxes and
199      * <code>STYLE_CIRCLE</code> for radiobuttons.
200      * @param checkbox the default style for new checkboxes
201      * @param radiobutton the default style for new radiobuttons
202      * @since 1.1.23
203      */

204     public void setButtonStyle(int checkbox, int radiobutton)
205     {
206         this.check=checkbox;
207     this.radio=radiobutton;
208     setBackgroundStyle(backstyle);
209     }
210
211     public String JavaDoc toString()
212     {
213         Map m = getElements();
214     String JavaDoc s = "<form>\n";
215     for (Iterator i = m.keySet().iterator();i.hasNext();) {
216         String JavaDoc name = (String JavaDoc)i.next();
217         FormElement val = (FormElement)m.get(name);
218         String JavaDoc z = val.toString();
219         int j = Math.min(z.indexOf(' '), z.indexOf('>'));
220         z = z.substring(0,j)+" name=\""+name+"\""+z.substring(j);
221         s+=" "+z+"\n";
222     }
223     return s+"</form>";
224     }
225 }
226
Popular Tags