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().getNavigator().navigateTo(
424                CmsSourceSearchAppConfiguration.APP_ID + "/" + CmsSourceSearchApp.generateState(settings));
425
426        }
427
428        /**
429         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
430         */
431        public String getTitle(Locale locale) {
432
433            return CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_SEARCH_RESOURCES_0);
434        }
435
436        /**
437         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
438         */
439        public CmsMenuItemVisibilityMode getVisibility(Set<String> data) {
440
441            if (data.size() > 1) {
442                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE.addMessageKey(
443                    Messages.GUI_RESOURCETYPE_APP_TABLE_NO_AVAILABLE_MULTIPLE_0);
444            }
445            try {
446                I_CmsResourceType type = OpenCms.getResourceManager().getResourceType(data.iterator().next());
447
448                return type instanceof A_CmsResourceTypeFolderBase
449                ? CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE
450                : CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
451            } catch (CmsLoaderException e) {
452                return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
453            }
454        }
455
456    }
457
458    /**
459     * All table properties.<p>
460     */
461    enum TableProperty {
462
463        /**Icon.*/
464        Icon(null, Resource.class, null, false),
465
466        /**Icon column.*/
467        Name(Messages.GUI_RESOURCETYPE_EDIT_DISPLAY_NAME_0, String.class, "", false),
468
469        /**Icon column.*/
470        ShortName(Messages.GUI_RESOURCETYPE_EDIT_SHORT_NAME_0, String.class, "", false),
471
472        /**Is Broadcast send but not displayed.*/
473        ID(Messages.GUI_RESOURCETYPE_ID_0, String.class, "", false),
474
475        /**Icon column.*/
476        Module(Messages.GUI_RESOURCETYPE_MODULE_0, String.class, "", false);
477
478        /**Indicates if column is collapsable.*/
479        private boolean m_collapsable;
480
481        /**Default value for column.*/
482        private Object m_defaultValue;
483
484        /**Header Message key.*/
485        private String m_headerMessage;
486
487        /**Type of column property.*/
488        private Class<?> m_type;
489
490        /**
491         * constructor.
492         *
493         * @param headerMessage key
494         * @param type to property
495         * @param defaultValue of column
496         * @param collapsable should this column be collapsable?
497         */
498        TableProperty(String headerMessage, Class<?> type, Object defaultValue, boolean collapsable) {
499
500            m_headerMessage = headerMessage;
501            m_type = type;
502            m_defaultValue = defaultValue;
503            m_collapsable = collapsable;
504        }
505
506        /**
507         * Returns list of all properties with non-empty header.<p>
508         *
509         * @return list of properties
510         */
511        static List<TableProperty> withHeader() {
512
513            List<TableProperty> props = new ArrayList<TableProperty>();
514
515            for (TableProperty prop : TableProperty.values()) {
516                if (prop.m_headerMessage != null) {
517                    props.add(prop);
518                }
519            }
520            return props;
521        }
522
523        /**
524         * Returns the default value of property.<p>
525         *
526         * @return object
527         */
528        Object getDefaultValue() {
529
530            return m_defaultValue;
531        }
532
533        /**
534         * Returns localized header.<p>
535         *
536         * @return string for header
537         */
538        String getLocalizedMessage() {
539
540            if (m_headerMessage == null) {
541                return "";
542            }
543            return CmsVaadinUtils.getMessageText(m_headerMessage);
544        }
545
546        /**
547         * Returns tye of value for given property.<p>
548         *
549         * @return type
550         */
551        Class<?> getType() {
552
553            return m_type;
554        }
555
556        /**
557         * Indicates if column is collapsable.<p>
558         *
559         * @return boolean, true = is collapsable
560         */
561        boolean isCollapsable() {
562
563            return m_collapsable;
564        }
565
566    }
567
568    private static final long serialVersionUID = 1L;
569
570    /** Logger instance for this class. */
571    static final Log LOG = CmsLog.getLog(CmsResourceTypesTable.class);
572
573    /** CmsObject.*/
574    CmsObject m_cms;
575
576    /** The context menu. */
577    private CmsContextMenu m_menu;
578
579    /** The available menu entries. */
580    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
581
582    /**Container holding table data.*/
583    private IndexedContainer m_container;
584
585    /**Resource type app instance. */
586    CmsResourceTypeApp m_app;
587
588    /**
589     * Public constructor.<p>
590     *
591     * @param app instance
592     */
593    public CmsResourceTypesTable(CmsResourceTypeApp app) {
594
595        m_app = app;
596        try {
597            m_cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
598        } catch (CmsException e) {
599            //
600        }
601
602        m_menu = new CmsContextMenu();
603        m_menu.setAsTableContextMenu(this);
604
605        setSizeFull();
606        init();
607
608        addItemClickListener(new ItemClickListener() {
609
610            private static final long serialVersionUID = 7957778390938304845L;
611
612            public void itemClick(ItemClickEvent event) {
613
614                onItemClick(event, event.getItemId(), event.getPropertyId());
615            }
616
617        });
618
619    }
620
621    /**
622     * Filters the table according to given string.<p>
623     *
624     * @param text to filter
625     */
626    public void filterTable(String text) {
627
628        m_container.removeAllContainerFilters();
629        if (!CmsStringUtil.isEmptyOrWhitespaceOnly(text)) {
630            m_container.addContainerFilter(
631                new Or(
632                    new SimpleStringFilter(TableProperty.Name, text, true, false),
633                    new SimpleStringFilter(TableProperty.ShortName, text, true, false),
634                    new SimpleStringFilter(TableProperty.Module, text, true, false)));
635        }
636
637    }
638
639    /**
640     * Returns the available menu entries.<p>
641     *
642     * @return the menu entries
643     */
644    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
645
646        if (m_menuEntries == null) {
647            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
648            m_menuEntries.add(new EditEntry());
649            m_menuEntries.add(new SchemaEditorEntry());
650            m_menuEntries.add(new SearchEntry());
651            m_menuEntries.add(new MoveEntry());
652            m_menuEntries.add(new DeleteEntry());
653        }
654        return m_menuEntries;
655    }
656
657    /**
658     * Init the table.<p>
659     */
660    void init() {
661
662        if (m_container == null) {
663            m_container = new IndexedContainer();
664            setContainerDataSource(m_container);
665        } else {
666            m_container.removeAllContainerFilters();
667            m_container.removeAllItems();
668        }
669        for (TableProperty prop : TableProperty.values()) {
670            m_container.addContainerProperty(prop, prop.getType(), prop.getDefaultValue());
671            setColumnHeader(prop, prop.getLocalizedMessage());
672        }
673
674        setItemIconPropertyId(TableProperty.Icon);
675        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
676        setColumnWidth(null, 40);
677        setSelectable(true);
678        setMultiSelect(true);
679
680        for (I_CmsResourceType type : CmsVaadinUtils.getResourceTypes()) {
681            CmsExplorerTypeSettings typeSetting = OpenCms.getWorkplaceManager().getExplorerTypeSetting(
682                type.getTypeName());
683            Item item = m_container.addItem(type.getTypeName());
684            item.getItemProperty(TableProperty.ID).setValue(String.valueOf(type.getTypeId()));
685            item.getItemProperty(TableProperty.Icon).setValue(CmsResourceUtil.getBigIconResource(typeSetting, null));
686            item.getItemProperty(TableProperty.Name).setValue(CmsVaadinUtils.getMessageText(typeSetting.getKey()));
687            item.getItemProperty(TableProperty.ShortName).setValue(type.getTypeName());
688            item.getItemProperty(TableProperty.Module).setValue(type.getModuleName());
689        }
690
691        setVisibleColumns(TableProperty.Name, TableProperty.ShortName, TableProperty.ID, TableProperty.Module);
692    }
693
694    /**
695     * Handles the table item clicks, including clicks on images inside of a table item.<p>
696     *
697     * @param event the click event
698     * @param itemId of the clicked row
699     * @param propertyId column id
700     */
701    @SuppressWarnings("unchecked")
702    void onItemClick(MouseEvents.ClickEvent event, Object itemId, Object propertyId) {
703
704        if (!event.isCtrlKey() && !event.isShiftKey()) {
705            changeValueIfNotMultiSelect(itemId);
706            // don't interfere with multi-selection using control key
707            if (event.getButton().equals(MouseButton.RIGHT) || (propertyId == null)) {
708                m_menu.setEntries(getMenuEntries(), (Set<String>)getValue());
709                m_menu.openForTable(event, itemId, propertyId, this);
710            } else if (event.getButton().equals(MouseButton.LEFT) && TableProperty.Name.equals(propertyId)) {
711                String typeName = (String)itemId;
712                openEditDialog(typeName);
713            }
714        }
715    }
716
717    /**
718     * Opens the edit dialog.<p>
719     *
720     * @param typeName type to be edited.
721     */
722    void openEditDialog(String typeName) {
723
724        try {
725            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
726
727            window.setContent(
728                new CmsEditResourceTypeDialog(window, m_app, OpenCms.getResourceManager().getResourceType(typeName)));
729            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_RESOURCETYPE_EDIT_WINDOW_CAPTION_0));
730            A_CmsUI.get().addWindow(window);
731        } catch (CmsLoaderException e) {
732            LOG.error("Unable to read resource type by name", e);
733        }
734    }
735
736    /**
737     * Checks value of table and sets it new if needed:<p>
738     * if multiselect: new itemId is in current Value? -> no change of value<p>
739     * no multiselect and multiselect, but new item not selected before: set value to new item<p>
740     *
741     * @param itemId if of clicked item
742     */
743    private void changeValueIfNotMultiSelect(Object itemId) {
744
745        @SuppressWarnings("unchecked")
746        Set<String> value = (Set<String>)getValue();
747        if (value == null) {
748            select(itemId);
749        } else if (!value.contains(itemId)) {
750            setValue(null);
751            select(itemId);
752        }
753    }
754}