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.linkvalidation;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.OpenCms;
033import org.opencms.ui.A_CmsUI;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.apps.Messages;
036import org.opencms.ui.components.editablegroup.CmsEditableGroup;
037import org.opencms.ui.components.editablegroup.I_CmsEditableGroupRow;
038import org.opencms.ui.components.fileselect.CmsPathSelectField;
039
040import java.util.ArrayList;
041import java.util.List;
042
043import com.google.common.base.Supplier;
044import com.vaadin.ui.Button;
045import com.vaadin.ui.Button.ClickEvent;
046import com.vaadin.ui.Component;
047import com.vaadin.v7.ui.VerticalLayout;
048
049/**
050 * Class for the layout for choosing the resources.<p>
051 */
052public class CmsInternalResources extends VerticalLayout {
053
054    /**vaadin serial id.*/
055    private static final long serialVersionUID = 6880701403593873461L;
056
057    /**Editable resource group. */
058    CmsEditableGroup m_resourcesGroup;
059
060    /**Button to update table.*/
061    Button m_okButton;
062
063    /**Layout holding the text components for resources. */
064    VerticalLayout m_resources;
065
066    /**
067     * Public constructor.<p>
068     *
069     * @param table linked table to be updated if button was pressed
070     */
071    public CmsInternalResources(final I_CmsUpdatableComponent table) {
072
073        setHeightUndefined();
074        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
075
076        m_okButton.addClickListener(new Button.ClickListener() {
077
078            private static final long serialVersionUID = -5668840121832993312L;
079
080            public void buttonClick(ClickEvent event) {
081
082                table.update(getResources());
083            }
084        });
085
086        m_resourcesGroup = new CmsEditableGroup(m_resources, new Supplier<Component>() {
087
088            public Component get() {
089
090                return getResourceComponent(null);
091            }
092
093        }, CmsVaadinUtils.getMessageText(Messages.GUI_LINKVALIDATION_LINKS_ADDRESOURCES_0));
094
095        m_resourcesGroup.init();
096
097        m_resourcesGroup.addRow(getResourceComponent(null));
098
099    }
100
101    /**
102     * Adds a resource to the form.<p>
103     *
104     * @param resource to be added
105     */
106    public void addResource(String resource) {
107
108        m_resourcesGroup.addRow(getResourceComponent(resource));
109
110    }
111
112    /**
113     * Clear resources.<p>
114     *
115     */
116    public void clearResources() {
117
118        for (I_CmsEditableGroupRow row : m_resourcesGroup.getRows()) {
119            m_resourcesGroup.remove(row);
120        }
121    }
122
123    /**
124     * Reads out resource paths from Layout.<p>
125     *
126     * @return List of Strings with all entered paths
127     */
128    public List<String> getResources() {
129
130        List<String> res = new ArrayList<String>();
131        for (I_CmsEditableGroupRow row : m_resourcesGroup.getRows()) {
132            res.add(((CmsPathSelectField)row.getComponent()).getValue());
133        }
134        return res;
135    }
136
137    /**
138     * Get vaadin component with given path.<p>
139     *
140     * @param path of resource
141     * @return Vaadin component
142     */
143    protected Component getResourceComponent(String path) {
144
145        try {
146            CmsPathSelectField field = new CmsPathSelectField();
147            field.setUseRootPaths(true);
148            CmsObject cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
149            cms.getRequestContext().setSiteRoot("");
150            field.setCmsObject(cms);
151            if (path != null) {
152                field.setValue(path);
153            }
154            return field;
155        } catch (CmsException e) {
156            //
157        }
158        return null;
159    }
160
161    /**
162     * Adds an empty path field to layout.<p>
163     *
164     * @param defaultValue of new field
165     */
166    void addEmptyPathFieldToLayout(String defaultValue) {
167
168        m_resourcesGroup.addRow(getResourceComponent(defaultValue));
169    }
170}