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.acacia.client.widgets;
029
030import org.opencms.gwt.client.ui.input.CmsSelectBox;
031import org.opencms.gwt.shared.categorizedselect.I_CmsCategorizedSelectData;
032import org.opencms.gwt.shared.categorizedselect.I_CmsCategorizedSelectData.Option;
033import org.opencms.gwt.shared.categorizedselect.I_CmsCategorizedSelectDataFactory;
034
035import java.util.LinkedHashMap;
036import java.util.List;
037
038import com.google.gwt.core.client.GWT;
039import com.google.gwt.dom.client.Element;
040import com.google.gwt.event.dom.client.FocusEvent;
041import com.google.gwt.event.dom.client.FocusHandler;
042import com.google.gwt.event.logical.shared.ValueChangeEvent;
043import com.google.gwt.event.logical.shared.ValueChangeHandler;
044import com.google.gwt.event.shared.HandlerRegistration;
045import com.google.gwt.uibinder.client.UiBinder;
046import com.google.gwt.uibinder.client.UiField;
047import com.google.gwt.user.client.ui.Composite;
048import com.google.gwt.user.client.ui.FlowPanel;
049import com.google.gwt.user.client.ui.Label;
050import com.google.gwt.user.client.ui.Widget;
051
052/**
053 * Client-side implementation of the categorized select widget.
054 *
055 * <p>Provides two select fields, one for filter categories and one for the actual selection to be made.
056 * No RPC is used, all data for the filter categories and options has to be supplied initially.
057 */
058public class CmsCategorizedSelectWidget extends Composite implements I_CmsEditWidget {
059
060    /**
061     * The Interface I_CmsWidgetUiBinder.
062     */
063    interface I_CmsWidgetUiBinder extends UiBinder<Widget, CmsCategorizedSelectWidget> {
064        // uibinder interface
065    }
066
067    /** The config factory. */
068    public static I_CmsCategorizedSelectDataFactory configFactory = GWT.create(I_CmsCategorizedSelectDataFactory.class);
069
070    /** The Constant NO_FILTER. */
071    public static final String NO_FILTER = "--";
072
073    /** The Constant uibinder. */
074    private static final I_CmsWidgetUiBinder uibinder = GWT.create(I_CmsWidgetUiBinder.class);
075
076    /** The category select. */
077    @UiField
078    protected CmsSelectBox m_categorySelect;
079
080    /** The filter label. */
081    @UiField
082    protected Label m_filterLabel;
083
084    /** The main select. */
085    @UiField
086    protected CmsSelectBox m_mainSelect;
087
088    /** The active. */
089    private boolean m_active = true;
090
091    /** The config. */
092    private I_CmsCategorizedSelectData m_config;
093
094    /** The container. */
095    private FlowPanel m_container = new FlowPanel();
096
097    /**
098     * Instantiates a new cms categorized select widget.
099     *
100     * @param config the config
101     */
102    public CmsCategorizedSelectWidget(I_CmsCategorizedSelectData config) {
103
104        m_config = config;
105        m_container = (FlowPanel)uibinder.createAndBindUi(this);
106        m_filterLabel.setText(config.getFilterLabel());
107        LinkedHashMap<String, String> options = new LinkedHashMap<>();
108
109        for (Option option : config.getOptions()) {
110            options.put(option.getKey(), option.getLabel());
111        }
112        m_mainSelect.setItems(options);
113
114        LinkedHashMap<String, String> categoryOptions = new LinkedHashMap<>();
115        categoryOptions.put(NO_FILTER, NO_FILTER);
116        for (I_CmsCategorizedSelectData.Category category : config.getCategories()) {
117            categoryOptions.put(category.getKey(), category.getLabel());
118        }
119        m_categorySelect.setItems(categoryOptions);
120        m_categorySelect.addValueChangeHandler(new ValueChangeHandler<String>() {
121
122            @SuppressWarnings("synthetic-access")
123            public void onValueChange(ValueChangeEvent<String> event) {
124
125                updateCategory(event.getValue());
126            }
127        });
128
129        initWidget(m_container);
130        m_mainSelect.addValueChangeHandler(new ValueChangeHandler<String>() {
131
132            public void onValueChange(ValueChangeEvent<String> event) {
133
134                CmsCategorizedSelectWidget.this.fireChangeEvent();
135            }
136        });
137
138    }
139
140    /**
141     * Adds the focus handler.
142     *
143     * @param handler the handler
144     * @return the handler registration
145     * @see com.google.gwt.event.dom.client.HasFocusHandlers#addFocusHandler(com.google.gwt.event.dom.client.FocusHandler)
146     */
147    public HandlerRegistration addFocusHandler(FocusHandler handler) {
148
149        return addDomHandler(handler, FocusEvent.getType());
150    }
151
152    /**
153     * Adds the value change handler.
154     *
155     * @param handler the handler
156     * @return the handler registration
157     * @see com.google.gwt.event.logical.shared.HasValueChangeHandlers#addValueChangeHandler(com.google.gwt.event.logical.shared.ValueChangeHandler)
158     */
159    public HandlerRegistration addValueChangeHandler(ValueChangeHandler<String> handler) {
160
161        return addHandler(handler, ValueChangeEvent.getType());
162    }
163
164    /**
165     * Represents a value change event.<p>
166     *
167     */
168    public void fireChangeEvent() {
169
170        String val = m_mainSelect.getFormValueAsString();
171        if (val != null) {
172            ValueChangeEvent.fire(this, val);
173        }
174
175    }
176
177    /**
178     * @see com.google.gwt.user.client.ui.HasValue#getValue()
179     */
180    public String getValue() {
181
182        return m_mainSelect.getFormValueAsString();
183    }
184
185    /**
186     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#isActive()
187     */
188    public boolean isActive() {
189
190        return m_active;
191    }
192
193    /**
194     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#onAttachWidget()
195     */
196    public void onAttachWidget() {
197
198        super.onAttach();
199
200    }
201
202    /**
203     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#owns(com.google.gwt.dom.client.Element)
204     */
205    public boolean owns(Element element) {
206
207        // TODO Auto-generated method stub
208        return false;
209    }
210
211    /**
212     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setActive(boolean)
213     */
214    public void setActive(boolean active) {
215
216        if (active == m_active) {
217            return;
218        }
219        m_active = active;
220        m_mainSelect.setEnabled(active);
221        m_categorySelect.setEnabled(active);
222        if (active) {
223            fireChangeEvent();
224        }
225    }
226
227    /**
228     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setName(java.lang.String)
229     */
230    public void setName(String name) {
231
232        // do nothing
233
234    }
235
236    /**
237     * Sets the value.
238     *
239     * @param value the new value
240     * @see com.google.gwt.user.client.ui.HasValue#setValue(java.lang.Object)
241     */
242    public void setValue(String value) {
243
244        setValue(value, false);
245
246    }
247
248    /**
249     * Sets the value.
250     *
251     * @param value the value
252     * @param fireEvent the fire event
253     * @see org.opencms.acacia.client.widgets.I_CmsEditWidget#setValue(java.lang.String, boolean)
254     */
255    public void setValue(String value, boolean fireEvent) {
256
257        m_mainSelect.setFormValueAsString(value);
258        if (fireEvent) {
259            fireChangeEvent();
260        }
261
262    }
263
264    /**
265     * Update category.
266     *
267     * @param category the category
268     */
269    private void updateCategory(String category) {
270
271        LinkedHashMap<String, String> options = new LinkedHashMap<>();
272        boolean noFilter = NO_FILTER.equals(category);
273        for (Option option : m_config.getOptions()) {
274            List<String> categories = option.getCategories();
275            if (noFilter || categories.contains(category)) {
276                options.put(option.getKey(), option.getLabel());
277            }
278        }
279        m_mainSelect.setItems(options);
280        if (options.size() > 0) {
281            String nextValue = options.keySet().iterator().next();
282            m_mainSelect.setFormValue(nextValue, true);
283            fireChangeEvent();
284        }
285    }
286}