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.user;
029
030import org.opencms.file.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.security.CmsPrincipal;
034import org.opencms.ui.CmsVaadinUtils;
035import org.opencms.ui.FontOpenCms;
036import org.opencms.ui.apps.Messages;
037import org.opencms.ui.components.CmsBasicDialog;
038import org.opencms.util.CmsUUID;
039
040import java.util.ArrayList;
041import java.util.Collections;
042import java.util.Iterator;
043import java.util.List;
044import java.util.Set;
045
046import org.apache.commons.logging.Log;
047
048import com.vaadin.ui.Button;
049import com.vaadin.ui.Button.ClickEvent;
050import com.vaadin.ui.Button.ClickListener;
051import com.vaadin.ui.Component;
052import com.vaadin.ui.UI;
053import com.vaadin.ui.Window;
054import com.vaadin.ui.themes.ValoTheme;
055import com.vaadin.v7.data.util.IndexedContainer;
056import com.vaadin.v7.event.FieldEvents.TextChangeEvent;
057import com.vaadin.v7.event.FieldEvents.TextChangeListener;
058import com.vaadin.v7.ui.HorizontalLayout;
059import com.vaadin.v7.ui.Label;
060import com.vaadin.v7.ui.TextField;
061import com.vaadin.v7.ui.VerticalLayout;
062
063/**
064 * Abstract class for dialogs to change role or groups of a given user.<p>
065 */
066public abstract class A_CmsEditUserGroupRoleDialog extends CmsBasicDialog {
067
068    /**Height of table. */
069    private static final String ITEM_HEIGHT = "550px";
070
071    /** The log object for this class. */
072    private static final Log LOG = CmsLog.getLog(A_CmsEditUserGroupRoleDialog.class);
073
074    /**Vaadin serial id. */
075    private static final long serialVersionUID = -5088800626506962263L;
076
077    /** The app instance. */
078    protected CmsAccountsApp m_app;
079
080    /**CmsObject. */
081    protected CmsObject m_cms;
082
083    /**User object to be edited.*/
084    protected CmsPrincipal m_principal;
085
086    /**
087     * public constructor.<p>
088     *
089     * @param cms CmsObject
090     * @param userId id of user
091     * @param window window
092     * @param app the app instance
093     */
094    public A_CmsEditUserGroupRoleDialog(CmsObject cms, CmsUUID userId, final Window window, final CmsAccountsApp app) {
095
096        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
097        m_cms = cms;
098        m_app = app;
099
100        try {
101            m_principal = m_cms.readUser(userId);
102            displayResourceInfoDirectly(Collections.singletonList(CmsAccountsApp.getPrincipalInfo(m_principal)));
103            window.setCaption(CmsVaadinUtils.getMessageText(getWindowCaptionMessageKey(), m_principal.getSimpleName()));
104        } catch (CmsException e) {
105            LOG.error("Can't read user", e);
106        }
107
108        getHLayout().setExpandRatio(getHLayout().getComponent(0), 1);
109        getHLayout().setExpandRatio(getHLayout().getComponent(1), 1);
110
111        getCloseButton().addClickListener(new ClickListener() {
112
113            private static final long serialVersionUID = -6272630588945629762L;
114
115            public void buttonClick(ClickEvent event) {
116
117                window.close();
118                app.reload();
119            }
120        });
121        init();
122    }
123
124    /**
125     * Adds given items.<p>
126     *
127     * @param data data containing information about item to add (see getStringSetValue())
128     */
129    public abstract void addItem(Set<String> data);
130
131    /**
132     * Caption for the add action.<p>
133     *
134     * @return String
135     */
136    public abstract String getAddActionCaption();
137
138    /**
139     * Caption for the list with items to add.<p>
140     *
141     * @return String
142     */
143    public abstract String getAddCaptionText();
144
145    /**
146     * Gets container with items which are available but not set.<p>
147     *
148     * @param caption caption property
149     * @param propIcon icon property
150     * @return IndexedContainer
151     */
152    public abstract IndexedContainer getAvailableItemsIndexedContainer(String caption, String propIcon);
153
154    /**
155     * Gets the close button to close the window.<p>
156     *
157     * @return the vaadin button
158     */
159    public abstract Button getCloseButton();
160
161    /**
162     * Gets the caption for the table with the currently set elements for the user.<p>
163     *
164     * @return String
165     */
166    public abstract String getCurrentTableCaption();
167
168    /**
169     * Gets the description for the item.<p>
170     *
171     * @param itemId to get description for
172     * @return String
173     */
174    public abstract String getDescriptionForItemId(Object itemId);
175
176    /**
177     * Gets the empty message.<p>
178     *
179     * @return String
180     */
181    public abstract String getEmptyMessage();
182
183    /**
184     * Further ID for a column.
185     *
186     * @return id
187     */
188    public abstract String getFurtherColumnId();
189
190    /**
191     * Gets the horizontal layout holding the tables.<p>
192     *
193     * @return horizontal layout
194     */
195    public abstract HorizontalLayout getHLayout();
196
197    /**
198     * Get name of the items.<p>
199     *
200     * @return String
201     */
202    public abstract String getItemName();
203
204    /**
205     * Gets container for items which are set for the user.<p>
206     *
207     * @param propName caption property
208     * @param propIcon icon property
209     * @param propStatus status property
210     * @return IndexedContainer
211     */
212    public abstract IndexedContainer getItemsOfUserIndexedContainer(
213        String propName,
214        String propIcon,
215        String propStatus);
216
217    /**
218     *Gets layout for the table with items which are set to the user.<p>
219     *
220     * @return layout
221     */
222    public abstract VerticalLayout getLeftTableLayout();
223
224    /**
225     * Gets the parent layout.<p>
226     *
227     * @return layout
228     */
229    public abstract VerticalLayout getParentLayout();
230
231    /**
232     * Gets layout for the table with available item.<p>
233     *
234     * @return layout
235     */
236    public abstract VerticalLayout getRightTableLayout();
237
238    /**
239     * Get string values from given set of item-object.<p>
240     *
241     * @param value set of items to create string set from
242     * @return set of strings
243     */
244    public abstract Set<String> getStringSetValue(Set<Object> value);
245
246    /**
247     * Gets the window caption message key.<p>
248     *
249     * @return message key
250     */
251    public abstract String getWindowCaptionMessageKey();
252
253    /**
254     * Remove items represented as strings (see getStringSetValue()).<p>
255     *
256     * @param items to be removed
257     */
258    public abstract void removeItem(Set<String> items);
259
260    /**
261     * Init method.<p>
262     */
263    protected void init() {
264
265        setHeightUndefined();
266        removeExistingTable(getLeftTableLayout());
267        removeExistingTable(getRightTableLayout());
268
269        final CmsAvailableRoleOrPrincipalTable table = new CmsAvailableRoleOrPrincipalTable(this);
270        if (getAvailableItemsIndexedContainer("caption", "icon").size() > 0) {
271            getRightTableLayout().addComponent(new FixedHeightPanel(table, ITEM_HEIGHT), 0);
272        } else {
273            getRightTableLayout().addComponent(
274                new FixedHeightPanel(CmsVaadinUtils.getInfoLayout(getEmptyMessage()), ITEM_HEIGHT));
275        }
276        if (getItemsOfUserIndexedContainer("prop1", "prop2", "prop3").size() > 0) {
277            getLeftTableLayout().addComponent(
278                new FixedHeightPanel(new CmsCurrentRoleOrPrincipalTable(this, m_cms, m_principal), ITEM_HEIGHT),
279                0);
280        } else {
281            getLeftTableLayout().addComponent(
282                new FixedHeightPanel(CmsVaadinUtils.getInfoLayout(getEmptyMessage()), ITEM_HEIGHT));
283        }
284
285        TextField siteTableFilter = new TextField();
286        siteTableFilter.setIcon(FontOpenCms.FILTER);
287        siteTableFilter.setInputPrompt(
288            Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
289        siteTableFilter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
290        siteTableFilter.setWidth("200px");
291        siteTableFilter.addTextChangeListener(new TextChangeListener() {
292
293            private static final long serialVersionUID = 1L;
294
295            public void textChange(TextChangeEvent event) {
296
297                table.filterTable(event.getText());
298            }
299        });
300        if (getParentLayout().getComponent(0) instanceof TextField) {
301            getParentLayout().removeComponent(getParentLayout().getComponent(1));
302            getParentLayout().removeComponent(getParentLayout().getComponent(0));
303        }
304        HorizontalLayout caps = new HorizontalLayout();
305        caps.setSpacing(true);
306        caps.setWidth("100%");
307        caps.setHeight("30px");
308        caps.addComponent(new Label(getCurrentTableCaption()));
309        caps.addComponent(new Label(getAddCaptionText()));
310        getParentLayout().addComponent(caps, 0);
311        getParentLayout().addComponent(siteTableFilter, 0);
312        getParentLayout().setComponentAlignment(siteTableFilter, com.vaadin.ui.Alignment.TOP_RIGHT);
313        getParentLayout().setExpandRatio(getParentLayout().getComponent(2), 1);
314    }
315
316    /**
317     * Check if table exists in given layout. Removes all tables.<p>
318     *
319     * @param layout to be cleaned from tables
320     */
321    private void removeExistingTable(VerticalLayout layout) {
322
323        List<Component> tobeRemoved = new ArrayList<Component>();
324        Iterator<Component> it = layout.iterator();
325        while (it.hasNext()) {
326            Component comp = it.next();
327            if ((comp instanceof FixedHeightPanel) | (comp instanceof TextField) | (comp instanceof VerticalLayout)) {
328                tobeRemoved.add(comp);
329            }
330        }
331        for (Component c : tobeRemoved) {
332            layout.removeComponent(c);
333        }
334    }
335}