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.actions;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.ui.I_CmsDialogContext;
036import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
037import org.opencms.ui.contextmenu.CmsStandardVisibilityCheck;
038
039import java.util.ArrayList;
040import java.util.Arrays;
041import java.util.List;
042
043import org.apache.commons.logging.Log;
044
045/**
046 * Class representing an abstract gallery dialog action.<p>
047 */
048public abstract class A_CmsGalleryDialogAction extends A_CmsWorkplaceAction {
049
050    /** Logger instance for this class. */
051    static final Log LOG = CmsLog.getLog(A_CmsGalleryDialogAction.class);
052
053    /**
054     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
055     */
056    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
057
058        if (resources.size() == 1) {
059            CmsResource resource = resources.get(0);
060            if (resource.isFolder()) {
061                String type = OpenCms.getResourceManager().getResourceType(resource).getTypeName();
062                if (Arrays.asList(getSupportedGalleryTypes()).contains(type)) {
063                    return CmsStandardVisibilityCheck.VISIBLE.getVisibility(cms, resources);
064                }
065            } else {
066                CmsResource parentGallery = getParentGallery(cms, resource);
067                if (parentGallery != null) {
068                    List<CmsResource> parentList = new ArrayList<CmsResource>();
069                    parentList.add(parentGallery);
070                    return getVisibility(cms, parentList);
071                }
072            }
073        }
074        return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
075    }
076
077    /**
078     * Returns the gallery.<p>
079     *
080     * @param context the dialog context
081     * @return the gallery
082     */
083    protected CmsResource getGallery(I_CmsDialogContext context) {
084
085        CmsResource resource = context.getResources().get(0);
086        return resource.isFolder() ? resource : getParentGallery(context.getCms(), resource);
087    }
088
089    /**
090     * Returns the gallery types supported by this dialog action.
091     *
092     * @return the gallery types
093     */
094    abstract protected String[] getSupportedGalleryTypes();
095
096    /**
097     * If the context of this gallery dialog action is a gallery item, returns the parent gallery.<p>
098     *
099     * @param cms the CMS object
100     * @param resource the resource that has opened the context menu
101     * @return the parent gallery
102     */
103    private CmsResource getParentGallery(CmsObject cms, CmsResource resource) {
104
105        String parentFolder = CmsResource.getParentFolder(resource.getRootPath());
106        parentFolder = cms.getRequestContext().removeSiteRoot(parentFolder);
107        CmsResource parentGallery = null;
108        try {
109            parentGallery = cms.readResource(parentFolder);
110        } catch (CmsException e) {
111            LOG.error(e.getLocalizedMessage(), e);
112        }
113        return parentGallery;
114    }
115}