KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FormRadioButton.java,v 1.4 2003/10/16 15:46:42 mike Exp $
2

3 package org.faceless.pdf;
4
5 import java.io.*;
6 import java.util.*;
7 import java.awt.Color JavaDoc;
8 import org.faceless.util.OrderedMap;
9
10 /**
11  * <p>
12  * A type of form element representing a group of Radio buttons. Unlike
13  * the other <code>FormElements</code>, a Radio Button usually has several
14  * visible annotations on the page - one for each value. These are set
15  * with the {@link #setButton setButton} method.
16  * </p><p>
17  * This example creates a group of four buttons and adds them to the form:
18  * </p>
19  * <pre>
20  * FormRadioButton buttons = new FormRadioButton();
21  * buttons.setButton("Visa", currentpage, 30,100,40,110);
22  * buttons.setButton("Mastercard", currentpage, 30,120,40,130);
23  * buttons.setButton("Amex", currentpage, 30,140,40,150);
24  * buttons.setButton("Diners", currentpage, 30,160,40,170);
25  * pdf.getForm().add("CreditCard", buttons);
26  * </pre>
27  * <p>
28  * This example shows how to set the value of an existing form:
29  * </p>
30  * <pre>
31  * Form form = pdf.getForm();
32  * FormRadioButton buttons = (FormRadioButtons)form.getElement("CreditCard");
33  * buttons.setValue("Mastercard");
34  * </pre>
35  * @since 1.1.23
36  */

37 public final class FormRadioButton extends FormElement
38 {
39     FormRadioButton(org.faceless.pdf2.FormRadioButton b)
40     {
41         super(b);
42     }
43
44     /**
45      * Create a new Radio Button set. The newly created field doesn't
46      * have an appearance on the page until the {@link #setButton}
47      * method is called.
48      */

49     public FormRadioButton()
50     {
51     super(new org.faceless.pdf2.FormRadioButton());
52     }
53
54     /**
55      * Add a new value for this radio button at the specified position.
56      * If the value already exists, it's overwritten.
57      * @param value the value for this radio button
58      * @param page the page to place the button on
59      * @param x1 the left-most X co-ordinate of the button
60      * @param y1 the top-most Y co-ordinate of the button
61      * @param x2 the right-most X co-ordinate of the button
62      * @param x2 the bottom-most Y co-ordinate of the button
63      * @return the annotation created by this method (since 1.2.1 - prior to this it returned void)
64      */

65     public PDFAnnotation setButton(String JavaDoc value, PDFPage page, float x1, float y1, float x2, float y2)
66     {
67         return (PDFAnnotation)PeeredObject.getPeer(((org.faceless.pdf2.FormRadioButton)element).addAnnotation(value, page==null ? null : page.page, x1, y1, x2, y2));
68     }
69
70     /**
71      * <p>
72      * Set the background style of the buttons. The button can have
73      * one of six different appearances, and can be drawn in three colors.
74      * </p><p>
75      * The first parameter, <code>style</code>, must be one of
76      * {@link #STYLE_CIRCLE STYLE_CIRCLE} (the default), {@link #STYLE_SQUARE STYLE_SQUARE},
77      * {@link #STYLE_DIAMOND STYLE_DIAMOND}, {@link #STYLE_STAR}, {@link #STYLE_CHECK STYLE_CHECK} or
78      * {@link #STYLE_CROSS STYLE_CROSS}. The remaining parameters control the color
79      * of the button:
80      * </p>
81      * <ul>
82      * <li>The <code>centercolor</code> is the color of the marker in the
83      * middle of the button that indicates it's selected.</li>
84      * <li>The <code>background</code> parameters FillColor is the color
85      * of the background of the button. It may be null</li>
86      * <li>The <code>background</code> parameters LineColor is the color
87      * of the border of the button. It may be null</li>
88      * </ul>
89      * <p>
90      * Other background style parameters may also be set to further
91      * control the appearance of the button.
92      * </p>
93      *
94      * @param style the style to draw the marker in the center of the button
95      * @param centercolor the color to draw the marker in the center of the button
96      * @param background the style in which to draw the background of the button
97      * @see PDFStyle#setFormStyle
98      * @see Form#setBackgroundStyle
99      */

100     public void setStyle(int style, Color JavaDoc centercolor, PDFStyle background)
101     {
102     char newstyle = org.faceless.pdf2.PDFStyle.FORMRADIOBUTTONSTYLE_CIRCLE;
103
104     org.faceless.pdf2.PDFStyle backstyle = (background==null ? null : background.style);
105     org.faceless.pdf2.PDFStyle textstyle = new org.faceless.pdf2.PDFStyle();
106     textstyle.setFillColor(centercolor==null ? backstyle==null ? Color.black : backstyle.getLineColor() : centercolor);
107     textstyle.setFormRadioButtonStyle(newstyle);
108     textstyle.setFont(new org.faceless.pdf2.StandardFont(org.faceless.pdf2.StandardFont.ZAPFDINGBATS), 0);
109
110     for (int i=0;i<element.getAnnotations().size();i++) {
111         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
112         annot.setTextStyle(textstyle);
113         annot.setBackgroundStyle(backstyle);
114     }
115     }
116
117     /**
118      * Return a Map containing all the values for this RadioButton.
119      * The key of each map is a {@link java.lang.String} which is the
120      * value of the button, and the value is a {@link PDFAnnotation}
121      * which has information on the page and location of that button.
122      * The returned map is unmodifiable - changes to the Map are not
123      * allowed.
124      * @return an unmodifiable <code>Map</code> containing all the annotations
125      * for this RadioButton
126      */

127     public Map getOptions()
128     {
129     Map m = new OrderedMap();
130     List l = element.getAnnotations();
131     for (int i=0;i<l.size();i++) {
132         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
133         m.put(annot.getValue(), PeeredObject.getPeer(annot));
134     }
135     return Collections.unmodifiableMap(m);
136     }
137
138     /**
139      * Set the value of this set of Radio buttons. The button with the
140      * specified value is selected. If no button exists with the specified
141      * value, an <code>IllegalArgumentException</code> is thrown.
142      * @param value the button to select
143      * @throws IllegalArgumentException if no such button exists
144      */

145     public void setValue(String JavaDoc value)
146     {
147     ((org.faceless.pdf2.FormRadioButton)element).setValue(value);
148     }
149
150     /**
151      * Get the value of this set of Radio buttons. The value is whichever
152      * button is selected, or <code>null</code> if no buttons are selected
153      * @return the value of this set of buttons, or <code>null</code> if no value is set
154      */

155     public String JavaDoc getValue()
156     {
157     return ((org.faceless.pdf2.FormRadioButton)element).getValue();
158     }
159
160     /**
161      * Set the defualt value of this set of Radio buttons. The button with the
162      * specified value is selected when the form is reset. If no button exists
163      * with the specified value, an <code>IllegalArgumentException</code> is thrown.
164      * @param value the button to select
165      * @throws IllegalArgumentException if no such button exists
166      */

167     public void setDefaultValue(boolean value)
168     {
169     String JavaDoc v = value ? element.getAnnotation(0).getValue() : null;
170     ((org.faceless.pdf2.FormRadioButton)element).setDefaultValue(v);
171     }
172
173     /**
174      * Get the default value of this set of Radio buttons. The value is whichever
175      * button is selected, or <code>null</code> if no buttons are selected
176      * @return the value of this set of buttons, or <code>null</code> if no value is set
177      */

178     public String JavaDoc getDefaultValue()
179     {
180     return ((org.faceless.pdf2.FormRadioButton)element).getDefaultValue();
181     }
182 }
183
Popular Tags