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.ade.galleries.client.ui;
029
030import org.opencms.ade.galleries.client.CmsSearchTabHandler;
031import org.opencms.ade.galleries.client.Messages;
032import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
033import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
034import org.opencms.gwt.client.ui.CmsPushButton;
035import org.opencms.gwt.client.ui.I_CmsAutoHider;
036import org.opencms.gwt.client.ui.css.I_CmsInputLayoutBundle;
037import org.opencms.gwt.client.ui.input.CmsCheckBox;
038import org.opencms.gwt.client.ui.input.CmsLabelSelectCell;
039import org.opencms.gwt.client.ui.input.CmsSelectBox;
040import org.opencms.gwt.client.ui.input.datebox.CmsDateBox;
041import org.opencms.util.CmsStringUtil;
042
043import java.util.ArrayList;
044import java.util.Date;
045import java.util.List;
046import java.util.Map;
047
048import com.google.gwt.core.client.GWT;
049import com.google.gwt.dom.client.Style.Display;
050import com.google.gwt.event.dom.client.ClickEvent;
051import com.google.gwt.event.logical.shared.ValueChangeEvent;
052import com.google.gwt.uibinder.client.UiBinder;
053import com.google.gwt.uibinder.client.UiField;
054import com.google.gwt.uibinder.client.UiHandler;
055import com.google.gwt.user.client.ui.HTMLPanel;
056import com.google.gwt.user.client.ui.Label;
057
058/**
059 * Provides the widget for the full text search tab.<p>
060 *
061 * @since 8.0.
062 */
063public class CmsSearchTab extends A_CmsTab {
064
065    /** The parameter types of this tab. */
066    public enum ParamType {
067        /** The creation range type. */
068        creation,
069        /** The expired resources type. */
070        expired,
071        /** The language type. */
072        language,
073        /** The modification range type. */
074        modification;
075    }
076
077    /** The ui-binder interface. */
078    interface I_CmsSearchTabUiBinder extends UiBinder<HTMLPanel, CmsSearchTab> {
079        // GWT interface, nothing to do here
080    }
081
082    /** A constant for the "not set" valueof the language selection. */
083    private static final String NOT_SET_OPTION_VALUE = "notSet";
084
085    /** The ui-binder instance. */
086    private static I_CmsSearchTabUiBinder uiBinder = GWT.create(I_CmsSearchTabUiBinder.class);
087
088    /** The button to clear the tab input. */
089    @UiField
090    protected CmsPushButton m_clearButton;
091
092    /** The current locale. */
093    protected String m_currentLocale;
094
095    /** The date box for the created until date. */
096    @UiField
097    protected CmsDateBox m_dateCreatedEndDateBox;
098
099    /** The label for the created until date. */
100    @UiField
101    protected Label m_dateCreatedEndLabel;
102
103    /** The date box for the created since date. */
104    @UiField
105    protected CmsDateBox m_dateCreatedStartDateBox;
106
107    /** The label for the created since date. */
108    @UiField
109    protected Label m_dateCreatedStartLabel;
110
111    /** The date box for the modified until date. */
112    @UiField
113    protected CmsDateBox m_dateModifiedEndDateBox;
114
115    /** The label for the modified until date. */
116    @UiField
117    protected Label m_dateModifiedEndLabel;
118
119    /** The date box for the modified since date. */
120    @UiField
121    protected CmsDateBox m_dateModifiedStartDateBox;
122
123    /** The label for the modified since date. */
124    @UiField
125    protected Label m_dateModifiedStartLabel;
126
127    /** The include expired resources check-box. */
128    @UiField
129    protected CmsCheckBox m_includeExpiredCheckBox;
130
131    /** The label for the language selection. */
132    @UiField
133    protected Label m_localeLabel;
134
135    /** The row for the language selection. */
136    @UiField
137    protected HTMLPanel m_localeRow;
138
139    /** The select box for the language selection. */
140    @UiField
141    protected CmsSelectBox m_localeSelection;
142
143    /** The tab handler. */
144    CmsSearchTabHandler m_tabHandler;
145
146    /** The parent popup to this dialog if present. */
147    private I_CmsAutoHider m_autoHideParent;
148
149    /** The map of available locales. */
150    private Map<String, String> m_availableLocales;
151
152    /** The tab panel. */
153    private HTMLPanel m_tab;
154
155    /**
156     * Constructor for the search tab.<p>
157     *
158     * @param tabHandler the tab handler
159     * @param autoHideParent the auto-hide parent to this dialog if present
160     * @param currentLocale the current content locale
161     * @param availableLocales the available locales
162     * @param defaultIncludeExpired true if 'show expired' should be enabled by default
163     */
164    @SuppressWarnings("deprecation")
165    public CmsSearchTab(
166        CmsSearchTabHandler tabHandler,
167        I_CmsAutoHider autoHideParent,
168        String currentLocale,
169        Map<String, String> availableLocales,
170        boolean defaultIncludeExpired) {
171
172        // initialize the tab
173        super(GalleryTabId.cms_tab_search.name());
174        m_tab = uiBinder.createAndBindUi(this);
175        initWidget(m_tab);
176        addStyleName(I_CmsInputLayoutBundle.INSTANCE.inputCss().highTextBoxes());
177        m_tabHandler = tabHandler;
178        m_autoHideParent = autoHideParent;
179        m_currentLocale = currentLocale;
180        m_availableLocales = availableLocales;
181
182        // add the language selection
183        m_localeLabel.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_LANGUAGE_LABEL_TEXT_0));
184        CmsLabelSelectCell notSelectedCell = new CmsLabelSelectCell(
185            NOT_SET_OPTION_VALUE,
186            Messages.get().key(Messages.GUI_TAB_SEARCH_LANGUAGE_NOT_SEL_0));
187        notSelectedCell.setVisible(false);
188        m_localeSelection.addOption(notSelectedCell);
189        for (Map.Entry<String, String> entry : availableLocales.entrySet()) {
190            m_localeSelection.addOption(entry.getKey(), entry.getValue());
191        }
192        // hide language selection if only one locale is available
193        if (availableLocales.size() <= 1) {
194            m_localeRow.getElement().getStyle().setDisplay(Display.NONE);
195        }
196        m_includeExpiredCheckBox.setChecked(defaultIncludeExpired);
197        m_tabHandler.setIncludeExpired(defaultIncludeExpired, false);
198        m_includeExpiredCheckBox.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_INCLUDE_EXPIRED_0));
199        // set the labels for the date box widgets
200        m_dateCreatedStartLabel.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_CREATED_SINCE_0));
201        m_dateCreatedEndLabel.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_CREATED_UNTIL_0));
202        m_dateModifiedStartLabel.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_MODIFIED_SINCE_0));
203        m_dateModifiedEndLabel.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_MODIFIED_UNTIL_0));
204
205        if (m_autoHideParent != null) {
206            m_dateCreatedEndDateBox.setAutoHideParent(m_autoHideParent);
207            m_dateCreatedStartDateBox.setAutoHideParent(m_autoHideParent);
208            m_dateModifiedEndDateBox.setAutoHideParent(m_autoHideParent);
209            m_dateModifiedStartDateBox.setAutoHideParent(m_autoHideParent);
210        }
211        Date initialStartDate = new Date();
212        initialStartDate.setHours(0);
213        initialStartDate.setMinutes(0);
214        m_dateModifiedStartDateBox.setInitialDate(initialStartDate);
215        m_dateCreatedStartDateBox.setInitialDate(initialStartDate);
216        Date initialEndDate = new Date();
217        initialEndDate.setHours(23);
218        initialEndDate.setMinutes(59);
219        m_dateModifiedEndDateBox.setInitialDate(initialEndDate);
220        m_dateCreatedEndDateBox.setInitialDate(initialEndDate);
221        // add the clear button
222        m_clearButton.setText(Messages.get().key(Messages.GUI_TAB_SEARCH_BUTTON_CLEAR_0));
223        m_clearButton.setUseMinWidth(true);
224    }
225
226    /**
227     * Clears the search tab input.<p>
228     */
229    public void clearInput() {
230
231        m_dateCreatedStartDateBox.setValue(null, true);
232        m_dateCreatedEndDateBox.setValue(null, true);
233        m_dateModifiedStartDateBox.setValue(null, true);
234        m_dateModifiedEndDateBox.setValue(null, true);
235        m_includeExpiredCheckBox.setChecked(false);
236        m_localeSelection.reset();
237    }
238
239    /**
240     * Sets the form fields to the values from the stored  gallery search.<p>
241     *
242     * @param search a previously stored gallery search
243     */
244    public void fillParams(CmsGallerySearchBean search) {
245
246        m_localeSelection.setFormValue(search.getLocale(), false);
247        m_includeExpiredCheckBox.setChecked(search.isIncludeExpired());
248        if (search.getDateCreatedStart() > 9) {
249            m_dateCreatedStartDateBox.setValue(new Date(search.getDateCreatedStart()));
250        }
251        if (search.getDateCreatedEnd() > 0) {
252            m_dateCreatedEndDateBox.setValue(new Date(search.getDateCreatedEnd()));
253        }
254        if (search.getDateModifiedStart() > 0) {
255            m_dateModifiedStartDateBox.setValue(new Date(search.getDateModifiedStart()));
256        }
257        if (search.getDateModifiedEnd() > 0) {
258            m_dateModifiedEndDateBox.setValue(new Date(search.getDateModifiedEnd()));
259        }
260
261    }
262
263    /**
264     * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getParamPanels(org.opencms.ade.galleries.shared.CmsGallerySearchBean)
265     */
266    @Override
267    public List<CmsSearchParamPanel> getParamPanels(CmsGallerySearchBean searchObj) {
268
269        List<CmsSearchParamPanel> result = new ArrayList<CmsSearchParamPanel>();
270        // get the required data
271        String createdStart = m_dateCreatedStartDateBox.getValueAsFormatedString();
272        String createdEnd = m_dateCreatedEndDateBox.getValueAsFormatedString();
273        String modifiedStart = m_dateModifiedStartDateBox.getValueAsFormatedString();
274        String modifiedEnd = m_dateModifiedEndDateBox.getValueAsFormatedString();
275
276        // append the language
277        String locale = m_localeSelection.getFormValueAsString();
278        String language = m_availableLocales.get(locale);
279        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(locale)
280            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(language)
281            && !locale.equals(NOT_SET_OPTION_VALUE)) {
282
283            CmsSearchParamPanel panel = new CmsSearchParamPanel(
284                Messages.get().key(Messages.GUI_TAB_SEARCH_LANGUAGE_LABEL_TEXT_0),
285                this);
286            panel.setContent(language, ParamType.language.name());
287            result.add(panel);
288        }
289
290        // append the date created range
291        StringBuffer createdResult = new StringBuffer();
292        if ((CmsStringUtil.isNotEmptyOrWhitespaceOnly(createdStart)
293            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(createdEnd))) {
294            CmsSearchParamPanel panel = new CmsSearchParamPanel(
295                Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_CREATED_RANGE_0),
296                this);
297            panel.setContent(createdStart + " - " + createdEnd, ParamType.creation.name());
298            result.add(panel);
299        } else if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(createdStart)) {
300            CmsSearchParamPanel panel = new CmsSearchParamPanel(
301                Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_CREATED_SINCE_0),
302                this);
303            panel.setContent(createdStart, ParamType.creation.name());
304            result.add(panel);
305        } else if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(createdEnd)) {
306            createdResult.append(Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_CREATED_UNTIL_0)).append(" ").append(
307                createdEnd);
308
309            CmsSearchParamPanel panel = new CmsSearchParamPanel(
310                Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_CREATED_UNTIL_0),
311                this);
312            panel.setContent(createdEnd, ParamType.creation.name());
313            result.add(panel);
314        }
315
316        // append the date modified range
317        if ((CmsStringUtil.isNotEmptyOrWhitespaceOnly(modifiedStart)
318            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(modifiedEnd))) {
319            CmsSearchParamPanel panel = new CmsSearchParamPanel(
320                Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_MODIFIED_RANGE_0),
321                this);
322            panel.setContent(modifiedStart + " - " + modifiedEnd, ParamType.modification.name());
323            result.add(panel);
324        } else if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(modifiedStart)) {
325            CmsSearchParamPanel panel = new CmsSearchParamPanel(
326                Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_MODIFIED_SINCE_0),
327                this);
328            panel.setContent(modifiedStart, ParamType.modification.name());
329            result.add(panel);
330        } else if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(modifiedEnd)) {
331            CmsSearchParamPanel panel = new CmsSearchParamPanel(
332                Messages.get().key(Messages.GUI_TAB_SEARCH_LABEL_MODIFIED_UNTIL_0),
333                this);
334            panel.setContent(modifiedEnd, ParamType.modification.name());
335            result.add(panel);
336        }
337
338        if (m_includeExpiredCheckBox.getFormValue().booleanValue()) {
339            CmsSearchParamPanel panel = new CmsSearchParamPanel(
340                Messages.get().key(Messages.GUI_PARAMS_LABEL_INCLUDING_EXPIRED_0),
341                this);
342            panel.setContent("", ParamType.expired.name());
343            result.add(panel);
344        }
345        return result;
346    }
347
348    /**
349     * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getRequiredHeight()
350     */
351    @Override
352    public int getRequiredHeight() {
353
354        return 153;
355    }
356
357    /**
358     * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getTabHandler()
359     */
360    @Override
361    public CmsSearchTabHandler getTabHandler() {
362
363        return m_tabHandler;
364    }
365
366    /**
367     * Removes the given parameter type.<p>
368     *
369     * @param type the parameter type
370     */
371    public void removeParameter(ParamType type) {
372
373        switch (type) {
374            case language:
375                m_localeSelection.reset();
376                break;
377            case expired:
378                m_includeExpiredCheckBox.setChecked(false);
379                break;
380            case creation:
381                m_dateCreatedStartDateBox.setValue(null, true);
382                m_dateCreatedEndDateBox.setValue(null, true);
383                break;
384            case modification:
385                m_dateModifiedStartDateBox.setValue(null, true);
386                m_dateModifiedEndDateBox.setValue(null, true);
387                break;
388            default:
389        }
390    }
391
392    /**
393     * Clears the search tab input.<p>
394     *
395     * @param event the click event
396     */
397    @UiHandler("m_clearButton")
398    protected void clearInput(ClickEvent event) {
399
400        clearInput();
401    }
402
403    /**
404     * Handles changes of date created range end box.<p>
405     *
406     * @param event the change event
407     */
408    @UiHandler("m_dateCreatedEndDateBox")
409    protected void onDateCreatedEndChange(ValueChangeEvent<Date> event) {
410
411        if (event.getValue() != null) {
412            m_tabHandler.setDateCreatedEnd(event.getValue().getTime());
413        } else {
414            // if the field is empty take the max value
415            m_tabHandler.setDateCreatedEnd(-1L);
416        }
417    }
418
419    /**
420     * Handles changes of date created range start box.<p>
421     *
422     * @param event the change event
423     */
424    @UiHandler("m_dateCreatedStartDateBox")
425    protected void onDateCreatedStartChange(ValueChangeEvent<Date> event) {
426
427        if (event.getValue() != null) {
428            m_tabHandler.setDateCreatedStart(event.getValue().getTime());
429        } else {
430            // if the field is empty take the min value
431            m_tabHandler.setDateCreatedStart(-1L);
432        }
433    }
434
435    /**
436     * Handles changes of date modified range end box.<p>
437     *
438     * @param event the change event
439     */
440    @UiHandler("m_dateModifiedEndDateBox")
441    protected void onDateModifiedEndChange(ValueChangeEvent<Date> event) {
442
443        if (event.getValue() != null) {
444            m_tabHandler.setDateModifiedEnd(event.getValue().getTime());
445        } else {
446            // if the field is empty take the max value
447            m_tabHandler.setDateModifiedEnd(-1L);
448        }
449    }
450
451    /**
452     * Handles changes of date modified range start box.<p>
453     *
454     * @param event the change event
455     */
456    @UiHandler("m_dateModifiedStartDateBox")
457    protected void onDateModifiedStartChange(ValueChangeEvent<Date> event) {
458
459        if (event.getValue() != null) {
460            m_tabHandler.setDateModifiedStart(event.getValue().getTime());
461        } else {
462            // if the field is empty take the min value
463            m_tabHandler.setDateModifiedStart(-1L);
464        }
465    }
466
467    /**
468     * Handles changes of the include expired check box.<p>
469     *
470     * @param event the change event
471     */
472    @UiHandler("m_includeExpiredCheckBox")
473    protected void onIncludeExpiredChange(ValueChangeEvent<Boolean> event) {
474
475        Boolean value = event.getValue();
476        m_tabHandler.setIncludeExpired(value.booleanValue(), true);
477    }
478
479    /**
480     * Handles the change event of the locale select box.<p>
481     *
482     * @param event the change event
483     */
484    @UiHandler("m_localeSelection")
485    protected void onLocaleChange(ValueChangeEvent<String> event) {
486
487        String value = event.getValue();
488        if (CmsStringUtil.isEmptyOrWhitespaceOnly(value) || value.equals(NOT_SET_OPTION_VALUE)) {
489            value = m_currentLocale;
490        }
491        m_tabHandler.setLocale(value);
492    }
493
494}