//
// $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 com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * A text area with a character limit that displays the limit to the user.
 */
public class LimitedTextArea extends VerticalPanel
{
    public LimitedTextArea (int maxChars, int width, int height)
    {
        setStyleName("gwt-LimitedTextArea");
        _maxChars = maxChars;

        add(_area = new TextArea());
        _area.addKeyUpHandler(_limiter);
        if (width > 0) {
            _area.setCharacterWidth(width);
        }
        _area.setVisibleLines(height);
        setHorizontalAlignment(ALIGN_RIGHT);
        add(_remaining = new Label());
        _remaining.setStyleName("Remaining");
        updateRemaining();
    }

    public void setText (String text)
    {
        // we'd use a change listener here but browsers conveniently don't report a change event
        // when a text area's text changes, awesome!
        _area.setText(text);
        updateRemaining();
    }

    public String getText ()
    {
        return _area.getText();
    }

    /**
     * Returns the text area being limited. Don't call {@link TextArea#setText} directly on this
     * area. Use {@link #setText}.
     */
    public TextArea getTextArea ()
    {
        return _area;
    }

    protected void updateRemaining ()
    {
        String text = _area.getText();
        if (text.length() > _maxChars) {
            _area.setText(text = text.substring(0, _maxChars));
        }
        _remaining.setText(_msgs.charRemaining(String.valueOf(_maxChars - text.length())));
    }

    protected KeyUpHandler _limiter = new KeyUpHandler() {
        public void onKeyUp (KeyUpEvent event) {
            updateRemaining();
        }
    };

    protected int _maxChars;
    protected TextArea _area;
    protected Label _remaining;

    protected static final UIMessages _msgs = GWT.create(UIMessages.class);
}

@@ -637,17 +637,16 @@
 PURPOSE.
- 
  See the
@@ -650,16 +650,16 @@
 the GNU%0A
+
 // Lesse
@@ -990,16 +990,297 @@
 nt.GWT;%0A
+import com.google.gwt.event.dom.client.ChangeHandler;%0Aimport com.google.gwt.event.dom.client.HasAllKeyHandlers;%0Aimport com.google.gwt.event.dom.client.HasChangeHandlers;%0Aimport com.google.gwt.event.dom.client.KeyDownHandler;%0Aimport com.google.gwt.event.dom.client.KeyPressHandler;%0A
 import c
@@ -1367,32 +1367,134 @@
 t.KeyUpHandler;%0A
+import com.google.gwt.event.shared.HandlerRegistration;%0Aimport com.google.gwt.user.client.ui.HasText;%0A
 import com.googl
@@ -1755,16 +1755,77 @@
 alPanel%0A
+    implements HasText, HasAllKeyHandlers, HasChangeHandlers%0A
 %7B%0A    pu
@@ -2837,32 +2837,32 @@
 xtArea ()%0A    %7B%0A
-
         return _
@@ -2866,32 +2866,626 @@
 n _area;%0A    %7D%0A%0A
+    @Override%0A    public HandlerRegistration addKeyDownHandler (KeyDownHandler handler)%0A    %7B%0A        return _area.addKeyDownHandler(handler);%0A    %7D%0A%0A    @Override%0A    public HandlerRegistration addKeyUpHandler (KeyUpHandler handler)%0A    %7B%0A        return _area.addKeyUpHandler(handler);%0A    %7D%0A%0A    @Override%0A    public HandlerRegistration addKeyPressHandler (KeyPressHandler handler)%0A    %7B%0A        return _area.addKeyPressHandler(handler);%0A    %7D%0A%0A    @Override%0A    public HandlerRegistration addChangeHandler (ChangeHandler handler)%0A    %7B%0A        return _area.addChangeHandler(handler);%0A    %7D%0A%0A
     protected vo

Allow a limited text area to be used in most places where a text box or text area is expected.