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.CmsResourceTypeJsp;
033import org.opencms.file.types.CmsResourceTypePlain;
034import org.opencms.file.types.CmsResourceTypeXmlContainerPage;
035import org.opencms.file.types.CmsResourceTypeXmlContent;
036import org.opencms.file.types.CmsResourceTypeXmlPage;
037import org.opencms.i18n.CmsVfsBundleManager;
038import org.opencms.main.OpenCms;
039import org.opencms.ui.I_CmsDialogContext;
040import org.opencms.ui.apps.CmsAppView;
041import org.opencms.ui.apps.CmsAppView.CacheStatus;
042import org.opencms.ui.apps.CmsAppWorkplaceUi;
043import org.opencms.ui.apps.CmsEditor;
044import org.opencms.ui.apps.CmsEditorConfiguration;
045import org.opencms.ui.apps.lists.CmsListManager;
046import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
047import org.opencms.ui.contextmenu.CmsMenuItemVisibilitySingleOnly;
048import org.opencms.ui.contextmenu.CmsStandardVisibilityCheck;
049import org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility;
050import org.opencms.util.CmsStringUtil;
051import org.opencms.workplace.explorer.Messages;
052
053import java.util.List;
054
055import com.vaadin.navigator.View;
056import com.vaadin.ui.UI;
057
058/**
059 * The edit dialog action. Used for all but container page contents.<p>
060 */
061public class CmsEditDialogAction extends A_CmsWorkplaceAction implements I_CmsDefaultAction {
062
063    /** The action id. */
064    public static final String ACTION_ID = "edit";
065
066    /** The action visibility. */
067    public static final I_CmsHasMenuItemVisibility VISIBILITY = new CmsMenuItemVisibilitySingleOnly(
068        CmsStandardVisibilityCheck.EDIT);
069
070    /**
071     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#executeAction(org.opencms.ui.I_CmsDialogContext)
072     */
073    public void executeAction(I_CmsDialogContext context) {
074
075        View view = CmsAppWorkplaceUi.get().getCurrentView();
076        if (view instanceof CmsAppView) {
077            ((CmsAppView)view).setCacheStatus(CacheStatus.cacheOnce);
078        }
079        CmsResource resource = context.getResources().get(0);
080        String editState = CmsEditor.getEditState(
081            resource.getStructureId(),
082            false,
083            UI.getCurrent().getPage().getLocation().toString());
084
085        CmsAppWorkplaceUi.get().showApp(
086            OpenCms.getWorkplaceAppManager().getAppConfiguration(CmsEditorConfiguration.APP_ID),
087            editState);
088    }
089
090    /**
091     * @see org.opencms.ui.actions.I_CmsDefaultAction#getDefaultActionRank(org.opencms.ui.I_CmsDialogContext)
092     */
093    public int getDefaultActionRank(I_CmsDialogContext context) {
094
095        CmsResource res = context.getResources().get(0);
096
097        boolean editAsDefault = (CmsResourceTypeXmlContent.isXmlContent(res)
098            || (CmsResourceTypePlain.getStaticTypeId() == res.getTypeId())
099            || CmsResourceTypeXmlPage.isXmlPage(res))
100            && !OpenCms.getResourceManager().getResourceType(res).getTypeName().equals(
101                CmsListManager.RES_TYPE_LIST_CONFIG)
102            && (!(res.getName().endsWith(".html") || res.getName().endsWith(".htm"))
103                || CmsStringUtil.isEmptyOrWhitespaceOnly(context.getCms().getRequestContext().getSiteRoot()));
104
105        editAsDefault = editAsDefault
106            || (CmsResourceTypeJsp.isJsp(res) && !(res.getName().endsWith(".html") || res.getName().endsWith(".htm")));
107
108        boolean isPropertyBundle = OpenCms.getResourceManager().getResourceType(res).getTypeName().equals(
109            CmsVfsBundleManager.TYPE_PROPERTIES_BUNDLE);
110        editAsDefault = editAsDefault || isPropertyBundle;
111
112        if (editAsDefault) {
113            return 20;
114        }
115        return 0;
116    }
117
118    /**
119     * @see org.opencms.ui.actions.I_CmsWorkplaceAction#getId()
120     */
121    public String getId() {
122
123        return ACTION_ID;
124    }
125
126    /**
127     * @see org.opencms.ui.contextmenu.I_CmsHasMenuItemVisibility#getVisibility(org.opencms.file.CmsObject, java.util.List)
128     */
129    public CmsMenuItemVisibilityMode getVisibility(CmsObject cms, List<CmsResource> resources) {
130
131        if ((resources.size() == 1) && !CmsResourceTypeXmlContainerPage.isContainerPage(resources.get(0))) {
132            return VISIBILITY.getVisibility(cms, resources);
133        } else {
134            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
135        }
136    }
137
138    /**
139     * @see org.opencms.ui.actions.A_CmsWorkplaceAction#getTitleKey()
140     */
141    @Override
142    protected String getTitleKey() {
143
144        return Messages.GUI_EXPLORER_CONTEXT_EDIT_0;
145    }
146}