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.ui.apps;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsProject;
032import org.opencms.file.CmsResource;
033import org.opencms.file.CmsResourceFilter;
034import org.opencms.main.CmsException;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.components.CmsErrorDialog;
038import org.opencms.ui.components.CmsFileTable;
039import org.opencms.ui.components.CmsFileTableDialogContext;
040import org.opencms.util.CmsUUID;
041
042import java.util.Collection;
043import java.util.List;
044
045/**
046 * Dialog context for the explorer.<p>
047 */
048public class CmsExplorerDialogContext extends CmsFileTableDialogContext {
049
050    /** The explorer instance. */
051    private CmsFileExplorer m_explorer;
052
053    /**
054     * Creates a new instance.<p>
055     *
056     * @param contextType the context type
057     * @param fileTable the file table
058     * @param explorer the explorer app instance
059     * @param resources the list of selected resources
060     */
061    public CmsExplorerDialogContext(
062        ContextType contextType,
063        CmsFileTable fileTable,
064        CmsFileExplorer explorer,
065        List<CmsResource> resources) {
066        super(CmsFileExplorerConfiguration.APP_ID, contextType, fileTable, resources);
067        m_explorer = explorer;
068    }
069
070    /**
071     * @see org.opencms.ui.A_CmsDialogContext#finish(org.opencms.file.CmsProject, java.lang.String)
072     */
073    @Override
074    public void finish(CmsProject project, String siteRoot) {
075
076        finish(null);
077        m_explorer.onSiteOrProjectChange(project, siteRoot);
078    }
079
080    /**
081     * @see org.opencms.ui.I_CmsDialogContext#finish(java.util.Collection)
082     */
083    @Override
084    public void finish(Collection<CmsUUID> ids) {
085
086        closeWindow();
087        CmsAppWorkplaceUi.get().enableGlobalShortcuts();
088        if (ids != null) {
089            for (CmsUUID id : ids) {
090                if (id.isNullUUID()) {
091                    m_explorer.updateAll(false);
092                    return;
093                }
094            }
095            m_explorer.update(ids);
096        }
097    }
098
099    /**
100     * @see org.opencms.ui.I_CmsDialogContext#focus(org.opencms.util.CmsUUID)
101     */
102    @Override
103    public void focus(CmsUUID cmsUUID) {
104
105        try {
106            CmsObject cms = A_CmsUI.getCmsObject();
107            CmsResource res = cms.readResource(cmsUUID, CmsResourceFilter.ALL);
108            String rootPath = res.getRootPath();
109            String siteRoot = OpenCms.getSiteManager().getSiteRoot(rootPath);
110            String sitePath = null;
111            if (siteRoot == null) {
112                if (OpenCms.getSiteManager().startsWithShared(rootPath)) {
113                    siteRoot = OpenCms.getSiteManager().getSharedFolder();
114                    sitePath = CmsResource.getParentFolder(
115                        rootPath.substring(OpenCms.getSiteManager().getSharedFolder().length()));
116                } else {
117                    sitePath = CmsResource.getParentFolder(rootPath);
118                    siteRoot = "";
119                }
120            } else {
121                CmsObject otherSiteCms = OpenCms.initCmsObject(cms);
122                otherSiteCms.getRequestContext().setSiteRoot(siteRoot);
123                sitePath = otherSiteCms.getRequestContext().removeSiteRoot(CmsResource.getParentFolder(rootPath));
124            }
125            m_explorer.changeSite(siteRoot, sitePath, true);
126        } catch (CmsException e) {
127            CmsErrorDialog.showErrorDialog(e);
128        }
129    }
130
131    /**
132     * @see org.opencms.ui.components.CmsFileTableDialogContext#updateUserInfo()
133     */
134    @Override
135    public void updateUserInfo() {
136
137        m_explorer.m_appContext.updateUserInfo();
138    }
139}