001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.ade.containerpage.client.ui;
029
030import org.opencms.ade.containerpage.shared.CmsReuseInfo;
031import org.opencms.gwt.client.ui.CmsList;
032import org.opencms.gwt.client.ui.CmsListItem;
033import org.opencms.gwt.client.ui.CmsListItemWidget;
034import org.opencms.gwt.client.ui.CmsPopup;
035import org.opencms.gwt.client.ui.CmsPushButton;
036import org.opencms.gwt.client.ui.FontOpenCms;
037import org.opencms.gwt.client.ui.I_CmsButton.ButtonColor;
038import org.opencms.gwt.client.ui.I_CmsButton.ButtonStyle;
039import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuButton;
040import org.opencms.gwt.client.ui.contextmenu.CmsDialogContextMenuHandler;
041import org.opencms.gwt.client.ui.css.I_CmsConstantsBundle;
042import org.opencms.gwt.shared.CmsCoreData.AdeContext;
043import org.opencms.gwt.shared.CmsListInfoBean;
044import org.opencms.gwt.shared.CmsResourceListInfo;
045
046import java.util.Arrays;
047import java.util.List;
048import java.util.function.Consumer;
049
050import com.google.gwt.core.client.GWT;
051import com.google.gwt.uibinder.client.UiBinder;
052import com.google.gwt.uibinder.client.UiField;
053import com.google.gwt.user.client.ui.FlowPanel;
054import com.google.gwt.user.client.ui.Label;
055import com.google.gwt.user.client.ui.Panel;
056import com.google.gwt.user.client.ui.Widget;
057
058/**
059 * A dialog for showing the usages of a reused element before editing it.
060 */
061public class CmsReuseInfoDialog extends CmsPopup {
062
063    /**
064     * UiBinder interface for this dialog.<p>
065     */
066    interface I_UiBinder extends UiBinder<Panel, CmsReuseInfoDialog> {
067        // empty uibinder interface
068    }
069
070    /** UiBinder instance for this dialog. */
071    private static I_UiBinder uibinder = GWT.create(I_UiBinder.class);
072
073    /** Container for the label. */
074    @UiField
075    protected FlowPanel m_labelBox;
076
077    /** The container for the file info box. */
078    @UiField
079    protected Panel m_infoBoxContainer;
080
081    /** The message label. */
082    @UiField
083    protected Label m_label;
084
085    /** The list item container for the element usages. */
086    @UiField
087    protected CmsList<CmsListItem> m_listPanel;
088
089    /** The ok button. */
090    @UiField
091    protected CmsPushButton m_okButton;
092
093    /** The cancel button. */
094    @UiField
095    protected CmsPushButton m_cancelButton;
096
097    /** The callback for the dialog. */
098    private Consumer<Boolean> m_callback;
099
100    /**
101     * Creates a new instance.
102     *
103     * @param reuseInfo the reuse info bean
104     * @param callback the callback to call with the dialog result
105     */
106    public CmsReuseInfoDialog(CmsReuseInfo reuseInfo, Consumer<Boolean> callback) {
107
108        super(reuseInfo.getTitle());
109        setModal(true);
110        setGlassEnabled(true);
111        m_callback = callback;
112        CmsListInfoBean elementInfo = reuseInfo.getElementInfo();
113        Panel panel = uibinder.createAndBindUi(this);
114        Widget warnIcon = FontOpenCms.WARNING.getWidget(20, I_CmsConstantsBundle.INSTANCE.css().colorWarning());
115        m_labelBox.insert(warnIcon, 0);
116        CmsListItemWidget infoBox = new CmsListItemWidget(elementInfo);
117        m_infoBoxContainer.add(infoBox);
118        setMainContent(panel);
119        m_okButton.setButtonStyle(ButtonStyle.TEXT, ButtonColor.RED);
120        for (CmsPushButton button : getDialogButtons()) {
121            addButton(button);
122        }
123        CmsDialogContextMenuHandler menuHandler = new CmsDialogContextMenuHandler();
124        m_okButton.setText(org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_OK_0));
125        m_cancelButton.setText(org.opencms.gwt.client.Messages.get().key(org.opencms.gwt.client.Messages.GUI_CANCEL_0));
126        m_label.setText(reuseInfo.getMessage());
127
128        for (CmsResourceListInfo bean : reuseInfo.getUsageInfos()) {
129            CmsListItemWidget liw = new CmsListItemWidget(bean);
130            CmsListItem li = new CmsListItem(liw);
131            CmsContextMenuButton button = new CmsContextMenuButton(
132                bean.getStructureId(),
133                menuHandler,
134                AdeContext.resourceinfo);
135            liw.addButton(button);
136            m_listPanel.add(li);
137        }
138
139        m_okButton.addClickHandler(event -> {
140            CmsReuseInfoDialog.this.hide();
141            m_callback.accept(Boolean.TRUE);
142        });
143
144        m_cancelButton.addClickHandler(event -> {
145            CmsReuseInfoDialog.this.hide();
146            m_callback.accept(Boolean.FALSE);
147        });
148
149    }
150
151    /**
152     * Gets the dialog buttons.
153     *
154     * @return the dialog buttons
155     */
156    private List<CmsPushButton> getDialogButtons() {
157
158        return Arrays.asList(m_cancelButton, m_okButton);
159    }
160
161}