KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > forms > formmodel > ImageMap


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
package org.apache.cocoon.forms.formmodel;
16
17 import org.apache.cocoon.environment.Request;
18 import org.apache.cocoon.forms.FormContext;
19 import org.apache.cocoon.forms.event.*;
20 import org.apache.cocoon.xml.AttributesImpl;
21
22 /**
23  * A server-side map widget. An ImageMap widget can cause a {@link ImageMapEvent} to be triggered
24  * on the server side, which will be handled by either the event handlers defined in the
25  * form definition, and/or by the {@link org.apache.cocoon.forms.event.FormHandler FormHandler}
26  * registered with the form, if any. An ImageMap widget is basically an Action widget
27  * displayed as an image and with mouse coordinates stored upon clicking.
28  * The image's URI can be set or get, or bind via the binding framework, mouse coordinates
29  * can be either retrieved from the ImageMapEvent triggered or from the widget itself.
30  *
31  * @version $Id$
32  * @since 2.1.8
33  */

34 public class ImageMap extends AbstractWidget implements ActionListenerEnabled {
35
36     private final ImageMapDefinition definition;
37     private ActionListener listener;
38     private String JavaDoc imgURI; // URI of widget's image
39
private int x; // Mouse x coordinate
40
private int y; // Mouse y coordinate
41

42     // XML element and attributes
43
public static final String JavaDoc COMMAND_AT = "command";
44     public static final String JavaDoc VALUE_EL = "imageuri";
45     public static final String JavaDoc ONACTION_EL = "on-action";
46     public static final String JavaDoc IMAGEMAP_EL = "imagemap";
47
48     public ImageMap(ImageMapDefinition definition) {
49         super(definition);
50         this.definition = definition;
51         this.imgURI= definition.getImageURI();
52         this.x= 0;
53         this.y= 0;
54     }
55
56     public WidgetDefinition getDefinition() {
57         return this.definition;
58     }
59
60     // Retrieves mouse coordinates
61
public int getX() {
62         return this.x;
63     }
64     
65     public int getY() {
66         return this.y;
67     }
68     
69     // Get/set image URI
70
public String JavaDoc getImageURI() {
71         if ( this.imgURI != null ) {
72             return this.imgURI;
73         } else {
74             return "";
75         }
76     }
77
78     public void setImageURI(String JavaDoc newImgURI) {
79         this.imgURI= newImgURI;
80     }
81
82     // The set/getValue methods are used to set the widget's image URI during binding
83
public void setValue(Object JavaDoc newImgURI) {
84         this.setImageURI(newImgURI.toString());
85     }
86
87     public Object JavaDoc getValue() {
88         return this.getImageURI();
89     }
90
91     public void readFromRequest(final FormContext formContext) {
92         if (!getCombinedState().isAcceptingInputs())
93             return;
94
95         Form form = getForm();
96
97         // Set the submit widget if we can determine it from the request
98
String JavaDoc fullId = getRequestParameterName();
99         Request request = formContext.getRequest();
100
101         // Extracts mouse coordinates from request (ignores malformed numbers)
102
try {
103             this.x= (new Integer JavaDoc(formContext.getRequest().getParameter(fullId + ".x"))).intValue();
104             this.y= (new Integer JavaDoc(formContext.getRequest().getParameter(fullId + ".y"))).intValue();
105         } catch (java.lang.NumberFormatException JavaDoc e) {
106             this.x= 0;
107             this.y= 0;
108         }
109         
110         String JavaDoc value = request.getParameter(fullId);
111         if (value != null && value.length() > 0) {
112             form.setSubmitWidget(this);
113
114         } else {
115             // Special workaround an IE bug for <input type="image" name="foo"> :
116
// in that case, IE only sends "foo.x" and "foo.y" and not "foo" whereas
117
// standards-compliant browsers such as Mozilla do send the "foo" parameter.
118
//
119
// Note that since actions are terminal widgets, there's no chance of conflict
120
// with a child "x" or "y" widget.
121
value = request.getParameter(fullId + ".x");
122             if ((value != null) && value.length() > 0) {
123                 form.setSubmitWidget(this);
124             }
125         }
126
127         if (form.getSubmitWidget() == this) {
128             form.addWidgetEvent(new ImageMapEvent(this, definition.getActionCommand()));
129
130             handleActivate();
131         }
132     }
133
134     /**
135      * Adds the @imageuri attribute to the XML element
136      */

137     public AttributesImpl getXMLElementAttributes() {
138         AttributesImpl attrs = super.getXMLElementAttributes();
139         attrs.addCDATAAttribute("imageuri", this.imgURI);
140         return attrs;
141     }
142     
143     /**
144      * Handle the fact that this action was activated. The default here is to end the
145      * current form processing and redisplay the form, which means that actual behaviour
146      * should be implemented in event listeners.
147      */

148     protected void handleActivate() {
149         getForm().endProcessing(true);
150     }
151
152     /**
153      * Always return <code>true</code> (an action has no validation)
154      */

155     public boolean validate() {
156         return true;
157     }
158
159     public String JavaDoc getXMLElementName() {
160         return IMAGEMAP_EL;
161     }
162
163     /**
164      * Adds an ActionListener to this widget instance. Listeners defined
165      * on the widget instance will be executed in addtion to any listeners
166      * that might have been defined in the widget definition.
167      */

168     public void addActionListener(ActionListener listener) {
169         this.listener = WidgetEventMulticaster.add(this.listener, listener);
170     }
171
172     public void removeActionListener(ActionListener listener) {
173         this.listener = WidgetEventMulticaster.remove(this.listener, listener);
174     }
175
176     private void fireActionEvent(ActionEvent event) {
177         if (this.listener != null) {
178             this.listener.actionPerformed(event);
179         }
180     }
181
182     public void broadcastEvent(WidgetEvent event) {
183         if (event instanceof ActionEvent) {
184             this.definition.fireActionEvent((ActionEvent)event);
185             fireActionEvent((ActionEvent)event);
186         } else {
187             // Other kinds of events
188
super.broadcastEvent(event);
189         }
190     }
191
192 }
193
Popular Tags