<commit_before>/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.client.ui.impl.javafx.handler.event;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import net.sf.mmm.client.ui.api.common.EventType;
import net.sf.mmm.client.ui.api.feature.UiFeatureEvent;
import net.sf.mmm.client.ui.api.handler.event.UiHandlerEvent;

/**
 * This class is the JavaFx specific adapter for {@link UiHandlerEvent}.
 * 
 * @author <NAME> (hohwille at users.sourceforge.net)
 * @since 1.0.0
 */
public class EventAdapterJavaFx implements ChangeListener<Boolean>, EventHandler<ActionEvent> {

  /** The source of the events (UiWidget). */
  private final UiFeatureEvent source;

  /** The event sender. */
  private final UiHandlerEvent sender;

  /** @see #setProgrammaticEventType(EventType) */
  private EventType programmaticEventType;

  /**
   * The constructor.
   * 
   * @param source is the source of the events (typically {@link net.sf.mmm.client.ui.api.widget.UiWidget}).
   * @param sender is the sender of events (an adapter that delegates to the individual handlers/listeners).
   */
  public EventAdapterJavaFx(UiFeatureEvent source, UiHandlerEvent sender) {

    super();
    this.source = source;
    this.sender = sender;
  }

  /**
   * Fires an event of the given {@link EventType}.
   * 
   * @param type is the {@link EventType} to fire.
   */
  protected void fireEvent(EventType type) {

    boolean programmtic = (this.programmaticEventType == type);
    this.sender.onEvent(this.source, EventType.FOCUS_LOSS, programmtic);
    if (programmtic) {
      this.programmaticEventType = null;
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

    if (Boolean.TRUE.equals(newValue)) {
      fireEvent(EventType.FOCUS_GAIN);
    } else {
      fireEvent(EventType.FOCUS_LOSS);
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void handle(ActionEvent event) {

    fireEvent(EventType.CLICK);
  }

  /**
   * We can NOT prevent JavaFx from firing some events. So we set the {@link EventType} before we
   * programmatically invoke the action that will cause the JavaFx event.
   * <commit_msg>#48: client-ui-widget: create native javafx implementation * stupid bugfix, need to extract common code for EventAdapter now<commit_after>/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
 * http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.client.ui.impl.javafx.handler.event;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import net.sf.mmm.client.ui.api.common.EventType;
import net.sf.mmm.client.ui.api.feature.UiFeatureEvent;
import net.sf.mmm.client.ui.api.handler.event.UiHandlerEvent;

/**
 * This class is the JavaFx specific adapter for {@link UiHandlerEvent}.
 * 
 * @author <NAME> (hohwille at users.sourceforge.net)
 * @since 1.0.0
 */
public class EventAdapterJavaFx implements ChangeListener<Boolean>, EventHandler<ActionEvent> {

  /** The source of the events (UiWidget). */
  private final UiFeatureEvent source;

  /** The event sender. */
  private final UiHandlerEvent sender;

  /** @see #setProgrammaticEventType(EventType) */
  private EventType programmaticEventType;

  /**
   * The constructor.
   * 
   * @param source is the source of the events (typically {@link net.sf.mmm.client.ui.api.widget.UiWidget}).
   * @param sender is the sender of events (an adapter that delegates to the individual handlers/listeners).
   */
  public EventAdapterJavaFx(UiFeatureEvent source, UiHandlerEvent sender) {

    super();
    this.source = source;
    this.sender = sender;
  }

  /**
   * Fires an event of the given {@link EventType}.
   * 
   * @param type is the {@link EventType} to fire.
   */
  public void fireEvent(EventType type) {

    boolean programmtic = (this.programmaticEventType == type);
    this.sender.onEvent(this.source, type, programmtic);
    if (programmtic) {
      this.programmaticEventType = null;
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) {

    if (Boolean.TRUE.equals(newValue)) {
      fireEvent(EventType.FOCUS_GAIN);
    } else {
      fireEvent(EventType.FOCUS_LOSS);
    }
  }

  /**
   * {@inheritDoc}
   */
  @Override
  public void handle(ActionEvent event) {

    fireEvent(EventType.CLICK);
  }

  /**
   * We can NOT prevent JavaFx from firing some events. So we set the {@link EventType} before we
   * programmatically invoke the action that will cause the JavaFx event.
   * 
{'fraction_non_alphanumeric': 0.1148538961038961, 'fraction_numerical': 0.003246753246753247, 'mean_word_length': 3.572356215213358, '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': '3116693', 'n_tokens_mistral': 1566, 'n_tokens_neox': 1520, 'n_words': 405}