jaxx.runtime.swing.model.AbstractGenericListSelectionModel Maven / Gradle / Ivy
/*
* #%L
* JAXX :: Runtime
* $Id: AbstractGenericListSelectionModel.java 2455 2012-07-30 18:47:43Z tchemit $
* $HeadURL: https://nuiton.org/svn/jaxx/tags/jaxx-2.8.5/jaxx-runtime/src/main/java/jaxx/runtime/swing/model/AbstractGenericListSelectionModel.java $
* %%
* Copyright (C) 2008 - 2012 CodeLutin
* %%
* 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.model;
import com.google.common.collect.Lists;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.Collection;
import java.util.List;
import javax.swing.DefaultListSelectionModel;
import javax.swing.event.EventListenerList;
/**
* @author sletellier
*/
public abstract class AbstractGenericListSelectionModel extends DefaultListSelectionModel {
public static final String PROPERTY_SELECTED_VALUE = "selectedValues";
protected EventListenerList listenerList = new EventListenerList();
protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
protected List selectedValues;
public AbstractGenericListSelectionModel() {
this.selectedValues = Lists.newArrayList();
}
public B getSelectedValue() {
return selectedValues.get(0);
}
public List getSelectedValues() {
return Lists.newArrayList(selectedValues);
}
public void setSelectedValues(List selectedValues) {
this.selectedValues = selectedValues;
}
protected void unSelectItems(Collection values) {
Collection oldValue = Lists.newArrayList(selectedValues);
for (B value : values) {
int index = selectedValues.indexOf(value);
removeSelectionIntervalWithoutFire(index, index);
}
fireSelectionRemoved(values);
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
protected void unSelectItem(B value) {
Collection oldValue = Lists.newArrayList(selectedValues);
int index = selectedValues.indexOf(value);
removeSelectionIntervalWithoutFire(index, index);
fireSelectionRemoved(Lists.newArrayList(value));
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
public void addSelectedItem(B toSelect) {
Collection oldValue = Lists.newArrayList(selectedValues);
selectedValues.add(toSelect);
int index = selectedValues.indexOf(toSelect);
super.addSelectionInterval(index, index);
fireSelectionAdded(Lists.newArrayList(toSelect));
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
public boolean hasSelectedIndex() {
return !selectedValues.isEmpty();
}
@Override
public void addSelectionInterval(int index0, int index1) {
Collection oldValue = Lists.newArrayList(selectedValues);
addSelectionIntervalWithFire(index0, index1);
super.addSelectionInterval(index0, index1);
Collection newValue = Lists.newArrayList(selectedValues);
newValue.removeAll(oldValue);
fireSelectionAdded(newValue);
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
@Override
public void setSelectionInterval(int index0, int index1) {
Collection oldValue = Lists.newArrayList(selectedValues);
selectedValues.clear();
addSelectionIntervalWithFire(index0, index1);
super.setSelectionInterval(index0, index1);
Collection newValue = Lists.newArrayList(selectedValues);
newValue.removeAll(oldValue);
fireSelectionAdded(newValue);
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
@Override
public void removeSelectionInterval(int index0, int index1) {
Collection oldValue = Lists.newArrayList(selectedValues);
removeSelectionIntervalWithoutFire(index0, index1);
Collection newValue = Lists.newArrayList(selectedValues);
newValue.removeAll(oldValue);
fireSelectionRemoved(newValue);
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
protected void removeSelectionIntervalWithoutFire(int index0, int index1) {
if (index0 > index1) {
int tmp = index1;
index1 = index0;
index0 = tmp;
}
for (int i=index0;i<=index1;i++) {
if (selectedValues.size() > i && i != -1) {
selectedValues.remove(i);
}
}
super.removeSelectionInterval(index0, index1);
}
protected void addSelectionIntervalWithFire(int index0, int index1) {
if (index0 > index1) {
int tmp = index1;
index1 = index0;
index0 = tmp;
}
for (int i=index0;i<=index1;i++) {
if (getSize() > i && i != -1) {
B value = getValueAt(i);
selectedValues.add(value);
}
}
}
public abstract int getSize();
public abstract B getValueAt(int i);
@Override
public void clearSelection() {
Collection oldValue = Lists.newArrayList(selectedValues);
selectedValues.clear();
super.clearSelection();
fireSelectionRemoved(oldValue);
firePropertyChange(PROPERTY_SELECTED_VALUE, oldValue, selectedValues);
}
@Override
public int getSelectionMode() {
return MULTIPLE_INTERVAL_SELECTION;
}
protected void fireValuesAdded(Collection values) {
if (values.isEmpty()) {
return;
}
Object[] listeners = listenerList.getListenerList();
GenericListEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == GenericListListener.class) {
if (e == null) {
e = new GenericListEvent(this, values);
}
((GenericListListener)listeners[i+1]).valuesAdded(e);
}
}
}
protected void fireValuesRemoved(Collection values) {
if (values.isEmpty()) {
return;
}
Object[] listeners = listenerList.getListenerList();
GenericListEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == GenericListListener.class) {
if (e == null) {
e = new GenericListEvent(this, values);
}
((GenericListListener)listeners[i+1]).valuesRemoved(e);
}
}
}
protected void fireSelectionAdded(Collection selectedValues) {
if (selectedValues.isEmpty()) {
return;
}
Object[] listeners = listenerList.getListenerList();
GenericListEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == GenericListListener.class) {
if (e == null) {
e = new GenericListEvent(this, selectedValues);
}
((GenericListListener)listeners[i+1]).selectionAdded(e);
}
}
}
protected void fireSelectionRemoved(Collection selectedValues) {
if (selectedValues.isEmpty()) {
return;
}
Object[] listeners = listenerList.getListenerList();
GenericListEvent e = null;
for (int i = listeners.length - 2; i >= 0; i -= 2) {
if (listeners[i] == GenericListListener.class) {
if (e == null) {
e = new GenericListEvent(this, selectedValues);
}
((GenericListListener)listeners[i+1]).selectionAdded(e);
}
}
}
public void addGenericListListener(GenericListListener l) {
listenerList.add(GenericListListener.class, l);
}
public void removeGenericListListener(GenericListListener l) {
listenerList.remove(GenericListListener.class, 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);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy