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