/**
 * Copyright (c) 2011, Martin Foster <martin@ethereal-realms.org>
 * 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 Ethereal-Realms.org 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 HOLDER 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.
 */
package org.lyra.Widgets;

import javax.swing.DefaultListSelectionModel;
import javax.swing.JTable;

/**
 * ListSelectionModels provide no inherent ability to identify which model is
 * called from which element.   They also do not provide the capability to
 * return to the JTable/JList which makes use of it.   This variant provides
 * both.
 * <p>
 * @author Martin Foster
 * @version 1.0.1, %Revision, 99%, %LastChangedDate, 17/03/11 8:17PM%
 */
public class EnhancedListSelectionModel extends DefaultListSelectionModel {
	
	// Serial Lunacy
	private static final long serialVersionUID = 1L;

	// Identifier capability
	private String myName;
	
	// Component which has linked to this model
	private JTable mySource;
	
	
	/**
	 * Default constructor which will not provide any additional capability
	 * unless it is added manually through <i>setName</i> and <i>setSource</i>
	 * methods. 
	 */
	public EnhancedListSelectionModel() {
		
		// Call Super
		super();
	}
	// End of Constructor Method
	
	
	/**
	 * Constructor method which will take the source element and adjust both
	 * source and identifier elements.
	 * <p>
	 * @param source Source JTable attached.
	 */
	public EnhancedListSelectionModel(JTable source) {
		
		// Straight pass through
		super();
		
		/*
		 * Source elements should already bear an identifier so we can just
		 * make use of that one directly without problem.
		 */
		setSource(source);
		setName(source.getName());
	}
	// End of Constructor Method

	
	/**
	 * More complex constructor which will enable specifying both elements.
	 * <p>
	 * @param source Source JTable attached.
	 * @param name Identifier string.
	 */
	public EnhancedListSelectionModel(JTable source, String name) {
		
		// Straight pass through
		super();
		
		/*
		 * Source elements should already bear an identifier so we can just
		 * make use of that one directly without problem.
		 */
		setSource(source);
		setName(name);	
	}
	// End of Constructor Method
	
	
	/**
	 * Gets the source element attached to this model.
	 * <p>
	 * @return JTable source instance.
	 */
	public JTable getSource() {
		return mySource;
	}
	// End of getSource Method
	
	
	/**
	 * Sets the source element attached to the this model.
	 * <p>
	 * @param source JTable instance as source.
	 */
	public void setSource(JTable source) {
		
		// Store instance
		mySource = source;
		
		/*
		 * Now that we have a source element, we may as well add teether this
		 * class to the source elements and avoid duplications.
		 */
		source.setSelectionModel(this);
	}
	// End of setSource Method
	
	
	/**
	 * Gets the identifier name.
	 * <p>
	 * @return String containing identifier.
	 */
	public String getName() {
		return myName;
	}
	// End of getName Method
	
	
	/**
	 * Sets the identifier name.
	 * <p>
	 * @param name String containing identifier.
	 */
	public void setName(String name) {
		myName = name;
	}
	// End of setName Method
}
