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.I_CmsButton.ButtonStyle;
032import org.opencms.gwt.client.ui.I_CmsButton.Size;
033import org.opencms.gwt.client.util.I_CmsAdditionalInfoLoader;
034import org.opencms.gwt.shared.CmsAdditionalInfoBean;
035import org.opencms.gwt.shared.CmsListInfoBean;
036
037import java.util.ArrayList;
038import java.util.Collections;
039import java.util.List;
040
041import com.google.gwt.event.dom.client.ClickEvent;
042import com.google.gwt.event.dom.client.ClickHandler;
043import com.google.gwt.user.client.rpc.AsyncCallback;
044
045/**
046 * This class extends the basic list item widget with the ability to load additional info items
047 * asnchronously via RPC.<p>
048 *
049 * The loaded additional info items will be displayed after the additional info items contained in the
050 * bean which is passed into the constructor.<p>
051 *
052 * @since 8.0.0
053 */
054public class CmsInfoLoadingListItemWidget extends CmsListItemWidget {
055
056    /**
057     * The default loader for additional info items, which does nothing.
058     */
059    public class DummyAdditionalInfoLoader implements I_CmsAdditionalInfoLoader {
060
061        /**
062         * @see org.opencms.gwt.client.util.I_CmsAdditionalInfoLoader#load(com.google.gwt.user.client.rpc.AsyncCallback)
063         */
064        public void load(AsyncCallback<List<AdditionalInfoItem>> callback) {
065
066            callback.onSuccess(Collections.<AdditionalInfoItem> emptyList());
067        }
068    }
069
070    /** Flag which keeps track of whether the additional info panel is shown. */
071    protected boolean m_additionalInfoOpen;
072
073    /** Flag which keeps track of whether additional info items are currently being loaded. */
074    protected boolean m_loading;
075
076    /** The loader for additional info items. */
077    protected I_CmsAdditionalInfoLoader m_additionalInfoLoader = new DummyAdditionalInfoLoader();
078
079    /** The dynamically loaded additional info items. */
080    private List<AdditionalInfoItem> m_dynamicInfo = new ArrayList<AdditionalInfoItem>();
081
082    /**
083     * Creates a new list item widget from an info bean.<p>
084     *
085     * @param infoBean the bean containing the information to display
086     */
087    public CmsInfoLoadingListItemWidget(CmsListInfoBean infoBean) {
088
089        super(infoBean);
090    }
091
092    /**
093     * Sets the loader for additional info items.<p>
094     *
095     * @param loader the loader for additional info items
096     */
097    public void setAdditionalInfoLoader(I_CmsAdditionalInfoLoader loader) {
098
099        m_additionalInfoLoader = loader;
100    }
101
102    /**
103     * @see org.opencms.gwt.client.ui.CmsListItemWidget#initAdditionalInfo(org.opencms.gwt.shared.CmsListInfoBean)
104     */
105    @Override
106    protected void initAdditionalInfo(final CmsListInfoBean infoBean) {
107
108        if (infoBean.hasAdditionalInfo()) {
109            m_openClose = new CmsPushButton(I_CmsButton.TRIANGLE_RIGHT, I_CmsButton.TRIANGLE_DOWN);
110            m_openClose.setButtonStyle(ButtonStyle.FONT_ICON, null);
111            m_openClose.setSize(Size.small);
112            m_titleRow.insert(m_openClose, 0);
113            m_openClose.addClickHandler(new ClickHandler() {
114
115                /**
116                 * @see com.google.gwt.event.dom.client.ClickHandler#onClick(com.google.gwt.event.dom.client.ClickEvent)
117                 */
118                public void onClick(ClickEvent event) {
119
120                    if (m_additionalInfoOpen) {
121                        setAdditionalInfoVisible(false);
122                        m_additionalInfoOpen = false;
123                    } else {
124                        if (!m_loading) {
125                            m_loading = true;
126                            m_openClose.setDown(true);
127                            m_openClose.disable(Messages.get().getBundle().key(Messages.GUI_LOADING_0));
128                            m_additionalInfoLoader.load(new AsyncCallback<List<AdditionalInfoItem>>() {
129
130                                /**
131                                 * @see com.google.gwt.user.client.rpc.AsyncCallback#onFailure(java.lang.Throwable)
132                                 */
133                                public void onFailure(Throwable caught) {
134
135                                    m_loading = false;
136                                    m_openClose.enable();
137                                    m_openClose.setDown(false);
138                                }
139
140                                /**
141                                 * @see com.google.gwt.user.client.rpc.AsyncCallback
142                                 */
143                                public void onSuccess(List<AdditionalInfoItem> result) {
144
145                                    m_openClose.enable();
146                                    m_loading = false;
147                                    m_additionalInfoOpen = true;
148                                    setDynamicInfo(result);
149                                    setAdditionalInfoVisible(true);
150                                }
151                            });
152                        }
153                    }
154                }
155            });
156            m_additionalInfo.clear();
157            for (CmsAdditionalInfoBean additionalInfo : infoBean.getAdditionalInfo()) {
158                m_additionalInfo.add(new AdditionalInfoItem(additionalInfo));
159            }
160        }
161    }
162
163    /**
164     * Sets the dynamically loaded additional info items.<p>
165     *
166     * @param info the dynamically loaded additional info items
167     */
168    protected void setDynamicInfo(List<AdditionalInfoItem> info) {
169
170        for (AdditionalInfoItem item : m_dynamicInfo) {
171            item.removeFromParent();
172        }
173        for (AdditionalInfoItem item : info) {
174            m_dynamicInfo.add(item);
175            m_additionalInfo.add(item);
176        }
177        updateTruncation();
178    }
179
180}