001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.gwt.client.ui.input.location;
029
030import org.opencms.gwt.client.Messages;
031import org.opencms.gwt.client.ui.CmsPushButton;
032import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
033import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
034import org.opencms.gwt.client.ui.input.CmsSelectBox;
035import org.opencms.gwt.client.ui.input.CmsSuggestBox;
036import org.opencms.gwt.client.ui.input.CmsTextBox;
037
038import java.util.Map;
039
040import com.google.gwt.core.client.GWT;
041import com.google.gwt.dom.client.Element;
042import com.google.gwt.dom.client.Style;
043import com.google.gwt.dom.client.Style.Display;
044import com.google.gwt.event.dom.client.ClickEvent;
045import com.google.gwt.event.logical.shared.SelectionEvent;
046import com.google.gwt.event.logical.shared.ValueChangeEvent;
047import com.google.gwt.uibinder.client.UiBinder;
048import com.google.gwt.uibinder.client.UiField;
049import com.google.gwt.uibinder.client.UiHandler;
050import com.google.gwt.user.client.ui.Composite;
051import com.google.gwt.user.client.ui.HTMLPanel;
052import com.google.gwt.user.client.ui.Label;
053import com.google.gwt.user.client.ui.SuggestOracle;
054
055/**
056 * The location picker map view.<p>
057 */
058public class CmsLocationPopupContent extends Composite {
059
060    /**
061     * @see com.google.gwt.uibinder.client.UiBinder
062     */
063    protected interface I_CmsLocationPopupContentUiBinder extends UiBinder<HTMLPanel, CmsLocationPopupContent> {
064        // GWT interface, nothing to do here
065    }
066
067    /** The ui-binder instance for this class. */
068    private static I_CmsLocationPopupContentUiBinder uiBinder = GWT.create(I_CmsLocationPopupContentUiBinder.class);
069
070    /** The address suggest box. */
071    @UiField(provided = true)
072    CmsSuggestBox m_addressField;
073
074    /** The address label. */
075    @UiField
076    Label m_addressLabel;
077
078    /** The cancel button. */
079    @UiField
080    CmsPushButton m_cancelButton;
081
082    /** The height field. */
083    @UiField
084    CmsTextBox m_heightField;
085
086    /** The label field. */
087    @UiField
088    CmsTextBox m_latitudeField;
089
090    /** The label label. */
091    @UiField
092    Label m_latitudeLabel;
093
094    /** The longitude field. */
095    @UiField
096    CmsTextBox m_longitudeField;
097
098    /** The longitude label. */
099    @UiField
100    Label m_longitudeLabel;
101
102    /** The map canvas element. */
103    @UiField
104    Element m_mapCanvas;
105
106    /** The mode field. */
107    @UiField(provided = true)
108    CmsSelectBox m_modeField;
109
110    /** The mode label. */
111    @UiField
112    Label m_modeLabel;
113
114    /** The OK button. */
115    @UiField
116    CmsPushButton m_okButton;
117
118    /** The map size label. */
119    @UiField
120    Label m_sizeLabel;
121
122    /** The map type field. */
123    @UiField(provided = true)
124    CmsSelectBox m_typeField;
125
126    /** The map type label. */
127    @UiField
128    Label m_typeLabel;
129
130    /** The width field. */
131    @UiField
132    CmsTextBox m_widthField;
133
134    /** The zoom field. */
135    @UiField(provided = true)
136    CmsSelectBox m_zoomField;
137
138    /** The zoom label. */
139    @UiField
140    Label m_zoomLabel;
141
142    /** The location picker. */
143    private CmsLocationController m_controller;
144
145    /**
146     * Constructor.<p>
147     *
148     * @param controller the location controller
149     * @param addressOracle the address suggest oracle to use for the address suggest box
150     * @param modeItems the available map modes
151     * @param typeItems the available map types
152     * @param zoomItems the available zoom levels
153     */
154    public CmsLocationPopupContent(
155        CmsLocationController controller,
156        SuggestOracle addressOracle,
157        Map<String, String> modeItems,
158        Map<String, String> typeItems,
159        Map<String, String> zoomItems) {
160
161        // create fields annotated as provided before running the ui-binder
162        m_addressField = new CmsSuggestBox(addressOracle);
163        m_modeField = new CmsSelectBox(modeItems);
164        m_typeField = new CmsSelectBox(typeItems);
165        m_zoomField = new CmsSelectBox(zoomItems);
166        initWidget(uiBinder.createAndBindUi(this));
167        m_controller = controller;
168        m_mapCanvas.setId(HTMLPanel.createUniqueId());
169        initLabels();
170        initFields();
171    }
172
173    /**
174     * Displays the location value fields.<p>
175     *
176     * @param value the location value
177     */
178    protected void displayValues(CmsLocationValue value) {
179
180        m_addressField.setTextValue(value.getAddress());
181        m_latitudeField.setFormValueAsString(value.getLatitudeString());
182        m_longitudeField.setFormValueAsString(value.getLongitudeString());
183        m_heightField.setFormValueAsString("" + value.getHeight());
184        m_widthField.setFormValueAsString("" + value.getWidth());
185        m_zoomField.setFormValueAsString("" + value.getZoom());
186        m_modeField.setFormValueAsString(value.getMode());
187        m_typeField.setFormValueAsString(value.getType());
188    }
189
190    /**
191     * Returns the map canvas element.<p>
192     *
193     * @return the map canvas element
194     */
195    protected Element getMapCanvas() {
196
197        return m_mapCanvas;
198    }
199
200    /**
201     * Sets the field visibility.<p>
202     *
203     * @param visible <code>true</code> to show the field
204     */
205    protected void setAddressVisible(boolean visible) {
206
207        Style style = m_addressLabel.getElement().getParentElement().getStyle();
208        if (visible) {
209            style.clearDisplay();
210        } else {
211            style.setDisplay(Display.NONE);
212        }
213    }
214
215    /**
216     * Sets the field visibility.<p>
217     *
218     * @param visible <code>true</code> to show the field
219     */
220    protected void setLatLngVisible(boolean visible) {
221
222        Style styleLat = m_latitudeLabel.getElement().getParentElement().getStyle();
223        Style styleLng = m_longitudeLabel.getElement().getParentElement().getStyle();
224        if (visible) {
225            styleLat.clearDisplay();
226            styleLng.clearDisplay();
227        } else {
228            styleLat.setDisplay(Display.NONE);
229            styleLng.setDisplay(Display.NONE);
230        }
231    }
232
233    /**
234     * Sets the field visibility.<p>
235     *
236     * @param visible <code>true</code> to show the field
237     */
238    protected void setMapVisible(boolean visible) {
239
240        Style style = m_mapCanvas.getStyle();
241        if (visible) {
242            style.clearDisplay();
243        } else {
244            style.setDisplay(Display.NONE);
245        }
246    }
247
248    /**
249     * Sets the field visibility.<p>
250     *
251     * @param visible <code>true</code> to show the field
252     */
253    protected void setModeVisible(boolean visible) {
254
255        Style style = m_modeLabel.getElement().getParentElement().getStyle();
256        if (visible) {
257            style.clearDisplay();
258        } else {
259            style.setDisplay(Display.NONE);
260        }
261    }
262
263    /**
264     * Sets the field visibility.<p>
265     *
266     * @param visible <code>true</code> to show the field
267     */
268    protected void setSizeVisible(boolean visible) {
269
270        Style style = m_sizeLabel.getElement().getParentElement().getStyle();
271        if (visible) {
272            style.clearDisplay();
273        } else {
274            style.setDisplay(Display.NONE);
275        }
276    }
277
278    /**
279     * Sets the field visibility.<p>
280     *
281     * @param visible <code>true</code> to show the field
282     */
283    protected void setTypeVisible(boolean visible) {
284
285        Style style = m_typeLabel.getElement().getParentElement().getStyle();
286        if (visible) {
287            style.clearDisplay();
288        } else {
289            style.setDisplay(Display.NONE);
290        }
291    }
292
293    /**
294     * Sets the field visibility.<p>
295     *
296     * @param visible <code>true</code> to show the field
297     */
298    protected void setZoomVisible(boolean visible) {
299
300        Style style = m_zoomLabel.getElement().getParentElement().getStyle();
301        if (visible) {
302            style.clearDisplay();
303        } else {
304            style.setDisplay(Display.NONE);
305        }
306    }
307
308    /**
309     * On address field value change.<p>
310     *
311     * @param event the change event
312     */
313    @UiHandler("m_addressField")
314    void onAddressChange(ValueChangeEvent<String> event) {
315
316        m_controller.onAddressChange(event.getValue());
317    }
318
319    /**
320     * On address suggest selection.<p>
321     *
322     * @param event the selection event
323     */
324    @UiHandler("m_addressField")
325    void onAddressSelection(SelectionEvent<SuggestOracle.Suggestion> event) {
326
327        m_controller.onAddressChange(event.getSelectedItem());
328    }
329
330    /**
331     * Handles the cancel click.<p>
332     *
333     * @param event the mouse event
334     */
335    @UiHandler("m_cancelButton")
336    void onCancel(ClickEvent event) {
337
338        m_controller.onCancel();
339    }
340
341    /**
342     * On height value change.<p>
343     *
344     * @param event the change event
345     */
346    @UiHandler("m_heightField")
347    void onHeightChange(ValueChangeEvent<String> event) {
348
349        m_controller.onHeightChange(event.getValue());
350    }
351
352    /**
353     * On latitude value change.<p>
354     *
355     * @param event the change event
356     */
357    @UiHandler("m_latitudeField")
358    void onLatitudeChange(ValueChangeEvent<String> event) {
359
360        m_controller.onLatitudeChange(event.getValue());
361    }
362
363    /**
364     * On longitude value change.<p>
365     *
366     * @param event the change event
367     */
368    @UiHandler("m_longitudeField")
369    void onLongitudeChange(ValueChangeEvent<String> event) {
370
371        m_controller.onLongitudeChange(event.getValue());
372    }
373
374    /**
375     * On mode value change.<p>
376     *
377     * @param event the change event
378     */
379    @UiHandler("m_modeField")
380    void onModeChange(ValueChangeEvent<String> event) {
381
382        m_controller.onModeChange(event.getValue());
383    }
384
385    /**
386     * Handles the OK click.<p>
387     *
388     * @param event the mouse event
389     */
390    @UiHandler("m_okButton")
391    void onOk(ClickEvent event) {
392
393        m_controller.onOk();
394    }
395
396    /**
397     * On type value change.<p>
398     *
399     * @param event the change event
400     */
401    @UiHandler("m_typeField")
402    void onTypeChange(ValueChangeEvent<String> event) {
403
404        m_controller.onTypeChange(event.getValue());
405    }
406
407    /**
408     * On width value change.<p>
409     *
410     * @param event the change event
411     */
412    @UiHandler("m_widthField")
413    void onWidthChange(ValueChangeEvent<String> event) {
414
415        m_controller.onWidthChange(event.getValue());
416    }
417
418    /**
419     * On zoom value change.<p>
420     *
421     * @param event the change event
422     */
423    @UiHandler("m_zoomField")
424    void onZoomChange(ValueChangeEvent<String> event) {
425
426        m_controller.onZoomChange(event.getValue());
427    }
428
429    /**
430     * Initializes the form fields.<p>
431     */
432    private void initFields() {
433
434        m_latitudeField.setTriggerChangeOnKeyPress(true);
435        m_longitudeField.setTriggerChangeOnKeyPress(true);
436        m_heightField.setTriggerChangeOnKeyPress(true);
437        m_widthField.setTriggerChangeOnKeyPress(true);
438    }
439
440    /**
441     * Initializes the form labels.<p>
442     */
443    private void initLabels() {
444
445        m_addressLabel.setText(Messages.get().key(Messages.GUI_LOCATION_ADDRESS_0));
446        m_longitudeLabel.setText(Messages.get().key(Messages.GUI_LOCATION_LONGITUDE_0));
447        m_latitudeLabel.setText(Messages.get().key(Messages.GUI_LOCATION_LATITUDE_0));
448        m_sizeLabel.setText(Messages.get().key(Messages.GUI_LOCATION_SIZE_0));
449        m_zoomLabel.setText(Messages.get().key(Messages.GUI_LOCATION_ZOOM_0));
450        m_typeLabel.setText(Messages.get().key(Messages.GUI_LOCATION_TYPE_0));
451        m_modeLabel.setText(Messages.get().key(Messages.GUI_LOCATION_MODE_0));
452
453        m_okButton.setText(Messages.get().key(Messages.GUI_OK_0));
454        m_okButton.setUseMinWidth(true);
455        m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
456        m_cancelButton.setText(Messages.get().key(Messages.GUI_CANCEL_0));
457        m_cancelButton.setUseMinWidth(true);
458        m_cancelButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.BLUE);
459    }
460}