KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lamatek > servlets > google > DefaultEventListenerServlet


1 package com.lamatek.servlets.google;
2
3 import java.io.IOException JavaDoc;
4
5 import javax.servlet.ServletException JavaDoc;
6 import javax.servlet.ServletRequest JavaDoc;
7 import javax.servlet.http.HttpServlet JavaDoc;
8 import javax.servlet.http.HttpServletRequest JavaDoc;
9 import javax.servlet.http.HttpServletResponse JavaDoc;
10
11 import com.lamatek.tags.google.GoogleMapEventHandler;
12 import com.lamatek.tags.google.GoogleMapTag;
13
14 /**
15  * DefaultEventListenerServlet
16  *
17  * This class is the parent class for all servlet based event listeners
18  * for the <googlemaps> JSP Taglibrary. It provides support for all the
19  * events available. When declaring <googlemaps:event> tags in your
20  * map, pass the URL to your subclass of DefaultEventListenerServlet as
21  * the url attribute.
22  *
23  * Users extending this class should override the methods for the event
24  * types they're interested in. It is also recommended that the user
25  * first call super.processXXXEvent() for the method to ensure standard
26  * map behaviour.
27  *
28  * The standard process method is called after all event based centric methods
29  * have been called and gives the user the opportunity to handle other non-event
30  * based interactions in the same class. Using this method, it is quite possible
31  * to cleanly handle the interactions for an entire application in one handler.
32  *
33  * Users may also opt to use the JavaBean event listening.
34  * @see com.lamatek.beans.google.DefaultEventListenerBean
35  *
36  * @author Tom Cole
37  * @version 0.40
38  */

