DefaultTrackSelectionModel.java :  » Testing » jacareto » jacareto » trackimpl » Java Open Source

Java Open Source » Testing » jacareto 
jacareto » jacareto » trackimpl » DefaultTrackSelectionModel.java
/*
 * Jacareto Copyright (c) 2002-2005
 * Applied Computer Science Research Group, Darmstadt University of
 * Technology, Institute of Mathematics & Computer Science,
 * Ludwigsburg University of Education, and Computer Based
 * Learning Research Group, Aachen University. All rights reserved.
 *
 * Jacareto is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * Jacareto 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
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with Jacareto; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package jacareto.trackimpl;


import jacareto.track.TrackSelectionEvent;
import jacareto.track.TrackSelectionListener;
import jacareto.track.TrackSelectionModel;
import jacareto.track.block.Block;
import jacareto.track.block.BlockType;

import org.apache.commons.lang.Validate;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * Implementation of the {@link TrackSelectionModel} interface.
 * </p>
 *
 * @author Oliver Specht
 * @version $revision$
 */
public class DefaultTrackSelectionModel implements TrackSelectionModel {
    /** List of all listeners listening to changes in this model */
    List listeners = new ArrayList();

    /** HashMap which holds List of selected blocks referenced by BlockType */
    Map selectedBlocks = new HashMap();

    /**
     * Creates a new DefaultTrackSelectionModel
     */
    public DefaultTrackSelectionModel () {
        init ();
    }

    /**
     * Initializes the HashMap with all possible types and an empty list as selection
     */
    private void init () {
        Iterator iter = BlockType.iterator ();

        while (iter.hasNext ()) {
            selectedBlocks.put (iter.next (), Collections.EMPTY_LIST);
        }
    }

    /**
     * {@inheritDoc}
     */
    public void addTrackSelectionListener (TrackSelectionListener listener) {
        Validate.notNull (listener);
        Validate.isTrue (! listeners.contains (listener));

        listeners.add (listener);
    }

    /**
     * {@inheritDoc}
     */
    public void removeTrackSelectionListener (TrackSelectionListener listener) {
        Validate.notNull (listener);
        Validate.isTrue (listeners.contains (listener));

        listeners.remove (listener);
    }

    /**
     * {@inheritDoc}
     */
    public void setSelection (Block block) {
        Validate.notNull (block);
        Validate.isTrue (selectedBlocks.keySet ().contains (block.getType ()));

        List list = new ArrayList();
        list.add (block);
        selectedBlocks.put (block.getType (), list);
        fireBlockSelected (block);
    }

    /**
     * {@inheritDoc}
     */
    public List getSelection (BlockType type) {
        Validate.notNull (type);
        Validate.isTrue (this.selectedBlocks.keySet ().contains (type));

        return (List) this.selectedBlocks.get (type);
    }

    /**
     * {@inheritDoc}
     */
    public void setSelectionRange (Block start, Block end) {
        Validate.notNull (start);
        Validate.notNull (end);
        Validate.notNull (this.selectedBlocks);
        Validate.isTrue (start.getType () == end.getType ());

        List list = new ArrayList();
        list.add (start);
        list.add (end);
        this.selectedBlocks.put (start.getType (), list);

        fireRangeSelected (start, end);
    }

    public void removeSelection () {
        Validate.notNull (this.selectedBlocks);

        Iterator iter = this.selectedBlocks.keySet ().iterator ();

        while (iter.hasNext ()) {
            ((List) this.selectedBlocks.get (iter.next ())).clear ();
        }

        fireSelectionRemoved ();
    }

    /**
     * {@inheritDoc}
     */
    public void removeSelection (BlockType type) {
        Validate.notNull (type);
        Validate.notNull (this.selectedBlocks);
        Validate.isTrue (this.selectedBlocks.keySet ().contains (type));

        ((List) this.selectedBlocks.get (type)).clear ();

        fireSelectionRemoved ();
    }

    /**
     * Informs all listeners that a single block has been selected
     *
     * @param block the block that has been selected
     */
    void fireBlockSelected (Block block) {
        Validate.notNull (this.listeners);

        Iterator iter = this.listeners.iterator ();

        while (iter.hasNext ()) {
            ((TrackSelectionListener) iter.next ()).blockSelected (new TrackSelectionEvent(this,
                    block));
        }
    }

    /**
     * Informs all listeners that a range of blocks has been selected
     *
     * @param firstBlock the first selected {@link Block}
     * @param lastBlock the last selected {@link Block}
     */
    void fireRangeSelected (Block firstBlock, Block lastBlock) {
        Validate.notNull (this.listeners);

        Iterator iter = this.listeners.iterator ();

        while (iter.hasNext ()) {
            ((TrackSelectionListener) iter.next ()).rangeSelected (new TrackSelectionEvent(this,
                    firstBlock, lastBlock));
        }
    }

    /**
     * Informs all listeners that the selection has been removed
     */
    void fireSelectionRemoved () {
        Validate.notNull (this.listeners);

        Iterator iter = this.listeners.iterator ();

        while (iter.hasNext ()) {
            ((TrackSelectionListener) iter.next ()).selectionRemoved (new TrackSelectionEvent(this));
        }
    }

    /**
     * {@inheritDoc}
     */
    public boolean hasSelectedBlocks (BlockType type) {
        Validate.notNull (type);
        Validate.notNull (this.selectedBlocks);
        Validate.isTrue (this.selectedBlocks.keySet ().contains (type));

        if (((List) this.selectedBlocks.get (type)).size () > 0) {
            return true;
        }

        return false;
    }

    /**
     * {@inheritDoc}
     */
    public boolean hasSelectedBlocks () {
        Validate.notNull (this.selectedBlocks);

        if (this.selectedBlocks.size () == 0) {
            return false;
        }

        return true;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.