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.dbmanager;
029
030import org.opencms.main.OpenCms;
031
032import java.io.File;
033
034import com.vaadin.v7.data.Property.ValueChangeEvent;
035import com.vaadin.v7.data.Property.ValueChangeListener;
036import com.vaadin.v7.data.util.IndexedContainer;
037import com.vaadin.v7.ui.AbstractSelect.ItemCaptionMode;
038import com.vaadin.v7.ui.ComboBox;
039
040/**
041 * Abstract class for the import from a folder on the server.<p>
042 */
043public abstract class A_CmsServerImportForm extends A_CmsImportForm {
044
045    /**Vaadin serial id.*/
046    private static final long serialVersionUID = 5493880295543227220L;
047
048    /**
049     * public constructor.<p>
050     *
051     * @param app which uses this form
052     * @param pathToServer path where the files should be read
053     * @param validate indicates if file gets validated (only possible for modules)
054     */
055    public A_CmsServerImportForm(I_CmsReportApp app, String pathToServer, final boolean validate) {
056        super(app);
057        IndexedContainer options = new IndexedContainer();
058        options.addContainerProperty("label", String.class, "");
059        getImportSelect().setContainerDataSource(options);
060        getImportSelect().setItemCaptionMode(ItemCaptionMode.PROPERTY);
061        getImportSelect().setItemCaptionPropertyId("label");
062        getImportSelect().setNullSelectionAllowed(false);
063        String moduleDir = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(pathToServer);
064        File moduleDirFile = new File(moduleDir);
065        if (moduleDirFile.exists()) {
066            for (File file : moduleDirFile.listFiles()) {
067                String path = file.getAbsolutePath();
068                String name = file.getName();
069                options.addItem(path).getItemProperty("label").setValue(name);
070            }
071        }
072
073        getImportSelect().addValueChangeListener(new ValueChangeListener() {
074
075            private static final long serialVersionUID = -8550460711407604364L;
076
077            public void valueChange(ValueChangeEvent event) {
078
079                String path = (String)(event.getProperty().getValue());
080                m_importFile = new CmsImportFile(path);
081                if (validate) {
082                    getOkButton().setEnabled(false);
083                    validateModuleFile();
084                    return;
085                }
086                getOkButton().setEnabled(true);
087            }
088        });
089    }
090
091    /**
092     * Gets a combo box for selecting the file on server.<p>
093     *
094     * @return a vaadin combo box
095     */
096    public abstract ComboBox getImportSelect();
097}