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.modules;
029
030import org.opencms.file.types.I_CmsResourceType;
031import org.opencms.main.OpenCms;
032import org.opencms.module.CmsModule;
033import org.opencms.ui.CmsVaadinUtils;
034import org.opencms.ui.apps.Messages;
035import org.opencms.ui.components.CmsBasicDialog;
036import org.opencms.ui.components.CmsResourceInfo;
037import org.opencms.workplace.explorer.CmsExplorerTypeSettings;
038import org.opencms.workplace.explorer.CmsResourceUtil;
039
040import java.util.Arrays;
041import java.util.Set;
042import java.util.function.Consumer;
043
044import com.google.common.collect.Sets;
045import com.vaadin.server.Resource;
046import com.vaadin.ui.Button;
047import com.vaadin.ui.Button.ClickEvent;
048import com.vaadin.ui.Button.ClickListener;
049import com.vaadin.ui.Panel;
050import com.vaadin.v7.shared.ui.label.ContentMode;
051import com.vaadin.v7.ui.Label;
052import com.vaadin.v7.ui.VerticalLayout;
053
054/**
055 * Widget to display the list of resource / explorer types defined in a module.<p>
056 */
057public class CmsModuleInfoDialog extends CmsBasicDialog {
058
059    /** Serial version id. */
060    private static final long serialVersionUID = 1L;
061
062    /** The label displaying the module description. */
063    private Label m_description;
064
065    /** The Edit button. */
066    private Button m_edit;
067
068    /** The list for the explorer types. */
069    private VerticalLayout m_explorerTypes;
070
071    /** The panel containing the explorer types. */
072    private Panel m_explorerTypesPanel;
073
074    /** The OK button. */
075    private Button m_ok;
076
077    /** The list for the resource types. */
078    private VerticalLayout m_resourceTypes;
079
080    /**
081     * Creates a new instance.<p>
082     *
083     * @param moduleName the module name
084     */
085    public CmsModuleInfoDialog(String moduleName, Consumer<String> editAction) {
086
087        CmsModule module = OpenCms.getModuleManager().getModule(moduleName);
088        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
089        CmsResourceInfo resInfo = new CmsResourceInfo(
090            module.getName(),
091            module.getNiceName(),
092            CmsModuleApp.Icons.RESINFO_ICON);
093        displayResourceInfoDirectly(Arrays.asList(resInfo));
094        m_description.setContentMode(ContentMode.HTML);
095        m_description.setValue(module.getDescription());
096        m_ok.addClickListener(new ClickListener() {
097
098            private static final long serialVersionUID = 1L;
099
100            public void buttonClick(ClickEvent event) {
101
102                CmsVaadinUtils.getWindow(CmsModuleInfoDialog.this).close();
103            }
104        });
105        m_edit.addClickListener(event -> {
106            CmsVaadinUtils.getWindow(CmsModuleInfoDialog.this).close();
107            editAction.accept(moduleName);
108
109        });
110        initialize(module);
111    }
112
113    /**
114     * Fills the widget content.<p>
115     *
116     * @param module the module
117     */
118    public void initialize(CmsModule module) {
119
120        boolean empty = true;
121        Set<String> resTypeNames = Sets.newHashSet();
122        for (I_CmsResourceType type : module.getResourceTypes()) {
123            m_resourceTypes.addComponent(formatResourceType(type));
124            resTypeNames.add(type.getTypeName());
125            empty = false;
126        }
127        if (empty) {
128            m_resourceTypes.addComponent(
129                new Label(CmsVaadinUtils.getMessageText(Messages.GUI_MODULES_NO_RESOURCE_TYPES_0)));
130        }
131        empty = true;
132        for (CmsExplorerTypeSettings expType : module.getExplorerTypes()) {
133            if (resTypeNames.contains(expType.getName())) {
134                continue;
135            }
136            m_explorerTypes.addComponent(formatExplorerType(expType));
137            empty = false;
138        }
139
140        if (empty) {
141            m_explorerTypesPanel.setVisible(false);
142        }
143    }
144
145    /**
146     * Creates the resource info box for an explorer type.<p>
147     *
148     * @param explorerType the explorer type
149     * @return the resource info box
150     */
151    CmsResourceInfo formatExplorerType(CmsExplorerTypeSettings explorerType) {
152
153        Resource icon = CmsResourceUtil.getBigIconResource(explorerType, null);
154        String title = CmsVaadinUtils.getMessageText(explorerType.getKey());
155        if (title.startsWith("???")) {
156            title = explorerType.getName();
157        }
158        String subtitle = explorerType.getName();
159        if (explorerType.getReference() != null) {
160            subtitle += " (" + explorerType.getReference() + ")";
161        }
162        CmsResourceInfo info = new CmsResourceInfo(title, subtitle, icon);
163        return info;
164    }
165
166    /**
167     * Creates the resource info box for a resource type.<p>
168     *
169     * @param type the resource type
170     * @return the resource info box
171     */
172    @SuppressWarnings("deprecation")
173    CmsResourceInfo formatResourceType(I_CmsResourceType type) {
174
175        CmsExplorerTypeSettings settings = OpenCms.getWorkplaceManager().getExplorerTypeSetting(type.getTypeName());
176        Resource icon;
177        String title;
178        String subtitle;
179        if (settings != null) {
180            icon = CmsResourceUtil.getBigIconResource(settings, null);
181            title = CmsVaadinUtils.getMessageText(settings.getKey());
182            if (title.startsWith("???")) {
183                title = type.getTypeName();
184            }
185            subtitle = type.getTypeName()
186                + " (ID: "
187                + type.getTypeId()
188                + (settings.getReference() != null ? (", " + settings.getReference()) : "")
189                + ")";
190        } else {
191            icon = CmsResourceUtil.getBigIconResource(
192                OpenCms.getWorkplaceManager().getExplorerTypeSetting("unknown"),
193                null);
194            title = type.getTypeName();
195            subtitle = type.getTypeName() + " (ID: " + type.getTypeId() + ")";
196        }
197        CmsResourceInfo info = new CmsResourceInfo(title, subtitle, icon);
198        return info;
199    }
200}