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