/*
 * Copyright 2002-2004 the original author or authors.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.springframework.richclient.forms;

import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.text.JTextComponent;

import org.springframework.binding.value.ValueModel;

public class AsYouTypeTextValueSetter extends AbstractValueSetter implements
        DocumentListener {
    private JTextComponent component;

    protected AsYouTypeTextValueSetter(JTextComponent component) {
        super(null);
        this.component = component;
        this.component.getDocument().addDocumentListener(this);
    }

    public AsYouTypeTextValueSetter(JTextComponent component,
            ValueModel valueModel) {
        super(valueModel);
        this.component = component;
        this.component.getDocument().addDocumentListener(this);
    }

    protected void setComponentValue(Object value) {
        component.setText((String)value);
    }

    public void removeUpdate(DocumentEvent e) {
        componentValueChanged();
    }

    public void insertUpdate(DocumentEvent e) {
        componentValueChanged();
    }

    public void changedUpdate(DocumentEvent e) {
        componentValueChanged();
    }
    
    private void componentValueChanged() {
        componentValueChanged(component.getText());
    }

}
@@ -941,16 +941,17 @@
 tener %7B%0A
+%0A
     priv
@@ -979,16 +979,54 @@
 mponent;
+%0A    %0A    private boolean settingText;
 %0A%0A    pr
@@ -1512,39 +1512,352 @@
     
-component.setText((String)value
+// this try block will coalesce the 2 DocumentEvents that%0A        // JTextComponent.setText() fires into 1 call to %0A        // componentValueChanged()%0A        try %7B%0A            settingText = true;%0A            component.setText((String)value);%0A        %7D%0A        finally %7B%0A            settingText = false;%0A        %7D%0A        componentValueChanged(
 );%0A 
@@ -2126,21 +2126,17 @@
 ;%0A    %7D%0A
-    
 %0A
+
     priv
@@ -2166,24 +2166,56 @@
 Changed() %7B%0A
+        if (!settingText) %7B%0A    
         comp
@@ -2258,12 +2258,21 @@
 ));%0A    
+    %7D%0A    
 %7D
-%0A
 %0A%7D

make sure that only 1 value change event fires on setComponentValue. this makes a *big* difference to the responsiveness on quite a few of my forms