001/*
002 * $Id: DefaultMultiThumbModel.java 3935 2011-03-02 19:06:41Z kschaefe $
003 *
004 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
005 * Santa Clara, California 95054, U.S.A. All rights reserved.
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 * 
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015 * Lesser General Public License for more details.
016 * 
017 * You should have received a copy of the GNU Lesser General Public
018 * License along with this library; if not, write to the Free Software
019 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020 */
021
022package org.jdesktop.swingx.multislider;
023
024
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.Comparator;
028import java.util.Iterator;
029import java.util.List;
030
031/**
032 *
033 * @author joshy
034 */
035public class DefaultMultiThumbModel<E> extends AbstractMultiThumbModel<E> {
036    
037    protected List<Thumb<E>>thumbs = new ArrayList<Thumb<E>>();
038    
039    /** Creates a new instance of DefaultMultiThumbModel */
040    public DefaultMultiThumbModel() {
041        setMinimumValue(0.0f);
042        setMaximumValue(1.0f);
043    }
044    // returns the index of the newly added thumb
045    @Override
046    public int addThumb(float value, E obj) {
047        Thumb<E> thumb = new Thumb<E>(this);
048        thumb.setPosition(value);
049        thumb.setObject(obj);
050        thumbs.add(thumb);
051        int n = thumbs.size();
052        ThumbDataEvent evt = new ThumbDataEvent(this,-1,thumbs.size()-1,thumb);
053        for(ThumbDataListener tdl : thumbDataListeners) {
054            tdl.thumbAdded(evt);
055        }
056        return n-1;
057    }
058
059    @Override
060    public void insertThumb(float value, E obj, int index) {
061        Thumb<E> thumb = new Thumb<E>(this);
062        thumb.setPosition(value);
063        thumb.setObject(obj);
064        thumbs.add(index,thumb);
065        ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
066        for(ThumbDataListener tdl : thumbDataListeners) {
067            tdl.thumbAdded(evt);
068        }
069    }
070
071    @Override
072    public void removeThumb(int index) {
073        Thumb<E> thumb = thumbs.remove(index);
074        ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
075        for(ThumbDataListener tdl : thumbDataListeners) {
076            tdl.thumbRemoved(evt);
077        }
078    }
079
080    @Override
081    public int getThumbCount() {
082        return thumbs.size();
083    }
084
085    @Override
086    public Thumb<E> getThumbAt(int index) {
087        return thumbs.get(index);
088    }
089
090    @Override
091    public List<Thumb<E>> getSortedThumbs() {
092        List<Thumb<E>> list = new ArrayList<Thumb<E>>();
093        list.addAll(thumbs);
094        Collections.sort(list, new Comparator<Thumb<E>>() {
095            @Override
096            public int compare(Thumb<E> o1, Thumb<E> o2) {
097                float f1 = o1.getPosition();
098                float f2 = o2.getPosition();
099                if(f1<f2) {
100                    return -1;
101                }
102                if(f1>f2) {
103                    return 1;
104                }
105                return 0;
106            }
107        });
108        return list;
109    }
110
111    @Override
112    public Iterator<Thumb<E>> iterator() {
113        return thumbs.iterator();
114    }
115
116    @Override
117    public int getThumbIndex(Thumb<E> thumb) {
118        return thumbs.indexOf(thumb);
119    }
120}