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

com.googlecode.blaisemath.util.SetSelectionModel Maven / Gradle / Ivy

/**
 * SetSelectionModel.java
 * Created Aug 1, 2012
 */
package com.googlecode.blaisemath.util;

/*
 * #%L
 * BlaiseGraphics
 * --
 * Copyright (C) 2014 - 2017 Elisha Peterson
 * --
 * 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.
 * #L%
 */

import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Sets;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collection;
import java.util.Collections;
import java.util.Set;

/**
 * 

* Model maintaining a collection of selected objects, and notifying listeners * when that set changes. *

* @param type of object that can be selected * @author elisha */ public class SetSelectionModel { public static final String SELECTION_PROPERTY = "selection"; private final Set selected = Sets.newHashSet(); protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this); /** Initialize without arguments */ public SetSelectionModel() { } // // // PROPERTIES // public boolean isEmpty() { return selected.isEmpty(); } /** * Return a copy of the selection. * @return copy of selected */ public Set getSelection() { return ImmutableSet.copyOf(selected); } public void setSelection(Set selection) { if (!selection.containsAll(selected) || !selected.containsAll(selection)) { Set old = getSelection(); selected.clear(); selected.addAll(selection); pcs.firePropertyChange(SELECTION_PROPERTY, old, getSelection()); } } // public void clearSelection() { setSelection(Collections.emptySet()); } public void select(G g) { if (g != null && !selected.contains(g)) { Set old = getSelection(); selected.add(g); pcs.firePropertyChange(SELECTION_PROPERTY, old, getSelection()); } } public boolean isSelected(G g) { return selected.contains(g); } /** * Adds all to the current selection. * @param g elements to add */ public void selectAll(Collection g) { checkNotNull(g); if (!selected.containsAll(g)) { Set old = getSelection(); Iterables.addAll(selected, g); pcs.firePropertyChange(SELECTION_PROPERTY, old, getSelection()); } } public void deselect(G g) { Set old = getSelection(); if (g != null && selected.remove(g)) { pcs.firePropertyChange(SELECTION_PROPERTY, old, getSelection()); } } public void deselectAll(Collection g) { checkNotNull(g); Set old = getSelection(); if (selected.removeAll(g)) { pcs.firePropertyChange(SELECTION_PROPERTY, old, getSelection()); } } /** * Toggle selection status of g * @param g object to toggle */ public void toggleSelection(G g) { if (g != null) { Set old = getSelection(); if (selected.contains(g)) { selected.remove(g); } else { selected.add(g); } pcs.firePropertyChange(SELECTION_PROPERTY, old, getSelection()); } } // // // EVENT HANDLING // public void addPropertyChangeListener(PropertyChangeListener listener) { pcs.addPropertyChangeListener(listener); } public void removePropertyChangeListener(PropertyChangeListener listener) { pcs.removePropertyChangeListener(listener); } public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) { pcs.addPropertyChangeListener(propertyName, listener); } public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) { pcs.removePropertyChangeListener(propertyName, listener); } // }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy