KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package org.faceless.pdf;
4
5 import java.util.*;
6 import java.awt.Color JavaDoc;
7
8 /**
9  * A type of form element representing a list of values in the form
10  * of a scrollable or drop-down list - the same as an HTML <code>&lt;select&gt;</code>
11  * </p><p>
12  * There are three different types of choice field - a "scrollable" list, which
13  * as the name suggests appears as a scrolling window over a list of values; a
14  * "dropdown" list, where the list looks like a drop-down menu, or a "combo",
15  * which is identical to a dropdown list but allows the value to be typed in directly
16  * as well as being selected from the list.
17  * </p><p>
18  * Each drop down list may have one or more values, which are set by adding
19  * values to the {@link java.util.Map} returned by {@link #getOptions}. The
20  * values are displayed in the order they're added to the map.
21  * </p><p>
22  * Here's an example creating a simple list of values:
23  * </p>
24  * <pre>
25  * Form form = pdf.getForm();
26  * FormChoice colors = new FormChoice(FormChoice.TYPE_SCROLLABLE, page, 100,100,300,300);
27  * Map vals = colors.getOptions();
28  * vals.put("Red", null);
29  * vals.put("Green", null);
30  * vals.put("Blue", null);
31  * colors.setValue("Green");
32  * form.addElement("FavoriteColor", colors);
33  * </pre>
34  * <p>and here's an example showing how to retrieve the value of an element</p>
35  * <pre>
36  * Form form = pdf.getForm();
37  * FormChoice choice = (FormChoice)form.getElement("FavoriteColor");
38  * String value = choice.getValue();
39  * </pre>
40  *
41  * @since 1.1.23
42  */

