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.components;
029
030import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_CACHE;
031import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_COPYRIGHT;
032import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_DATE_CREATED;
033import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_DATE_EXPIRED;
034import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_DATE_MODIFIED;
035import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_DATE_RELEASED;
036import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_INSIDE_PROJECT;
037import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_INTERNAL_RESOURCE_TYPE;
038import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_IN_NAVIGATION;
039import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_IS_FOLDER;
040import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_NAVIGATION_POSITION;
041import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_NAVIGATION_TEXT;
042import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_PERMISSIONS;
043import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_PROJECT;
044import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_RELEASED_NOT_EXPIRED;
045import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_RESOURCE_NAME;
046import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_RESOURCE_TYPE;
047import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_SITE_PATH;
048import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_SIZE;
049import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_STATE;
050import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_STATE_NAME;
051import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_TITLE;
052import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_TYPE_ICON;
053import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_USER_CREATED;
054import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_USER_LOCKED;
055import static org.opencms.ui.components.CmsResourceTableProperty.PROPERTY_USER_MODIFIED;
056
057import org.opencms.ade.sitemap.shared.CmsClientSitemapEntry;
058import org.opencms.db.CmsResourceState;
059import org.opencms.file.CmsObject;
060import org.opencms.file.CmsProperty;
061import org.opencms.file.CmsPropertyDefinition;
062import org.opencms.file.CmsResource;
063import org.opencms.file.types.I_CmsResourceType;
064import org.opencms.main.CmsException;
065import org.opencms.main.CmsLog;
066import org.opencms.main.OpenCms;
067import org.opencms.ui.A_CmsUI;
068import org.opencms.ui.CmsCssIcon;
069import org.opencms.ui.CmsVaadinUtils;
070import org.opencms.ui.util.I_CmsItemSorter;
071import org.opencms.util.CmsStringUtil;
072import org.opencms.util.CmsUUID;
073import org.opencms.workplace.CmsWorkplaceMessages;
074import org.opencms.workplace.explorer.CmsResourceUtil;
075
076import java.util.ArrayList;
077import java.util.Arrays;
078import java.util.Collection;
079import java.util.Collections;
080import java.util.HashMap;
081import java.util.LinkedHashSet;
082import java.util.List;
083import java.util.Locale;
084import java.util.Map;
085import java.util.Set;
086
087import org.apache.commons.lang3.ClassUtils;
088import org.apache.commons.logging.Log;
089
090import com.google.common.collect.Lists;
091import com.google.common.collect.Sets;
092import com.vaadin.event.dd.DropHandler;
093import com.vaadin.ui.Component;
094import com.vaadin.ui.CustomComponent;
095import com.vaadin.v7.data.Item;
096import com.vaadin.v7.data.Property;
097import com.vaadin.v7.data.util.IndexedContainer;
098import com.vaadin.v7.data.util.converter.Converter;
099import com.vaadin.v7.shared.ui.label.ContentMode;
100import com.vaadin.v7.ui.AbstractSelect.ItemDescriptionGenerator;
101import com.vaadin.v7.ui.Label;
102import com.vaadin.v7.ui.Table;
103import com.vaadin.v7.ui.Table.RowHeaderMode;
104import com.vaadin.v7.ui.Table.TableDragMode;
105
106/**
107 * Generic table for displaying lists of resources.<p>
108 */
109@SuppressWarnings("deprecation")
110public class CmsResourceTable extends CustomComponent {
111
112    /**
113     * Helper class for easily configuring a set of columns to display, together with their visibility / collapsed status.<p>
114     */
115    public class ColumnBuilder {
116
117        /** The column entries configured so far. */
118        private List<ColumnEntry> m_columnEntries = Lists.newArrayList();
119
120        /**
121         * Sets up the table and its container using the columns configured so far.<p>
122         */
123        public void buildColumns() {
124
125            Set<CmsResourceTableProperty> visible = new LinkedHashSet<CmsResourceTableProperty>();
126            Set<CmsResourceTableProperty> collapsed = new LinkedHashSet<CmsResourceTableProperty>();
127            for (ColumnEntry entry : m_columnEntries) {
128                CmsResourceTableProperty prop = entry.getColumn();
129                m_container.addContainerProperty(prop, prop.getColumnType(), prop.getDefaultValue());
130                if (entry.isCollapsed()) {
131                    collapsed.add(entry.getColumn());
132                }
133                if (entry.isVisible()) {
134                    visible.add(entry.getColumn());
135                }
136            }
137            m_fileTable.setVisibleColumns(visible.toArray(new Object[0]));
138            setCollapsedColumns(collapsed.toArray(new Object[0]));
139            for (CmsResourceTableProperty visibleProp : visible) {
140                String headerKey = visibleProp.getHeaderKey();
141                if (!CmsStringUtil.isEmptyOrWhitespaceOnly(headerKey)) {
142                    m_fileTable.setColumnHeader(visibleProp, CmsVaadinUtils.getMessageText(headerKey));
143                } else {
144                    m_fileTable.setColumnHeader(visibleProp, "");
145                }
146                m_fileTable.setColumnCollapsible(visibleProp, visibleProp.isCollapsible());
147                if (visibleProp.getColumnWidth() > 0) {
148                    m_fileTable.setColumnWidth(visibleProp, visibleProp.getColumnWidth());
149                }
150                if (visibleProp.getExpandRatio() > 0) {
151                    m_fileTable.setColumnExpandRatio(visibleProp, visibleProp.getExpandRatio());
152                }
153                if (visibleProp.getConverter() != null) {
154                    m_fileTable.setConverter(visibleProp, visibleProp.getConverter());
155                }
156            }
157        }
158
159        /**
160         * Adds a new column.<p>
161         *
162         * @param prop the column
163         *
164         * @return this object
165         */
166        public ColumnBuilder column(CmsResourceTableProperty prop) {
167
168            column(prop, 0);
169            return this;
170        }
171
172        /**
173         * Adds a new column.<p<
174         *
175         * @param prop the column
176         * @param flags the flags for the column
177         *
178         * @return this object
179         */
180        public ColumnBuilder column(CmsResourceTableProperty prop, int flags) {
181
182            ColumnEntry entry = new ColumnEntry();
183            entry.setColumn(prop);
184            entry.setFlags(flags);
185            m_columnEntries.add(entry);
186            return this;
187        }
188    }
189
190    /**
191     * Contains the data for the given column, along with some flags to control visibility/collapsed status.<p>
192     *
193     */
194    public static class ColumnEntry {
195
196        /** The column. */
197        private CmsResourceTableProperty m_column;
198
199        /** The flags. */
200        private int m_flags;
201
202        /**
203         * Returns the column.<p>
204         *
205         * @return the column
206         */
207        public CmsResourceTableProperty getColumn() {
208
209            return m_column;
210        }
211
212        /**
213         * Returns the collapsed.<p>
214         *
215         * @return the collapsed
216         */
217        public boolean isCollapsed() {
218
219            return (m_flags & COLLAPSED) != 0;
220        }
221
222        /**
223         * Returns the visible.<p>
224         *
225         * @return the visible
226         */
227        public boolean isVisible() {
228
229            return 0 == (m_flags & INVISIBLE);
230        }
231
232        /**
233         * Sets the column.<p>
234         *
235         * @param column the column to set
236         */
237        public void setColumn(CmsResourceTableProperty column) {
238
239            m_column = column;
240        }
241
242        /**
243         * Sets the flags.<p>
244         *
245         * @param flags the flags to set
246         */
247        public void setFlags(int flags) {
248
249            m_flags = flags;
250        }
251
252    }
253
254    /**
255     * Default description generator for table entries.
256     */
257    public static class DefaultItemDescriptionGenerator implements ItemDescriptionGenerator {
258
259        /** Serial version id.*/
260        private static final long serialVersionUID = 1L;
261
262        /**
263         * @see com.vaadin.v7.ui.AbstractSelect.ItemDescriptionGenerator#generateDescription(com.vaadin.ui.Component, java.lang.Object, java.lang.Object)
264         */
265        @SuppressWarnings("synthetic-access")
266        public String generateDescription(Component source, Object itemId, Object propertyId) {
267
268            Table table = (Table)source;
269            try {
270                if ((propertyId != null) && (itemId != null)) {
271                    Property prop = table.getContainerDataSource().getItem(itemId).getItemProperty(propertyId);
272                    Converter<String, Object> converter = table.getConverter(propertyId);
273                    if (CmsResourceTableProperty.PROPERTY_RESOURCE_NAME == propertyId) {
274                        // when working with the explorer, tool tips constantly showing up when
275                        // hovering over the file name accidentally seems more annoying than useful
276                        return null;
277                    } else if ((converter != null) && String.class.equals(converter.getPresentationType())) {
278                        return converter.convertToPresentation(
279                            prop.getValue(),
280                            String.class,
281                            A_CmsUI.get().getLocale());
282                    } else if (String.class.equals(prop.getType()) || ClassUtils.isPrimitiveOrWrapper(prop.getType())) {
283                        Object value = prop.getValue();
284                        if (value != null) {
285                            return "" + value;
286                        }
287                    }
288                }
289            } catch (Exception e) {
290                LOG.warn(e.getLocalizedMessage(), e);
291            }
292            return null;
293        }
294    }
295
296    /**
297     * Provides item property values for additional table columns.<p>
298     */
299    public static interface I_ResourcePropertyProvider {
300
301        /**
302         * Adds the property values to the given item.<p>
303         *
304         * @param resourceItem the resource item
305         * @param cms the cms context
306         * @param resource the resource
307         * @param locale  the workplace locale
308         */
309        void addItemProperties(Item resourceItem, CmsObject cms, CmsResource resource, Locale locale);
310    }
311
312    /**
313     * Extending the indexed container to make the number of un-filtered items available.<p>
314     */
315    protected static class ItemContainer extends IndexedContainer {
316
317        /** The serial version id. */
318        private static final long serialVersionUID = -2033722658471550506L;
319
320        /**
321         * @see com.vaadin.v7.data.util.IndexedContainer#getSortableContainerPropertyIds()
322         */
323        @Override
324        public Collection<?> getSortableContainerPropertyIds() {
325
326            if (getItemSorter() instanceof I_CmsItemSorter) {
327                return ((I_CmsItemSorter)getItemSorter()).getSortableContainerPropertyIds(this);
328            } else {
329                return super.getSortableContainerPropertyIds();
330            }
331        }
332
333        /**
334         * Returns the number of items in the container, not considering any filters.<p>
335         *
336         * @return the number of items
337         */
338        protected int getItemCount() {
339
340            return getAllItemIds().size();
341        }
342    }
343
344    /** Flag to mark columns as initially collapsed.*/
345    public static final int COLLAPSED = 1;
346
347    /** Flag to mark columns as invisible. */
348    public static final int INVISIBLE = 2;
349
350    /** The logger instance for this class. */
351    private static final Log LOG = CmsLog.getLog(CmsResourceTable.class);
352
353    /** Serial version id. */
354    private static final long serialVersionUID = 1L;
355
356    /** The resource data container. */
357    protected ItemContainer m_container = new ItemContainer();
358
359    /** The table used to display the resource data. */
360    protected Table m_fileTable = new Table();
361
362    /** Property provider for additional columns. */
363    protected List<I_ResourcePropertyProvider> m_propertyProviders;
364
365    /**
366     * Creates a new instance.<p>
367     *
368     * This constructor does *not* set up the columns of the table; use the ColumnBuilder inner class for this.
369     */
370    public CmsResourceTable() {
371
372        m_propertyProviders = new ArrayList<I_ResourcePropertyProvider>();
373        m_fileTable.setContainerDataSource(m_container);
374        setCompositionRoot(m_fileTable);
375        m_fileTable.setRowHeaderMode(RowHeaderMode.HIDDEN);
376        m_fileTable.setItemDescriptionGenerator(new DefaultItemDescriptionGenerator());
377    }
378
379    /**
380     * Static helper method to initialize the 'standard' properties of a data item from a given resource.<p>
381     * @param resourceItem the resource item to fill
382     * @param cms the CMS context
383     * @param resource the resource
384     * @param locale the locale
385     */
386    public static void fillItemDefault(Item resourceItem, CmsObject cms, CmsResource resource, Locale locale) {
387
388        if (resource == null) {
389            LOG.error("Error rendering item for 'null' resource");
390            return;
391        }
392
393        if (resourceItem == null) {
394            LOG.error("Error rendering 'null' item for resource " + resource.getRootPath());
395            return;
396        }
397        if (cms == null) {
398            cms = A_CmsUI.getCmsObject();
399            LOG.warn("CmsObject was 'null', using thread local CmsObject");
400        }
401        CmsResourceUtil resUtil = new CmsResourceUtil(cms, resource);
402        Map<String, CmsProperty> resourceProps = null;
403        try {
404            List<CmsProperty> props = cms.readPropertyObjects(resource, false);
405            resourceProps = new HashMap<String, CmsProperty>();
406            for (CmsProperty prop : props) {
407                resourceProps.put(prop.getName(), prop);
408            }
409        } catch (CmsException e1) {
410            LOG.debug("Unable to read properties for resource '" + resource.getRootPath() + "'.", e1);
411        }
412        I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(resource);
413        if (resourceItem.getItemProperty(PROPERTY_TYPE_ICON) != null) {
414            resourceItem.getItemProperty(PROPERTY_TYPE_ICON).setValue(
415                new CmsResourceIcon(resUtil, resource.getState(), true));
416        }
417
418        if (resourceItem.getItemProperty(PROPERTY_PROJECT) != null) {
419            Label projectFlag = null;
420            switch (resUtil.getProjectState().getMode()) {
421                case 1:
422                    projectFlag = new Label(
423                        new CmsCssIcon(OpenCmsTheme.ICON_PROJECT_CURRENT).getHtml(resUtil.getLockedInProjectName()),
424                        ContentMode.HTML);
425                    break;
426                case 2:
427                    projectFlag = new Label(
428                        new CmsCssIcon(OpenCmsTheme.ICON_PROJECT_OTHER).getHtml(resUtil.getLockedInProjectName()),
429                        ContentMode.HTML);
430                    break;
431                case 5:
432                    projectFlag = new Label(
433                        new CmsCssIcon(OpenCmsTheme.ICON_PUBLISH).getHtml(resUtil.getLockedInProjectName()),
434                        ContentMode.HTML);
435                    break;
436                default:
437            }
438            resourceItem.getItemProperty(PROPERTY_PROJECT).setValue(projectFlag);
439        }
440
441        if (resourceItem.getItemProperty(PROPERTY_INSIDE_PROJECT) != null) {
442            resourceItem.getItemProperty(PROPERTY_INSIDE_PROJECT).setValue(Boolean.valueOf(resUtil.isInsideProject()));
443        }
444
445        if (resourceItem.getItemProperty(PROPERTY_RELEASED_NOT_EXPIRED) != null) {
446            resourceItem.getItemProperty(PROPERTY_RELEASED_NOT_EXPIRED).setValue(
447                Boolean.valueOf(resUtil.isReleasedAndNotExpired()));
448        }
449
450        if (resourceItem.getItemProperty(PROPERTY_RESOURCE_NAME) != null) {
451            resourceItem.getItemProperty(PROPERTY_RESOURCE_NAME).setValue(resource.getName());
452        }
453
454        if (resourceItem.getItemProperty(PROPERTY_SITE_PATH) != null) {
455            resourceItem.getItemProperty(PROPERTY_SITE_PATH).setValue(cms.getSitePath(resource));
456        }
457
458        if ((resourceItem.getItemProperty(PROPERTY_TITLE) != null) && (resourceProps != null)) {
459            resourceItem.getItemProperty(PROPERTY_TITLE).setValue(
460                resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_TITLE)
461                ? resourceProps.get(CmsPropertyDefinition.PROPERTY_TITLE).getValue()
462                : "");
463        }
464        boolean inNavigation = false;
465        if ((resourceItem.getItemProperty(PROPERTY_NAVIGATION_TEXT) != null) && (resourceProps != null)) {
466            resourceItem.getItemProperty(PROPERTY_NAVIGATION_TEXT).setValue(
467                resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_NAVTEXT)
468                ? resourceProps.get(CmsPropertyDefinition.PROPERTY_NAVTEXT).getValue()
469                : "");
470            inNavigation = resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_NAVTEXT);
471        }
472
473        if ((resourceItem.getItemProperty(PROPERTY_NAVIGATION_POSITION) != null) && (resourceProps != null)) {
474            try {
475                Float navPos = resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_NAVPOS)
476                ? Float.valueOf(resourceProps.get(CmsPropertyDefinition.PROPERTY_NAVPOS).getValue())
477                : (inNavigation ? Float.valueOf(Float.MAX_VALUE) : null);
478                resourceItem.getItemProperty(PROPERTY_NAVIGATION_POSITION).setValue(navPos);
479                inNavigation = navPos != null;
480            } catch (Exception e) {
481                LOG.debug("Error evaluating navPos property", e);
482            }
483        }
484
485        if (resourceItem.getItemProperty(PROPERTY_IN_NAVIGATION) != null) {
486            if (inNavigation
487                && (resourceProps != null)
488                && resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_NAVINFO)
489                && CmsClientSitemapEntry.HIDDEN_NAVIGATION_ENTRY.equals(
490                    resourceProps.get(CmsPropertyDefinition.PROPERTY_NAVINFO).getValue())) {
491                inNavigation = false;
492            }
493            resourceItem.getItemProperty(PROPERTY_IN_NAVIGATION).setValue(Boolean.valueOf(inNavigation));
494        }
495
496        if ((resourceItem.getItemProperty(PROPERTY_COPYRIGHT) != null) && (resourceProps != null)) {
497            resourceItem.getItemProperty(PROPERTY_COPYRIGHT).setValue(
498                resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_COPYRIGHT)
499                ? resourceProps.get(CmsPropertyDefinition.PROPERTY_COPYRIGHT).getValue()
500                : "");
501        }
502
503        if ((resourceItem.getItemProperty(PROPERTY_CACHE) != null) && (resourceProps != null)) {
504            resourceItem.getItemProperty(PROPERTY_CACHE).setValue(
505                resourceProps.containsKey(CmsPropertyDefinition.PROPERTY_CACHE)
506                ? resourceProps.get(CmsPropertyDefinition.PROPERTY_CACHE).getValue()
507                : "");
508        }
509
510        if (resourceItem.getItemProperty(PROPERTY_RESOURCE_TYPE) != null) {
511            resourceItem.getItemProperty(PROPERTY_RESOURCE_TYPE).setValue(
512                CmsWorkplaceMessages.getResourceTypeName(locale, type.getTypeName()));
513        }
514
515        if (resourceItem.getItemProperty(PROPERTY_INTERNAL_RESOURCE_TYPE) != null) {
516            resourceItem.getItemProperty(PROPERTY_INTERNAL_RESOURCE_TYPE).setValue(type.getTypeName());
517        }
518
519        if (resourceItem.getItemProperty(PROPERTY_IS_FOLDER) != null) {
520            resourceItem.getItemProperty(PROPERTY_IS_FOLDER).setValue(Boolean.valueOf(resource.isFolder()));
521        }
522
523        if (resourceItem.getItemProperty(PROPERTY_SIZE) != null) {
524            if (resource.isFile()) {
525                resourceItem.getItemProperty(PROPERTY_SIZE).setValue(Integer.valueOf(resource.getLength()));
526            }
527        }
528
529        if (resourceItem.getItemProperty(PROPERTY_PERMISSIONS) != null) {
530            resourceItem.getItemProperty(PROPERTY_PERMISSIONS).setValue(resUtil.getPermissionString());
531        }
532
533        if (resourceItem.getItemProperty(PROPERTY_DATE_MODIFIED) != null) {
534            resourceItem.getItemProperty(PROPERTY_DATE_MODIFIED).setValue(Long.valueOf(resource.getDateLastModified()));
535        }
536
537        if (resourceItem.getItemProperty(PROPERTY_USER_MODIFIED) != null) {
538            resourceItem.getItemProperty(PROPERTY_USER_MODIFIED).setValue(resUtil.getUserLastModified());
539        }
540
541        if (resourceItem.getItemProperty(PROPERTY_DATE_CREATED) != null) {
542            resourceItem.getItemProperty(PROPERTY_DATE_CREATED).setValue(Long.valueOf(resource.getDateCreated()));
543        }
544
545        if (resourceItem.getItemProperty(PROPERTY_USER_CREATED) != null) {
546            resourceItem.getItemProperty(PROPERTY_USER_CREATED).setValue(resUtil.getUserCreated());
547        }
548
549        if (resourceItem.getItemProperty(PROPERTY_DATE_RELEASED) != null) {
550            long release = resource.getDateReleased();
551            if (release != CmsResource.DATE_RELEASED_DEFAULT) {
552                resourceItem.getItemProperty(PROPERTY_DATE_RELEASED).setValue(Long.valueOf(release));
553            } else {
554                resourceItem.getItemProperty(PROPERTY_DATE_RELEASED).setValue(null);
555            }
556        }
557
558        if (resourceItem.getItemProperty(PROPERTY_DATE_EXPIRED) != null) {
559            long expire = resource.getDateExpired();
560            if (expire != CmsResource.DATE_EXPIRED_DEFAULT) {
561                resourceItem.getItemProperty(PROPERTY_DATE_EXPIRED).setValue(Long.valueOf(expire));
562            } else {
563                resourceItem.getItemProperty(PROPERTY_DATE_EXPIRED).setValue(null);
564            }
565        }
566
567        if (resourceItem.getItemProperty(PROPERTY_STATE_NAME) != null) {
568            resourceItem.getItemProperty(PROPERTY_STATE_NAME).setValue(resUtil.getStateName());
569        }
570
571        if (resourceItem.getItemProperty(PROPERTY_STATE) != null) {
572            resourceItem.getItemProperty(PROPERTY_STATE).setValue(resource.getState());
573        }
574
575        if (resourceItem.getItemProperty(PROPERTY_USER_LOCKED) != null) {
576            resourceItem.getItemProperty(PROPERTY_USER_LOCKED).setValue(resUtil.getLockedByName());
577        }
578    }
579
580    /**
581     * Gets the CSS style name for the given resource state.<p>
582     *
583     * @param state the resource state
584     * @return the CSS style name
585     */
586    public static String getStateStyle(CmsResourceState state) {
587
588        String stateStyle = "";
589        if (state != null) {
590            if (state.isDeleted()) {
591                stateStyle = OpenCmsTheme.STATE_DELETED;
592            } else if (state.isNew()) {
593                stateStyle = OpenCmsTheme.STATE_NEW;
594            } else if (state.isChanged()) {
595                stateStyle = OpenCmsTheme.STATE_CHANGED;
596            }
597        }
598        return stateStyle;
599    }
600
601    /**
602     * Adds a property provider.<p>
603     *
604     * @param provider the property provider
605     */
606    public void addPropertyProvider(I_ResourcePropertyProvider provider) {
607
608        m_propertyProviders.add(provider);
609    }
610
611    /**
612     * Clears the value selection.<p>
613     */
614    public void clearSelection() {
615
616        m_fileTable.setValue(Collections.emptySet());
617    }
618
619    /**
620     * Fills the resource table.<p>
621     *
622     * @param cms the current CMS context
623     * @param resources the resources which should be displayed in the table
624     */
625    public void fillTable(CmsObject cms, List<CmsResource> resources) {
626
627        fillTable(cms, resources, true);
628    }
629
630    /**
631     * Fills the resource table.<p>
632     *
633     * @param cms the current CMS context
634     * @param resources the resources which should be displayed in the table
635     * @param clearFilter <code>true</code> to clear the search filter
636     */
637    public void fillTable(CmsObject cms, List<CmsResource> resources, boolean clearFilter) {
638
639        fillTable(cms, resources, clearFilter, true);
640    }
641
642    /**
643     * Fills the resource table.<p>
644     *
645     * @param cms the current CMS context
646     * @param resources the resources which should be displayed in the table
647     * @param clearFilter <code>true</code> to clear the search filter
648     * @param sort <code>true</code> to sort the table entries
649     */
650    public void fillTable(CmsObject cms, List<CmsResource> resources, boolean clearFilter, boolean sort) {
651
652        Locale wpLocale = OpenCms.getWorkplaceManager().getWorkplaceLocale(cms);
653        m_container.removeAllItems();
654        if (clearFilter) {
655            m_container.removeAllContainerFilters();
656        }
657        for (CmsResource resource : resources) {
658            fillItem(cms, resource, wpLocale);
659        }
660        if (sort) {
661            m_fileTable.sort();
662        }
663        clearSelection();
664    }
665
666    /**
667     * Gets structure ids of resources for current folder in current sort order.<p>
668     *
669     * @return the structure ids of the current folder contents
670     */
671    @SuppressWarnings("unchecked")
672    public List<CmsUUID> getAllIds() {
673
674        return itemIdsToUUIDs((List<String>)m_fileTable.getContainerDataSource().getItemIds());
675    }
676
677    /**
678     * Returns the number of currently visible items.<p>
679     *
680     * @return the number of currentliy visible items
681     */
682    public int getItemCount() {
683
684        return m_container.getItemCount();
685    }
686
687    /**
688     * Returns the structure id to the given string item id.<p>
689     *
690     * @param itemId the item id
691     *
692     * @return the structure id
693     */
694    public CmsUUID getUUIDFromItemID(String itemId) {
695
696        return new CmsUUID(itemId);
697    }
698
699    /**
700     * Returns if the column with the given property id is visible and not collapsed.<p>
701     *
702     * @param propertyId the property id
703     *
704     * @return <code>true</code> if the column is visible
705     */
706    public boolean isColumnVisible(CmsResourceTableProperty propertyId) {
707
708        return Arrays.asList(m_fileTable.getVisibleColumns()).contains(propertyId)
709            && !m_fileTable.isColumnCollapsed(propertyId);
710    }
711
712    /**
713     * Removes a property provider.<p>
714     *
715     * @param provider the provider to remove
716     */
717    public void removePropertyProvider(I_ResourcePropertyProvider provider) {
718
719        m_propertyProviders.remove(provider);
720    }
721
722    /**
723     * Selects all resources.<p>
724     */
725    public void selectAll() {
726
727        m_fileTable.setValue(m_fileTable.getItemIds());
728    }
729
730    /**
731     * Sets the list of collapsed columns.<p>
732     *
733     * @param collapsedColumns the list of collapsed columns
734     */
735    public void setCollapsedColumns(Object... collapsedColumns) {
736
737        Set<Object> collapsedSet = Sets.newHashSet();
738        for (Object collapsed : collapsedColumns) {
739            collapsedSet.add(collapsed);
740        }
741        for (Object key : m_fileTable.getVisibleColumns()) {
742            m_fileTable.setColumnCollapsed(key, collapsedSet.contains(key));
743        }
744    }
745
746    /**
747     * Sets the table drag mode.<p>
748     *
749     * @param dragMode the drag mode
750     */
751    public void setDragMode(TableDragMode dragMode) {
752
753        m_fileTable.setDragMode(dragMode);
754    }
755
756    /**
757     * Sets the table drop handler.<p>
758     *
759     * @param handler the drop handler
760     */
761    public void setDropHandler(DropHandler handler) {
762
763        m_fileTable.setDropHandler(handler);
764    }
765
766    /**
767     * Selects an given object in table.<p>
768     *
769     * @param o object to be selected.
770     */
771    public void setValue(Set<String> o) {
772
773        m_fileTable.setValue(o);
774    }
775
776    /**
777     * Fills the file item data.<p>
778     *
779     * @param cms the cms context
780     * @param resource the resource
781     * @param locale the workplace locale
782     */
783    protected void fillItem(CmsObject cms, CmsResource resource, Locale locale) {
784
785        Item resourceItem = m_container.getItem(resource.getStructureId().toString());
786        if (resourceItem == null) {
787            resourceItem = m_container.addItem(resource.getStructureId().toString());
788        }
789        fillItemDefault(resourceItem, cms, resource, locale);
790        for (I_ResourcePropertyProvider provider : m_propertyProviders) {
791            provider.addItemProperties(resourceItem, cms, resource, locale);
792        }
793    }
794
795    /**
796     * Transforms the given item ids into UUIDs.<p>
797     *
798     * @param itemIds the item ids
799     *
800     * @return the UUIDs
801     */
802    protected List<CmsUUID> itemIdsToUUIDs(Collection<String> itemIds) {
803
804        List<CmsUUID> ids = new ArrayList<CmsUUID>();
805        for (String itemId : itemIds) {
806            if (itemId != null) {
807                ids.add(getUUIDFromItemID(itemId));
808            }
809        }
810        return ids;
811    }
812}