/* * Version: MPL 1.1/GPL 2.0/LGPL 2.1 * * "The contents of this file are subject to the Mozilla Public License * Version 1.1 (the "License"); you may not use this file except in * compliance with the License. You may obtain a copy of the License at * http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS IS" * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the * License for the specific language governing rights and limitations under * the License. * * The Original Code is ICEfaces 1.5 open source software code, released * November 5, 2006. The Initial Developer of the Original Code is ICEsoft * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C) * 2004-2010 ICEsoft Technologies Canada, Corp. All Rights Reserved. * * Contributor(s): _____________________. * * Alternatively, the contents of this file may be used under the terms of * the GNU Lesser General Public License Version 2.1 or later (the "LGPL" * License), in which case the provisions of the LGPL License are * applicable instead of those above. If you wish to allow use of your * version of this file only under the terms of the LGPL License and not to * allow others to use your version of this file under the MPL, indicate * your decision by deleting the provisions above and replace them with * the notice and other provisions required by the LGPL License. If you do * not delete the provisions above, a recipient may use your version of * this file under either the MPL or the LGPL License." */ package com.icesoft.faces.component.gmap; import java.beans.Beans; import java.io.IOException; import java.util.Map; import javax.faces.component.UIPanel; import javax.faces.context.FacesContext; import javax.faces.el.ValueBinding; import com.icesoft.faces.component.CSS_DEFAULT; import com.icesoft.faces.component.ext.taglib.Util; import com.icesoft.faces.context.effects.JavascriptContext; public class GMap extends UIPanel{ public static final String COMPONENT_TYPE = "com.icesoft.faces.GMap"; public static final String DEFAULT_RENDERER_TYPE = "com.icesoft.faces.GMapRenderer"; private static final String DEFAULT_LONGITUDE = "-101.162109375"; private static final String DEFAULT_LATITUDE = "56.46249048388979"; private String longitude; private String latitude; private Integer zoomLevel; private Boolean locateAddress ; private boolean initilized = false; private String address; private String type; private String style = null; private String styleClass = null; private String renderedOnUserRole = null; private transient boolean jsLibraryLoaded = false; public String getRendererType() { return DEFAULT_RENDERER_TYPE; } public String getComponentType() { return COMPONENT_TYPE; } public String getLongitude() { if (longitude != null) { return longitude; } ValueBinding vb = getValueBinding("longitude"); return vb != null ? (String) vb.getValue(getFacesContext()) : DEFAULT_LONGITUDE; } public String getLatitude() { if (latitude != null) { return latitude; } ValueBinding vb = getValueBinding("latitude"); return vb != null ? (String) vb.getValue(getFacesContext()) : DEFAULT_LATITUDE; } public void setLongitude(String longitude) { this.longitude = longitude; //TODO need to be changed in the processUpdate. However this property is not //involved in any validator or convertor so it will not harm ValueBinding vb = getValueBinding("longitude"); if (vb != null) { vb.setValue(getFacesContext(), longitude); this.longitude = null; } } public void setLatitude(String latitude) { this.latitude = latitude; //TODO need to be changed in the processUpdate. However this property is not //involved in any validator or convertor so it will not harm ValueBinding vb = getValueBinding("latitude"); if (vb != null) { vb.setValue(getFacesContext(), latitude); this.latitude = null; } } public void decode(FacesContext facesContext) { Map map = facesContext.getExternalContext().getRequestParameterMap(); String clientId = getClientId(facesContext); if (map.get(clientId+ "event") != null && map.get(clientId+ "event") .toString().length() > 0) { if (map.containsKey(clientId + "lat")) { setLatitude(String.valueOf(map.get(clientId + "lat"))); } if(map.containsKey(clientId + "lng")){ setLongitude(String.valueOf(map.get(clientId + "lng"))); } if(map.containsKey(clientId + "zoom")){ setZoomLevel(Integer.valueOf(String. valueOf(map.get(clientId + "zoom"))).intValue()); } if(map.containsKey(clientId + "type")){ setType(String.valueOf(map.get(clientId + "type"))); } } } public void encodeBegin(FacesContext context) throws IOException { loadJsLibrary(context); super.encodeBegin(context); //assume it page refresh/redirect if(context.getExternalContext().getRequestParameterMap() != null && context.getExternalContext().getRequestParameterMap().size() <= 1) { initilized = false; } if ((isLocateAddress() || !initilized) && (getAddress() != null && getAddress().length() > 2)) { JavascriptContext.addJavascriptCall(context, "Ice.GoogleMap.locateAddress('"+ getClientId(context)+"', '"+ getAddress() +"');"); initilized = true; } else { if (isLocatedByGeocoder(context)) { JavascriptContext.addJavascriptCall(context, "Ice.GoogleMap.getGMapWrapper('"+ getClientId(context)+ "').getRealGMap().setZoom("+ getZoomLevel() +");"); } else { String latitude = getLatitude(); String longitude = getLongitude(); try { Float.parseFloat(latitude); Float.parseFloat(longitude); } catch (NumberFormatException e) { latitude = DEFAULT_LATITUDE; longitude = DEFAULT_LONGITUDE; } JavascriptContext.addJavascriptCall(context, "Ice.GoogleMap.getGMapWrapper('"+ getClientId(context)+ "').getRealGMap().setCenter(new GLatLng("+ latitude +", "+ longitude +"), "+ getZoomLevel() +");"); } } JavascriptContext.addJavascriptCall(context, "Ice.GoogleMap.setMapType('"+ getClientId(context)+"', '"+ getType() +"');"); } private void loadJsLibrary(FacesContext context) { if (jsLibraryLoaded) return; String key = context.getCurrentInstance().getExternalContext().getInitParameter("com.icesoft.faces.gmapKey"); if(key != null) { JavascriptContext.includeLib("http://maps.google.com/maps?file=api&v=2&key="+ key, FacesContext.getCurrentInstance()); jsLibraryLoaded = true; } else { //log you must need to define googlemap key in web.xml } } public int getZoomLevel() { if (zoomLevel != null) { return zoomLevel.intValue(); } ValueBinding vb = getValueBinding("zoomLevel"); return vb != null ? ((Integer) vb.getValue(getFacesContext())).intValue() : 3; } public void setZoomLevel(int zoomLevel) { this.zoomLevel = new Integer(zoomLevel); } private boolean isLocatedByGeocoder(FacesContext context) { Object event = context.getExternalContext().getRequestParameterMap() .get(getClientId(context)+ "event"); if (event != null && "geocoder".equals(event)) { return true; } else { return false; } } public boolean isLocateAddress() { if (locateAddress != null) { return locateAddress.booleanValue(); } ValueBinding vb = getValueBinding("locateAddress"); return vb != null ? ((Boolean) vb.getValue(getFacesContext())).booleanValue() : false; } public void setLocateAddress(boolean locateAddress) { this.locateAddress = new Boolean(locateAddress); } public String getAddress() { if (address != null) { return address; } ValueBinding vb = getValueBinding("address"); return vb != null ? (String) vb.getValue(getFacesContext()) : null; } public void setAddress(String address) { this.address = address; } public String getType() { if (type != null) { return type; } ValueBinding vb = getValueBinding("type"); return vb != null ? (String) vb.getValue(getFacesContext()) : "Map"; } public void setType(String type) { this.type = type; } /** *
Set the value of the renderedOnUserRole property.
Return the value of the renderedOnUserRole property.
Return the value of the rendered property.
Set the value of the style property.
Return the value of the style property.
Set the value of the styleClass property.
Return the value of the styleClass property.