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 GmbH & Co. KG, 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.workplace.list;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResourceFilter;
032import org.opencms.i18n.CmsMessageContainer;
033import org.opencms.lock.CmsLock;
034import org.opencms.main.OpenCms;
035import org.opencms.util.CmsStringUtil;
036import org.opencms.workplace.explorer.CmsResourceUtil;
037
038/**
039 * Opens the selected resource in a new window.<p>
040 *
041 * @since 6.0.0
042 */
043public class CmsListEditResourceAction extends CmsListDirectAction {
044
045    /** Id of the column with the resource root path. */
046    private final String m_resColumnPathId;
047
048    /** The current resource util object. */
049    private CmsResourceUtil m_resourceUtil;
050
051    /**
052     * Default Constructor.<p>
053     *
054     * @param id the unique id
055     * @param resColumnPathId the id of the column with the resource root path
056     */
057    public CmsListEditResourceAction(String id, String resColumnPathId) {
058
059        super(id);
060        m_resColumnPathId = resColumnPathId;
061    }
062
063    /**
064     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getHelpText()
065     */
066    @Override
067    public CmsMessageContainer getHelpText() {
068
069        CmsMessageContainer helptext = super.getHelpText();
070        if (helptext == null) {
071            if (isEnabled()) {
072                helptext = Messages.get().container(Messages.GUI_EDITRESOURCE_ACTION_HELP_0);
073            } else {
074                helptext = Messages.get().container(Messages.GUI_EDITRESOURCE_DISABLED_ACTION_HELP_0);
075            }
076        }
077        return helptext;
078    }
079
080    /**
081     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
082     */
083    @Override
084    public String getIconPath() {
085
086        String iconpath = super.getIconPath();
087        if (iconpath == null) {
088            if (isEnabled()) {
089                iconpath = "list/resource_edit.png";
090            } else {
091                iconpath = "list/resource_edit_disabled.png";
092            }
093        }
094        return iconpath;
095    }
096
097    /**
098     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getName()
099     */
100    @Override
101    public CmsMessageContainer getName() {
102
103        CmsMessageContainer name = super.getName();
104        if (name == null) {
105            if (isEnabled()) {
106                name = Messages.get().container(Messages.GUI_EDITRESOURCE_ACTION_NAME_0);
107            } else {
108                name = Messages.get().container(Messages.GUI_EDITRESOURCE_DISABLED_ACTION_NAME_0);
109            }
110        }
111        return name;
112    }
113
114    /**
115     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isVisible()
116     */
117    @Override
118    public boolean isVisible() {
119
120        if (getResourceName() != null) {
121            try {
122                // if resource type if editable
123                if (OpenCms.getWorkplaceManager().getEditorHandler().getEditorUri(
124                    getResourceName(),
125                    getWp().getJsp()) != null) {
126                    // check lock state
127                    CmsLock lock = getResourceUtil().getLock();
128                    if (lock.isNullLock() || lock.isOwnedBy((getWp().getCms().getRequestContext().getCurrentUser()))) {
129                        return isEnabled();
130                    }
131                }
132            } catch (Throwable e) {
133                // ignore
134            }
135        }
136        return !isEnabled();
137    }
138
139    /**
140     * @see org.opencms.workplace.list.I_CmsListDirectAction#setItem(org.opencms.workplace.list.CmsListItem)
141     */
142    @Override
143    public void setItem(CmsListItem item) {
144
145        m_resourceUtil = ((A_CmsListExplorerDialog)getWp()).getResourceUtil(item);
146        super.setItem(item);
147    }
148
149    /**
150     * Returns the current result Util.<p>
151     *
152     * @return the current result Util
153     */
154    protected CmsResourceUtil getResourceUtil() {
155
156        return m_resourceUtil;
157    }
158
159    /**
160     * Returns the most possible right resource name.<p>
161     *
162     * @return the most possible right resource name
163     */
164    private String getResourceName() {
165
166        CmsResourceUtil resUtil = getResourceUtil();
167        if (resUtil != null) {
168            CmsObject cms = resUtil.getCms();
169            String resource = cms.getSitePath(resUtil.getResource());
170            return resource;
171        } else {
172            String resource = getItem().get(m_resColumnPathId).toString();
173            if (!getWp().getCms().existsResource(resource, CmsResourceFilter.DEFAULT)) {
174                String siteRoot = OpenCms.getSiteManager().getSiteRoot(resource);
175                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(siteRoot)) {
176                    resource = resource.substring(siteRoot.length());
177                }
178                if (!getWp().getCms().existsResource(resource, CmsResourceFilter.DEFAULT)) {
179                    resource = null;
180                }
181            }
182            return resource;
183        }
184    }
185}