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

jaxx.runtime.swing.list.DefaultCheckListModel Maven / Gradle / Ivy

There is a newer version: 3.0-alpha-1
Show newest version
/*
 * Copyright (c) 2009-2011, EzWare
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.Redistributions
 * in binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or
 * other materials provided with the distribution.Neither the name of the
 * EzWare nor the names of its contributors may be used to endorse or
 * promote products derived from this software without specific prior
 * written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * %%Ignore-License%%
 */

package jaxx.runtime.swing.list;

import jaxx.runtime.JAXXUtil;
import org.nuiton.decorator.Decorator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.AbstractListModel;

/**
 * Default model for check list. It is based on the list of items
 * Implementation of checks is based on HashSet of checked items
 *
 * @author Eugene Ryzhikov
 * @author Kevin Morin
 *
 * @param  list element type
 */
public class DefaultCheckListModel extends AbstractListModel implements CheckListModel {

	private static final long serialVersionUID = 1L;

	protected final List data = new ArrayList();
    protected final Set checks = new HashSet();

	public DefaultCheckListModel( Collection data ) {

		if ( data == null ) return;
		for (T object : data) {
            this.data.add( object );
			checks.clear();
		}
	}

	public DefaultCheckListModel( T... data ) {
		this( Arrays.asList( data ));
	}

	/* (non-Javadoc)
	 * @see org.oxbow.swingbits.list.ICheckListModel#getSize()
	 */
	@Override
	public int getSize() {
        return data().size();
    }

    @Override
    public int getOriginalSize() {
		return data.size();
	}

	protected List data() {
		return data;
	}


	/* (non-Javadoc)
	 * @see org.oxbow.swingbits.list.ICheckListModel#getElementAt(int)
	 */
	@Override
	public Object getElementAt(int index) {
		return data().get(index);
	}

	/* (non-Javadoc)
	 * @see org.oxbow.swingbits.list.ICheckListModel#isChecked(int)
	 */
	@Override
	public boolean isCheckedIndex( int index ) {
		return checks.contains( data().get(index));
	}

	/* (non-Javadoc)
	 * @see org.oxbow.swingbits.list.ICheckListModel#setChecked(int, boolean)
	 */
	@Override
	public void setCheckedIndex( int index, boolean value ) {
		T o = data().get(index);
		if ( value ) checks.add(o); else checks.remove(o);
		fireContentsChanged(this, index, index);
	}

	/* (non-Javadoc)
	 * @see org.oxbow.swingbits.list.ICheckListModel#getChecked()
	 */
	@Override
	public Collection getCheckedItems() {
		List items = new ArrayList(checks);
		items.retainAll(data);
		return Collections.unmodifiableList( items );
	}

	/* (non-Javadoc)
	 * @see org.oxbow.swingbits.list.ICheckListModel#setChecked(java.util.Collection)
	 */
	@Override
	public void setCheckedItems( Collection items ) {

		List correctedItems = new ArrayList(items);
		correctedItems.retainAll(data);

		checks.clear();
		checks.addAll( correctedItems );
		fireContentsChanged(this, 0, checks.size()-1);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy