001    /*
002     * $Id: DefaultMultiThumbModel.java 3259 2009-02-17 21:06:19Z 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    
022    package org.jdesktop.swingx.multislider;
023    
024    
025    import java.util.ArrayList;
026    import java.util.Collections;
027    import java.util.Comparator;
028    import java.util.Iterator;
029    import java.util.List;
030    
031    /**
032     *
033     * @author joshy
034     */
035    public class DefaultMultiThumbModel<E> extends AbstractMultiThumbModel<E> implements MultiThumbModel<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        public int addThumb(float value, E obj) {
046            Thumb<E> thumb = new Thumb<E>(this);
047            thumb.setPosition(value);
048            thumb.setObject(obj);
049            thumbs.add(thumb);
050            int n = thumbs.size();
051            ThumbDataEvent evt = new ThumbDataEvent(this,-1,thumbs.size()-1,thumb);
052            for(ThumbDataListener tdl : thumbDataListeners) {
053                tdl.thumbAdded(evt);
054            }
055            return n-1;
056        }
057    
058        public void insertThumb(float value, E obj, int index) {
059            Thumb<E> thumb = new Thumb<E>(this);
060            thumb.setPosition(value);
061            thumb.setObject(obj);
062            thumbs.add(index,thumb);
063            ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
064            for(ThumbDataListener tdl : thumbDataListeners) {
065                tdl.thumbAdded(evt);
066            }
067        }
068    
069        public void removeThumb(int index) {
070            Thumb<E> thumb = thumbs.remove(index);
071            ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
072            for(ThumbDataListener tdl : thumbDataListeners) {
073                tdl.thumbRemoved(evt);
074            }
075        }
076    
077        public int getThumbCount() {
078            return thumbs.size();
079        }
080    
081        public Thumb<E> getThumbAt(int index) {
082            return thumbs.get(index);
083        }
084    
085        public List<Thumb<E>> getSortedThumbs() {
086            List<Thumb<E>> list = new ArrayList<Thumb<E>>();
087            list.addAll(thumbs);
088            Collections.sort(list, new Comparator<Thumb<E>>() {
089                public int compare(Thumb<E> o1, Thumb<E> o2) {
090                    float f1 = o1.getPosition();
091                    float f2 = o2.getPosition();
092                    if(f1<f2) {
093                        return -1;
094                    }
095                    if(f1>f2) {
096                        return 1;
097                    }
098                    return 0;
099                }
100            });
101            return list;
102        }
103    
104        public Iterator<Thumb<E>> iterator() {
105            return thumbs.iterator();
106        }
107    
108        public int getThumbIndex(Thumb<E> thumb) {
109            return thumbs.indexOf(thumb);
110        }
111    }