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.components;
029
030import org.opencms.ui.FontOpenCms;
031
032import com.vaadin.ui.Button;
033import com.vaadin.ui.Button.ClickEvent;
034import com.vaadin.ui.Button.ClickListener;
035import com.vaadin.ui.Component;
036import com.vaadin.ui.ComponentContainer;
037import com.vaadin.ui.HasComponents;
038import com.vaadin.v7.ui.HorizontalLayout;
039
040/**
041 * Removable form row.<p>
042 *
043 * @param <T> the filed type
044 */
045public class CmsRemovableFormRow<T extends Component> extends HorizontalLayout {
046
047    /** Serial version id. */
048    private static final long serialVersionUID = 1L;
049
050    /** Style name for the remove button. */
051    public static final String REMOVE_BUTTON_STYLE = "o-formrow-remove-button";
052
053    /** The text input field. */
054    private T m_input;
055
056    private Runnable m_remove;
057
058    /**
059     * Constructor.<p>
060     *
061     * @param input the input field
062     * @param removeLabel the remove button label
063     */
064    public CmsRemovableFormRow(T input, String removeLabel) {
065        setWidth("100%");
066        m_input = input;
067        setSpacing(true);
068        input.setWidth("100%");
069        addComponent(input);
070        setExpandRatio(input, 1f);
071        Button deleteButton = new Button("");
072        deleteButton.setIcon(FontOpenCms.CUT_SMALL);
073        deleteButton.addStyleName(REMOVE_BUTTON_STYLE);
074        deleteButton.addStyleName(OpenCmsTheme.BUTTON_ICON);
075        deleteButton.setDescription(removeLabel);
076        deleteButton.addClickListener(new ClickListener() {
077
078            private static final long serialVersionUID = 1L;
079
080            public void buttonClick(ClickEvent event) {
081
082                removeRow();
083            }
084        });
085        addComponent(deleteButton);
086    }
087
088    /**
089     * Returns the input field.<p>
090     *
091     * @return the input field
092     */
093    public T getInput() {
094
095        return m_input;
096    }
097
098    /**
099     * Enables or diables the remove button.<p>
100     *
101     * @param enabled true -> remove is clickable
102     */
103    public void setEnabledRemoveOption(boolean enabled) {
104
105        getComponent(1).setEnabled(enabled);
106    }
107
108    /**
109     * Sets a runnable, which runs when row gets removed.<p>
110     *
111     * @param remove runnable
112     */
113    public void setRemoveRunnable(Runnable remove) {
114
115        m_remove = remove;
116    }
117
118    /**
119     * Method to remove row.<p>
120     */
121    void removeRow() {
122
123        HasComponents parent = CmsRemovableFormRow.this.getParent();
124        if (parent instanceof ComponentContainer) {
125            ((ComponentContainer)parent).removeComponent(CmsRemovableFormRow.this);
126        }
127        if (m_remove != null) {
128            m_remove.run();
129        }
130    }
131
132}