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.CmsProperty;
032import org.opencms.file.CmsPropertyDefinition;
033import org.opencms.file.CmsResource;
034import org.opencms.gwt.shared.CmsCoreData.AdeContext;
035import org.opencms.gwt.shared.CmsGwtConstants;
036import org.opencms.loader.CmsTemplateContextManager;
037import org.opencms.loader.I_CmsTemplateContextProvider;
038import org.opencms.main.CmsLog;
039import org.opencms.main.OpenCms;
040import org.opencms.ui.I_CmsDialogContext;
041import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
042
043import java.util.List;
044import java.util.Map;
045
046import org.apache.commons.logging.Log;
047
048/**
049 * Template context selection action.
050 *
051 * <p>This is handled specially by the client side code.
052 */
053public class CmsTemplateContextsAction extends A_CmsWorkplaceAction implements I_CmsADEAction {
054
055    /** Logger instance for this class. */
056    private static final Log LOG = CmsLog.getLog(CmsTemplateContextsAction.class);
057
058    /** Special integer that indicates one of several positions in the context menu. Currently can only be 0 or 1. */
059    private int m_menuPosition;
060
061    /**
062     * Creates a new instance.
063     *
064     * @param menuPosition the position in the context menu
065     */
066    public CmsTemplateContextsAction(int menuPosition) {
067
068        if ((menuPosition != 0) && (menuPosition != 1)) {
069            throw new IllegalArgumentException("Menu position must be 0 or 1");
070        }
071        m_menuPosition = menuPosition;
072
073    }
074
075    /**
076     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#executeAction(org.opencms.ui.I_CmsDialogContext)
077     */
078    public void executeAction(I_CmsDialogContext context) {
079
080        // not supported
081    }
082
083    /**
084     * @see org.opencms.ui.actions.I_CmsADEAction#getCommandClassName()
085     */
086    public String getCommandClassName() {
087
088        return CmsGwtConstants.TEMPLATECONTEXT_MENU_PLACEHOLDER;
089    }
090
091    /**
092     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
093     */
094    public String getId() {
095
096        return CmsGwtConstants.ACTION_TEMPLATECONTEXTS + "_" + m_menuPosition;
097    }
098
099    /**
100     * @see org.opencms.ui.actions.I_CmsADEAction#getJspPath()
101     */
102    public String getJspPath() {
103
104        return null;
105    }
106
107    /**
108     * @see org.opencms.ui.actions.I_CmsADEAction#getParams()
109     */
110    public Map<String, String> getParams() {
111
112        return null;
113    }
114
115    /**
116     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
117     */
118    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
119
120        return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
121    }
122
123    /**
124     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getVisibility(org.opencms.ui.I_CmsDialogContext)
125     */
126    @Override
127    public CmsMenuItemVisibilityMode getVisibility(I_CmsDialogContext context) {
128
129        if (!AdeContext.pageeditor.name().equals(context.getAppId())) {
130            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
131        }
132        List<CmsResource> resources = context.getResources();
133        if (resources.size() != 1) {
134            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
135        }
136        CmsObject cms = context.getCms();
137        CmsResource resource = resources.get(0);
138
139        try {
140            List<CmsProperty> properties = cms.readPropertyObjects(resource, true);
141            // this menu entry is only available in the container page editor, so we know we have to use the template property,
142            // not template-elements
143            CmsProperty templateProp = CmsProperty.get(CmsPropertyDefinition.PROPERTY_TEMPLATE, properties);
144            if ((templateProp != null) && !templateProp.isNullProperty()) {
145                String propertyValue = templateProp.getValue();
146                if (CmsTemplateContextManager.hasPropertyPrefix(propertyValue)) {
147                    I_CmsTemplateContextProvider provider = OpenCms.getTemplateContextManager().getTemplateContextProvider(
148                        CmsTemplateContextManager.removePropertyPrefix(propertyValue));
149                    if (provider != null) {
150                        if (provider.getMenuPosition() != m_menuPosition) {
151                            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
152                        }
153                        if (!provider.shouldShowContextMenuOption(cms)) {
154                            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
155                        }
156                    }
157                }
158            }
159        } catch (Exception e) {
160            LOG.error(e.getLocalizedMessage(), e);
161
162        }
163        return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
164
165    }
166
167    /**
168     * @see org.opencms.ui.actions.I_CmsADEAction#isAdeSupported()
169     */
170    public boolean isAdeSupported() {
171
172        return true;
173    }
174
175    /**
176     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
177     */
178    @Override
179    protected String getTitleKey() {
180
181        return "TEMPLATECONTEXTS";
182    }
183}