KickJava   Java API By Example, From Geeks To Geeks.

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


1 // $Id: FormButton.java,v 1.3 2003/10/06 12:40:06 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  * <p>
11  * A type of form element representing a push Button, of the kind used to
12  * trigger an action like submitting or resetting a form.
13  * </p><p>
14  * A buttons value doesn't change when clicked, and it's not included in the
15  * list of fields submitted by the form. Here's an example showing how to add
16  * a pushbutton which submits the form.
17  * </p>
18  * <pre>
19  * Form form = pdf.getForm();
20  * FormButton button = new FormButton(pdf.getLastPage(), 100,100,200,120);
21  * button.setValue("Submit");
22  * button.setAction(PDFAction.formSubmit("/servlet/Submit", PDFAction.METHOD_HTTP_POST));
23  * form.addElement("SubmitButton", button);
24  * </pre>
25  * @since 1.1.23
26  */

27 public final class FormButton extends FormElement
28 {
29     FormButton(org.faceless.pdf2.FormButton b)
30     {
31         super(b);
32     }
33
34     /**
35      * Create a new FormButton element. With this constructor the page annotation
36      * must be positioned explicitly by calling the {@link PDFAnnotation#setPage}
37      * and {@link PDFAnnotation#setRectangle} methods.
38      * @since 1.1.26
39      */

40     public FormButton()
41     {
42     this(null,0,0,0,0);
43     }
44
45     /**
46      * Create a new FormButton.
47      * @param page the page to place the button on
48      * @param x1 the left-most X co-ordinate of the button
49      * @param y1 the top-most Y co-ordinate of the button
50      * @param x2 the right-most X co-ordinate of the button
51      * @param x2 the bottom-most Y co-ordinate of the button
52      */

53     public FormButton(PDFPage page, float x1, float y1, float x2, float y2)
54     {
55     super(new org.faceless.pdf2.FormButton(page==null ? null : page.page, x1, y1, x2, y2));
56     }
57
58     /**
59      * <p>
60      * Set the style of the button. The two parameters control both the
61      * style of the text (or "value") of the button and the background
62      * style of the button. The text style, if not null, must
63      * define a font and a fill color. The background style may specify
64      * a line and/or fill color, as well has having other fields like the
65      * {@link PDFStyle#setLineWeighting} or * {@link PDFStyle#setFormStyle} set.
66      * </p><p>
67      * If an image is also set using the {@link #setImage setImage} method, the
68      * text will be overlaid above the image, unless the text style
69      * {@link PDFStyle#setTextAlign alignment} is set to left or right.
70      * </p>
71      * @param text the style to draw the text of the button in, or <code>null</code>.
72      * @param background the style to draw the background of the button in, or
73      * <code>null</code>
74      * @see #setImage
75      * @see #setValue
76      * @see PDFStyle#setFormStyle
77      */

78     public void setStyle(PDFStyle text, PDFStyle background)
79     {
80     List l = element.getAnnotations();
81     for (int i=0;i<l.size();i++) {
82         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
83         annot.setTextStyle(text==null ? null : text.style);
84         annot.setBackgroundStyle(background==null ? null : background.style);
85     }
86     }
87
88     /**
89      * <p>
90      * Set the image for the button. This is optional, but if set will draw
91      * the specified image in the button on top of whatever background style
92      * has been set by the {@link #setStyle setStyle} method. The image will be
93      * stretched to fill the button, unless the alignment of the text style is
94      * set to left or right.
95      * </p>
96      * @param image the background image to draw on the button, or
97      * <tt>null</tt> to remove the current background image
98      * @see #setStyle
99      */

100     public void setImage(PDFImage image)
101     {
102     List l = element.getAnnotations();
103     for (int i=0;i<l.size();i++) {
104         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
105         annot.setButtonImage(image==null ? null : image.image);
106     }
107     }
108
109     /**
110      * <p>
111      * Set the "value" of the button - the text which is displayed in the
112      * button. This is optional, but if specified the <tt>text style</tt>
113      * passed to the {@link #setStyle setStyle} method must define a font
114      * and a fill color.
115      * </p><p>
116      * The label is positioned in the left, center or right of the button
117      * (depending on the alignment of the text style), and is drawn on a
118      * single line - it must not contain any newlines (<tt>\n</tt>).
119      * </p>
120      * @param label the label to print on the button, or <tt>null</tt> for no label
121      * @throws IllegalArgumentException if the label contains newlines
122      */

123     public void setValue(String JavaDoc label)
124     {
125     List l = element.getAnnotations();
126     for (int i=0;i<l.size();i++) {
127         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
128         annot.setValue(label);
129     }
130     }
131
132     /**
133      * Return the value of the button - the text which is displayed in
134      * the button.
135      * @return the label of the button
136      */

137     public String JavaDoc getValue()
138     {
139     if (element.getAnnotations().size()>0) {
140         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(0);
141         return annot.getValue();
142     } else {
143         return null;
144     }
145     }
146
147     /**
148      * Set the action when the button is clicked. Shorthand for
149      * <code>getAnnotations()[0].setEventAction(PDFAnnotation.EVENT_ONCLICK)</code>
150      * @param action the action to perform when the button is clicked,
151      * or <code>null</code> to remove the current action
152      * @see PDFAnnotation#setEventAction
153      * @see #getAnnotations
154      */

155     public void setAction(PDFAction action)
156     {
157     List l = element.getAnnotations();
158     for (int i=0;i<l.size();i++) {
159         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(i);
160         annot.setAction(org.faceless.pdf2.Event.CLICK, action==null ? null : action.action);
161     }
162     }
163
164     /**
165      * Return the action performed when the button is clicked. Shorthand for
166      * <code>getAnnotations()[0].getEventAction(PDFAnnotation.EVENT_ONCLICK)</code>
167      * @return the action when the button is clicked, or <code>null</code> to remove the
168      * current action
169      * @see PDFAnnotation#getEventAction
170      * @see #getAnnotations
171      */

172     public PDFAction getAction()
173     {
174     if (element.getAnnotations().size()>0) {
175         org.faceless.pdf2.WidgetAnnotation annot = element.getAnnotation(0);
176         return (PDFAction)PeeredObject.getPeer(annot.getAction(org.faceless.pdf2.Event.CLICK));
177     } else {
178         return null;
179     }
180     }
181 }
182
Popular Tags