39 public class DefaultEventListenerServlet extends HttpServlet JavaDoc implements GoogleMapEventHandler {
40
41     /**
42      * Overrides doGet from HttpServlet.
43      *
44      * Users should <em>not</em> override this method. Rather override
45      * the method that handles the event(s) you're interested in. You can
46      * handle non-event related requests through the process() method.
47      *
48      * @param request HttpServletRequest
49      * @param response HttpServletResponse
50      * @throws ServletException
51      * @throws IOException
52      */

53     public void doGet(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response)
54             throws ServletException JavaDoc, IOException JavaDoc {
55         String JavaDoc event = request.getParameter("event");
56         if (event != null) {
57             String JavaDoc mapName = request.getParameter("map");
58             if (mapName != null) {
59                 Object JavaDoc o = null;
60                 o = request.getSession().getAttribute(mapName);
61                 if (o != null) {
62                     GoogleMapTag map = (GoogleMapTag) o;
63                     if (event.equals("click")) {
64                         if (request.getParameter("component") == null) {
65                             double latitude = -181.0d;
66                             double longitude = -181.0d;
67                             try {
68                                 latitude = Double.parseDouble(request.getParameter("latitude"));
69                                 longitude = Double.parseDouble(request.getParameter("longitude"));
70                             }
71                             catch(Exception JavaDoc ex) {
72                                 
73                             }
74                             if ((longitude > -181.0 && latitude > -181))
75                                 processClickEvent(map, longitude, latitude);
76                         }
77                         else {
78                             processOverlayClickEvent(map, request.getParameter("component"), request.getParameter("type"));
79                         }
80                     }
81                     else if (event.equals("dblclick")) {
82                         if (request.getParameter("component") == null) {
83                             double latitude = -181.0d;
84                             double longitude = -181.0d;
85                             try {
86                                 latitude = Double.parseDouble(request.getParameter("latitude"));
87                                 longitude = Double.parseDouble(request.getParameter("longitude"));
88                             }
89                             catch(Exception JavaDoc ex) {
90                                 
91                             }
92                             if ((longitude > -181.0 && latitude > -181))
93                                 processDoubleClickEvent(map, longitude, latitude);
94                         }
95                         else {
96                             processOverlayDoubleClickEvent(map, request.getParameter("component"), request.getParameter("type"));
97                         }
98                     }
99                     else if (event.equals("zoom")) {
100                         int zoom = -1;
101                         try {
102                             zoom = Integer.parseInt(request.getParameter("zoom"));
103                         }
104                         catch(Exception JavaDoc ex) {
105                             
106                         }
107                         if (zoom >= 0)
108                             processZoomEvent(map, zoom);
109                     }
110                     else if (event.equals("moveend")) {
111                         double latitude = -181.0d;
112                         double longitude = -181.0d;
113                         try {
114                             latitude = Double.parseDouble(request.getParameter("latitude"));
115                             longitude = Double.parseDouble(request.getParameter("longitude"));
116                         }
117                         catch(Exception JavaDoc ex) {
118                             
119                         }
120                         if ((longitude > -181.0 && latitude > -181))
121                             processMoveEndEvent(map, longitude, latitude);
122                     }
123                     else if (event.equals("maptypechanged")) {
124                         String JavaDoc type = request.getParameter("type");
125                         if (type != null)
126                             processMapTypeChangedEvent(map, type);
127                     }
128                     process(map, request);
129                 }
130                 else {
131                     System.out.println("Unable to load map named " + mapName);
132                 }
133             }
134             else {
135                 System.out.println("Unable to locate target map name.");
136             }
137         }
138         else {
139             System.out.println("No event found to process.");
140         }
141     }
142     /**
143      * This method is called after all the event based methods have been called.
144      * Developers can use this method to handle non-event based requests.
145      *
146      * The default implementation is empty to there is no need to call
147      * super.process().
148      *
149      * @param map The GoogleMapTag that generated the event.
150      * @param request The original ServletRequest.
151      */

152     public void process(GoogleMapTag map, ServletRequest JavaDoc request) {
153         
154     }
155     /**
156      * This method is called if a click event is detected that was generated
157      * by an overlay (box, circle, marker, polygon, polyline).
158      *
159      * The default implementation is empty so there is no need to call
160      * super.processMarkerClickEvent().
161      *
162      * @param map The GoogleMapTag parent of the marker clicked.
163      * @param id The id of the overlay that was clicked.
164      * @param type The type of overlay clicked (box, circle, marker, polygon, polyline)
165      */

166     public void processOverlayClickEvent(GoogleMapTag map, String JavaDoc id, String JavaDoc type) {
167         
168     }
169     /**
170      * This method is called when a click event is detected that was generated
171      * by a GoogleMapTag (&lt;googlemaps:map>).
172      *
173      * The default implementation of this method is empty so there is no need
174      * to call super.processClickEvent().
175      *
176      * @param map The GoogleMapTag that generated the event.
177      * @param longitude The longitude (in decimal form) where the click occured.
178      * @param latitude The latitude (in decimal form) where the click occured.
179      */

180     public void processClickEvent(GoogleMapTag map, double longitude, double latitude) {
181         
182     }
183     /**
184      * This method is called when a double-click event is detected that was
185      * generated by an overlay (box, circle, marker, polygon, polyline).
186      *
187      * The default implementation of this method is empty so there is no need
188      * to call super.processMarkerDoubleClickEvent().
189      *
190      * @param map The GoogleMapTag parent of the marker double-clicked.
191      * @param id The id of the overlay double-clicked.
192      * @param type The type of overlay double-clicked (box, circle, marker, polygon, polyline)
193      */

194     public void processOverlayDoubleClickEvent(GoogleMapTag map, String JavaDoc id, String JavaDoc type) {
195         
196     }
197     /**
198      * This method is called when a double-click event is detected that was
199      * generated by a GoogleMapTag (&lt;googlemaps:map>).
200      *
201      * The default implementation of this method is empty so there is no
202      * need to call super.processDoubleClickEvent().
203      *
204      * @param map The GoogleMapTag that generated the event.
205      * @param longitude The longitude (in decimal form) where the double-click occured.
206      * @param latitude The latitude (in decimal form) where the double-click occured.
207      */

208     public void processDoubleClickEvent(GoogleMapTag map, double longitude, double latitude) {
209         
210     }
211     /**
212      * This method is called when a zoom event is detected.
213      *
214      * The default implementation of this method simulates the default behaviour of
215      * a Google Map and any subclasses should call super.processZoomEvent() to
216      * ensure consistent behaviour.
217      *
218      * @param map The GoogleMapTag that generated the event.
219      * @param zoom The new requested zoom level.
220      */

221     public void processZoomEvent(GoogleMapTag map, int zoom) {
222         map.setZoom(zoom);
223     }
224     /**
225      * This method is called when a moveend event is detected.
226      *
227      * The default implementation of this method simulates the default behaviour of
228      * a Google Map and any subclasses should call super.processMoveEndEvent() to
229      * ensure consistent behaviour.
230      *
231      * @param map The GoogleMapTag that generated the event.
232      * @param longitude The new center longitude (in decimal form).
233      * @param latitude The new center latitude (in decimal form).
234      */

235     public void processMoveEndEvent(GoogleMapTag map, double longitude, double latitude) {
236         map.setCenterLatitude(latitude);
237         map.setCenterLongitude(longitude);
238     }
239     /**
240      * This method is called when a maptypechanged event is detected.
241      *
242      * The default implementation of this method simulates the default behaviour of
243      * a Google Map and any subclasses should call super.processMapTypeChangedEvent() to
244      * ensure consistent behaviour.
245      *
246      * @param map The GoogleMapTag that generated the event.
247      * @param type The new requested map type (map | satellite | hybrid).
248      */

249     public void processMapTypeChangedEvent(GoogleMapTag map, String JavaDoc type) {
250         map.setType(type);
251     }
252 }
253
Popular Tags