All Downloads are FREE. Search and download functionalities are using the official Maven repository.

de.micromata.mgc.javafx.FXEvents Maven / Gradle / Ivy

There is a newer version: 3.1.3
Show newest version
//
// Copyright (C) 2010-2016 Micromata GmbH
//
// 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 de.micromata.mgc.javafx;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.WeakHashMap;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import de.micromata.genome.util.validation.ValMessage;
import de.micromata.mgc.javafx.launcher.gui.AbstractModelController;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.scene.Node;

/**
 * 
 * @author Roger Rene Kommer ([email protected])
 *
 */
public class FXEvents
{
  private static final Logger LOG = Logger.getLogger(FXEvents.class);
  private static FXEvents INSTANCE = new FXEvents();
  private final WeakHashMap, List> listeners = new WeakHashMap<>();

  public static FXEvents get()
  {
    return INSTANCE;
  }

  public void registerListener(AbstractModelController controller, Node node)
  {
    listeners.putIfAbsent(controller, new ArrayList());
    listeners.get(controller).add(node);
  }

  /**
   * Adds an eventhandler to the selected node.
   * 
   * @param node node to append the eventhandler to.
   * @param handler the event handler to append
   * @param type event type
   * @param controller the controller
   * @param  the type of the event
   */
  public  void addEventHandler(AbstractModelController controller, Node node, EventType type,
      EventHandler handler)
  {
    node.addEventHandler(type, handler);

    List eventSinks = listeners.get(controller);
    if (eventSinks == null) {
      eventSinks = new ArrayList<>();
      listeners.put(controller, eventSinks);
    }
    Optional optexist = eventSinks.stream().filter(lnode -> lnode.equals(node)).findFirst();
    if (optexist.isPresent() == false) {
      eventSinks.add(node);
    }
  }

  public void registerValMessageReceiver(AbstractModelController controller, Node node,
      Class referenceType, String property)
  {
    registerListener(controller, node);
    node.addEventHandler(ValMessageEvent.MESSAGE_EVENT_TYPE, event -> {
      ValMessage msg = event.getMessage();
      if (msg.isConsumed() == true) {
        return;
      }
      if (referenceType != null && msg.getReference() != null) {
        if (referenceType.isAssignableFrom(msg.getReference().getClass()) == false) {
          return;
        }
        if (StringUtils.isNotBlank(property) && StringUtils.isNotBlank(msg.getProperty()) == true) {
          if (property.equals(msg.getProperty()) == true) {
            controller.addToFeedback(msg);
            FXGuiUtils.markErroneousField(controller, node, msg);

            event.consume();

          }
        }
        controller.addToFeedback(msg);
      }

    });
  }

  public void registerValMessageReceiver(AbstractModelController controller, Node node,
      Object model, String property)
  {
    if (node == null) {
      LOG.error("Node is null on property: " + model.getClass().getSimpleName() + "." + property);
    }
    registerListener(controller, node);
    node.addEventHandler(ValMessageEvent.MESSAGE_EVENT_TYPE, event -> {
      ValMessage msg = event.getMessage();
      if (msg.isConsumed() == true) {
        return;
      }
      Object themod = model;
      String theprop = property;
      if (themod == msg.getReference()) {
        if (StringUtils.isNotBlank(property) && StringUtils.isNotBlank(msg.getProperty()) == true
            && property.equals(msg.getProperty()) == true) {

          controller.addToFeedback(msg);
          FXGuiUtils.markErroneousField(controller, node, msg);
          event.consume();
        }

      } else if (msg.isConsumed() == false && themod == msg.getReference()) {
        controller.addToFeedback(msg);
      }

    });
  }

  public void fireEvent(Event event)
  {
    for (List nodes : listeners.values()) {
      for (Node node : nodes) {
        node.fireEvent(event);
        if (event.isConsumed() == true) {
          break;
        }
      }
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy