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