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.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.v7.data.Container;
042import com.vaadin.v7.data.util.IndexedContainer;
043import com.vaadin.ui.FormLayout;
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        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
065        IndexedContainer container = new IndexedContainer();
066        List<String> moduleNames = Lists.newArrayList();
067        for (CmsModule module : OpenCms.getModuleManager().getAllInstalledModules()) {
068            String name = module.getName();
069            moduleNames.add(name);
070        }
071        Collections.sort(moduleNames);
072        for (String name : moduleNames) {
073            container.addItem(name);
074        }
075        m_moduleSelect.setContainerDataSource(container);
076        m_moduleSelect.setNewValueHandler(new I_NewValueHandler() {
077
078            public Object ensureItem(Container cnt, Object id) {
079
080                if (!cnt.containsId(id)) {
081                    cnt.addItem(id);
082                }
083                return id;
084            }
085        });
086        setWidth("100%");
087    }
088
089    /**
090     * Creates a new widget instance for the given module dependency.<p>
091     *
092     * @param dep the module dependency
093     * @return the new widget
094     */
095    public static CmsModuleDependencyWidget create(CmsModuleDependency dep) {
096
097        CmsModuleDependencyWidget result = new CmsModuleDependencyWidget();
098        if (dep != null) {
099            result.m_moduleSelect.setValue(dep.getName());
100            result.m_version.setValue(dep.getVersion().toString());
101        }
102        return result;
103    }
104
105    /**
106     * Gets the module name.<p>
107     *
108     * @return the module name
109     */
110    public String getModuleName() {
111
112        return (String)m_moduleSelect.getValue();
113    }
114
115    /**
116     * Gets the module version.<p>
117     *
118     * @return the module version
119     */
120    public String getModuleVersion() {
121
122        return m_version.getValue();
123    }
124
125}