View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.osmap;
23  
24  import java.util.*;
25  
26  import javax.faces.FacesException;
27  import javax.faces.application.ResourceDependency;
28  import javax.faces.context.FacesContext;
29  import javax.faces.event.AjaxBehaviorEvent;
30  import javax.faces.event.BehaviorEvent;
31  import javax.faces.event.FacesEvent;
32  
33  import org.primefaces.event.map.*;
34  import org.primefaces.model.map.LatLng;
35  import org.primefaces.model.map.LatLngBounds;
36  import org.primefaces.model.map.Marker;
37  import org.primefaces.util.ComponentUtils;
38  import org.primefaces.util.Constants;
39  import org.primefaces.util.MapBuilder;
40  
41  @ResourceDependency(library = "primefaces-extensions", name = "leaflet/leaflet.css")
42  @ResourceDependency(library = "primefaces-extensions", name = "leaflet/leaflet.js")
43  @ResourceDependency(library = "primefaces-extensions", name = "osmap/osmap.js")
44  @ResourceDependency(library = "primefaces-extensions", name = "primefaces-extensions.js")
45  public class OSMap extends OSMapBase {
46  
47      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.OSMap";
48  
49      private static final Map<String, Class<? extends BehaviorEvent>> BEHAVIOR_EVENT_MAPPING = MapBuilder.<String, Class<? extends BehaviorEvent>> builder()
50                  .put("overlaySelect", OverlaySelectEvent.class)
51                  .put("overlayDblSelect", OverlaySelectEvent.class)
52                  .put("stateChange", StateChangeEvent.class)
53                  .put("pointSelect", PointSelectEvent.class)
54                  .put("pointDblSelect", PointSelectEvent.class)
55                  .put("markerDrag", MarkerDragEvent.class)
56                  .build();
57  
58      private static final Collection<String> EVENT_NAMES = BEHAVIOR_EVENT_MAPPING.keySet();
59  
60      @Override
61      public Map<String, Class<? extends BehaviorEvent>> getBehaviorEventMapping() {
62          return BEHAVIOR_EVENT_MAPPING;
63      }
64  
65      @Override
66      public Collection<String> getEventNames() {
67          return EVENT_NAMES;
68      }
69  
70      @Override
71      public void queueEvent(FacesEvent event) {
72          FacesContext context = getFacesContext();
73          Map<String, String> params = context.getExternalContext().getRequestParameterMap();
74          String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
75          String clientId = getClientId(context);
76  
77          if (ComponentUtils.isRequestSource(this, context)) {
78  
79              AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
80              FacesEvent wrapperEvent = null;
81  
82              if ("overlaySelect".equals(eventName) || "overlayDblSelect".equals(eventName)) {
83                  wrapperEvent = new OverlaySelectEvent(this, behaviorEvent.getBehavior(), getModel().findOverlay(params.get(clientId + "_overlayId")));
84              }
85              else if ("stateChange".equals(eventName)) {
86                  String[] centerLoc = params.get(clientId + "_center").split(",");
87                  String[] northeastLoc = params.get(clientId + "_northeast").split(",");
88                  String[] southwestLoc = params.get(clientId + "_southwest").split(",");
89                  int zoomLevel = Integer.parseInt(params.get(clientId + "_zoom"));
90  
91                  LatLng center = new LatLng(Double.parseDouble(centerLoc[0]), Double.parseDouble(centerLoc[1]));
92                  LatLng northeast = new LatLng(Double.parseDouble(northeastLoc[0]), Double.parseDouble(northeastLoc[1]));
93                  LatLng southwest = new LatLng(Double.parseDouble(southwestLoc[0]), Double.parseDouble(southwestLoc[1]));
94  
95                  wrapperEvent = new StateChangeEvent(this, behaviorEvent.getBehavior(), new LatLngBounds(northeast, southwest), zoomLevel, center);
96              }
97              else if ("pointSelect".equals(eventName) || "pointDblSelect".equals(eventName)) {
98                  String[] latlng = params.get(clientId + "_pointLatLng").split(",");
99                  LatLng position = new LatLng(Double.parseDouble(latlng[0]), Double.parseDouble(latlng[1]));
100 
101                 wrapperEvent = new PointSelectEvent(this, behaviorEvent.getBehavior(), position);
102             }
103             else if ("markerDrag".equals(eventName)) {
104                 Marker marker = (Marker) getModel().findOverlay(params.get(clientId + "_markerId"));
105                 double lat = Double.parseDouble(params.get(clientId + "_lat"));
106                 double lng = Double.parseDouble(params.get(clientId + "_lng"));
107                 marker.setLatlng(new LatLng(lat, lng));
108 
109                 wrapperEvent = new MarkerDragEvent(this, behaviorEvent.getBehavior(), marker);
110             }
111 
112             if (wrapperEvent == null) {
113                 throw new FacesException("Component " + getClass().getName() + " does not support event " + eventName + "!");
114             }
115 
116             wrapperEvent.setPhaseId(behaviorEvent.getPhaseId());
117 
118             super.queueEvent(wrapperEvent);
119         }
120         else {
121             super.queueEvent(event);
122         }
123     }
124 }