//
// $Id$
//
// OOO GWT Utils - utilities for creating GWT applications
// Copyright (C) 2009-2010 Three Rings Design, Inc., All Rights Reserved
// http://code.google.com/p/ooo-gwt-utils/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.gwt.ui;

import java.util.List;
import java.util.Map;

import com.google.common.collect.Maps;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.FocusWidget;
import com.google.gwt.user.client.ui.Widget;

import com.threerings.gwt.util.ExpanderResult;

/**
 * A "click to see more" type of control that displays more elements on a click. This is an
 * alternative to PagedWidget if you don't want most of its features.
 */
public abstract class ExpanderWidget<T> extends FlowPanel
{
    public ExpanderWidget (FocusWidget expandButton)
    {
        this(expandButton, true);
    }

    /**
     * @param expandButton The clickable used to expand the list.
     * @param appendMode True to append pages to the end of the list,
     *     false to prepend to the beginning.
     */
    public ExpanderWidget (FocusWidget expandButton, boolean appendMode)
    {
        _expandButton = expandButton;
        _expandButton.addClickHandler(new ClickHandler() {
            public void onClick (ClickEvent event) {
                expand();
            }
        });
        add(_expandButton);

        // Elements go above the expand button in append mode
        _appendMode = appendMode;
        if (_appendMode) {
            insert(_content, 0);
        } else {
            add(_content);
        }
    }

    protected abstract Widget createElement (T element);

    protected abstract void fetchElements (AsyncCallback<ExpanderResult<T>> callback);

    protected void handleError (Throwable error)
    {
        // Optionally implemented in subclasses
    }

    public void expand ()
    {
        _expandButton.setEnabled(false);

        fetchElements(new AsyncCallback<ExpanderResult<T>>() {
            public void onSuccess (ExpanderResult<T> result) {
                _expandButton.setEnabled(true);
                _expandButton.setVisible(result.hasMore);

                addElements(result.page);
            }
            public void onFailure (Throwable error) {
                handleError(error);
            }
        });
    }

    public void addElements (List<T> elements)
    {
        FlowPanel page = new FlowPanel();
        for (T element : elements) {
            Widget w = createElement(element);
            _elements.put(element, w);
            page.add(w);
        }

        if (_appendMode) {
            _content.add(page);
        } else {
            _content.insert(page, 0);
        }
    }

    public void removeElement (T element)
    {
        Widget w = _elements.remove(element);
        if (w != null) {
            w.removeFromParent();
        }
    }

    protected FocusWidget _expandButton;
    protected FlowPanel _content = new FlowPanel();
    protected boolean _appendMode;

    protected Map<T, Widget> _elements = Maps.newHashMap();
}

@@ -1280,27 +1280,21 @@
 ient.ui.
-FocusWidget
+Label
 ;%0Aimport
@@ -1642,32 +1642,25 @@
 et (
-FocusWidget
+String
  expand
-Button
+Text
 )%0A  
@@ -1682,22 +1682,20 @@
 s(expand
-Button
+Text
 , true);
@@ -1714,74 +1714,8 @@
 /**%0A
-     * @param expandButton The clickable used to expand the list.%0A
     
@@ -1865,32 +1865,25 @@
 et (
-FocusWidget
+String
  expand
-Button
+Text
 , bo
@@ -1925,29 +1925,37 @@
 pand
-Button = expandButton
+Label = new Label(expandText)
 ;%0A  
@@ -1967,22 +1967,21 @@
  _expand
-Button
+Label
 .addClic
@@ -2134,22 +2134,21 @@
 (_expand
-Button
+Label
 );%0A%0A    
@@ -2660,39 +2660,77 @@
     
-_expandButton.setEnabled(false)
+if (_loading) %7B%0A            return;%0A        %7D%0A        _loading = true
 ;%0A%0A 
@@ -2875,37 +2875,23 @@
    _
-expandButton.setEnabled(true)
+loading = false
 ;%0A  
@@ -2911,22 +2911,21 @@
  _expand
-Button
+Label
 .setVisi
@@ -3052,24 +3052,58 @@
 le error) %7B%0A
+                _loading = false;%0A
             
@@ -3204,32 +3204,152 @@
 elements)%0A    %7B%0A
+        addElements(elements, _appendMode);%0A    %7D%0A%0A    public void addElements (List%3CT%3E elements, boolean append)%0A    %7B%0A
         FlowPane
@@ -3541,35 +3541,30 @@
         if (
-_
 append
-Mode
 ) %7B%0A        
@@ -3847,27 +3847,21 @@
 ted 
-FocusWidget
+Label
  _expand
 Butt
@@ -3860,14 +3860,13 @@
 pand
-Button
+Label
 ;%0A  
@@ -3949,16 +3949,48 @@
 endMode;
+%0A    protected boolean _loading;
 %0A%0A    pr

Use a label instead of a button, and allow addElements to both append and prepend.