43 public final class FormChoice extends FormElement
44 {
45     FormChoice(org.faceless.pdf2.FormChoice b)
46     {
47         super(b);
48     }
49
50     /**
51      * A type passed to the constructor representing a dropdown list,
52      * similar to a drop-down menu
53      */

54     public static final int TYPE_DROPDOWN=131272;
55
56     /**
57      * A type passed to the constructor representing a scollable list, which
58      * displays one or more lines at once.
59      */

60     public static final int TYPE_SCROLLABLE=0;
61
62     /**
63      * A type passed to the constructor representing a dropdown list where
64      * the value can also be edited like a text field.
65      */

66     public static final int TYPE_COMBO=393216;
67
68     /**
69      * Create a new FormChoice element. With this constructor the page annotation
70      * must be positioned explicitly by calling the {@link PDFAnnotation#setPage}
71      * and {@link PDFAnnotation#setRectangle} methods.
72      * @param type one of {@link #TYPE_DROPDOWN}, {@link #TYPE_SCROLLABLE} or {@link #TYPE_COMBO}
73      * @since 1.1.26
74      */

75     public FormChoice(int type)
76     {
77     this(null, type, 0,0,0,0);
78     }
79
80     /**
81      * Create a new FormChoice
82      * @param page the page to place the field on
83      * @param type one of {@link #TYPE_DROPDOWN}, {@link #TYPE_SCROLLABLE} or {@link #TYPE_COMBO}
84      * @param x1 the left-most X co-ordinate of the field
85      * @param y1 the top-most Y co-ordinate of the field
86      * @param x2 the right-most X co-ordinate of the field
87      * @param x2 the bottom-most Y co-ordinate of the field
88      */

89     public FormChoice(PDFPage page, int type, float x1, float y1, float x2, float y2)
90     {
91     super(new org.faceless.pdf2.FormChoice(type==TYPE_COMBO ? org.faceless.pdf2.FormChoice.TYPE_COMBO : (type==TYPE_SCROLLABLE ? org.faceless.pdf2.FormChoice.TYPE_SCROLLABLE : org.faceless.pdf2.FormChoice.TYPE_DROPDOWN), page==null ? null : page.page, x1, y1, x2, y2));
92     }
93
94     /**
95      * <p>
96      * Set the style of the field. The two parameters control the
97      * style of the text and the background style of the field. The text
98      * style must be specified, and must define a font and a fill color
99      * (the font should be a {@link StandardFont} for Acrobat 4 compatibility,
100      * or a {@link TrueTypeFont} or {@link StandardCJKFont} for Acrobat 5),
101      * and the background style may specify a line and/or fill color, or may
102      * be <code>null</code> to use the defaults.
103      * </p>
104      * @param text the style to draw the text of the button in
105      * @param background the style to draw the background of the button in
106      * @throws IllegalArgumentException if text is <tt>null</tt> or doesn't
107      * meet the criteria above
108      * @see #getOptions
109      * @see #getValue
110      * @see PDFStyle#setFormStyle
111      */

112     public void setStyle(PDFStyle text, PDFStyle background)
113         throws IllegalArgumentException JavaDoc
114     {
115     List l = element.getAnnotations();
116     for (int i=0;i<l.size();i++) {
117         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
118         annot.setTextStyle(text==null ? null : text.style);
119         annot.setBackgroundStyle(background==null ? null : background.style);
120     }
121     }
122
123     /**
124      * Return the type of choice field this object represents -
125      * one of {@link #TYPE_SCROLLABLE}, {@link #TYPE_DROPDOWN} or {@link #TYPE_COMBO}
126      * @return the type of choice field
127      */

128     public int getType()
129     {
130     int style = ((org.faceless.pdf2.FormChoice)element).getType();
131
132     int oldtype=TYPE_DROPDOWN;
133     if (style==org.faceless.pdf2.FormChoice.TYPE_COMBO) oldtype=TYPE_COMBO;
134     if (style==org.faceless.pdf2.FormChoice.TYPE_SCROLLABLE) oldtype=TYPE_SCROLLABLE;
135     return oldtype;
136     }
137
138     /**
139      * <p>
140      * Return a <code>Map</code> listing the values available in this
141      * choice field.
142      * </p><p>
143      * A <code>Map</code> contains keys and their corresponding values,
144      * which is the way the choice fields are done in PDF (and HTML too). They
145      * key is displayed to the user, and the value, if specified, is what's
146      * included when the form is submitted. So, for example, the line
147      * <code>choice.getOptions().put("Red", "1")</code> will display the value
148      * "Red" on screen, but send the value of "1" when the form is submitted.
149      * If the submitted value should be the same as the displayed value, just
150      * do <code>choice.getOptions().put("Red", null)</code>.
151      * </p><p>
152      * Values are displayed in the field in the order that they are added to
153      * the list, <code>key</code> may not be null, and both key and value must
154      * be Strings, otherwise an {@link java.lang.IllegalArgumentException} is thrown
155      * when the values are added to the list.
156      * </p>
157      * @return a Map which should be modified to set the values of the choice field
158      */

159     public Map getOptions()
160     {
161     return ((org.faceless.pdf2.FormChoice)element).getOptions();
162     }
163
164     /**
165      * <p>
166      * Set the value of the choice field. This is the currently selected entry, and
167      * must be in the list of all values returned from {@link #getOptions} unless
168      * the field is a combo-type field. The value may be <code>null</code> to select
169      * no entry at all.
170      * </p>
171      * @param value the value to set the choice field to
172      */

173     public void setValue(String JavaDoc value)
174     {
175     ((org.faceless.pdf2.FormChoice)element).setValue(value);
176     }
177
178     /**
179      * Return the current value of the choice field if set, or <code>null</code>
180      * otherwise.
181      * @return the value of the field
182      * @see #setValue
183      */

184     public String JavaDoc getValue()
185     {
186     return ((org.faceless.pdf2.FormChoice)element).getValue();
187     }
188
189     /**
190      * Set the default value of the choice field, which is what the
191      * value is set to if the form is reset. A default value is optional.
192      * The value may be <code>null</code> to specify no default
193      * @param value the default value of the field
194      */

195     public void setDefaultValue(String JavaDoc value)
196     {
197     ((org.faceless.pdf2.FormChoice)element).setDefaultValue(value);
198     }
199
200     /**
201      * Return the default value of the choice field if set, or <code>null</code>
202      * otherwise.
203      * @return the default value of the field
204      * @see #setDefaultValue
205      */

206     public String JavaDoc getDefaultValue()
207     {
208     return ((org.faceless.pdf2.FormChoice)element).getDefaultValue();
209     }
210 }
211
Popular Tags