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.tools.accounts;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.util.CmsUUID;
033import org.opencms.workplace.CmsWorkplace;
034import org.opencms.workplace.list.CmsListResourceIconAction;
035import org.opencms.workplace.tools.A_CmsHtmlIconButton;
036
037/**
038 * Displays an icon action for dependency lists.<p>
039 *
040 * @since 6.0.0
041 */
042public class CmsDependencyIconAction extends CmsListResourceIconAction {
043
044    /** Path to the list buttons. */
045    public static final String PATH_BUTTONS = "tools/accounts/buttons/";
046
047    /** the type of the icon. */
048    private final CmsDependencyIconActionType m_type;
049
050    /**
051     * Default Constructor.<p>
052     *
053     * @param id the unique id
054     * @param type the type of the icon
055     * @param cms the cms context
056     */
057    public CmsDependencyIconAction(String id, CmsDependencyIconActionType type, CmsObject cms) {
058
059        super(id + type.getId(), CmsGroupPrincipalDependenciesList.LIST_COLUMN_TYPE, cms);
060        m_type = type;
061    }
062
063    /**
064     * @see org.opencms.workplace.list.CmsListDirectAction#buttonHtml(org.opencms.workplace.CmsWorkplace)
065     */
066    @Override
067    public String buttonHtml(CmsWorkplace wp) {
068
069        if (!isVisible()) {
070            return "";
071        }
072        if (m_type == CmsDependencyIconActionType.RESOURCE) {
073            return super.buttonHtml(wp);
074        } else {
075            return A_CmsHtmlIconButton.defaultButtonHtml(
076                resolveButtonStyle(),
077                getId() + getItem().getId(),
078                getId(),
079                resolveName(wp.getLocale()),
080                resolveHelpText(wp.getLocale()),
081                isEnabled(),
082                getIconPath(),
083                null,
084                resolveOnClic(wp.getLocale()),
085                getColumnForTexts() == null,
086                null);
087        }
088    }
089
090    /**
091     * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
092     */
093    @Override
094    public String getIconPath() {
095
096        if (m_type == CmsDependencyIconActionType.USER) {
097            return PATH_BUTTONS + "user.png";
098        } else if (m_type == CmsDependencyIconActionType.GROUP) {
099            return PATH_BUTTONS + "group.png";
100        } else {
101            return super.getIconPath();
102        }
103    }
104
105    /**
106     * Returns the type.<p>
107     *
108     * @return the type
109     */
110    public CmsDependencyIconActionType getType() {
111
112        return m_type;
113    }
114
115    /**
116     * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
117     */
118    @Override
119    public boolean isVisible() {
120
121        boolean visible = false;
122        if (getItem() != null) {
123            CmsUUID id = new CmsUUID(getItem().getId());
124            try {
125                if (m_type == CmsDependencyIconActionType.RESOURCE) {
126                    try {
127                        getCms().readUser(id);
128                    } catch (CmsException e1) {
129                        try {
130                            getCms().readGroup(id);
131                        } catch (CmsException e2) {
132                            visible = true;
133                        }
134                    }
135                } else if (m_type == CmsDependencyIconActionType.USER) {
136                    getCms().readUser(id);
137                    visible = true;
138                } else if (m_type == CmsDependencyIconActionType.GROUP) {
139                    getCms().readGroup(id);
140                    visible = true;
141                }
142            } catch (CmsException e) {
143                // not visible
144            }
145        } else {
146            visible = super.isVisible();
147        }
148        return visible;
149    }
150}