<commit_before>/*
 * Copyright 2007 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.codehaus.groovy.binding;

import groovy.lang.MissingMethodException;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision: 7046 $
 * @since Groovy 1.1
 */
public class PropertyChangeTriggerBinding implements TriggerBinding {

    Object bean;
    String propertyName;

    public PropertyChangeTriggerBinding(Object bean, String propertyName) {
        this.bean = bean;
        this.propertyName = propertyName;
    }

    public FullBinding createBinding(SourceBinding source, TargetBinding target) {
        return new PropertyListener(source, target);
    }

    class PropertyListener implements PropertyChangeListener, FullBinding {
        SourceBinding source;
        TargetBinding target;

        PropertyListener(SourceBinding source, TargetBinding target) {
            this.source = source;
            this.target = target;
        }

        public void propertyChange(PropertyChangeEvent event) {
            if ((event == null) || event.getPropertyName().equals(propertyName)) {
                target.updateTargetValue(source);
            } 
        }

        public void bind() {
            try {
                InvokerHelper.invokeMethodSafe(bean, "addPropretyChangeListener", new Object[] {propertyName, this});
            } catch (MissingMethodException mme) {
                InvokerHelper.invokeMethodSafe(bean, "addPropertyChangeListener", new Object[] {this});
                //don't catch this MissingMethodException, we will throw it out in this case
            }
        }

        public void unbind() {
            try {
                InvokerHelper.invokeMethodSafe(bean, "removePropertyChangeListener", new Object[] {propertyName, this});
            } catch (MissingMethodException mme) {
                mme.printStackTrace();
                try {
                    InvokerHelper.invokeMethodSafe(bean, "removePropertyChangeListener", new Object[] {this});
                } catch (MissingMethodException mme2) {
                    mme2.printStackTrace();
                }
                //don't catch this MissingMethodException, we will throw it out in this case
            }
        }
<commit_msg>oops, call the closure before setting it on the target<commit_after>/*
 * Copyright 2007 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.codehaus.groovy.binding;

import groovy.lang.MissingMethodException;
import org.codehaus.groovy.runtime.InvokerHelper;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

/**
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 * @version $Revision: 7046 $
 * @since Groovy 1.1
 */
public class PropertyChangeTriggerBinding implements TriggerBinding {

    Object bean;
    String propertyName;

    public PropertyChangeTriggerBinding(Object bean, String propertyName) {
        this.bean = bean;
        this.propertyName = propertyName;
    }

    public FullBinding createBinding(SourceBinding source, TargetBinding target) {
        return new PropertyListener(source, target);
    }

    class PropertyListener implements PropertyChangeListener, FullBinding {
        SourceBinding source;
        TargetBinding target;

        PropertyListener(SourceBinding source, TargetBinding target) {
            this.source = source;
            this.target = target;
        }

        public void propertyChange(PropertyChangeEvent event) {
            if ((event == null) || event.getPropertyName().equals(propertyName)) {
                target.updateTargetValue(source.getSourceValueClosure().call());
            } 
        }

        public void bind() {
            try {
                InvokerHelper.invokeMethodSafe(bean, "addPropretyChangeListener", new Object[] {propertyName, this});
            } catch (MissingMethodException mme) {
                InvokerHelper.invokeMethodSafe(bean, "addPropertyChangeListener", new Object[] {this});
                //don't catch this MissingMethodException, we will throw it out in this case
            }
        }

        public void unbind() {
            try {
                InvokerHelper.invokeMethodSafe(bean, "removePropertyChangeListener", new Object[] {propertyName, this});
            } catch (MissingMethodException mme) {
                mme.printStackTrace();
                try {
                    InvokerHelper.invokeMethodSafe(bean, "removePropertyChangeListener", new Object[] {this});
                } catch (MissingMethodException mme2) {
                    mme2.printStackTrace();
                }
                //don't catch this MissingMethodException, we will throw it out in this case
            }
        }

{'fraction_non_alphanumeric': 0.08333333333333333, 'fraction_numerical': 0.0053655264922870555, 'mean_word_length': 2.6371951219512195, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4741727', 'n_tokens_mistral': 1483, 'n_tokens_neox': 1405, 'n_words': 459}