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 com.vaadin.v7.data.Container;
031import com.vaadin.v7.data.util.IndexedContainer;
032import com.vaadin.v7.ui.ComboBox;
033
034/**
035 * A combo box which automatically creates a new option if setValue is called with an item id not already contained
036 * in the data source.
037 *
038 * This class only supports IndexedContainer as a data source.
039 *
040 * TODO: Fix this so it works for the value automatically set by setPropertyDataSource()
041 */
042public class CmsAutoItemCreatingComboBox extends ComboBox {
043
044    /**
045     * Handles automatic creation of new values.<p>
046     */
047    public interface I_NewValueHandler {
048
049        /**
050         * Ensure that an item with the given id or a suitable replacement id exists.<p>
051         *
052         * @param cnt the container
053         * @param id the id
054         * @return the id actually used for the item
055         */
056        Object ensureItem(Container cnt, Object id);
057    }
058
059    /** Serial version id. */
060    private static final long serialVersionUID = 1L;
061
062    /** The caption id. */
063    public static final String CAPTION_ID = "name";
064
065    /** The handler for new values. */
066    private I_NewValueHandler m_newValueHandler;
067
068    /**
069     * Creates a new instance.<p>
070     */
071    public CmsAutoItemCreatingComboBox() {
072
073        super();
074    }
075
076    /**
077     * @see com.vaadin.v7.ui.AbstractSelect#setContainerDataSource(com.vaadin.v7.data.Container)
078     */
079    @Override
080    public void setContainerDataSource(Container newDataSource) {
081
082        if (newDataSource instanceof IndexedContainer) {
083            super.setContainerDataSource(newDataSource);
084        } else {
085            throw new IllegalArgumentException("only IndexedContainer supported as data source.");
086        }
087    }
088
089    /**
090     * Sets the new value handler.<p>
091     *
092     * @param handler the handler instance
093     */
094    public void setNewValueHandler(I_NewValueHandler handler) {
095
096        m_newValueHandler = handler;
097    }
098
099    /**
100     * @see com.vaadin.v7.ui.AbstractSelect#setValue(java.lang.Object)
101     */
102    @Override
103    public void setValue(Object newValue) throws com.vaadin.v7.data.Property.ReadOnlyException {
104
105        IndexedContainer container = (IndexedContainer)getContainerDataSource();
106        if ((m_newValueHandler != null) && (newValue != null)) {
107            newValue = m_newValueHandler.ensureItem(container, newValue);
108        }
109        super.setValue(newValue);
110    }
111}