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.CmsSitemapTab;
031import org.opencms.ade.galleries.shared.CmsSiteSelectorOption;
032import org.opencms.ade.galleries.shared.CmsSitemapEntryBean;
033import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
034import org.opencms.gwt.client.CmsCoreProvider;
035import org.opencms.gwt.client.util.I_CmsSimpleCallback;
036import org.opencms.util.CmsStringUtil;
037import org.opencms.util.CmsUUID;
038
039import java.util.HashSet;
040import java.util.LinkedHashMap;
041import java.util.List;
042import java.util.Set;
043
044/**
045 * Handler class for the sitemap tree tab.<p>
046 *
047 * @since 8.5.0
048 */
049public class CmsSitemapTabHandler extends A_CmsTabHandler {
050
051    /** The site root used for loading / saving tree state data. */
052    private String m_siteRoot;
053
054    /**
055     * Creates a new sitemap tab handler.<p>
056     *
057     * @param controller the gallery controller
058     */
059    public CmsSitemapTabHandler(CmsGalleryController controller) {
060
061        super(controller);
062    }
063
064    /**
065     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#clearParams()
066     */
067    @Override
068    public void clearParams() {
069
070        // nothing to do, no parameters from this tab
071    }
072
073    /**
074     * Gets the selected site root.<p>
075     *
076     * @return the selected site root
077     */
078    public String getDefaultSelectedSiteRoot() {
079
080        return m_controller.getDefaultSitemapTabSiteRoot();
081    }
082
083    /**
084     * Gets the path which is used when the sitemap entry is selected.<p>
085     *
086     * @param sitemapEntry the sitemap entry
087     *
088     * @return the path to use when the entry is selected
089     */
090    public String getSelectPath(CmsSitemapEntryBean sitemapEntry) {
091
092        String normalizedSiteRoot = CmsStringUtil.joinPaths(CmsCoreProvider.get().getSiteRoot(), "/");
093        String rootPath = sitemapEntry.getRootPath();
094        if (rootPath.startsWith(normalizedSiteRoot)) {
095            return rootPath.substring(normalizedSiteRoot.length() - 1);
096        }
097        return sitemapEntry.getRootPath();
098    }
099
100    /**
101     * Gets the select options for the sort list.<p>
102     *
103     * @return the select options for the sort list
104     */
105    public LinkedHashMap<String, String> getSortList() {
106
107        if (!m_controller.isShowSiteSelector() || !(m_controller.getSitemapSiteSelectorOptions().size() > 1)) {
108            return null;
109        }
110        LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
111        for (CmsSiteSelectorOption option : m_controller.getSitemapSiteSelectorOptions()) {
112            options.put(option.getSiteRoot(), option.getMessage());
113        }
114        return options;
115    }
116
117    /**
118     * Loads the sub entries for the given path.<p>
119     *
120     * @param rootPath the root path
121     * @param isRoot <code>true</code> if the requested entry is the root entry
122     * @param callback the callback to execute with the result
123     */
124    public void getSubEntries(
125        String rootPath,
126        boolean isRoot,
127        I_CmsSimpleCallback<List<CmsSitemapEntryBean>> callback) {
128
129        m_controller.getSubEntries(rootPath, isRoot, null, callback);
130    }
131
132    /**
133     * Returns if this tab should offer select resource buttons.<p>
134     *
135     * @return <code>true</code> if this tab should offer select resource buttons
136     */
137    public boolean hasSelectResource() {
138
139        return m_controller.hasSelectFolder();
140    }
141
142    /**
143     * Initializes the sitemap tab's content.<p>
144     */
145    public void initializeSitemapTab() {
146
147        String siteRoot = m_controller.getPreselectOption(
148            m_controller.getStartSiteRoot(),
149            m_controller.getSitemapSiteSelectorOptions());
150        getTab().setSortSelectBoxValue(siteRoot, true);
151        m_controller.getDefaultScope();
152        if (siteRoot == null) {
153            siteRoot = m_controller.getDefaultSitemapTabSiteRoot();
154        }
155        m_siteRoot = siteRoot;
156        getSubEntries(siteRoot, true, new I_CmsSimpleCallback<List<CmsSitemapEntryBean>>() {
157
158            public void execute(List<CmsSitemapEntryBean> result) {
159
160                getTab().fillDefault(result);
161                getTab().onContentChange();
162            }
163        });
164
165    }
166
167    /**
168     * This method is called when the tree open state changes.<p>
169     *
170     * @param openItemIds the structure ids of open entries
171     */
172    public void onChangeTreeState(Set<CmsUUID> openItemIds) {
173
174        m_controller.saveTreeState(I_CmsGalleryProviderConstants.TREE_SITEMAP, m_siteRoot, openItemIds);
175    }
176
177    /**
178     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#onSelection()
179     */
180    @Override
181    public void onSelection() {
182
183        if (getTab().isInitialized()) {
184            getTab().onContentChange();
185        } else {
186            initializeSitemapTab();
187        }
188    }
189
190    /**
191     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#onSort(java.lang.String, java.lang.String)
192     */
193    @Override
194    public void onSort(final String sortParams, String filter) {
195
196        m_controller.getSubEntries(sortParams, true, filter, new I_CmsSimpleCallback<List<CmsSitemapEntryBean>>() {
197
198            public void execute(List<CmsSitemapEntryBean> entries) {
199
200                getTab().fill(entries);
201                setSiteRoot(sortParams);
202                onChangeTreeState(new HashSet<CmsUUID>());
203            }
204        });
205
206    }
207
208    /**
209     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#removeParam(java.lang.String)
210     */
211    @Override
212    public void removeParam(String paramKey) {
213
214        // nothing to do, no parameters from this tab
215    }
216
217    /**
218     * Returns the sitemap tab.<p>
219     *
220     * @return the sitemap tab
221     */
222    protected CmsSitemapTab getTab() {
223
224        return m_controller.m_handler.m_galleryDialog.getSitemapTab();
225    }
226
227    /**
228     * Setter for the site root attribute.<p>
229     *
230     * @param siteRoot the new value for the site root attribute
231     */
232    protected void setSiteRoot(String siteRoot) {
233
234        m_siteRoot = siteRoot;
235    }
236}