001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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 066 setWidth("100%"); 067 m_input = input; 068 setSpacing(true); 069 input.setWidth("100%"); 070 addComponent(input); 071 setExpandRatio(input, 1f); 072 Button deleteButton = new Button(""); 073 deleteButton.setIcon(FontOpenCms.CUT_SMALL); 074 deleteButton.addStyleName(REMOVE_BUTTON_STYLE); 075 deleteButton.addStyleName(OpenCmsTheme.BUTTON_ICON); 076 deleteButton.setDescription(removeLabel); 077 deleteButton.addClickListener(new ClickListener() { 078 079 private static final long serialVersionUID = 1L; 080 081 public void buttonClick(ClickEvent event) { 082 083 removeRow(); 084 } 085 }); 086 addComponent(deleteButton); 087 } 088 089 /** 090 * Returns the input field.<p> 091 * 092 * @return the input field 093 */ 094 public T getInput() { 095 096 return m_input; 097 } 098 099 /** 100 * Enables or diables the remove button.<p> 101 * 102 * @param enabled true -> remove is clickable 103 */ 104 public void setEnabledRemoveOption(boolean enabled) { 105 106 getComponent(1).setEnabled(enabled); 107 } 108 109 /** 110 * Sets a runnable, which runs when row gets removed.<p> 111 * 112 * @param remove runnable 113 */ 114 public void setRemoveRunnable(Runnable remove) { 115 116 m_remove = remove; 117 } 118 119 /** 120 * Method to remove row.<p> 121 */ 122 void removeRow() { 123 124 HasComponents parent = CmsRemovableFormRow.this.getParent(); 125 if (parent instanceof ComponentContainer) { 126 ((ComponentContainer)parent).removeComponent(CmsRemovableFormRow.this); 127 } 128 if (m_remove != null) { 129 m_remove.run(); 130 } 131 } 132 133}