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.file.types.CmsResourceTypeXmlContainerPage;
033import org.opencms.file.types.CmsResourceTypeXmlContent;
034import org.opencms.main.CmsException;
035import org.opencms.main.OpenCms;
036import org.opencms.ui.A_CmsUI;
037import org.opencms.ui.I_CmsDialogContext;
038import org.opencms.ui.Messages;
039import org.opencms.ui.apps.A_CmsWorkplaceApp;
040import org.opencms.ui.apps.CmsAppWorkplaceUi;
041import org.opencms.ui.apps.CmsEditor;
042import org.opencms.ui.apps.lists.CmsListManager;
043import org.opencms.ui.apps.lists.CmsListManagerConfiguration;
044import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
045import org.opencms.ui.contextmenu.CmsMenuItemVisibilitySingleOnly;
046import org.opencms.ui.contextmenu.CmsStandardVisibilityCheck;
047import org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility;
048import org.opencms.util.CmsStringUtil;
049
050import java.util.List;
051
052/**
053 * The display action. Renders the selected resource.<p>
054 */
055public class CmsDisplayAction extends A_CmsWorkplaceAction implements I_CmsDefaultAction {
056
057    /** The name of the online version window. */
058    public static final String ONLINE_WINDOW_NAME = "_blank";
059
060    /** The action id. */
061    public static final String ACTION_ID = "display";
062
063    /** The action visibility. */
064    public static final I_CmsHasMenuItemVisibility VISIBILITY = new CmsMenuItemVisibilitySingleOnly(
065        CmsStandardVisibilityCheck.EDIT);
066
067    /**
068     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#executeAction(org.opencms.ui.I_CmsDialogContext)
069     */
070    public void executeAction(I_CmsDialogContext context) {
071
072        if (context.getResources().size() == 1) {
073            CmsResource resource = context.getResources().get(0);
074            if (OpenCms.getResourceManager().getResourceType(resource).getTypeName().equals(
075                CmsListManager.RES_TYPE_LIST_CONFIG)) {
076                CmsAppWorkplaceUi.get().showApp(
077                    OpenCms.getWorkplaceAppManager().getAppConfiguration(CmsListManagerConfiguration.APP_ID),
078                    A_CmsWorkplaceApp.addParamToState(
079                        "",
080                        CmsEditor.RESOURCE_ID_PREFIX,
081                        resource.getStructureId().toString()));
082            } else {
083
084                CmsObject cms = context.getCms();
085                try {
086                    cms = OpenCms.initCmsObject(cms);
087                    cms.getRequestContext().setUri(cms.getSitePath(resource));
088                } catch (CmsException e) {
089                    // It's ok, we stick to the original context.
090                }
091
092                boolean isOnline = cms.getRequestContext().getCurrentProject().isOnlineProject();
093                String link;
094                if (isOnline
095                    && !(CmsStringUtil.isEmptyOrWhitespaceOnly(cms.getRequestContext().getSiteRoot())
096                        || OpenCms.getSiteManager().isSharedFolder(cms.getRequestContext().getSiteRoot()))) {
097                    // use the online link only in case the current site is not the root site or the shared folder
098                    link = OpenCms.getLinkManager().getOnlineLink(cms, cms.getSitePath(resource));
099                } else {
100                    link = OpenCms.getLinkManager().substituteLink(cms, resource);
101                }
102                if (isOnline
103                    || !(OpenCms.getResourceManager().getResourceType(resource) instanceof CmsResourceTypeXmlContent)) {
104                    A_CmsUI.get().openPageOrWarn(link, ONLINE_WINDOW_NAME);
105                } else {
106                    A_CmsUI.get().getPage().setLocation(link);
107                }
108            }
109        }
110    }
111
112    /**
113     * @see org.opencms.ui.actions.I_CmsDefaultAction#getDefaultActionRank(org.opencms.ui.I_CmsDialogContext)
114     */
115    public int getDefaultActionRank(I_CmsDialogContext context) {
116
117        return 10;
118    }
119
120    /**
121     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
122     */
123    public String getId() {
124
125        return ACTION_ID;
126    }
127
128    /**
129     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
130     */
131    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
132
133        if ((resources.size() == 1)
134            && resources.get(0).isFile()
135            && !CmsResourceTypeXmlContainerPage.isContainerPage(resources.get(0))) {
136            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
137        } else {
138            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
139        }
140    }
141
142    /**
143     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
144     */
145    @Override
146    protected String getTitleKey() {
147
148        return Messages.GUI_ACTION_DISPLAY_0;
149    }
150}