All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jdesktop.swingx.multislider.DefaultMultiThumbModel Maven / Gradle / Ivy

There is a newer version: 1.6.5-1
Show newest version
/*
 * $Id: DefaultMultiThumbModel.java 3935 2011-03-02 19:06:41Z kschaefe $
 *
 * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle,
 * Santa Clara, California 95054, U.S.A. All rights reserved.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

package org.jdesktop.swingx.multislider;


import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author joshy
 */
public class DefaultMultiThumbModel extends AbstractMultiThumbModel {
    
    protected List>thumbs = new ArrayList>();
    
    /** Creates a new instance of DefaultMultiThumbModel */
    public DefaultMultiThumbModel() {
        setMinimumValue(0.0f);
        setMaximumValue(1.0f);
    }
    // returns the index of the newly added thumb
    @Override
    public int addThumb(float value, E obj) {
        Thumb thumb = new Thumb(this);
        thumb.setPosition(value);
        thumb.setObject(obj);
        thumbs.add(thumb);
        int n = thumbs.size();
        ThumbDataEvent evt = new ThumbDataEvent(this,-1,thumbs.size()-1,thumb);
        for(ThumbDataListener tdl : thumbDataListeners) {
            tdl.thumbAdded(evt);
        }
        return n-1;
    }

    @Override
    public void insertThumb(float value, E obj, int index) {
        Thumb thumb = new Thumb(this);
        thumb.setPosition(value);
        thumb.setObject(obj);
        thumbs.add(index,thumb);
        ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
        for(ThumbDataListener tdl : thumbDataListeners) {
            tdl.thumbAdded(evt);
        }
    }

    @Override
    public void removeThumb(int index) {
        Thumb thumb = thumbs.remove(index);
        ThumbDataEvent evt = new ThumbDataEvent(this,-1,index,thumb);
        for(ThumbDataListener tdl : thumbDataListeners) {
            tdl.thumbRemoved(evt);
        }
    }

    @Override
    public int getThumbCount() {
        return thumbs.size();
    }

    @Override
    public Thumb getThumbAt(int index) {
        return thumbs.get(index);
    }

    @Override
    public List> getSortedThumbs() {
        List> list = new ArrayList>();
        list.addAll(thumbs);
        Collections.sort(list, new Comparator>() {
            @Override
            public int compare(Thumb o1, Thumb o2) {
                float f1 = o1.getPosition();
                float f2 = o2.getPosition();
                if(f1f2) {
                    return 1;
                }
                return 0;
            }
        });
        return list;
    }

    @Override
    public Iterator> iterator() {
        return thumbs.iterator();
    }

    @Override
    public int getThumbIndex(Thumb thumb) {
        return thumbs.indexOf(thumb);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy