001package jmri.profile;
002
003import java.beans.IndexedPropertyChangeEvent;
004import java.beans.PropertyChangeEvent;
005import javax.swing.AbstractListModel;
006
007/**
008 * A list of {@link jmri.profile.Profile}s, suitable for use in Swing components
009 * that display a list of items.
010 *
011 * @author Randall Wood
012 */
013public class ProfileListModel extends AbstractListModel<Profile> {
014
015    public ProfileListModel() {
016        ProfileManager.getDefault().addPropertyChangeListener(ProfileManager.PROFILES, (PropertyChangeEvent evt) -> {
017            if (evt instanceof IndexedPropertyChangeEvent
018                    && evt.getSource().equals(ProfileManager.getDefault())) {
019                if (evt.getOldValue() == null) {
020                    this.fireIntervalAdded(this, ((IndexedPropertyChangeEvent) evt).getIndex(), ((IndexedPropertyChangeEvent) evt).getIndex());
021                } else if (evt.getNewValue() == null) {
022                    this.fireIntervalRemoved(this, ((IndexedPropertyChangeEvent) evt).getIndex(), ((IndexedPropertyChangeEvent) evt).getIndex());
023                }
024                this.fireContentsChanged(this, ((IndexedPropertyChangeEvent) evt).getIndex(), ((IndexedPropertyChangeEvent) evt).getIndex());
025            }
026        });
027    }
028
029    @Override
030    public int getSize() {
031        return ProfileManager.getDefault().getProfiles().length;
032    }
033
034    @Override
035    public Profile getElementAt(int index) {
036        return ProfileManager.getDefault().getProfiles(index);
037    }
038}