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.CmsResourceFilter;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.main.OpenCms;
033import org.opencms.util.CmsStringUtil;
034
035/**
036 * Opens the selected resource in a new window.<p>
037 *
038 * @since 6.0.0
039 */
040public class CmsListOpenResourceAction extends A_CmsListDefaultJsAction {
041
042    /** Id of the column with the resource root path. */
043    private final String m_resColumnPathId;
044
045    /**
046     * Default Constructor.<p>
047     *
048     * @param id the unique id
049     * @param resColumnPathId the id of the column with the resource root path
050     */
051    public CmsListOpenResourceAction(String id, String resColumnPathId) {
052
053        super(id);
054        m_resColumnPathId = resColumnPathId;
055        setName(Messages.get().container(Messages.GUI_OPENRESOURCE_ACTION_NAME_0));
056        setHelpText(Messages.get().container(Messages.GUI_OPENRESOURCE_ACTION_HELP_0));
057    }
058
059    /**
060     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getHelpText()
061     */
062    @Override
063    public CmsMessageContainer getHelpText() {
064
065        if (isEnabled()) {
066            return super.getHelpText();
067        }
068        return Messages.get().container(Messages.GUI_OPENRESOURCE_ACTION_DISABLED_HELP_0);
069    }
070
071    /**
072     * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#isEnabled()
073     */
074    @Override
075    public boolean isEnabled() {
076
077        if (getResourceName() != null) {
078            return super.isEnabled();
079        }
080        return false;
081    }
082
083    /**
084     * @see org.opencms.workplace.list.A_CmsListDefaultJsAction#jsCode()
085     */
086    @Override
087    public String jsCode() {
088
089        StringBuffer jsCode = new StringBuffer(256);
090        jsCode.append("javascript:top.openwinfull('");
091        jsCode.append(getResourceName());
092        jsCode.append("', true)");
093        return jsCode.toString();
094    }
095
096    /**
097     * Returns the most possible right resource name.<p>
098     *
099     * @return the most possible right resource name
100     */
101    protected String getResourceName() {
102
103        String resource = getItem().get(m_resColumnPathId).toString();
104        if (!getWp().getCms().existsResource(resource, CmsResourceFilter.DEFAULT)) {
105            String siteRoot = OpenCms.getSiteManager().getSiteRoot(resource);
106            if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(siteRoot)) {
107                resource = resource.substring(siteRoot.length());
108            }
109            if (!getWp().getCms().existsResource(resource, CmsResourceFilter.DEFAULT)) {
110                resource = null;
111            }
112        }
113        return resource;
114    }
115}