<commit_before>package org.pentaho.ui.xul.swing;

import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulEventSource;
import org.pentaho.ui.xul.binding.BindingConvertor;
import org.pentaho.ui.xul.binding.BindingException;
import org.pentaho.ui.xul.binding.BindingUtil;
import org.pentaho.ui.xul.binding.DefaultBinding;
import org.pentaho.ui.xul.binding.BindingConvertor.Direction;
import org.pentaho.ui.xul.dom.Document;

public class SwingBinding extends DefaultBinding{

  public SwingBinding(Document document, String sourceId, String sourceAttr, String targetId, String targetAttr) {
    super(document, sourceId, sourceAttr, targetId, targetAttr);
  }

  public SwingBinding(Document document, Object source, String sourceAttr, String targetId, String targetAttr) {
    super(document, source, sourceAttr, targetId, targetAttr);
  }

  public SwingBinding(Document document, String sourceId, String sourceAttr, Object target, String targetAttr) {
    super(document, sourceId, sourceAttr, target, targetAttr);
  }
  
  public SwingBinding(Object source, String sourceAttr, Object target, String targetAttr) {
    super(source, sourceAttr, target, targetAttr);
  }
  
  @Override
  protected PropertyChangeListener setupBinding(final Reference a, final String va, final Reference b, final String vb,
      final Direction dir) {
    if (a.get() == null || va == null) {
      handleException(new BindingException("source bean or property is null"));
    }
    if (!(a.get() instanceof XulEventSource)) {
      handleException(new BindingException("Binding error, source object "+a.get()+" not a XulEventSource instance"));
    }
    if (b.get() == null || vb == null) {
      handleException(new BindingException("target bean or property is null"));
    }
    Method sourceGetMethod = BindingUtil.findGetMethod(a.get(), va);

    Class getterClazz = BindingUtil.getMethodReturnType(sourceGetMethod, a.get());
    getterMethods.push(sourceGetMethod);

    //find set method
    final Method targetSetMethod = BindingUtil.findSetMethod(b.get(), vb, getterClazz);

    //setup prop change listener to handle binding
    PropertyChangeListener listener = new PropertyChangeListener() {
      public void propertyChange(final PropertyChangeEvent evt) {
        final PropertyChangeListener cThis = this;
        if (evt.getPropertyName().equalsIgnoreCase(va)) {
          try {
            Object value = evaluateExpressions(evt.getNewValue());
            final Object finalVal = doConversions(value, dir);
            if(!EventQueue.isDispatchThread() && b.get() instanceof XulComponent){
              logger.error("Binding Error! Update to XulComponenet ("+target.get()+","+targetAttr+") outside of event thread!");
              
              EventQueue.invokeLater(new Runnable(){
                public void run() {
                  try{
                    Object targetObject = b.get();
                    if(targetObject == null){
                      logger.error("Binding target was Garbage Collected, removing propListener");
                      destroyBindings();                      
                      return;
                    }
                    targetSetMethod.invoke(targetObject, finalVal);
                  } catch(InvocationTargetException e){
                    handleException(new BindingException("Error invoking setter method [" + targetSetMethod.getName() + "] on target: "+target, e));
                  } catch(IllegalAccessException e){
                    handleException(new BindingException("Error invoking setter method [" + targetSetMethod.getName() + "] on target: "+target, e));<commit_msg>[PDI-4830] Ported a fix to the binding class that correctly handles boolean negation<commit_after>package org.pentaho.ui.xul.swing;

import java.awt.EventQueue;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.pentaho.ui.xul.XulComponent;
import org.pentaho.ui.xul.XulEventSource;
import org.pentaho.ui.xul.binding.BindingConvertor;
import org.pentaho.ui.xul.binding.BindingException;
import org.pentaho.ui.xul.binding.BindingUtil;
import org.pentaho.ui.xul.binding.DefaultBinding;
import org.pentaho.ui.xul.binding.BindingConvertor.Direction;
import org.pentaho.ui.xul.dom.Document;

public class SwingBinding extends DefaultBinding{

  public SwingBinding(Document document, String sourceId, String sourceAttr, String targetId, String targetAttr) {
    super(document, sourceId, sourceAttr, targetId, targetAttr);
  }

  public SwingBinding(Document document, Object source, String sourceAttr, String targetId, String targetAttr) {
    super(document, source, sourceAttr, targetId, targetAttr);
  }

  public SwingBinding(Document document, String sourceId, String sourceAttr, Object target, String targetAttr) {
    super(document, sourceId, sourceAttr, target, targetAttr);
  }
  
  public SwingBinding(Object source, String sourceAttr, Object target, String targetAttr) {
    super(source, sourceAttr, target, targetAttr);
  }
  
  @Override
  protected PropertyChangeListener setupBinding(final Reference a, final String va, final Reference b, final String vb,
      final Direction dir) {
    if (a.get() == null || va == null) {
      handleException(new BindingException("source bean or property is null"));
    }
    if (!(a.get() instanceof XulEventSource)) {
      handleException(new BindingException("Binding error, source object "+a.get()+" not a XulEventSource instance"));
    }
    if (b.get() == null || vb == null) {
      handleException(new BindingException("target bean or property is null"));
    }
    Method sourceGetMethod = BindingUtil.findGetMethod(a.get(), va);

    Class getterClazz = BindingUtil.getMethodReturnType(sourceGetMethod, a.get());
    getterMethods.push(sourceGetMethod);

    //find set method
    final Method targetSetMethod = BindingUtil.findSetMethod(b.get(), vb, getterClazz);

    //setup prop change listener to handle binding
    PropertyChangeListener listener = new PropertyChangeListener() {
      public void propertyChange(final PropertyChangeEvent evt) {
        final PropertyChangeListener cThis = this;
        if (evt.getPropertyName().equalsIgnoreCase(va)) {
          try {
            Object value = doConversions(evt.getNewValue(), dir);
            final Object finalVal = evaluateExpressions(value);
            
            if(!EventQueue.isDispatchThread() && b.get() instanceof XulComponent){
              logger.error("Binding Error! Update to XulComponenet ("+target.get()+","+targetAttr+") outside of event thread!");
              
              EventQueue.invokeLater(new Runnable(){
                public void run() {
                  try{
                    Object targetObject = b.get();
                    if(targetObject == null){
                      logger.error("Binding target was Garbage Collected, removing propListener");
                      destroyBindings();                      
                      return;
                    }
                    targetSetMethod.invoke(targetObject, finalVal);
                  } catch(InvocationTargetException e){
                    handleException(new BindingException("Error invoking setter method [" + targetSetMethod.getName() + "] on target: "+target, e));
                  } catch(IllegalAccessException e){
                    handleException(new BindingException("Error invoking setter method [" + targetSetMethod.getName() + "] on target: "+target, e));
{'fraction_non_alphanumeric': 0.10007571933366986, 'fraction_numerical': 0.0005047955577990914, 'mean_word_length': 3.2722371967654986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '2014361', 'n_tokens_mistral': 2099, 'n_tokens_neox': 2035, 'n_words': 537}