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.apps.modules.edit;
029
030import org.opencms.main.OpenCms;
031import org.opencms.module.CmsModule;
032import org.opencms.module.CmsModuleDependency;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.components.CmsAutoItemCreatingComboBox;
035import org.opencms.ui.components.CmsAutoItemCreatingComboBox.I_NewValueHandler;
036
037import java.util.Collections;
038import java.util.List;
039
040import com.google.common.collect.Lists;
041import com.vaadin.ui.FormLayout;
042import com.vaadin.v7.data.Container;
043import com.vaadin.v7.data.util.IndexedContainer;
044import com.vaadin.v7.ui.TextField;
045
046/**
047 * Widget used to edit a module dependency.<p>
048 */
049public class CmsModuleDependencyWidget extends FormLayout {
050
051    /** Serial version id. */
052    private static final long serialVersionUID = 1L;
053
054    /** The module selector. */
055    private CmsAutoItemCreatingComboBox m_moduleSelect;
056
057    /** Text field for the module version. */
058    private TextField m_version;
059
060    /**
061     * Creates a new instance.<p>
062     */
063    public CmsModuleDependencyWidget() {
064
065        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
066        IndexedContainer container = new IndexedContainer();
067        List<String> moduleNames = Lists.newArrayList();
068        for (CmsModule module : OpenCms.getModuleManager().getAllInstalledModules()) {
069            String name = module.getName();
070            moduleNames.add(name);
071        }
072        Collections.sort(moduleNames);
073        for (String name : moduleNames) {
074            container.addItem(name);
075        }
076        m_moduleSelect.setContainerDataSource(container);
077        m_moduleSelect.setNewValueHandler(new I_NewValueHandler() {
078
079            public Object ensureItem(Container cnt, Object id) {
080
081                if (!cnt.containsId(id)) {
082                    cnt.addItem(id);
083                }
084                return id;
085            }
086        });
087        setWidth("100%");
088    }
089
090    /**
091     * Creates a new widget instance for the given module dependency.<p>
092     *
093     * @param dep the module dependency
094     * @return the new widget
095     */
096    public static CmsModuleDependencyWidget create(CmsModuleDependency dep) {
097
098        CmsModuleDependencyWidget result = new CmsModuleDependencyWidget();
099        if (dep != null) {
100            result.m_moduleSelect.setValue(dep.getName());
101            result.m_version.setValue(dep.getVersion().toString());
102        }
103        return result;
104    }
105
106    /**
107     * Gets the module name.<p>
108     *
109     * @return the module name
110     */
111    public String getModuleName() {
112
113        return (String)m_moduleSelect.getValue();
114    }
115
116    /**
117     * Gets the module version.<p>
118     *
119     * @return the module version
120     */
121    public String getModuleVersion() {
122
123        return m_version.getValue();
124    }
125
126}