KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > lamatek > beans > google > DefaultEventListenerBean


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

36 public class DefaultEventListenerBean implements GoogleMapEventHandler {
37
38     /**
39      * The pageContext that contains the request information.
40      */

41     private PageContext JavaDoc page;
42     /**
43      * The scope identifier for the GoogleMapTag.
44      */

45     private String JavaDoc scope = "site";
46     
47     /**
48      * Sets the pageContext. Developers should not override this method.
49      * If users override this method they <em>must</em> call super.setPageContext()
50      * or else this bean will not function as expected.
51      *
52      * @param page The PageContext that loads this bean.
53      */

54     public void setPageContext(PageContext JavaDoc page) {
55         this.page = page;
56         processRequest(page.getRequest());
57     }
58     /**
59      * Returns the pageContext responsible for this bean.
60      *
61      * @return PageContext
62      */

63     public PageContext JavaDoc getPageContext() {
64         return page;
65     }
66     /**
67      * Processes the request and forwards it to the
68      * appropriate event handler methods. Once all
69      * events have been processes the requests is handed
70      * to the process() method.
71      *
72      * @param request The ServletRequest used to load this bean.
73      */

74     private void processRequest(ServletRequest JavaDoc request) {
75         String JavaDoc event = request.getParameter("event");
76         if (event != null) {
77             String JavaDoc mapName = request.getParameter("map");
78             if (mapName != null) {
79                 Object JavaDoc o = null;
80                 o = page.getSession().getAttribute(mapName);
81                 if (o == null) {
82                     o = page.getAttribute(mapName);
83                     scope = "page";
84                 }
85                 if (o != null) {
86                     GoogleMapTag map = (GoogleMapTag) o;
87                     if (event.equals("click")) {
88                         if (request.getParameter("component") == null) {
89                             double latitude = -181.0d;
90                             double longitude = -181.0d;
91                             try {
92                                 latitude = Double.parseDouble(request.getParameter("latitude"));
93                                 longitude = Double.parseDouble(request.getParameter("longitude"));
94                             }
95                             catch(Exception JavaDoc ex) {
96                                 
97                             }
98                             if ((longitude > -181.0 && latitude > -181))
99                                 processClickEvent(map, longitude, latitude);
100                         }
101                         else {
102                             processOverlayClickEvent(map, request.getParameter("component"), request.getParameter("type"));
103                         }
104                     }
105                     else if (event.equals("dblclick")) {
106                         if (request.getParameter("component") == null) {
107                             double latitude = -181.0d;
108                             double longitude = -181.0d;
109                             try {
110                                 latitude = Double.parseDouble(request.getParameter("latitude"));
111                                 longitude = Double.parseDouble(request.getParameter("longitude"));
112                             }
113                             catch(Exception JavaDoc ex) {
114                                 
115                             }
116                             if ((longitude > -181.0 && latitude > -181))
117                                 processDoubleClickEvent(map, longitude, latitude);
118                         }
119                         else {
120                             processOverlayDoubleClickEvent(map, request.getParameter("component"), request.getParameter("type"));
121                         }
122                     }
123                     else if (event.equals("zoom")) {
124                         int zoom = -1;
125                         try {
126                             zoom = Integer.parseInt(request.getParameter("zoom"));
127                         }
128                         catch(Exception JavaDoc ex) {
129                             
130                         }
131                         if (zoom >= 0)
132                             processZoomEvent(map, zoom);
133                     }
134                     else if (event.equals("moveend")) {
135                         double latitude = -181.0d;
136                         double longitude = -181.0d;
137                         try {
138                             latitude = Double.parseDouble(request.getParameter("latitude"));
139                             longitude = Double.parseDouble(request.getParameter("longitude"));
140                         }
141                         catch(Exception JavaDoc ex) {
142                             
143                         }
144                         if ((longitude > -181.0 && latitude > -181))
145                             processMoveEndEvent(map, longitude, latitude);
146                     }
147                     else if (event.equals("maptypechanged")) {
148                         String JavaDoc type = request.getParameter("type");
149                         if (type != null)
150                             processMapTypeChangedEvent(map, type);
151                     }
152                     process(map, page.getRequest());
153                 }
154                 else {
155                     System.out.println("Unable to load map named " + mapName);
156                 }
157             }
158             else {
159                 System.out.println("Unable to locate target map name.");
160             }
161         }
162         else {
163             System.out.println("No event found to process.");
164         }
165     }
166     /**
167      * This method is called after all the event based methods have been called.
168      * Developers can use this method to handle non-event based requests.
169      *
170      * The default implementation is empty to there is no need to call
171      * super.process().
172      *
173      * @param map The GoogleMapTag that generated the event.
174      * @param request The original ServletRequest.
175      */

176     public void process(GoogleMapTag map, ServletRequest JavaDoc request) {
177         
178     }
179     /**
180      * This method is called if a click event is detected that was generated
181      * by an overlay (box, circle, marker, polygon, polyline).
182      *
183      * The default implementation is empty so there is no need to call
184      * super.processMarkerClickEvent().
185      *
186      * @param map The GoogleMapTag parent of the marker clicked.
187      * @param id The id of the overlay that was clicked.
188      * @param type The type of overlay clicked (marker, box, circle, polygon, polyline)
189      */

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

204     public void processClickEvent(GoogleMapTag map, double longitude, double latitude) {
205         
206     }
207     /**
208      * This method is called when a double-click event is detected that was
209      * generated by an overlay (box, circle, marker, polygon, polyline).
210      *
211      * The default implementation of this method is empty so there is no need
212      * to call super.processMarkerDoubleClickEvent().
213      *
214      * @param map The GoogleMapTag parent of the marker double-clicked.
215      * @param id The id of the overlay double-clicked.
216      * @param type The type of overlay double-clicked (marker, circle, box, polygon, polyline)
217      */

218     public void processOverlayDoubleClickEvent(GoogleMapTag map, String JavaDoc id, String JavaDoc type) {
219         
220     }
221     /**
222      * This method is called when a double-click event is detected that was
223      * generated by a GoogleMapTag (&lt;googlemaps:map>).
224      *
225      * The default implementation of this method is empty so there is no
226      * need to call super.processDoubleClickEvent().
227      *
228      * @param map The GoogleMapTag that generated the event.
229      * @param longitude The longitude (in decimal form) where the double-click occured.
230      * @param latitude The latitude (in decimal form) where the double-click occured.
231      */

232     public void processDoubleClickEvent(GoogleMapTag map, double longitude, double latitude) {
233         
234     }
235     /**
236      * This method is called when a zoom event is detected.
237      *
238      * The default implementation of this method simulates the default behaviour of
239      * a Google Map and any subclasses should call super.processZoomEvent() to
240      * ensure consistent behaviour.
241      *
242      * @param map The GoogleMapTag that generated the event.
243      * @param zoom The new requested zoom level.
244      */

245     public void processZoomEvent(GoogleMapTag map, int zoom) {
246         map.setZoom(zoom);
247     }
248     /**
249      * This method is called when a moveend event is detected.
250      *
251      * The default implementation of this method simulates the default behaviour of
252      * a Google Map and any subclasses should call super.processMoveEndEvent() to
253      * ensure consistent behaviour.
254      *
255      * @param map The GoogleMapTag that generated the event.
256      * @param longitude The new center longitude (in decimal form).
257      * @param latitude The new center latitude (in decimal form).
258      */

259     public void processMoveEndEvent(GoogleMapTag map, double longitude, double latitude) {
260         map.setCenterLatitude(latitude);
261         map.setCenterLongitude(longitude);
262     }
263     /**
264      * This method is called when a maptypechanged event is detected.
265      *
266      * The default implementation of this method simulates the default behaviour of
267      * a Google Map and any subclasses should call super.processMapTypeChangedEvent() to
268      * ensure consistent behaviour.
269      *
270      * @param map The GoogleMapTag that generated the event.
271      * @param type The new requested map type (map | satellite | hybrid).
272      */

273     public void processMapTypeChangedEvent(GoogleMapTag map, String JavaDoc type) {
274         map.setType(type);
275     }
276 }
277
Popular Tags