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.gwt.client.ui.history;
029
030import com.google.common.base.Predicate;
031import com.google.gwt.cell.client.ActionCell;
032import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
033
034/**
035 * Cell class for rendering a button inside a cell table.<p>
036 *
037 * @param <T> the cell type
038 */
039public class CmsButtonCell<T> extends ActionCell<T> {
040
041    /** Function to check whether the button should be available. */
042    private Predicate<T> m_checkActive;
043
044    /** The value for the CSS class of the button. */
045    private String m_cssClass;
046
047    /** The value for the title attribute of the button. */
048    private String m_title;
049
050    /**
051     * Creates a new instance.<p>
052     *
053     * @param title the value for the title attribute of the button
054     * @param cssClass the value for the CSS class of the button
055     * @param delegate the delegate which should be called when the button is clicked
056     * @param checkActive a predicate to check whether the button should be active
057     */
058    public CmsButtonCell(
059        String title,
060        String cssClass,
061        final ActionCell.Delegate<T> delegate,
062        final Predicate<T> checkActive) {
063
064        super("", new ActionCell.Delegate<T>() {
065
066            public void execute(T object) {
067
068                if (checkActive.apply(object)) {
069                    delegate.execute(object);
070                }
071            }
072        });
073        m_title = title;
074        m_cssClass = cssClass;
075        m_checkActive = checkActive;
076    }
077
078    /**
079     * @see com.google.gwt.cell.client.ActionCell#render(com.google.gwt.cell.client.Cell.Context, java.lang.Object, com.google.gwt.safehtml.shared.SafeHtmlBuilder)
080     */
081    @Override
082    public void render(com.google.gwt.cell.client.Cell.Context context, T value, SafeHtmlBuilder sb) {
083
084        if (m_checkActive.apply(value)) {
085            sb.append(CmsResourceHistoryTable.templates.button(m_title, m_cssClass));
086        }
087    }
088}