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.tree;
029
030import org.opencms.gwt.client.Messages;
031import org.opencms.gwt.client.ui.input.CmsCheckBox;
032
033import com.google.gwt.user.client.ui.Label;
034import com.google.gwt.user.client.ui.Widget;
035
036/**
037 * Tree item for lazily loaded list trees.<p>
038 *
039 * @since 8.0.0
040 */
041public class CmsLazyTreeItem extends CmsTreeItem {
042
043    /** Enum for indicating the load state of a tree item. */
044    public enum LoadState {
045
046        /** children have been loaded. */
047        LOADED,
048
049        /** loading children. */
050        LOADING,
051
052        /** children haven't been loaded. */
053        UNLOADED;
054    }
055
056    /**
057     * Helper tree item which displays a "loading" message.<p>
058     */
059    protected class LoadingItem extends CmsTreeItem {
060
061        /**
062         * Constructs a new instance.<p>
063         */
064        public LoadingItem() {
065
066            super(true, new Label(Messages.get().key(Messages.GUI_LOADING_0)));
067        }
068    }
069
070    /** The loading item. */
071    private LoadingItem m_loadingItem = new LoadingItem();
072
073    /** The load state of this tree item. */
074    private LoadState m_loadState = LoadState.UNLOADED;
075
076    /** Flag to show a load item while children are being loaded. */
077    private boolean m_useLoadItem;
078
079    /**
080     * Constructs a new lazy tree item with a main widget and a check box.<p>
081     *
082     * @param checkbox the check box
083     * @param widget the main widget
084     * @param useLoadItem <code>true</code> to show a load item while children are being loaded
085     */
086    public CmsLazyTreeItem(CmsCheckBox checkbox, Widget widget, boolean useLoadItem) {
087
088        super(true, checkbox, widget);
089        m_useLoadItem = useLoadItem;
090    }
091
092    /**
093     * Constructs a new lazy tree item with a main widget.<p>
094     *
095     * @param widget the main widget
096     * @param useLoadItem <code>true</code> to show a load item while children are being loaded
097     */
098    public CmsLazyTreeItem(Widget widget, boolean useLoadItem) {
099
100        super(true, widget);
101        m_useLoadItem = useLoadItem;
102    }
103
104    /**
105     * Gets the load state of the tree item.<p>
106     *
107     * @return a load state
108     */
109    public LoadState getLoadState() {
110
111        return m_loadState;
112    }
113
114    /**
115     * Returns if tree item children have been loaded.<p>
116     *
117     * @return <code>true</code> if tree item children have been loaded
118     */
119    public boolean isLoaded() {
120
121        return LoadState.LOADED == m_loadState;
122    }
123
124    /**
125     * This method should be called when the item's children have finished loading.<p>
126     */
127    public void onFinishLoading() {
128
129        m_loadState = LoadState.LOADED;
130        if (m_useLoadItem) {
131            m_loadingItem.removeFromParent();
132        }
133        onChangeChildren();
134    }
135
136    /**
137     * This method is called when the tree item's children start being loaded.<p>
138     */
139    public void onStartLoading() {
140
141        m_loadState = LoadState.LOADING;
142        if (m_useLoadItem) {
143            addChild(m_loadingItem);
144        }
145    }
146
147    /**
148     * @see org.opencms.gwt.client.ui.tree.CmsTreeItem#onChangeChildren()
149     */
150    @Override
151    protected void onChangeChildren() {
152
153        if (m_loadState == LoadState.LOADED) {
154            super.onChangeChildren();
155        }
156    }
157
158}