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: import java.util.ArrayList;
025: import java.util.Collections;
026: import java.util.Comparator;
027: import java.util.Iterator;
028: import java.util.List;
029:
030: /**
031: *
032: * @author joshy
033: */
034: public class DefaultMultiThumbModel<E> extends
035: 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,
051: thumbs.size() - 1, thumb);
052: for (ThumbDataListener tdl : thumbDataListeners) {
053: tdl.thumbAdded(evt);
054: }
055: }
056:
057: public void insertThumb(float value, E obj, int index) {
058: Thumb thumb = new Thumb();
059: thumb.setPosition(value);
060: thumb.setObject(obj);
061: thumbs.add(index, thumb);
062: ThumbDataEvent evt = new ThumbDataEvent(this , -1, index, thumb);
063: for (ThumbDataListener tdl : thumbDataListeners) {
064: tdl.thumbAdded(evt);
065: }
066: /*
067: for(ThumbChangeListener tcl : thumbChangeListeners) {
068: tcl.thumbAdded(thumb,index);
069: }
070: */
071: }
072:
073: public void removeThumb(int index) {
074: Thumb thumb = thumbs.remove(index);
075: ThumbDataEvent evt = new ThumbDataEvent(this , -1, index, thumb);
076: for (ThumbDataListener tdl : thumbDataListeners) {
077: tdl.thumbRemoved(evt);
078: }
079: /*
080: for(ThumbChangeListener tcl : thumbChangeListeners) {
081: tcl.thumbRemoved(thumb, index);
082: }
083: */
084: }
085:
086: public int getThumbCount() {
087: return thumbs.size();
088: }
089:
090: public Thumb getThumbAt(int index) {
091: return thumbs.get(index);
092: }
093:
094: public List<Thumb<E>> getSortedThumbs() {
095: List<Thumb<E>> list = new ArrayList<Thumb<E>>();
096: list.addAll(thumbs);
097: Collections.sort(list, new Comparator<Thumb<E>>() {
098: public int compare(Thumb<E> o1, Thumb<E> o2) {
099: float f1 = o1.getPosition();
100: float f2 = o2.getPosition();
101: if (f1 < f2) {
102: return -1;
103: }
104: if (f1 > f2) {
105: return 1;
106: }
107: return 0;
108: }
109: });
110: return list;
111: }
112:
113: public Iterator<Thumb<E>> iterator() {
114: return thumbs.iterator();
115: }
116: }
|