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.ui.A_CmsUI;
032import org.opencms.ui.CmsVaadinUtils;
033import org.opencms.ui.apps.CmsFileExplorer;
034import org.opencms.ui.components.CmsErrorDialog;
035import org.opencms.ui.favorites.CmsFavoriteEntry.Type;
036import org.opencms.util.CmsUUID;
037
038import java.util.Optional;
039
040import com.vaadin.ui.Component;
041
042/**
043 * Context for the favorite dialog opened from the workplace.
044 */
045public class CmsExplorerFavoriteContext implements I_CmsFavoriteContext {
046
047    /** Current dialog instance. */
048    private Component m_dialog;
049
050    /** Favorite entry for current location. */
051    private CmsFavoriteEntry m_entry;
052
053    /**
054     * Creates a new instance.<p>
055     *
056     * @param cms the current CMS context
057     * @param explorer the current file explorer instance
058     */
059    public CmsExplorerFavoriteContext(CmsObject cms, CmsFileExplorer explorer) {
060
061        if (explorer != null) {
062            CmsUUID currentFolder = explorer.getCurrentFolder();
063            String siteRoot = cms.getRequestContext().getSiteRoot();
064            CmsUUID project = cms.getRequestContext().getCurrentProject().getId();
065            CmsFavoriteEntry entry = new CmsFavoriteEntry();
066            entry.setType(Type.explorerFolder);
067            entry.setSiteRoot(siteRoot);
068            entry.setStructureId(currentFolder);
069            entry.setProjectId(project);
070            m_entry = entry;
071        }
072    }
073
074    /**
075     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#close()
076     */
077    public void close() {
078
079        CmsVaadinUtils.getWindow(m_dialog).close();
080    }
081
082    /**
083     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#getFavoriteForCurrentLocation()
084     */
085    public Optional<CmsFavoriteEntry> getFavoriteForCurrentLocation() {
086
087        return Optional.ofNullable(m_entry);
088    }
089
090    /**
091     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#openFavorite(org.opencms.ui.favorites.CmsFavoriteEntry)
092     */
093    public void openFavorite(CmsFavoriteEntry entry) {
094
095        try {
096            String url = entry.updateContextAndGetFavoriteUrl(A_CmsUI.getCmsObject());
097            close(); // necessary for the case where we already are at the location of the entry
098            A_CmsUI.get().getPage().open(url, null);
099        } catch (Exception e) {
100            CmsErrorDialog.showErrorDialog(e);
101        }
102    }
103
104    /**
105     * @see org.opencms.ui.favorites.I_CmsFavoriteContext#setDialog(com.vaadin.ui.Component)
106     */
107    public void setDialog(Component component) {
108
109        m_dialog = component;
110    }
111
112}