KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FormCheckbox.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
9 /**
10  * A type of form element representing a Check Box, which can be
11  * either "checked" or "cleared" (on or off), in the same way as
12  * the HTML "checkbox" input type. Unlike Radio Buttons, multiple
13  * checkboxes may be set.
14  * </p><p>
15  * Here's an example showing how to add a set of checkboxes to
16  * the form
17  * </p>
18  * <pre>
19  * FormCheckbox check1 = new FormCheckbox(page, 100,100,110,110);
20  * form.addElement("PowerSteering", check1);
21  * FormCheckbox check2 = new FormCheckbox(page, 100,120,110,130);
22  * form.addElement("ElectricWindows", check2);
23  * FormCheckbox check3 = new FormCheckbox(page, 100,140,110,150);
24  * form.addElement("AirConditioning", check3);
25  * </pre>
26  * <p>and here's how to determine which of those values is checked</p>
27  * <pre>
28  * Form form = pdf.getForm();
29  * FormCheckbox check = (FormCheckbox)form.getElement("PowerSteering");
30  * boolean powersteering = check.getValue();
31  * check = (FormCheckbox)form.getElement("ElectricWindows");
32  * boolean electricwindows = check.getValue();
33  * <i>etc.</i>
34  * </pre>
35  * Unlike HTML, each checkbox is a separate entity, and any "grouping"
36  * of checkboxes is up to the programmer to implement.
37  *
38  * @since 1.1.23
39  */

40 public final class FormCheckbox extends FormElement
41 {
42     FormCheckbox(org.faceless.pdf2.FormCheckbox b)
43     {
44         super(b);
45     }
46
47     /**
48      * Create a new FormCheckbox element. With this constructor the page annotation
49      * must be positioned explicitly by calling the {@link PDFAnnotation#setPage}
50      * and {@link PDFAnnotation#setRectangle} methods.
51      * @since 1.1.26
52      */

53     public FormCheckbox()
54     {
55     this(null,0,0,0,0);
56     }
57
58     /**
59      * Create a new FormCheckbox
60      * @param page the page to place the box on
61      * @param x1 the left-most X co-ordinate of the box
62      * @param y1 the top-most Y co-ordinate of the box
63      * @param x2 the right-most X co-ordinate of the box
64      * @param x2 the bottom-most Y co-ordinate of the box
65      */

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

101     public void setStyle(int style, Color JavaDoc centercolor, PDFStyle background)
102     {
103     char newstyle = org.faceless.pdf2.PDFStyle.FORMRADIOBUTTONSTYLE_CHECK;
104
105     org.faceless.pdf2.PDFStyle backstyle = (background==null ? null : background.style);
106     org.faceless.pdf2.PDFStyle textstyle = new org.faceless.pdf2.PDFStyle();
107     textstyle.setFillColor(centercolor==null ? backstyle==null ? Color.black : backstyle.getLineColor() : centercolor);
108     textstyle.setFormCheckboxStyle(newstyle);
109     textstyle.setFont(new org.faceless.pdf2.StandardFont(org.faceless.pdf2.StandardFont.ZAPFDINGBATS), 0);
110
111     for (int i=0;i<element.getAnnotations().size();i++) {
112         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
113         annot.setTextStyle(textstyle);
114         annot.setBackgroundStyle(backstyle);
115     }
116     }
117
118     /**
119      * Set the value of the checkbox, which can be <code>true</code> for
120      * checked or <code>false</code> for clear.
121      * @param value whether to set or clear the checkbox
122      */

123     public void setValue(boolean value)
124     {
125     String JavaDoc v = value ? element.getAnnotation(0).getValue() : null;
126     ((org.faceless.pdf2.FormCheckbox)element).setValue(v);
127     }
128
129     /**
130      * Return the value of the checkbox. The value is <code>true</code> for
131      * checked or <code>false</code> for clear.
132      * @return whether the checkbox is checked
133      */

134     public boolean getValue()
135     {
136     String JavaDoc v = ((org.faceless.pdf2.FormCheckbox)element).getValue();
137     return v!=null;
138     }
139
140     /**
141      * Set the default value of the checkbox, which can be <code>true</code> for
142      * checked or <code>false</code> for clear. The default value is what the
143      * box is set to when the form is reset.
144      * @param value the default value of the checkbox
145      */

146     public void setDefaultValue(boolean value)
147     {
148     String JavaDoc v = value ? element.getAnnotation(0).getValue() : null;
149     ((org.faceless.pdf2.FormCheckbox)element).setDefaultValue(v);
150     }
151
152     /**
153      * Return the default value of the checkbox. The value is <code>true</code> for
154      * checked or <code>false</code> for clear.
155      * @return whether the checkbox is checked when the form is reset
156      */

157     public boolean getDefaultValue()
158     {
159     String JavaDoc v = ((org.faceless.pdf2.FormCheckbox)element).getDefaultValue();
160     return v!=null;
161     }
162 }
163
Popular Tags