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.favorites;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsLog;
032import org.opencms.ui.A_CmsUI;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.I_CmsDialogContext.ContextType;
035import org.opencms.ui.apps.CmsExplorerDialogContext;
036import org.opencms.ui.apps.CmsFileExplorer;
037import org.opencms.ui.components.CmsErrorDialog;
038import org.opencms.ui.components.CmsExtendedSiteSelector.SiteSelectorOption;
039import org.opencms.ui.dialogs.CmsProjectSelectDialog;
040import org.opencms.ui.favorites.CmsFavoriteEntry.Type;
041import org.opencms.util.CmsUUID;
042
043import java.util.ArrayList;
044import java.util.Optional;
045
046import org.apache.commons.logging.Log;
047
048import com.vaadin.ui.Component;
049
050/**
051 * Context for the favorite dialog opened from the workplace.
052 */
053public class CmsExplorerFavoriteContext implements I_CmsFavoriteContext {
054
055    /** The logger instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsExplorerFavoriteContext.class);
057
058    /** Current dialog instance. */
059    private Component m_dialog;
060
061    /** Favorite entry for current location. */
062    private CmsFavoriteEntry m_entry;
063
064    /** The active explorer instance. */
065    private CmsFileExplorer m_explorer;
066
067    /**
068     * Creates a new instance.<p>
069     *
070     * @param cms the current CMS context
071     * @param explorer the current file explorer instance
072     */
073    public CmsExplorerFavoriteContext(CmsObject cms, CmsFileExplorer explorer) {
074
075        if (explorer != null) {
076            m_explorer = explorer;
077            CmsUUID currentFolder = explorer.getCurrentFolder();
078            String siteRoot = cms.getRequestContext().getSiteRoot();
079            CmsUUID project = cms.getRequestContext().getCurrentProject().getId();
080            CmsFavoriteEntry entry = new CmsFavoriteEntry();
081            entry.setType(Type.explorerFolder);
082            entry.setSiteRoot(siteRoot);
083            entry.setStructureId(currentFolder);
084            entry.setProjectId(project);
085            m_entry = entry;
086        }
087    }
088
089    /**
090     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#changeProject(org.opencms.util.CmsUUID)
091     */
092    public void changeProject(CmsUUID value) {
093
094        close();
095        CmsExplorerDialogContext context = new CmsExplorerDialogContext(
096            ContextType.fileTable,
097            null,
098            m_explorer,
099            new ArrayList<>());
100        CmsProjectSelectDialog.changeSiteOrProject(context, value, null);
101    }
102
103    /**
104     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#changeSite(org.opencms.ui.components.CmsExtendedSiteSelector.SiteSelectorOption)
105     */
106    public void changeSite(SiteSelectorOption value) {
107
108        close();
109        CmsExplorerDialogContext context = new CmsExplorerDialogContext(
110            ContextType.fileTable,
111            null,
112            m_explorer,
113            new ArrayList<>());
114        CmsProjectSelectDialog.changeSiteOrProject(context, null, value);
115    }
116
117    /**
118     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#close()
119     */
120    public void close() {
121
122        CmsVaadinUtils.getWindow(m_dialog).close();
123    }
124
125    /**
126     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#getFavoriteForCurrentLocation()
127     */
128    public Optional<CmsFavoriteEntry> getFavoriteForCurrentLocation() {
129
130        return Optional.ofNullable(m_entry);
131    }
132
133    /**
134     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#openFavorite(org.opencms.ui.favorites.CmsFavoriteEntry)
135     */
136    public void openFavorite(CmsFavoriteEntry entry) {
137
138        try {
139            String url = entry.updateContextAndGetFavoriteUrl(A_CmsUI.getCmsObject());
140            close(); // necessary for the case where we already are at the location of the entry
141            A_CmsUI.get().getPage().open(url, null);
142        } catch (Exception e) {
143            CmsErrorDialog.showErrorDialog(e);
144        }
145    }
146
147    /**
148     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#setDialog(com.vaadin.ui.Component)
149     */
150    public void setDialog(Component component) {
151
152        m_dialog = component;
153    }
154
155}