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