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.CmsSitemapTabHandler;
031import org.opencms.ade.galleries.shared.CmsGallerySearchBean;
032import org.opencms.ade.galleries.shared.CmsSitemapEntryBean;
033import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants.GalleryTabId;
034import org.opencms.gwt.client.ui.CmsList;
035import org.opencms.gwt.client.ui.I_CmsListItem;
036import org.opencms.gwt.client.ui.input.A_CmsSelectBox;
037import org.opencms.gwt.client.ui.input.CmsFilterSelectBox;
038import org.opencms.gwt.client.ui.input.category.CmsDataValue;
039import org.opencms.gwt.client.ui.tree.A_CmsLazyOpenHandler;
040import org.opencms.gwt.client.ui.tree.CmsLazyTree;
041import org.opencms.gwt.client.ui.tree.CmsLazyTreeItem;
042import org.opencms.gwt.client.util.I_CmsSimpleCallback;
043import org.opencms.util.CmsStringUtil;
044import org.opencms.util.CmsUUID;
045
046import java.util.ArrayList;
047import java.util.Collections;
048import java.util.HashSet;
049import java.util.LinkedHashMap;
050import java.util.List;
051import java.util.Map;
052import java.util.Set;
053
054import com.google.gwt.event.dom.client.ClickEvent;
055import com.google.gwt.event.dom.client.ClickHandler;
056import com.google.gwt.event.logical.shared.CloseEvent;
057import com.google.gwt.event.logical.shared.CloseHandler;
058import com.google.gwt.event.logical.shared.OpenEvent;
059import com.google.gwt.event.logical.shared.OpenHandler;
060
061/**
062 * The tab widget for selecting sitemap entries.<p>
063 *
064 * @since 8.5.0
065 */
066public class CmsSitemapTab extends A_CmsListTab {
067
068    /** The tab handler. */
069    CmsSitemapTabHandler m_handler;
070
071    /** Flag to disable the fillDefault method (used when the tab is filled in some other way). */
072    private boolean m_disableFillDefault;
073
074    /** The initialized flag. */
075    private boolean m_initialized;
076
077    /** The list of sitemap tree items. */
078    private List<CmsLazyTreeItem> m_items = new ArrayList<CmsLazyTreeItem>();
079
080    /**
081     * Constructor.<p>
082     *
083     * @param handler the tab handler
084     */
085    public CmsSitemapTab(CmsSitemapTabHandler handler) {
086
087        super(GalleryTabId.cms_tab_sitemap);
088        m_handler = handler;
089        init();
090    }
091
092    /**
093     * Sets the initial folders in the VFS tab.<p>
094     *
095     * @param entries the root folders to display
096     */
097    public void fill(List<CmsSitemapEntryBean> entries) {
098
099        clear();
100        for (CmsSitemapEntryBean entry : entries) {
101            CmsLazyTreeItem item = createItem(entry);
102            addWidgetToList(item);
103        }
104        m_initialized = true;
105        onContentChange();
106    }
107
108    /**
109     * Default way to fill the sitemap tab.<p>
110     *
111     * @param entries the entries to fill the tab with
112     */
113    public void fillDefault(List<CmsSitemapEntryBean> entries) {
114
115        if (!m_disableFillDefault) {
116            fill(entries);
117            selectSite(m_handler.getDefaultSelectedSiteRoot());
118        }
119    }
120
121    /**
122     * Fills the sitemap tab with preloaded data.<p>
123     *
124     * @param entries the preloaded sitemap entries
125     */
126    public void fillWithPreloadInfo(List<CmsSitemapEntryBean> entries) {
127
128        fill(entries);
129        m_disableFillDefault = true;
130    }
131
132    /**
133     * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getParamPanels(org.opencms.ade.galleries.shared.CmsGallerySearchBean)
134     */
135    @Override
136    public List<CmsSearchParamPanel> getParamPanels(CmsGallerySearchBean searchObj) {
137
138        return Collections.emptyList();
139    }
140
141    /**
142     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#hasQuickFilter()
143     */
144    @Override
145    public boolean hasQuickFilter() {
146
147        return true;
148    }
149
150    /**
151     * Returns if the tab content has been initialized.<p>
152     *
153     * @return <code>true</code> if the tab content has been initialized
154     */
155    public boolean isInitialized() {
156
157        return m_initialized;
158    }
159
160    /**
161     * @see com.google.gwt.user.client.ui.Widget#onLoad()
162     */
163    @Override
164    public void onLoad() {
165
166        m_handler.initializeSitemapTab();
167
168    }
169
170    /**
171     * Method which is called when the sitemap preload data is received.<p>
172     *
173     * @param sitemapPreloadData the sitemap tree's preloaded root entry
174     */
175    public void onReceiveSitemapPreloadData(CmsSitemapEntryBean sitemapPreloadData) {
176
177        fillWithPreloadInfo(Collections.singletonList(sitemapPreloadData));
178        String siteRoot = sitemapPreloadData.getSiteRoot();
179        if (siteRoot != null) {
180            selectSite(siteRoot);
181        }
182    }
183
184    /**
185     * Clears the contents of the tab and resets the mapping from tree items to VFS beans.<p>
186     */
187    protected void clear() {
188
189        clearList();
190        m_items.clear();
191    }
192
193    /**
194     * Helper method for creating a VFS tree item widget from a VFS entry bean.<p>
195     *
196     * @param sitemapEntry the VFS entry bean
197     *
198     * @return the tree item widget
199     */
200    protected CmsLazyTreeItem createItem(final CmsSitemapEntryBean sitemapEntry) {
201
202        CmsDataValue dataValue = new CmsDataValue(
203            600,
204            3,
205            sitemapEntry.getSmallIconClasses(),
206            sitemapEntry.getDisplayName());
207        dataValue.setUnselectable();
208        if (sitemapEntry.isHiddenEntry()) {
209            dataValue.setColor("#aaaaaa");
210        }
211        dataValue.setSearchMatch(sitemapEntry.isSearchMatch());
212
213        CmsLazyTreeItem result = new CmsLazyTreeItem(dataValue, true);
214        result.setData(sitemapEntry);
215        if (getTabHandler().hasSelectResource()) {
216            dataValue.addButton(
217                createSelectResourceButton(
218                    m_handler.getSelectPath(sitemapEntry),
219                    sitemapEntry.getStructureId(),
220                    sitemapEntry.getDisplayName(),
221                    sitemapEntry.getType()));
222        }
223        result.setLeafStyle(!sitemapEntry.isFolder());
224        result.setSmallView(true);
225        if (sitemapEntry.hasChildren()) {
226            for (CmsSitemapEntryBean child : sitemapEntry.getChildren()) {
227                result.addChild(createItem(child));
228            }
229            result.setOpen(true, false);
230            result.onFinishLoading();
231        }
232        if ((sitemapEntry.getChildren() != null) && sitemapEntry.getChildren().isEmpty()) {
233            result.setLeafStyle(true);
234        }
235        m_items.add(result);
236        dataValue.addClickHandler(new ClickHandler() {
237
238            public void onClick(ClickEvent e) {
239
240                if (getTabHandler().hasSelectResource()) {
241                    getTabHandler().selectResource(
242                        m_handler.getSelectPath(sitemapEntry),
243                        sitemapEntry.getStructureId(),
244                        sitemapEntry.getDisplayName(),
245                        sitemapEntry.getType());
246                }
247            }
248        });
249        return result;
250    }
251
252    /**
253     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#createScrollList()
254     */
255    @Override
256    protected CmsList<? extends I_CmsListItem> createScrollList() {
257
258        CmsLazyTree<CmsLazyTreeItem> result = new CmsLazyTree<CmsLazyTreeItem>(
259            new A_CmsLazyOpenHandler<CmsLazyTreeItem>() {
260
261                /**
262                 * @see org.opencms.gwt.client.ui.tree.I_CmsLazyOpenHandler#load(org.opencms.gwt.client.ui.tree.CmsLazyTreeItem, java.lang.Runnable)
263                 */
264                public void load(final CmsLazyTreeItem target, Runnable loadCallback) {
265
266                    CmsSitemapEntryBean entry = target.getData();
267                    I_CmsSimpleCallback<List<CmsSitemapEntryBean>> callback = new I_CmsSimpleCallback<List<CmsSitemapEntryBean>>() {
268
269                        public void execute(List<CmsSitemapEntryBean> loadedEntries) {
270
271                            for (CmsSitemapEntryBean childEntry : loadedEntries) {
272                                CmsLazyTreeItem item = createItem(childEntry);
273                                target.addChild(item);
274                            }
275                            target.onFinishLoading();
276                            loadCallback.run();
277                        }
278                    };
279
280                    getTabHandler().getSubEntries(entry.getRootPath(), false, callback);
281                }
282
283                @Override
284                public void onFinishOpen(CmsLazyTreeItem target) {
285
286                    onContentChange();
287                }
288            });
289        result.addOpenHandler(new OpenHandler<CmsLazyTreeItem>() {
290
291            public void onOpen(OpenEvent<CmsLazyTreeItem> event) {
292
293                CmsLazyTreeItem target = event.getTarget();
294                CmsSitemapEntryBean entry = target.getData();
295                Set<CmsUUID> openItemIds = getOpenItemIds();
296                openItemIds.add(entry.getStructureId());
297                m_handler.onChangeTreeState(openItemIds);
298                onContentChange();
299            }
300
301        });
302        result.addCloseHandler(new CloseHandler<CmsLazyTreeItem>() {
303
304            public void onClose(CloseEvent<CmsLazyTreeItem> event) {
305
306                CmsLazyTreeItem target = event.getTarget();
307                Set<CmsUUID> openItemIds = getOpenItemIds();
308                CmsSitemapEntryBean entry = target.getData();
309                openItemIds.remove(entry.getStructureId());
310                m_handler.onChangeTreeState(openItemIds);
311            }
312        });
313        return result;
314
315    }
316
317    /**
318     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#createSelectBox(java.util.LinkedHashMap)
319     */
320    @Override
321    protected A_CmsSelectBox<?> createSelectBox(LinkedHashMap<String, String> options) {
322
323        return new CmsFilterSelectBox(options);
324    }
325
326    /**
327     * @see org.opencms.ade.galleries.client.ui.A_CmsListTab#getSortList()
328     */
329    @Override
330    protected LinkedHashMap<String, String> getSortList() {
331
332        return m_handler.getSortList();
333
334    }
335
336    /**
337     * @see org.opencms.ade.galleries.client.ui.A_CmsTab#getTabHandler()
338     */
339    @Override
340    protected CmsSitemapTabHandler getTabHandler() {
341
342        return m_handler;
343    }
344
345    /**
346     * Collects the structure ids belonging to open tree entries.<p>
347     *
348     * @return the collected set of structure ids
349     */
350    Set<CmsUUID> getOpenItemIds() {
351
352        Set<CmsUUID> result = new HashSet<CmsUUID>();
353        for (CmsLazyTreeItem item : m_items) {
354            CmsSitemapEntryBean entryBean = item.getData();
355            if (item.isOpen()) {
356                result.add(entryBean.getStructureId());
357            }
358        }
359        return result;
360    }
361
362    /**
363     * Selects a specific site root.<p>
364     *
365     * @param siteRoot the site root to select
366     */
367    private void selectSite(String siteRoot) {
368
369        if (m_sortSelectBox != null) {
370            Map<String, String> options = ((CmsFilterSelectBox)m_sortSelectBox).getItems();
371            String option = null;
372            for (Map.Entry<String, String> entry : options.entrySet()) {
373                if (CmsStringUtil.comparePaths(entry.getKey(), siteRoot)) {
374                    option = entry.getKey();
375                    break;
376                }
377            }
378            if (option != null) {
379                m_sortSelectBox.setFormValue(option, false);
380            }
381        }
382    }
383
384}