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;
029
030import org.opencms.ade.galleries.client.ui.CmsGalleryDialog;
031import org.opencms.ade.galleries.client.ui.CmsSearchTab.ParamType;
032import org.opencms.ade.galleries.client.ui.CmsSitemapTab;
033import org.opencms.ade.galleries.client.ui.CmsVfsTab;
034import org.opencms.ade.galleries.shared.CmsGalleryDataBean;
035import org.opencms.ade.galleries.shared.CmsGalleryFolderBean;
036import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
037import org.opencms.ade.galleries.shared.CmsGalleryTreeEntry;
038import org.opencms.ade.galleries.shared.CmsResourceTypeBean;
039import org.opencms.ade.galleries.shared.CmsSitemapEntryBean;
040import org.opencms.ade.galleries.shared.CmsVfsEntryBean;
041import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
042import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
043import org.opencms.gwt.shared.CmsCategoryBean;
044import org.opencms.gwt.shared.CmsCategoryTreeEntry;
045import org.opencms.gwt.shared.sort.CmsComparatorTitle;
046import org.opencms.util.CmsStringUtil;
047
048import java.util.Collection;
049import java.util.Collections;
050import java.util.List;
051import java.util.Set;
052
053import com.google.gwt.core.client.Scheduler;
054import com.google.gwt.core.client.Scheduler.ScheduledCommand;
055import com.google.gwt.event.logical.shared.ValueChangeEvent;
056import com.google.gwt.event.logical.shared.ValueChangeHandler;
057import com.google.gwt.user.client.Command;
058
059/**
060 * Gallery dialog controller handler.<p>
061 *
062 * Delegates the actions of the gallery controller to the gallery dialog.
063 *
064 * @since 8.0.0
065 */
066public class CmsGalleryControllerHandler implements ValueChangeHandler<CmsGallerySearchBean> {
067
068    /** The reference to the gallery dialog. */
069    protected CmsGalleryDialog m_galleryDialog;
070
071    /** The gallery mode. */
072    private I_CmsGalleryProviderConstants.GalleryMode m_mode;
073
074    /**
075     * Constructor.<p>
076     *
077     * @param galleryDialog the reference to the gallery dialog
078     */
079    public CmsGalleryControllerHandler(CmsGalleryDialog galleryDialog) {
080
081        m_galleryDialog = galleryDialog;
082    }
083
084    /**
085     * Returns true if a results tab exists.<p>
086     *
087     * @return true if a results tab exists
088     */
089    public boolean hasResultsTab() {
090
091        return m_galleryDialog.getResultsTab() != null;
092    }
093
094    /**
095     * Hides or shows the show-preview-button.<p>
096     *
097     * @param hide <code>true</code> to hide the button
098     */
099    public void hideShowPreviewButton(boolean hide) {
100
101        // buttons
102        switch (m_mode) {
103            case editor:
104            case widget:
105                m_galleryDialog.hideShowPreviewButton(hide);
106                break;
107            case ade:
108            case view:
109            case adeView:
110            default:
111                break;
112        }
113    }
114
115    /**
116     * Will be triggered when the categories tab is selected.<p>
117     */
118    public void onCategoriesTabSelection() {
119
120        if (!m_galleryDialog.getCategoriesTab().isInitOpen()) {
121            m_galleryDialog.getCategoriesTab().onContentChange();
122            return;
123        }
124        m_galleryDialog.getCategoriesTab().openFirstLevel();
125        m_galleryDialog.getCategoriesTab().setInitOpen(false);
126    }
127
128    /**
129     * Deletes the html content of the categories parameter and removes the style.<p>
130     *
131     * @param categories the categories to remove from selection
132     */
133    public void onClearCategories(List<String> categories) {
134
135        if (categories != null) {
136            m_galleryDialog.getCategoriesTab().uncheckCategories(categories);
137        }
138    }
139
140    /**
141     * The method which is executed when all folders are cleared from the search object.<p>
142     *
143     * @param folders the folders which have been cleared
144     */
145    public void onClearFolders(Collection<String> folders) {
146
147        m_galleryDialog.getVfsTab().uncheckFolders(folders);
148    }
149
150    /**
151     * clears the search tab input.<p>
152     */
153    public void onClearFullTextSearch() {
154
155        m_galleryDialog.getSearchTab().clearInput();
156
157    }
158
159    /**
160     * Deletes the html content of the galleries parameter and removes the style.<p>
161     *
162     * @param galleries the galleries to remove from selection
163     */
164    public void onClearGalleries(List<String> galleries) {
165
166        if (galleries != null) {
167            m_galleryDialog.getGalleriesTab().uncheckGalleries(galleries);
168        }
169    }
170
171    /**
172     * Deletes the html content of the types parameter and removes the style.<p>
173     *
174     * @param types the types to be removed from selection
175     */
176    public void onClearTypes(List<String> types) {
177
178        if (types != null) {
179            m_galleryDialog.getTypesTab().uncheckTypes(types);
180        }
181    }
182
183    /**
184     * Will be triggered when the galleries tab is selected.<p>
185     */
186    public void onGalleriesTabSelection() {
187
188        m_galleryDialog.getGalleriesTab().onContentChange();
189    }
190
191    /**
192     * Will be triggered when the initial search is performed.<p>
193     *
194     * @param searchObj the current search object
195     * @param dialogBean the current dialog data bean
196     * @param controller the dialog controller
197     * @param isFirstTime true if this method is called the first time for the gallery dialog instance
198     */
199    public void onInitialSearch(
200        final CmsGallerySearchBean searchObj,
201        final CmsGalleryDataBean dialogBean,
202        final CmsGalleryController controller,
203        boolean isFirstTime) {
204
205        m_mode = dialogBean.getMode();
206        if (isFirstTime) {
207            if ((dialogBean.getSitemapSiteSelectorOptions() == null)
208                || dialogBean.getSitemapSiteSelectorOptions().isEmpty()) {
209                controller.removeTab(GalleryTabId.cms_tab_sitemap);
210            }
211            m_galleryDialog.fillTabs(controller);
212        }
213
214        if ((m_galleryDialog.getGalleriesTab() != null) && (dialogBean.getGalleries() != null)) {
215            // select box may be initially set to grouped, we need to first change it to something else
216            // and then back to grouped to force the grouped view to render (triggered by change event)
217            m_galleryDialog.getGalleriesTab().setSortSelectBoxValue(
218                org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.SortParams.title_asc.name(),
219                false);
220            m_galleryDialog.getGalleriesTab().setSortSelectBoxValue(
221                org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.SortParams.grouped.name(),
222                true);
223        }
224
225        if ((m_galleryDialog.getTypesTab() != null) && (dialogBean.getTypes() != null)) {
226            setTypesTabContent(controller.getSearchTypes(), searchObj.getTypes());
227        }
228
229        if (m_galleryDialog.getSearchTab() != null) {
230            m_galleryDialog.getSearchTab().fillParams(searchObj);
231        }
232
233        if ((m_galleryDialog.getCategoriesTab() != null) && (dialogBean.getCategories() != null)) {
234            setCategoriesTabContent(dialogBean.getCategories(), searchObj.getCategories());
235        }
236        GalleryTabId startTab = dialogBean.getStartTab();
237
238        // start tab from the search bean may override the start tab from the data bean
239        GalleryTabId searchTabId = searchObj.getInitialTabId();
240        if ((searchTabId != null) && (m_galleryDialog.getTab(searchTabId) != null)) {
241            startTab = searchTabId;
242        }
243        if (startTab == GalleryTabId.cms_tab_results) {
244            if (!searchObj.isEmpty()) {
245                m_galleryDialog.fillResultTab(searchObj);
246            }
247        }
248        CmsSitemapEntryBean sitemapPreloadData = searchObj.getSitemapPreloadData();
249        if ((sitemapPreloadData != null) && (m_galleryDialog.getSitemapTab() != null)) {
250            onReceiveSitemapPreloadData(sitemapPreloadData);
251        }
252        CmsVfsEntryBean vfsPreloadData = searchObj.getVfsPreloadData();
253        if (vfsPreloadData == null) {
254            vfsPreloadData = dialogBean.getVfsPreloadData();
255        }
256        if (m_galleryDialog.getVfsTab() != null) {
257            if (vfsPreloadData != null) {
258                onReceiveVfsPreloadData(vfsPreloadData, searchObj.getFolders());
259            } else if ((dialogBean.getVfsRootFolders() != null)) {
260                m_galleryDialog.getVfsTab().fillInitially(
261                    dialogBean.getVfsRootFolders(),
262                    controller.getDefaultVfsTabSiteRoot());
263            }
264        }
265
266        if (startTab == GalleryTabId.cms_tab_results) {
267            if (searchObj.isEmpty()) {
268                startTab = dialogBean.getTabConfiguration().getDefaultTab();
269            }
270        }
271        m_galleryDialog.selectTab(startTab, startTab != GalleryTabId.cms_tab_results);
272
273        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(searchObj.getResourcePath())
274            && CmsStringUtil.isNotEmptyOrWhitespaceOnly(searchObj.getResourceType())
275            && !searchObj.isDisablePreview()) {
276            if (m_galleryDialog.isAttached()) {
277                controller.openPreview(searchObj.getResourcePath(), searchObj.getResourceType());
278            } else {
279                // gallery dialog has to be attached to open the preview
280                m_galleryDialog.setOnAttachCommand(new Command() {
281
282                    /**
283                     * @see com.google.gwt.user.client.Command#execute()
284                     */
285                    public void execute() {
286
287                        controller.openPreview(searchObj.getResourcePath(), searchObj.getResourceType());
288
289                    }
290                });
291            }
292        }
293        if (!searchObj.isEmpty()) {
294            m_galleryDialog.enableSearchTab();
295        }
296        Scheduler.get().scheduleDeferred(new ScheduledCommand() {
297
298            public void execute() {
299
300                m_galleryDialog.updateSizes();
301            }
302        });
303    }
304
305    /**
306     * This method is called when preloaded sitemap tree state data is loaded.<p>
307     *
308     * @param sitemapPreloadData the sitemap preload data
309     */
310    public void onReceiveSitemapPreloadData(CmsSitemapEntryBean sitemapPreloadData) {
311
312        CmsSitemapTab sitemapTab = m_galleryDialog.getSitemapTab();
313        if (sitemapTab != null) {
314            sitemapTab.onReceiveSitemapPreloadData(sitemapPreloadData);
315        }
316    }
317
318    /**
319     * This method is called when preloaded VFS tree state data is loaded.<p>
320     *
321     * @param vfsPreloadData the preload data
322     * @param folders the set of selected folders
323     */
324    public void onReceiveVfsPreloadData(CmsVfsEntryBean vfsPreloadData, Set<String> folders) {
325
326        CmsVfsTab vfsTab = m_galleryDialog.getVfsTab();
327        if (vfsTab != null) {
328            vfsTab.onReceiveVfsPreloadData(vfsPreloadData);
329            if (folders != null) {
330                vfsTab.checkFolders(folders);
331            }
332        }
333    }
334
335    /**
336     * Called when the query is removed.
337     */
338    public void onRemoveQuery() {
339
340        m_galleryDialog.getResultsTab().removeQuery();
341
342    }
343
344    /**
345     * Called when the search scope is removed.
346     */
347    public void onRemoveScope() {
348
349        m_galleryDialog.getResultsTab().removeScope();
350
351    }
352
353    /**
354     * Removes a parameter from the search tab.<p>
355     *
356     * @param type the parameter type
357     */
358    public void onRemoveSearchParam(ParamType type) {
359
360        m_galleryDialog.getSearchTab().removeParameter(type);
361    }
362
363    /**
364     * Will be triggered when the results tab is selected.<p>
365     *
366     * @param searchObj the current search object
367     */
368    public void onResultTabSelection(CmsGallerySearchBean searchObj) {
369
370        m_galleryDialog.fillResultTab(searchObj);
371    }
372
373    /**
374     * Will be triggered when the types tab is selected.<p>
375     */
376    public void onTypesTabSelection() {
377
378        m_galleryDialog.getTypesTab().onContentChange();
379    }
380
381    /**
382     * Will be triggered when categories list is sorted.<p>
383     *
384     * @param categoriesList the updated categories list
385     * @param selectedCategories the selected categories
386     */
387    public void onUpdateCategoriesList(List<CmsCategoryBean> categoriesList, List<String> selectedCategories) {
388
389        m_galleryDialog.getCategoriesTab().updateContentList(categoriesList, selectedCategories);
390    }
391
392    /**
393     * Will be triggered when the tree is selected.<p>
394     *
395     * @param categoryTreeEntry the category root entry
396     * @param selectedCategories the selected categories
397     */
398    public void onUpdateCategoriesTree(List<CmsCategoryTreeEntry> categoryTreeEntry, List<String> selectedCategories) {
399
400        m_galleryDialog.getCategoriesTab().updateContentTree(categoryTreeEntry, selectedCategories);
401    }
402
403    /**
404     * Will be triggered when the sort parameters of the galleries list are changed.<p>
405     *
406     * @param galleries the updated galleries list
407     * @param selectedGalleries the list of galleries to select
408     * @param grouped true if the gallery list should be shown as groups
409     */
410    public void onUpdateGalleries(
411        List<CmsGalleryFolderBean> galleries,
412        List<String> selectedGalleries,
413        boolean grouped) {
414
415        m_galleryDialog.getGalleriesTab().updateListContent(galleries, selectedGalleries, grouped);
416    }
417
418    /**
419     * Updates the gallery tree.<p>
420     *
421     * @param galleryTreeEntries the gallery tree entries
422     * @param selectedGalleries the selected galleries
423     */
424    public void onUpdateGalleryTree(List<CmsGalleryTreeEntry> galleryTreeEntries, List<String> selectedGalleries) {
425
426        m_galleryDialog.getGalleriesTab().updateTreeContent(galleryTreeEntries, selectedGalleries);
427    }
428
429    /**
430     * Will be triggered when the sort parameters of the types list are changed.<p>
431     *
432     * @param types the updated types list
433     * @param selectedTypes the list of types to select
434     */
435    public void onUpdateTypes(List<CmsResourceTypeBean> types, List<String> selectedTypes) {
436
437        m_galleryDialog.getTypesTab().updateContent(types, selectedTypes);
438    }
439
440    /**
441     * @see com.google.gwt.event.logical.shared.ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)
442     */
443    public void onValueChange(ValueChangeEvent<CmsGallerySearchBean> event) {
444
445        if (!m_galleryDialog.getController().isSearchObjectEmpty()) {
446            m_galleryDialog.enableSearchTab();
447        } else {
448            m_galleryDialog.disableSearchTab();
449        }
450    }
451
452    /**
453     * Selects the result tab.<p>
454     */
455    public void selectResultTab() {
456
457        m_galleryDialog.selectTab(GalleryTabId.cms_tab_results, true);
458    }
459
460    /**
461     * Sets the list content of the category tab.<p>
462     *
463     * @param categoryRoot the root category tree entry
464     * @param selected the selected categories
465     */
466    public void setCategoriesTabContent(List<CmsCategoryTreeEntry> categoryRoot, List<String> selected) {
467
468        m_galleryDialog.getCategoriesTab().fillContent(categoryRoot, selected);
469    }
470
471    /**
472     * Sets the list content of the galleries tab.<p>
473     *
474     * @param galleryInfos the gallery info beans
475     * @param selectedGalleries the selected galleries
476     * @param useGroups true if the gallery list should be shown as groups
477     */
478    public void setGalleriesTabContent(
479        List<CmsGalleryFolderBean> galleryInfos,
480        List<String> selectedGalleries,
481        boolean useGroups) {
482
483        m_galleryDialog.getGalleriesTab().fillContent(galleryInfos, selectedGalleries, useGroups);
484    }
485
486    /**
487     * Sets the list content of the types tab.<p>
488     *
489     * @param typeInfos the type info beans
490     * @param selectedTypes the selected types
491     */
492    public void setTypesTabContent(List<CmsResourceTypeBean> typeInfos, List<String> selectedTypes) {
493
494        m_galleryDialog.getTypesTab().fillContent(typeInfos, selectedTypes);
495    }
496
497    /**
498     * Shows the first available tab.<p>
499     */
500    public void showFirstTab() {
501
502        m_galleryDialog.selectTab(0, false);
503    }
504
505    /**
506     * Updates the gallery data.<p>
507     *
508     * @param searchObj the current search object
509     * @param dialogBean the gallery data
510     * @param controller he gallery controller
511     */
512    public void updateGalleryData(
513        CmsGallerySearchBean searchObj,
514        CmsGalleryDataBean dialogBean,
515        CmsGalleryController controller) {
516
517        if ((m_galleryDialog.getGalleriesTab() != null) && (dialogBean.getGalleries() != null)) {
518            Collections.sort(dialogBean.getGalleries(), new CmsComparatorTitle(true));
519            setGalleriesTabContent(dialogBean.getGalleries(), searchObj.getGalleries(), false);
520        }
521        if ((m_galleryDialog.getTypesTab() != null) && (dialogBean.getTypes() != null)) {
522            setTypesTabContent(controller.getSearchTypes(), searchObj.getTypes());
523        }
524        if ((m_galleryDialog.getCategoriesTab() != null) && (dialogBean.getCategories() != null)) {
525            setCategoriesTabContent(dialogBean.getCategories(), searchObj.getCategories());
526        }
527    }
528
529}