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.CmsVfsTab;
031import org.opencms.ade.galleries.shared.CmsSiteSelectorOption;
032import org.opencms.ade.galleries.shared.CmsVfsEntryBean;
033import org.opencms.ade.galleries.shared.I_CmsGalleryProviderConstants;
034import org.opencms.gwt.client.CmsCoreProvider;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.util.CmsUUID;
037
038import java.util.Collection;
039import java.util.Collections;
040import java.util.HashSet;
041import java.util.LinkedHashMap;
042import java.util.List;
043
044import com.google.gwt.user.client.rpc.AsyncCallback;
045
046/**
047 * Handler class for the VFS tree tab.<p>
048 *
049 * @since 8.0.0
050 */
051public class CmsVfsTabHandler extends A_CmsTabHandler {
052
053    /** The structure ids of open entries to save. */
054    Collection<CmsUUID> m_openItemIds = new HashSet<CmsUUID>();
055
056    /** The VFS tab which this handler belongs to. */
057    CmsVfsTab m_tab;
058
059    /** The site root to use for loading / saving tree state. */
060    private String m_siteRoot;
061
062    /**
063     * Creates a new VFS tab handler.<p>
064     *
065     * @param controller the gallery controller
066     */
067    public CmsVfsTabHandler(CmsGalleryController controller) {
068
069        super(controller);
070
071    }
072
073    /**
074     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#clearParams()
075     */
076    @Override
077    public void clearParams() {
078
079        m_controller.clearFolders(false);
080    }
081
082    /**
083     * Gets the path which should be set as a value when a VFS entry is selected in the VFS tab.<p>
084     *
085     * @param vfsEntry the VFS entry which has been selected
086     *
087     * @return the selection path for the given VFS entry
088     */
089    public String getSelectPath(CmsVfsEntryBean vfsEntry) {
090
091        String normalizedSiteRoot = CmsStringUtil.joinPaths(CmsCoreProvider.get().getSiteRoot(), "/");
092        String rootPath = vfsEntry.getRootPath();
093        if (rootPath.startsWith(normalizedSiteRoot)) {
094            return rootPath.substring(normalizedSiteRoot.length() - 1);
095        }
096        return vfsEntry.getRootPath();
097    }
098
099    /**
100     * Gets the sort list for the tab.<p>
101     *
102     * @return the sort list for the tab
103     */
104    public LinkedHashMap<String, String> getSortList() {
105
106        if (!m_controller.isShowSiteSelector() || !(m_controller.getVfsSiteSelectorOptions().size() > 1)) {
107            return null;
108        }
109        LinkedHashMap<String, String> options = new LinkedHashMap<String, String>();
110        for (CmsSiteSelectorOption option : m_controller.getVfsSiteSelectorOptions()) {
111            options.put(option.getSiteRoot(), option.getMessage());
112        }
113        return options;
114
115    }
116
117    /**
118     * Gets the sub-folders of a given folder.<p>
119     *
120     * @param path the path of the folder whose subfolders should be retrieved
121     * @param callback the callback for processing the subfolders
122     */
123    public void getSubFolders(String path, AsyncCallback<List<CmsVfsEntryBean>> callback) {
124
125        m_controller.getSubFolders(path, callback);
126    }
127
128    /**
129     * Returns if this tab should offer select resource buttons.<p>
130     *
131     * @return <code>true</code> if this tab should offer select resource buttons
132     */
133    public boolean hasSelectResource() {
134
135        return m_controller.hasSelectFolder();
136    }
137
138    /**
139     * This method is called when the tree open state is changed.<p>
140     *
141     * @param openItemIds the structure ids of open tree items
142     */
143    public void onChangeTreeState(Collection<CmsUUID> openItemIds) {
144
145        m_openItemIds = openItemIds;
146        saveTreeState();
147    }
148
149    /**
150     * This method is called when a folder is selected or deselected in the VFS tab.<p>
151     *
152     * @param folder the folder which is selected or deselected
153     *
154     * @param selected true if the folder has been selected, false if it has been deselected
155     */
156    public void onSelectFolder(String folder, boolean selected) {
157
158        if (selected) {
159            m_controller.addFolder(folder);
160        } else {
161            m_controller.removeFolder(folder);
162        }
163    }
164
165    /**
166     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#onSelection()
167     */
168    @Override
169    public void onSelection() {
170
171        if (m_tab.isInitialized()) {
172            m_tab.onContentChange();
173        } else {
174            String siteRoot = m_controller.getPreselectOption(
175                m_controller.getStartSiteRoot(),
176                m_controller.getVfsSiteSelectorOptions());
177            m_tab.setSortSelectBoxValue(siteRoot, true);
178            if (siteRoot == null) {
179                siteRoot = m_controller.getDefaultVfsTabSiteRoot();
180            }
181            m_siteRoot = siteRoot;
182            m_controller.loadVfsEntryBean(siteRoot, null, new AsyncCallback<CmsVfsEntryBean>() {
183
184                public void onFailure(Throwable caught) {
185
186                    // will never be called
187                }
188
189                public void onSuccess(CmsVfsEntryBean result) {
190
191                    m_tab.fillInitially(Collections.singletonList(result));
192                    m_tab.onContentChange();
193
194                }
195            });
196
197        }
198    }
199
200    /**
201     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#onSort(java.lang.String,java.lang.String)
202     */
203    @Override
204    public void onSort(final String sortParams, String filter) {
205
206        m_controller.loadVfsEntryBean(sortParams, filter, new AsyncCallback<CmsVfsEntryBean>() {
207
208            public void onFailure(Throwable caught) {
209
210                // will never be called.
211            }
212
213            public void onSuccess(CmsVfsEntryBean result) {
214
215                m_tab.fillInitially(Collections.singletonList(result));
216                setSiteRoot(sortParams);
217                m_openItemIds.clear();
218                if (result != null) {
219                    m_openItemIds.add(result.getStructureId());
220                }
221                saveTreeState();
222            }
223
224        });
225        m_controller.clearFolders(true);
226    }
227
228    /**
229     * @see org.opencms.ade.galleries.client.A_CmsTabHandler#removeParam(java.lang.String)
230     */
231    @Override
232    public void removeParam(String paramKey) {
233
234        m_controller.removeFolderParam(paramKey);
235    }
236
237    /**
238     * Sets the tab which this handler is bound to.<p>
239     *
240     * @param tab the VFS tab
241     */
242    public void setTab(CmsVfsTab tab) {
243
244        m_tab = tab;
245    }
246
247    /**
248     * Saves the tree state.<p>
249     */
250    protected void saveTreeState() {
251
252        m_controller.saveTreeState(
253            I_CmsGalleryProviderConstants.TREE_VFS,
254            m_siteRoot,
255            new HashSet<CmsUUID>(m_openItemIds));
256    }
257
258    /**
259     * Sets the site root to use for loading/saving tree state.<p>
260     *
261     * @param siteRoot the site root to set
262     */
263    protected void setSiteRoot(String siteRoot) {
264
265        m_siteRoot = siteRoot;
266    }
267
268}