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

net.anwiba.commons.model.SelectionModel Maven / Gradle / Ivy

There is a newer version: 1.0.185
Show newest version
/*
 * #%L
 * anwiba commons core
 * %%
 * Copyright (C) 2007 - 2016 Andreas Bartels
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */
package net.anwiba.commons.model;

import net.anwiba.commons.utilities.collection.IterableUtilities;
import net.anwiba.commons.utilities.collection.SetUtilities;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class SelectionModel implements ISelectionModel {

  private final List> listeners = new ArrayList<>();
  private final List selectedObjects = new ArrayList<>();

  @Override
  public void setSelectedObject(final T object) {
    boolean changed = false;
    synchronized (this.selectedObjects) {
      if (this.selectedObjects.size() == 1 && isSelected(object)) {
        return;
      }
      if (!this.selectedObjects.isEmpty()) {
        clear();
        changed = true;
      }
      if (object != null) {
        changed |= this.selectedObjects.add(object);
      }
    }
    if (changed) {
      fireSelectionChanged();
    }
  }

  @Override
  public void setSelectedObjects(final Collection objects) {
    boolean changed = false;
    synchronized (this.selectedObjects) {
      if (SetUtilities.equals(new HashSet<>(this.selectedObjects), objects)) {
        return;
      }
      if (!this.selectedObjects.isEmpty()) {
        clear();
        changed = true;
      }
      if (!objects.isEmpty()) {
        changed |= this.selectedObjects.addAll(objects);
      }
    }
    if (changed) {
      fireSelectionChanged();
    }
  }

  @Override
  public void addSelectedObject(final T object) {
    boolean changed = false;
    synchronized (this.selectedObjects) {
      if (object == null || isSelected(object)) {
        return;
      }
      changed = this.selectedObjects.add(object);
    }
    if (changed) {
      fireSelectionChanged();
    }
  }

  @Override
  public void addSelectedObjects(final Collection objects) {
    boolean changed = false;
    synchronized (this.selectedObjects) {
      if (objects == null || objects.isEmpty()) {
        return;
      }
      final Set diverence = SetUtilities.getContainsNot(new HashSet<>(this.selectedObjects), objects);
      changed = this.selectedObjects.addAll(diverence);
    }
    if (changed) {
      fireSelectionChanged();
    }
  }

  @Override
  public boolean isSelected(final T object) {
    synchronized (this.selectedObjects) {
      return this.selectedObjects.contains(object);
    }
  }

  @Override
  public void removeSelectedObject(final T object) {
    boolean removed = false;
    synchronized (this.selectedObjects) {
      if (object == null || !isSelected(object)) {
        return;
      }
      removed = this.selectedObjects.remove(object);
    }
    if (removed) {
      fireSelectionChanged();
    }
  }

  @Override
  public void removeSelectedObjects(final Collection objects) {
    boolean removed = false;
    synchronized (this.selectedObjects) {
      if (objects == null || objects.isEmpty()) {
        return;
      }
      removed = this.selectedObjects.removeAll(objects);
    }
    if (removed) {
      fireSelectionChanged();
    }
  }

  @Override
  public void removeAllSelectedObjects() {
    synchronized (this.selectedObjects) {
      if (this.selectedObjects.isEmpty()) {
        return;
      }
      clear();
    }
    fireSelectionChanged();
  }

  @Override
  public boolean isEmpty() {
    synchronized (this.selectedObjects) {
      return this.selectedObjects.isEmpty();
    }
  }

  @Override
  public int size() {
    synchronized (this.selectedObjects) {
      return this.selectedObjects.size();
    }
  }

  private void clear() {
    synchronized (this.selectedObjects) {
      this.selectedObjects.clear();
    }
  }

  @Override
  public void addSelectionListener(final ISelectionListener listener) {
    synchronized (this.listeners) {
      if (this.listeners.contains(listener)) {
        return;
      }
      this.listeners.add(listener);
    }
  }

  @Override
  public void removeSelectionListener(final ISelectionListener listener) {
    synchronized (this.listeners) {
      this.listeners.remove(listener);
    }
  }

  private void fireSelectionChanged() {
    final List> currentListeners;
    synchronized (this.listeners) {
      currentListeners = new ArrayList<>(this.listeners);
    }
    for (final ISelectionListener listener : currentListeners) {
      listener.selectionChanged(new SelectionEvent<>(this));
    }
  }

  @Override
  public Iterable getSelectedObjects() {
    synchronized (this.selectedObjects) {
      return IterableUtilities.asList(this.selectedObjects);
    }
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy