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.gwt.client.ui;
029
030import org.opencms.gwt.client.Messages;
031import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuButton;
032import org.opencms.gwt.client.ui.contextmenu.CmsContextMenuHandler;
033import org.opencms.gwt.client.ui.css.I_CmsLayoutBundle;
034import org.opencms.gwt.client.ui.tree.CmsTreeItem;
035import org.opencms.gwt.shared.CmsBrokenLinkBean;
036import org.opencms.gwt.shared.CmsCoreData.AdeContext;
037import org.opencms.gwt.shared.CmsListInfoBean;
038import org.opencms.util.CmsUUID;
039
040import java.util.List;
041import java.util.Map;
042
043import com.google.gwt.core.client.GWT;
044import com.google.gwt.event.logical.shared.AttachEvent;
045import com.google.gwt.uibinder.client.UiBinder;
046import com.google.gwt.uibinder.client.UiField;
047import com.google.gwt.user.client.Window;
048import com.google.gwt.user.client.ui.Composite;
049import com.google.gwt.user.client.ui.Label;
050import com.google.gwt.user.client.ui.Widget;
051
052/**
053 * A widget containing that links to a sitemap item which the user wants to delete will be broken.
054 *
055 * @since 8.0.0
056 */
057public class CmsLinkWarningPanel extends Composite {
058
059    /**
060     * @see com.google.gwt.uibinder.client.UiBinder
061     */
062    protected interface I_CmsLinkWarningPanelUiBinder extends UiBinder<Widget, CmsLinkWarningPanel> {
063        // GWT interface, nothing to do here
064    }
065
066    /** The ui-binder instance for this class. */
067    private static I_CmsLinkWarningPanelUiBinder uiBinder = GWT.create(I_CmsLinkWarningPanelUiBinder.class);
068
069    /** The label containing the warning that links will be broken. */
070    @UiField
071    protected Label m_label;
072
073    /** The panel containing the links that will be broken. */
074    @UiField
075    protected CmsList<CmsTreeItem> m_linkPanel;
076
077    /** Handler for context menus. */
078    private final CmsContextMenuHandler m_menuHandler = new CmsContextMenuHandler() {
079
080        @Override
081        public void refreshResource(CmsUUID structureId) {
082
083            Window.Location.reload();
084        }
085    };
086
087    /**
088     * Default constructor.<p>
089     */
090    public CmsLinkWarningPanel() {
091
092        initWidget(uiBinder.createAndBindUi(this));
093        m_label.setText(Messages.get().key(Messages.GUI_BROKEN_LINK_TEXT_0));
094    }
095
096    /**
097     * Fills the panel with the tree list of broken links.<p>
098     *
099     * @param brokenLinkBeans the beans representing the broken links
100     */
101    public void fill(List<CmsBrokenLinkBean> brokenLinkBeans) {
102
103        for (CmsBrokenLinkBean brokenLinkBean : brokenLinkBeans) {
104            m_linkPanel.add(createTreeItem(brokenLinkBean));
105        }
106    }
107
108    /**
109     * Helper method for creating a list item widget based on a bean.<p>
110     *
111     * @param brokenLinkBean the bean with the data for the list item widget
112     * @param contextMenu true if a context menu should be added
113     *
114     * @return the new list item widget
115     */
116    protected CmsListItemWidget createListItemWidget(CmsBrokenLinkBean brokenLinkBean, boolean contextMenu) {
117
118        CmsListInfoBean info = new CmsListInfoBean();
119        String title = brokenLinkBean.getTitle();
120        if ((title == null) || title.equals("")) {
121            title = Messages.get().key(Messages.GUI_BROKEN_LINK_NO_TITLE_0);
122        }
123        info.setTitle(title);
124        info.setSubTitle(brokenLinkBean.getSubTitle());
125        info.setBigIconClasses(brokenLinkBean.getIcon());
126        String type = brokenLinkBean.getType();
127        if (type != null) {
128            info.setResourceType(type);
129        }
130        for (Map.Entry<String, String> entry : brokenLinkBean.getInfo().entrySet()) {
131            info.addAdditionalInfo(entry.getKey(), entry.getValue());
132        }
133        final CmsListItemWidget widget = new CmsListItemWidget(info);
134        widget.addAttachHandler(new AttachEvent.Handler() {
135
136            public void onAttachOrDetach(AttachEvent event) {
137
138                if (event.isAttached()) {
139                    widget.truncateAdditionalInfo("addinfo", widget.getOffsetWidth());
140                }
141            }
142        });
143        CmsUUID structureId = brokenLinkBean.getStructureId();
144        if (contextMenu && (structureId != null) && !structureId.isNullUUID()) {
145
146            CmsContextMenuButton button = new CmsContextMenuButton(structureId, m_menuHandler, AdeContext.resourceinfo);
147
148            widget.addButton(button);
149        }
150        return widget;
151    }
152
153    /**
154     * Helper method for creating a tree item from a bean.<p>
155     *
156     * @param brokenLinkBean the bean containing the data for the tree item
157     *
158     * @return a tree item
159     */
160    protected CmsTreeItem createTreeItem(CmsBrokenLinkBean brokenLinkBean) {
161
162        CmsListItemWidget itemWidget = createListItemWidget(brokenLinkBean, /*contextmenu=*/true);
163        CmsTreeItem item = new CmsTreeItem(false, itemWidget);
164        item.getChildren().addStyleName(I_CmsLayoutBundle.INSTANCE.listTreeCss().bigIndentation());
165        for (CmsBrokenLinkBean child : brokenLinkBean.getChildren()) {
166            CmsListItemWidget childItemWidget = createListItemWidget(child, /*contextmenu=*/false);
167            childItemWidget.addTitleStyleName(I_CmsLayoutBundle.INSTANCE.linkWarningCss().deletedEntryLabel());
168            CmsTreeItem childItem = new CmsTreeItem(false, childItemWidget);
169            item.addChild(childItem);
170        }
171        return item;
172    }
173
174}