001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.forms.formmodel;
018:
019: import java.util.Iterator;
020: import java.util.List;
021: import java.util.Locale;
022:
023: import org.apache.cocoon.forms.FormContext;
024: import org.apache.cocoon.forms.FormsConstants;
025: import org.apache.cocoon.forms.event.ValueChangedEvent;
026: import org.apache.cocoon.forms.event.WidgetEvent;
027: import org.apache.cocoon.xml.AttributesImpl;
028:
029: import org.xml.sax.ContentHandler;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * @version $Id: GoogleMap.java 485499 2006-12-11 04:47:28Z crossley $
034: */
035: public class GoogleMap extends AbstractWidget {
036:
037: private static final String GOOGLEMAP_FIELD_EL = "googlemap";
038: private static final String VALUE_EL = "value";
039: private static final String MARKERS_EL = "markers";
040: private static final String MARKER_EL = "marker";
041: private static final String USERMARKER_EL = "usermarker";
042: private static final String MARKER_TEXT_EL = "text";
043:
044: private GoogleMapValue value;
045: private final GoogleMapDefinition definition;
046:
047: public GoogleMap(GoogleMapDefinition definition) {
048: super (definition);
049: this .definition = definition;
050: }
051:
052: public WidgetDefinition getDefinition() {
053: return this .definition;
054: }
055:
056: public void initialize() {
057: GoogleMapValue value = this .definition.getInitialValue();
058: if (value != null) {
059: setValue(value);
060: }
061: super .initialize();
062: }
063:
064: public void readFromRequest(FormContext formContext) {
065: if (!getCombinedState().isAcceptingInputs()) {
066: return;
067: }
068:
069: final String prefix = getRequestParameterName();
070: String paramLng = formContext.getRequest().getParameter(
071: prefix + "_lng");
072: String paramLat = formContext.getRequest().getParameter(
073: prefix + "_lat");
074: String paramZoom = formContext.getRequest().getParameter(
075: prefix + "_zoom");
076: String currentMarker = formContext.getRequest().getParameter(
077: prefix + "_current");
078: String userMarkerLng = formContext.getRequest().getParameter(
079: prefix + "_usermarker-lng");
080: String userMarkerLat = formContext.getRequest().getParameter(
081: prefix + "_usermarker-lat");
082:
083: GoogleMapValue newValue = new GoogleMapValue();
084: newValue.setLng(Float.valueOf(paramLng).floatValue());
085: newValue.setLat(Float.valueOf(paramLat).floatValue());
086: newValue.setZoom(Integer.valueOf(paramZoom).intValue());
087: newValue.setCurrentMarker(Integer.valueOf(currentMarker)
088: .intValue());
089: newValue.setMarkers(this .value.getMarkers());
090:
091: if (userMarkerLng != null && userMarkerLat != null) {
092: try {
093: GoogleMapMarker marker = new GoogleMapMarker();
094: marker
095: .setLng(Float.valueOf(userMarkerLng)
096: .floatValue());
097: marker
098: .setLat(Float.valueOf(userMarkerLat)
099: .floatValue());
100: newValue.setUsermarker(marker);
101: } catch (NumberFormatException e) { /* ignored */
102: }
103: }
104:
105: GoogleMapValue oldValue = this .value;
106: value = newValue;
107:
108: if (!value.equals(oldValue)) {
109: getForm().addWidgetEvent(
110: new ValueChangedEvent(this , oldValue, value));
111: }
112: }
113:
114: /**
115: * @return "googlemap"
116: */
117: public String getXMLElementName() {
118: return GOOGLEMAP_FIELD_EL;
119: }
120:
121: public void generateItemSaxFragment(ContentHandler contentHandler,
122: Locale locale) throws SAXException {
123: // value element
124: AttributesImpl attributesImpl = new AttributesImpl();
125: attributesImpl.addAttribute("", "lng", "lng", "CDATA",
126: this .value.getLng() + "");
127: attributesImpl.addAttribute("", "lat", "lat", "CDATA",
128: this .value.getLat() + "");
129: attributesImpl.addAttribute("", "zoom", "zoom", "CDATA",
130: this .value.getZoom() + "");
131: attributesImpl.addAttribute("", "current", "current", "CDATA",
132: this .value.getCurrentMarker() + "");
133:
134: contentHandler.startElement(FormsConstants.INSTANCE_NS,
135: VALUE_EL, FormsConstants.INSTANCE_PREFIX_COLON
136: + VALUE_EL, attributesImpl);
137:
138: // usermarker
139: if (this .value.getUsermarker() != null) {
140: attributesImpl = new AttributesImpl();
141: attributesImpl.addAttribute("", "lng", "lng", "CDATA",
142: this .value.getUsermarker().getLng() + "");
143: attributesImpl.addAttribute("", "lat", "lat", "CDATA",
144: this .value.getUsermarker().getLat() + "");
145: contentHandler.startElement(FormsConstants.INSTANCE_NS,
146: USERMARKER_EL, FormsConstants.INSTANCE_PREFIX_COLON
147: + USERMARKER_EL, attributesImpl);
148: contentHandler.endElement(FormsConstants.INSTANCE_NS,
149: USERMARKER_EL, FormsConstants.INSTANCE_PREFIX_COLON
150: + USERMARKER_EL);
151: }
152:
153: // markers
154: List markers = this .value.getMarkers();
155: if (markers != null) {
156: contentHandler.startElement(FormsConstants.INSTANCE_NS,
157: MARKERS_EL, FormsConstants.INSTANCE_PREFIX_COLON
158: + MARKERS_EL, new AttributesImpl());
159: for (Iterator iter = markers.iterator(); iter.hasNext();) {
160: GoogleMapMarker marker = (GoogleMapMarker) iter.next();
161: attributesImpl = new AttributesImpl();
162: attributesImpl.addAttribute("", "lng", "lng", "CDATA",
163: marker.getLng() + "");
164: attributesImpl.addAttribute("", "lat", "lat", "CDATA",
165: marker.getLat() + "");
166: contentHandler.startElement(FormsConstants.INSTANCE_NS,
167: MARKER_EL, FormsConstants.INSTANCE_PREFIX_COLON
168: + MARKER_EL, attributesImpl);
169: contentHandler.startElement(FormsConstants.INSTANCE_NS,
170: MARKER_TEXT_EL,
171: FormsConstants.INSTANCE_PREFIX_COLON
172: + MARKER_TEXT_EL, new AttributesImpl());
173: marker.getText().toSAX(contentHandler);
174: contentHandler.endElement(FormsConstants.INSTANCE_NS,
175: MARKER_TEXT_EL,
176: FormsConstants.INSTANCE_PREFIX_COLON
177: + MARKER_TEXT_EL);
178: contentHandler.endElement(FormsConstants.INSTANCE_NS,
179: MARKER_EL, FormsConstants.INSTANCE_PREFIX_COLON
180: + MARKER_EL);
181: }
182: contentHandler.endElement(FormsConstants.INSTANCE_NS,
183: MARKERS_EL, FormsConstants.INSTANCE_PREFIX_COLON
184: + MARKERS_EL);
185: }
186: contentHandler.endElement(FormsConstants.INSTANCE_NS, VALUE_EL,
187: FormsConstants.INSTANCE_PREFIX_COLON + VALUE_EL);
188: }
189:
190: public Object getValue() {
191: return value;
192: }
193:
194: /**
195: * Sets value of the field. If value is null, it is considered to be false
196: * (see class comment).
197: */
198: public void setValue(Object object) {
199: if (!(object instanceof GoogleMapValue)) {
200: throw new RuntimeException(
201: "Cannot set value of googlemap \""
202: + getRequestParameterName()
203: + "\" to a non-GoogleMapValue.");
204: }
205:
206: Object oldValue = value;
207: value = (GoogleMapValue) object;
208: if (!value.equals(oldValue)) {
209: Form form = getForm();
210: form.addWidgetUpdate(this );
211: }
212: }
213:
214: public void broadcastEvent(WidgetEvent event) {
215: if (event instanceof ValueChangedEvent) {
216: //
217: } else {
218: // Other kinds of events
219: super.broadcastEvent(event);
220: }
221: }
222:
223: }
|