jaxx.runtime.swing.ListSelectorModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jaxx-widgets Show documentation
Show all versions of jaxx-widgets Show documentation
Collection of swing widgets wrote with JAXX
/*
* #%L
* JAXX :: Widgets
* %%
* Copyright (C) 2008 - 2014 Code Lutin, Tony Chemit
* %%
* 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 3 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 jaxx.runtime.swing;
import com.google.common.collect.Lists;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collection;
import jaxx.runtime.swing.model.GenericListListener;
import jaxx.runtime.swing.model.GenericListModel;
/**
* @author Sylvain Lletellier
*/
public class ListSelectorModel {
protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
public static final String PROPERTY_ADD_ENABLED = "addEnabled";
public static final String PROPERTY_REMOVE_ENABLED = "removeEnabled";
public static final String PROPERTY_SELECTED_VALUES = "selectedValues";
public static final String PROPERTY_VALUES = "values";
protected boolean addEnabled;
protected boolean removeEnabled;
protected GenericListModel fromModel;
protected GenericListModel toModel;
protected ListSelectorModel() {
fromModel = new GenericListModel();
fromModel.addPropertyChangeListener(GenericListModel.PROPERTY_SELECTED_VALUE, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
setAddEnabled(fromModel.hasSelectedIndex());
}
});
toModel = new GenericListModel();
toModel.addPropertyChangeListener(GenericListModel.PROPERTY_SELECTED_VALUE, new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
setRemoveEnabled(toModel.hasSelectedIndex());
}
});
}
public boolean isAddEnabled() {
return addEnabled;
}
public void setAddEnabled(boolean addEnabled) {
boolean oldValue = isAddEnabled();
this.addEnabled = addEnabled;
firePropertyChange(PROPERTY_ADD_ENABLED, oldValue, addEnabled);
}
public boolean isRemoveEnabled() {
return removeEnabled;
}
public void setRemoveEnabled(boolean removeEnabled) {
boolean oldValue = isRemoveEnabled();
this.removeEnabled = removeEnabled;
firePropertyChange(PROPERTY_REMOVE_ENABLED, oldValue, removeEnabled);
}
public GenericListModel getFromModel() {
return fromModel;
}
public GenericListModel getToModel() {
return toModel;
}
public Collection getValues() {
return fromModel.getElements();
}
public void setValues(Collection values) {
Collection oldValues = getValues();
toModel.clearElements();
fromModel.setElements(values);
firePropertyChange(PROPERTY_VALUES, oldValues, values);
}
public void addValues(Collection values) {
Collection oldValues = getValues();
toModel.removeElements(values);
fromModel.removeElements(values);
fromModel.addElements(values);
firePropertyChange(PROPERTY_VALUES, oldValues, values);
}
public void removeValues(Collection values) {
Collection oldValues = getValues();
toModel.removeElements(values);
fromModel.removeElements(values);
firePropertyChange(PROPERTY_VALUES, oldValues, values);
}
public Collection getSelectedValues() {
return toModel.getElements();
}
public void setSelectedValues(Collection selectedValues) {
Collection oldValue = getSelectedValues();
fromModel.removeElements(selectedValues);
toModel.removeElements(selectedValues);
toModel.setElements(selectedValues);
firePropertyChange(PROPERTY_SELECTED_VALUES, oldValue, selectedValues);
}
public void add() {
Collection oldValues = getValues();
Collection oldSelectedValue = getSelectedValues();
moveSelect(fromModel, toModel);
firePropertyChange(PROPERTY_VALUES, oldValues, getValues());
firePropertyChange(PROPERTY_SELECTED_VALUES, oldSelectedValue, getSelectedValues());
}
public void remove() {
moveSelect(toModel, fromModel);
}
protected void moveSelect(GenericListModel from, GenericListModel to) {
Collection selectedValues = Lists.newArrayList(from.getSelectedValues());
from.removeElements(selectedValues);
to.addElements(selectedValues);
}
public void addGenericListListener(GenericListListener l) {
toModel.addGenericListListener(l);
}
public void removeGenericListListener(GenericListListener l) {
toModel.removeGenericListListener(l);
}
public void addPropertyChangeListener(PropertyChangeListener listener) {
pcs.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcs.addPropertyChangeListener(propertyName, listener);
}
public void removePropertyChangeListener(PropertyChangeListener listener) {
pcs.removePropertyChangeListener(listener);
}
public void removePropertyChangeListener(String propertyName, PropertyChangeListener listener) {
pcs.removePropertyChangeListener(propertyName, listener);
}
protected void firePropertyChange(String propertyName, Object oldValue, Object newValue) {
pcs.firePropertyChange(propertyName, oldValue, newValue);
}
protected void fireIndexedPropertyChange(String propertyName, int index, Object oldValue, Object newValue) {
pcs.fireIndexedPropertyChange(propertyName, index, oldValue, newValue);
}
}