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.I_CmsHasResizeOnShow;
031import org.opencms.gwt.client.Messages;
032import org.opencms.gwt.client.ui.CmsPushButton;
033import org.opencms.gwt.client.ui.I_CmsButton;
034import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
035import org.opencms.gwt.client.ui.I_CmsButton.Size;
036import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
037import org.opencms.gwt.client.ui.input.CmsSimpleTextBox;
038
039import java.util.Map;
040import java.util.Map.Entry;
041
042import com.google.gwt.core.client.GWT;
043import com.google.gwt.dom.client.Element;
044import com.google.gwt.dom.client.Style.Display;
045import com.google.gwt.event.dom.client.ClickEvent;
046import com.google.gwt.event.logical.shared.HasValueChangeHandlers;
047import com.google.gwt.event.logical.shared.ValueChangeEvent;
048import com.google.gwt.event.logical.shared.ValueChangeHandler;
049import com.google.gwt.event.shared.HandlerRegistration;
050import com.google.gwt.uibinder.client.UiBinder;
051import com.google.gwt.uibinder.client.UiField;
052import com.google.gwt.uibinder.client.UiHandler;
053import com.google.gwt.user.client.ui.Composite;
054import com.google.gwt.user.client.ui.HTMLPanel;
055
056/**
057 * A google maps based location picker widget.<p>
058 */
059public class CmsLocationPicker extends Composite implements HasValueChangeHandlers<String>, I_CmsHasResizeOnShow {
060
061    /**
062     * @see com.google.gwt.uibinder.client.UiBinder
063     */
064    protected interface I_CmsLocationPickerUiBinder extends UiBinder<HTMLPanel, CmsLocationPicker> {
065        // GWT interface, nothing to do here
066    }
067
068    /** The ui-binder instance for this class. */
069    private static I_CmsLocationPickerUiBinder uiBinder = GWT.create(I_CmsLocationPickerUiBinder.class);
070
071    /** The location picker controller. */
072    CmsLocationController m_controller;
073
074    /** The location info panel. */
075    @UiField
076    Element m_locationInfoPanel;
077
078    /** The map preview element. */
079    //@UiField
080    //Element m_mapPreview;
081
082    /** The popup opener button. */
083    @UiField(provided = true)
084    CmsPushButton m_openerButton;
085
086    /** The value display. */
087    @UiField
088    CmsSimpleTextBox m_textbox;
089
090    /**
091     * Constructor.<p>
092     *
093     * @param configuration the widget configuration
094     **/
095    public CmsLocationPicker(String configuration) {
096
097        I_CmsLayoutBundle.INSTANCE.locationPickerCss().ensureInjected();
098        m_openerButton = new CmsPushButton();
099        m_openerButton.setButtonStyle(ButtonStyle.FONT_ICON, null);
100        m_openerButton.setImageClass(I_CmsButton.GALLERY);
101        m_openerButton.setSize(Size.small);
102        m_openerButton.setTitle(Messages.get().key(Messages.GUI_LOCATION_DIALOG_TITLE_0));
103        initWidget(uiBinder.createAndBindUi(this));
104        // disable input, the picker popup is used for editing the value
105        m_textbox.setEnabled(false);
106        m_locationInfoPanel.getStyle().setDisplay(Display.NONE);
107        m_controller = new CmsLocationController(this, configuration);
108        // m_mapPreview.setId(HTMLPanel.createUniqueId());
109    }
110
111    /**
112     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
113     */
114    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
115
116        return addHandler(handler, ValueChangeEvent.getType());
117    }
118
119    /**
120     * Returns the location value.<p>
121     *
122     * @return the location value
123     */
124    public CmsLocationValue getLocationValue() {
125
126        return m_controller.getLocationValue();
127    }
128
129    /**
130     * Returns the JSON string representation of the value.<p>
131     *
132     * @return the JSON string representation
133     */
134    public String getStringValue() {
135
136        return m_controller.getStringValue();
137    }
138
139    /**
140     * @see org.opencms.gwt.client.I_CmsHasResizeOnShow#resizeOnShow()
141     */
142    public void resizeOnShow() {
143
144        m_controller.onPreviewResize();
145    }
146
147    /**
148     * Sets the picker enabled.<p>
149     *
150     * @param enabled <code>true</code> to enable the picker
151     */
152    public void setEnabled(boolean enabled) {
153
154        m_openerButton.setEnabled(enabled);
155    }
156
157    /**
158     * Sets the widget value.<p>
159     *
160     * @param value the value
161     */
162    public void setValue(String value) {
163
164        m_controller.setStringValue(value);
165    }
166
167    /**
168     * Displays the given value.<p>
169     *
170     * @param value the value to display
171     */
172    protected void displayValue(String value) {
173
174        m_textbox.setText(value);
175    }
176
177    //    /**
178    //     * Returns the map preview element.<p>
179    //     *
180    //     * @return the map preview element
181    //     */
182    //    protected Element getMapPreview() {
183    //
184    //        return m_mapPreview;
185    //    }
186
187    /**
188     * Returns whether the map preview is visible.<p>
189     *
190     * @return <code>true</code> in case the map preview is visible
191     */
192    protected boolean isPreviewVisible() {
193
194        return getElement().hasClassName(I_CmsLayoutBundle.INSTANCE.locationPickerCss().hasPreview());
195    }
196
197    /**
198     * Sets the location info to the info panel.<p>
199     *
200     * @param infos the location info items
201     */
202    protected void setLocationInfo(Map<String, String> infos) {
203
204        if (infos.isEmpty()) {
205            m_locationInfoPanel.getStyle().setDisplay(Display.NONE);
206        } else {
207            StringBuffer infoHtml = new StringBuffer();
208            for (Entry<String, String> info : infos.entrySet()) {
209                infoHtml.append("<p><span>").append(info.getKey()).append(":</span>").append(info.getValue()).append(
210                    "</p>");
211            }
212            m_locationInfoPanel.setInnerHTML(infoHtml.toString());
213            m_locationInfoPanel.getStyle().clearDisplay();
214        }
215    }
216
217    /**
218     * Sets the preview visible.<p>
219     *
220     * @param visible <code>true</code> to set the preview visible
221     */
222    protected void setPreviewVisible(boolean visible) {
223
224        if (visible) {
225            addStyleName(I_CmsLayoutBundle.INSTANCE.locationPickerCss().hasPreview());
226        } else {
227            removeStyleName(I_CmsLayoutBundle.INSTANCE.locationPickerCss().hasPreview());
228        }
229    }
230
231    /**
232     * Opens the location popup.<p>
233     *
234     * @param event the click event
235     */
236    @UiHandler("m_openerButton")
237    void onOpenerClick(ClickEvent event) {
238
239        m_openerButton.clearHoverState();
240        m_controller.openPopup();
241    }
242}