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.login;
029
030import org.opencms.security.CmsOrganizationalUnit;
031import org.opencms.ui.A_CmsUI;
032import org.opencms.util.CmsFileUtil;
033
034import java.util.List;
035
036import com.vaadin.v7.shared.ui.combobox.FilteringMode;
037import com.vaadin.v7.ui.ComboBox;
038import com.vaadin.ui.CustomComponent;
039
040/**
041 * Widget used to allow the user to search and select an organizational unit.<p>
042 */
043public class CmsLoginOuSelector extends CustomComponent {
044
045    /** Serial version id. */
046    private static final long serialVersionUID = 1L;
047
048    /** The combo box containing the OU options. */
049    private ComboBox m_ouSelect = new ComboBox();
050
051    /** Flag to always hide the selector. */
052    private boolean m_alwaysHidden;
053
054    /**
055     * Creates a new instance.<P>
056     */
057    public CmsLoginOuSelector() {
058
059        m_ouSelect.setWidth("100%");
060        setCompositionRoot(m_ouSelect);
061        m_ouSelect.setFilteringMode(FilteringMode.CONTAINS);
062        m_ouSelect.setNullSelectionAllowed(false);
063    }
064
065    /**
066     * Gets the selected OU.<p<
067     *
068     * @return the selected OU
069     */
070    public String getValue() {
071
072        return (String)m_ouSelect.getValue();
073    }
074
075    /**
076     * Initializes the select options.<p>
077     *
078     * @param orgUnits the selectable OUs
079     */
080    public void initOrgUnits(List<CmsOrganizationalUnit> orgUnits) {
081
082        if ((orgUnits.size() == 1) && (orgUnits.get(0).getParentFqn() == null)) {
083            setVisible(false);
084            m_alwaysHidden = true;
085        }
086        for (CmsOrganizationalUnit ou : orgUnits) {
087            String key = normalizeOuName(ou.getName());
088            m_ouSelect.addItem(key);
089            m_ouSelect.setItemCaption(key, ou.getDisplayName(A_CmsUI.get().getLocale()));
090        }
091    }
092
093    /**
094     * Returns true if the OU selector should remain hidden.<p>
095     *
096     * @return true if the OU selector should remain hidden
097     */
098    public boolean isAlwaysHidden() {
099
100        return m_alwaysHidden;
101    }
102
103    /**
104     * Sets the selected OU.<p>
105     *
106     * @param value the OU to select
107     */
108    public void setValue(String value) {
109
110        m_ouSelect.setValue(normalizeOuName(value));
111    }
112
113    /**
114     * Normalizes a given OU name.<p>
115     *
116     * @param ou the OU name
117     * @return the normalized version
118     */
119    String normalizeOuName(String ou) {
120
121        ou = CmsFileUtil.removeLeadingSeparator(ou);
122        ou = CmsFileUtil.removeTrailingSeparator(ou);
123        return ou;
124    }
125
126}