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.resourcetypes;
029
030import org.opencms.file.CmsObject;
031import org.opencms.file.CmsResource;
032import org.opencms.file.CmsResourceFilter;
033import org.opencms.file.types.A_CmsResourceTypeFolderBase;
034import org.opencms.file.types.CmsResourceTypeXmlContent;
035import org.opencms.file.types.I_CmsResourceType;
036import org.opencms.loader.CmsLoaderException;
037import org.opencms.main.CmsException;
038import org.opencms.main.CmsLog;
039import org.opencms.main.OpenCms;
040import org.opencms.module.CmsModule;
041import org.opencms.ui.A_CmsUI;
042import org.opencms.ui.CmsVaadinUtils;
043import org.opencms.ui.apps.CmsAppWorkplaceUi;
044import org.opencms.ui.apps.CmsEditor;
045import org.opencms.ui.apps.CmsEditorConfiguration;
046import org.opencms.ui.apps.Messages;
047import org.opencms.ui.apps.search.CmsSearchReplaceSettings;
048import org.opencms.ui.apps.search.CmsSourceSearchApp;
049import org.opencms.ui.apps.search.CmsSourceSearchAppConfiguration;
050import org.opencms.ui.apps.search.CmsSourceSearchForm.SearchType;
051import org.opencms.ui.components.CmsBasicDialog;
052import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
053import org.opencms.ui.components.CmsConfirmationDialog;
054import org.opencms.ui.contextmenu.CmsContextMenu;
055import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
056import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
057import org.opencms.util.CmsStringUtil;
058import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
059import org.opencms.workplace.explorer.CmsResourceUtil;
060
061import java.util.ArrayList;
062import java.util.Collections;
063import java.util.HashMap;
064import java.util.Iterator;
065import java.util.List;
066import java.util.Locale;
067import java.util.Map;
068import java.util.Set;
069
070import org.apache.commons.logging.Log;
071
072import com.google.common.collect.Lists;
073import com.vaadin.event.MouseEvents;
074import com.vaadin.server.Resource;
075import com.vaadin.shared.MouseEventDetails.MouseButton;
076import com.vaadin.ui.UI;
077import com.vaadin.ui.Window;
078import com.vaadin.ui.themes.ValoTheme;
079import com.vaadin.v7.data.Item;
080import com.vaadin.v7.data.util.IndexedContainer;
081import com.vaadin.v7.data.util.filter.Or;
082import com.vaadin.v7.data.util.filter.SimpleStringFilter;
083import com.vaadin.v7.event.ItemClickEvent;
084import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
085import com.vaadin.v7.ui.Table;
086
087/**
088 * Table for resource types on the system.<p>
089 */
090public class CmsResourceTypesTable extends Table {
091
092    /**
093     * The delete project context menu entry.<p>
094     */
095    class DeleteEntry implements I_CmsSimpleContextMenuEntry<Set<String>> {
096
097        /**
098         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
099         */
100        public void executeAction(final Set<String> data) {
101
102            try {
103                final Window window = CmsBasicDialog.prepareWindow();
104                Iterator<String> it = data.iterator();
105                boolean existResources = false;
106                while (it.hasNext()) {
107                    I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(it.next());
108                    if (m_cms.readResources("", CmsResourceFilter.requireType(type), true).size() > 0) {
109                        existResources = true;
110                    }
111                }
112
113                if (existResources) {
114                    CmsConfirmationDialog.show(
115                        CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_DELETE_NOT_POSSIBLE_0),
116                        CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_DELETE_NOT_POSSIBLE_LONG_0),
117                        new Runnable() {
118
119                            public void run() {
120
121                                window.close();
122                            }
123                        },
124                        null,
125                        true);
126
127                } else {
128                    CmsConfirmationDialog.show(
129                        CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_DELETE_CONFIRM_0),
130                        CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_DELETE_CONFIRM_LONG_0),
131                        new Runnable() {
132
133                            public void run() {
134
135                                try {
136                                    Iterator<String> it = data.iterator();
137                                    Map<String, CmsModule> modulesToBeUpdated = new HashMap<String, CmsModule>();
138                                    while (it.hasNext()) {
139                                        I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(
140                                            it.next());
141                                        CmsModule module;
142                                        if (!modulesToBeUpdated.containsKey(type.getModuleName())) {
143                                            modulesToBeUpdated.put(
144                                                type.getModuleName(),
145                                                OpenCms.getModuleManager().getModule(type.getModuleName()).clone());
146                                        }
147                                        module = modulesToBeUpdated.get(type.getModuleName());
148
149                                        List<CmsExplorerTypeSettings> typeSettings = Lists.newArrayList(
150                                            module.getExplorerTypes());
151                                        List<CmsExplorerTypeSettings> newTypeSettings = new ArrayList<CmsExplorerTypeSettings>();
152                                        for (CmsExplorerTypeSettings setting : typeSettings) {
153                                            if (!setting.getName().equals(type.getTypeName())) {
154                                                newTypeSettings.add(setting);
155                                            }
156                                        }
157                                        OpenCms.getWorkplaceManager().removeExplorerTypeSettings(module);
158
159                                        List<I_CmsResourceType> types = new ArrayList<I_CmsResourceType>(
160                                            module.getResourceTypes());
161
162                                        types.remove(type);
163
164                                        module.setResourceTypes(types);
165
166                                        module.setExplorerTypes(newTypeSettings);
167
168                                    }
169                                    for (String moduleName : modulesToBeUpdated.keySet()) {
170                                        OpenCms.getModuleManager().updateModule(
171                                            m_cms,
172                                            modulesToBeUpdated.get(moduleName));
173                                        OpenCms.getResourceManager().initialize(m_cms);
174                                        OpenCms.getWorkplaceManager().addExplorerTypeSettings(
175                                            modulesToBeUpdated.get(moduleName));
176                                    }
177                                    // re-initialize the workplace
178                                    OpenCms.getWorkplaceManager().initialize(m_cms);
179                                } catch (CmsException e) {
180                                    LOG.error("Unable to delete resource type", e);
181                                }
182                                window.close();
183                                m_app.reload();
184                            }
185                        },
186                        new Runnable() {
187
188                            public void run() {
189
190                                window.close();
191                            }
192                        });
193                }
194
195            } catch (CmsException e) {
196                LOG.error("Unable to delete resource type", e);
197            }
198        }
199
200        /**
201         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
202         */
203        public String getTitle(Locale locale) {
204
205            return CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_DELETE_0);
206        }
207
208        /**
209         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
210         */
211        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
212
213            try {
214                Iterator<String> it = data.iterator();
215                while (it.hasNext()) {
216                    I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(it.next());
217                    if (CmsStringUtil.isEmptyOrWhitespaceOnly(type.getModuleName())) {
218                        return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
219                            Messages.GUI_RESOURCETYPE_APP_TABLE_NOT_AVAILABLE_CORE_0);
220                    }
221                }
222                return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
223            } catch (CmsException e) {
224                LOG.error("Unable to read resourcetype", e);
225                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
226            }
227        }
228    }
229
230    /**
231     * The delete project context menu entry.<p>
232     */
233    class EditEntry implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles {
234
235        /**
236         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
237         */
238        public void executeAction(final Set<String> data) {
239
240            openEditDialog(data.iterator().next());
241        }
242
243        public String getStyles() {
244
245            return ValoTheme.LABEL_BOLD;
246        }
247
248        /**
249         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
250         */
251        public String getTitle(Locale locale) {
252
253            return CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_EDIT_0);
254        }
255
256        /**
257         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
258         */
259        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
260
261            if (data.size() > 1) {
262                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
263                    Messages.GUI_RESOURCETYPE_APP_TABLE_NO_AVAILABLE_MULTIPLE_0);
264            }
265            String typeName = data.iterator().next();
266            try {
267                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(typeName);
268                return CmsStringUtil.isEmptyOrWhitespaceOnly(type.getModuleName())
269                ? CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
270                    Messages.GUI_RESOURCETYPE_APP_TABLE_NOT_AVAILABLE_CORE_0)
271                : CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
272            } catch (CmsLoaderException e) {
273                LOG.error("Unable to read resource type by name", e);
274            }
275            return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
276        }
277    }
278
279    /**
280     * The delete project context menu entry.<p>
281     */
282    class MoveEntry implements I_CmsSimpleContextMenuEntry<Set<String>> {
283
284        /**
285         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
286         */
287        public void executeAction(final Set<String> data) {
288
289            try {
290                Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
291                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(data.iterator().next());
292                window.setContent(new CmsMoveResourceTypeDialog(window, type));
293                String moduleName = type.getModuleName();
294                window.setCaption(
295                    CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_MOVE_WINDOW_CAPTION_1, moduleName));
296                A_CmsUI.get().addWindow(window);
297            } catch (CmsLoaderException e) {
298                LOG.error("Unable to read resource type by name", e);
299            }
300        }
301
302        /**
303         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
304         */
305        public String getTitle(Locale locale) {
306
307            return CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_MOVE_0);
308        }
309
310        /**
311         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
312         */
313        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
314
315            if (data.size() > 1) {
316                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
317                    Messages.GUI_RESOURCETYPE_APP_TABLE_NO_AVAILABLE_MULTIPLE_0);
318            }
319            try {
320                Iterator<String> it = data.iterator();
321                while (it.hasNext()) {
322                    I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(it.next());
323                    if (CmsStringUtil.isEmptyOrWhitespaceOnly(type.getModuleName())) {
324                        return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
325                            Messages.GUI_RESOURCETYPE_APP_TABLE_NOT_AVAILABLE_CORE_0);
326                    }
327                }
328                return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
329            } catch (CmsException e) {
330                LOG.error("Unable to read resourcetype", e);
331                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
332            }
333        }
334    }
335
336    /**
337     * The menu entry to switch to the explorer of concerning site.<p>
338     */
339    class SchemaEditorEntry implements I_CmsSimpleContextMenuEntry<Set<String>> {
340
341        /**
342         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
343         */
344        public void executeAction(Set<String> data) {
345
346            try {
347                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(data.iterator().next());
348                if (type instanceof CmsResourceTypeXmlContent) {
349                    CmsResourceTypeXmlContent typeXML = (CmsResourceTypeXmlContent)type;
350
351                    CmsResource resource = m_cms.readResource(typeXML.getSchema());
352                    String editState = CmsEditor.getEditState(
353                        resource.getStructureId(),
354                        false,
355                        UI.getCurrent().getPage().getLocation().toString());
356
357                    CmsAppWorkplaceUi.get().showApp(
358                        OpenCms.getWorkplaceAppManager().getAppConfiguration(CmsEditorConfiguration.APP_ID),
359                        editState);
360                }
361
362            } catch (CmsLoaderException e) {
363                LOG.error("Unable to read resource type", e);
364            } catch (CmsException e) {
365                LOG.error("Unable to read schema file", e);
366            }
367        }
368
369        /**
370         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
371         */
372        public String getTitle(Locale locale) {
373
374            return CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_EDIT_SCHEMA_DEFINITION_0);
375        }
376
377        /**
378         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
379         */
380        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
381
382            if (data.size() > 1) {
383                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
384                    Messages.GUI_RESOURCETYPE_APP_TABLE_NO_AVAILABLE_MULTIPLE_0);
385            }
386            try {
387                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(data.iterator().next());
388                if (type instanceof CmsResourceTypeXmlContent) {
389                    CmsResourceTypeXmlContent typeXML = (CmsResourceTypeXmlContent)type;
390
391                    try {
392                        m_cms.readResource(typeXML.getSchema());
393                    } catch (CmsException e) {
394                        return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
395                    }
396                    return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
397                }
398
399            } catch (CmsLoaderException e) {
400                LOG.error("Unable to read resource type", e);
401            }
402            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
403        }
404
405    }
406
407    /**
408     * The menu entry to switch to the explorer of concerning site.<p>
409     */
410    class SearchEntry implements I_CmsSimpleContextMenuEntry<Set<String>> {
411
412        /**
413         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
414         */
415        public void executeAction(Set<String> data) {
416
417            CmsSearchReplaceSettings settings = new CmsSearchReplaceSettings();
418            settings.setPaths(Collections.singletonList("/"));
419            settings.setSiteRoot(m_cms.getRequestContext().getSiteRoot());
420            settings.setSearchpattern(".*");
421            settings.setTypes(data.iterator().next());
422            settings.setType(SearchType.fullText);
423            CmsAppWorkplaceUi.get().showApp(
424                CmsSourceSearchAppConfiguration.APP_ID,
425                CmsSourceSearchApp.generateState(settings));
426
427        }
428
429        /**
430         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
431         */
432        public String getTitle(Locale locale) {
433
434            return CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_SEARCH_RESOURCES_0);
435        }
436
437        /**
438         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
439         */
440        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
441
442            if (data.size() > 1) {
443                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
444                    Messages.GUI_RESOURCETYPE_APP_TABLE_NO_AVAILABLE_MULTIPLE_0);
445            }
446            try {
447                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(data.iterator().next());
448
449                return type instanceof A_CmsResourceTypeFolderBase
450                ? CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE
451                : CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
452            } catch (CmsLoaderException e) {
453                return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
454            }
455        }
456
457    }
458
459    /**
460     * All table properties.<p>
461     */
462    enum TableProperty {
463
464        /**Icon.*/
465        Icon(null, Resource.class, null, false),
466
467        /**Icon column.*/
468        Name(Messages.GUI_RESOURCETYPE_EDIT_DISPLAY_NAME_0, String.class, "", false),
469
470        /**Icon column.*/
471        ShortName(Messages.GUI_RESOURCETYPE_EDIT_SHORT_NAME_0, String.class, "", false),
472
473        /**Is Broadcast send but not displayed.*/
474        ID(Messages.GUI_RESOURCETYPE_ID_0, Integer.class, null, false),
475
476        /**Icon column.*/
477        Module(Messages.GUI_RESOURCETYPE_MODULE_0, String.class, "", false);
478
479        /**Indicates if column is collapsable.*/
480        private boolean m_collapsable;
481
482        /**Default value for column.*/
483        private Object m_defaultValue;
484
485        /**Header Message key.*/
486        private String m_headerMessage;
487
488        /**Type of column property.*/
489        private Class<?> m_type;
490
491        /**
492         * constructor.
493         *
494         * @param headerMessage key
495         * @param type to property
496         * @param defaultValue of column
497         * @param collapsable should this column be collapsable?
498         */
499        TableProperty(String headerMessage, Class<?> type, Object defaultValue, boolean collapsable) {
500
501            m_headerMessage = headerMessage;
502            m_type = type;
503            m_defaultValue = defaultValue;
504            m_collapsable = collapsable;
505        }
506
507        /**
508         * Returns list of all properties with non-empty header.<p>
509         *
510         * @return list of properties
511         */
512        static List<TableProperty> withHeader() {
513
514            List<TableProperty> props = new ArrayList<TableProperty>();
515
516            for (TableProperty prop : TableProperty.values()) {
517                if (prop.m_headerMessage != null) {
518                    props.add(prop);
519                }
520            }
521            return props;
522        }
523
524        /**
525         * Returns the default value of property.<p>
526         *
527         * @return object
528         */
529        Object getDefaultValue() {
530
531            return m_defaultValue;
532        }
533
534        /**
535         * Returns localized header.<p>
536         *
537         * @return string for header
538         */
539        String getLocalizedMessage() {
540
541            if (m_headerMessage == null) {
542                return "";
543            }
544            return CmsVaadinUtils.getMessageText(m_headerMessage);
545        }
546
547        /**
548         * Returns tye of value for given property.<p>
549         *
550         * @return type
551         */
552        Class<?> getType() {
553
554            return m_type;
555        }
556
557        /**
558         * Indicates if column is collapsable.<p>
559         *
560         * @return boolean, true = is collapsable
561         */
562        boolean isCollapsable() {
563
564            return m_collapsable;
565        }
566
567    }
568
569    private static final long serialVersionUID = 1L;
570
571    /** Logger instance for this class. */
572    static final Log LOG = CmsLog.getLog(CmsResourceTypesTable.class);
573
574    /** CmsObject.*/
575    CmsObject m_cms;
576
577    /** The context menu. */
578    private CmsContextMenu m_menu;
579
580    /** The available menu entries. */
581    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
582
583    /**Container holding table data.*/
584    private IndexedContainer m_container;
585
586    /**Resource type app instance. */
587    CmsResourceTypeApp m_app;
588
589    /**
590     * Public constructor.<p>
591     *
592     * @param app instance
593     */
594    public CmsResourceTypesTable(CmsResourceTypeApp app) {
595
596        m_app = app;
597        try {
598            m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
599        } catch (CmsException e) {
600            //
601        }
602
603        m_menu = new CmsContextMenu();
604        m_menu.setAsTableContextMenu(this);
605
606        setSizeFull();
607        init();
608
609        addItemClickListener(new ItemClickListener() {
610
611            private static final long serialVersionUID = 7957778390938304845L;
612
613            public void itemClick(ItemClickEvent event) {
614
615                onItemClick(event, event.getItemId(), event.getPropertyId());
616            }
617
618        });
619
620    }
621
622    /**
623     * Filters the table according to given string.<p>
624     *
625     * @param text to filter
626     */
627    public void filterTable(String text) {
628
629        m_container.removeAllContainerFilters();
630        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(text)) {
631            m_container.addContainerFilter(
632                new Or(
633                    new SimpleStringFilter(TableProperty.Name, text, true, false),
634                    new SimpleStringFilter(TableProperty.ShortName, text, true, false),
635                    new SimpleStringFilter(TableProperty.Module, text, true, false)));
636        }
637
638    }
639
640    /**
641     * Returns the available menu entries.<p>
642     *
643     * @return the menu entries
644     */
645    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
646
647        if (m_menuEntries == null) {
648            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
649            m_menuEntries.add(new EditEntry());
650            m_menuEntries.add(new SchemaEditorEntry());
651            m_menuEntries.add(new SearchEntry());
652            m_menuEntries.add(new MoveEntry());
653            m_menuEntries.add(new DeleteEntry());
654        }
655        return m_menuEntries;
656    }
657
658    /**
659     * Init the table.<p>
660     */
661    void init() {
662
663        if (m_container == null) {
664            m_container = new IndexedContainer();
665            setContainerDataSource(m_container);
666        } else {
667            m_container.removeAllContainerFilters();
668            m_container.removeAllItems();
669        }
670        for (TableProperty prop : TableProperty.values()) {
671            m_container.addContainerProperty(prop, prop.getType(), prop.getDefaultValue());
672            setColumnHeader(prop, prop.getLocalizedMessage());
673        }
674
675        setItemIconPropertyId(TableProperty.Icon);
676        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
677        setColumnWidth(null, 40);
678        setSelectable(true);
679        setMultiSelect(true);
680
681        for (I_CmsResourceType type : CmsVaadinUtils.getResourceTypes()) {
682            CmsExplorerTypeSettings typeSetting = OpenCms.getWorkplaceManager().getExplorerTypeSetting(
683                type.getTypeName());
684            Item item = m_container.addItem(type.getTypeName());
685            item.getItemProperty(TableProperty.ID).setValue(Integer.valueOf(type.getTypeId()));
686            item.getItemProperty(TableProperty.Icon).setValue(CmsResourceUtil.getBigIconResource(typeSetting, null));
687            item.getItemProperty(TableProperty.Name).setValue(CmsVaadinUtils.getMessageText(typeSetting.getKey()));
688            item.getItemProperty(TableProperty.ShortName).setValue(type.getTypeName());
689            item.getItemProperty(TableProperty.Module).setValue(type.getModuleName());
690        }
691
692        setVisibleColumns(TableProperty.Name, TableProperty.ShortName, TableProperty.ID, TableProperty.Module);
693    }
694
695    /**
696     * Handles the table item clicks, including clicks on images inside of a table item.<p>
697     *
698     * @param event the click event
699     * @param itemId of the clicked row
700     * @param propertyId column id
701     */
702    @SuppressWarnings("unchecked")
703    void onItemClick(MouseEvents.ClickEvent event, Object itemId, Object propertyId) {
704
705        if (!event.isCtrlKey() && !event.isShiftKey()) {
706            changeValueIfNotMultiSelect(itemId);
707            // don't interfere with multi-selection using control key
708            if (event.getButton().equals(MouseButton.RIGHT) || (propertyId == null)) {
709                m_menu.setEntries(getMenuEntries(), (Set<String>)getValue());
710                m_menu.openForTable(event, itemId, propertyId, this);
711            } else if (event.getButton().equals(MouseButton.LEFT) && TableProperty.Name.equals(propertyId)) {
712                String typeName = (String)itemId;
713                openEditDialog(typeName);
714            }
715        }
716    }
717
718    /**
719     * Opens the edit dialog.<p>
720     *
721     * @param typeName type to be edited.
722     */
723    void openEditDialog(String typeName) {
724
725        try {
726            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
727
728            window.setContent(
729                new CmsEditResourceTypeDialog(window, m_app, OpenCms.getResourceManager().getResourceType(typeName)));
730            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_EDIT_WINDOW_CAPTION_0));
731            A_CmsUI.get().addWindow(window);
732        } catch (CmsLoaderException e) {
733            LOG.error("Unable to read resource type by name", e);
734        }
735    }
736
737    /**
738     * Checks value of table and sets it new if needed:<p>
739     * if multiselect: new itemId is in current Value? -> no change of value<p>
740     * no multiselect and multiselect, but new item not selected before: set value to new item<p>
741     *
742     * @param itemId if of clicked item
743     */
744    private void changeValueIfNotMultiSelect(Object itemId) {
745
746        @SuppressWarnings("unchecked")
747        Set<String> value = (Set<String>)getValue();
748        if (value == null) {
749            select(itemId);
750        } else if (!value.contains(itemId)) {
751            setValue(null);
752            select(itemId);
753        }
754    }
755}