001 /*
002 * $Id: DefaultMultiThumbModel.java,v 1.2 2006/03/29 02:42:48 rbair Exp $
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
045 public void addThumb(float value, E obj) {
046 Thumb<E> thumb = new Thumb<E>();
047 thumb.setPosition(value);
048 thumb.setObject(obj);
049 thumbs.add(thumb);
050 ThumbDataEvent evt = new ThumbDataEvent(this,-1,thumbs.size()-1,thumb);
051 for(ThumbDataListener tdl : thumbDataListeners) {
052 tdl.thumbAdded(evt);
053 }
054 }
055
056 public void insertThumb(float value, E obj, int index) {
057 Thumb thumb = new Thumb();
058 thumb.setPosition(value);
059 thumb.setObject(obj);
060 thumbs.add(index,thumb);
061 ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
062 for(ThumbDataListener tdl : thumbDataListeners) {
063 tdl.thumbAdded(evt);
064 }
065 /*
066 for(ThumbChangeListener tcl : thumbChangeListeners) {
067 tcl.thumbAdded(thumb,index);
068 }
069 */
070 }
071
072 public void removeThumb(int index) {
073 Thumb 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 for(ThumbChangeListener tcl : thumbChangeListeners) {
080 tcl.thumbRemoved(thumb, index);
081 }
082 */
083 }
084
085 public int getThumbCount() {
086 return thumbs.size();
087 }
088
089 public Thumb getThumbAt(int index) {
090 return thumbs.get(index);
091 }
092
093 public List<Thumb<E>> getSortedThumbs() {
094 List<Thumb<E>> list = new ArrayList<Thumb<E>>();
095 list.addAll(thumbs);
096 Collections.sort(list, new Comparator<Thumb<E>>() {
097 public int compare(Thumb<E> o1, Thumb<E> o2) {
098 float f1 = o1.getPosition();
099 float f2 = o2.getPosition();
100 if(f1<f2) {
101 return -1;
102 }
103 if(f1>f2) {
104 return 1;
105 }
106 return 0;
107 }
108 });
109 return list;
110 }
111
112 public Iterator<Thumb<E>> iterator() {
113 return thumbs.iterator();
114 }
115 }