view java/src/uk/ac/qmul/eecs/ccmi/checkboxtree/CheckBoxTreeNode.java @ 3:9e67171477bc

PHANTOM Omni Heptic device release
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Wed, 25 Apr 2012 17:09:09 +0100
parents
children
line wrap: on
line source
/*  
 CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
  
 Copyright (C) 2011  Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)

 This program 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 3 of the License, or
 (at your option) any later version.

 This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package uk.ac.qmul.eecs.ccmi.checkboxtree;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeNode;


/**
 * A special {@code DefaultMutableTreeNode} that has, in addition, the property of being (or not) 
 * selected.    
 *
 */
@SuppressWarnings("serial")
public class CheckBoxTreeNode extends DefaultMutableTreeNode {

	/**
	 * Construct a new instance of this class.
	 * 
	 * @param userObject a user object for this tree node @see javax.swing.tree#DefaultMutableTreeNode
	 * @param selectable whether or not this node is selectable. A non selectable node is pretty much 
	 * equivalent to a {@code DefaultMutableTreeNode}. 
	 */
	public CheckBoxTreeNode(Object userObject, boolean selectable){
		super(userObject);
		this.selectable = selectable;
		selected = false;
	}
	
	/**
	 * Returns {@code true} if the node is selected, or {@code false} otherwise. 
	 * 
	 * @return {@code true} if the node is selected, or {@code false} otherwise. 
	 */
	public boolean isSelected() {
		return selected;
	}

	/**
	 * Makes the node selected or unselected 
	 * 
	 * @param selected {@code true} to select the node, {@code false} to unselect it. 
	 */
	public void setSelected(boolean selected) {
		if(selectable){			
			this.selected = selected;
		}
	}
	
	/**
	 * Whether the node is selectable or not. This depends on the value of the 
	 * {@code selected} parameter passed to the constructor. 
	 * 
	 * @return {@code true} if the node is selectable, {@code false} otherwise.
	 */
	public boolean isSelectable(){
		return selectable;
	}
	
	/**
	 * Returns a string representation of the audio description of this node. 
	 * The value returned by this method can be passed to a Text-To-Speech synthesizer
	 * for speech rendering.
	 * 
	 * @return the audio description of this node.
	 */
	public String spokenText(){
		if(selectable)
			return toString()+", "+ (selected ? "checked": "unchecked");
		else
			return toString();
	}
	
	/**
	 * Returns a string representation of the path of this tree node. The the string is made up
	 * as the concatenation of the tree node names from the root to this node, separated by
	 * {@link #STRING_PATH_SEPARATOR} 
	 * 
	 * @return the node path as a String 
	 */
	public String getPathAsString(){
		StringBuilder builder = new StringBuilder();
		TreeNode [] path = getPath();
		for(int i=0; i<path.length; i++){
			builder.append(path[i].toString());
			if(i != path.length-1)
				builder.append(STRING_PATH_SEPARATOR);
		}
		return builder.toString();
	}
	
	private boolean selected;
	private boolean selectable;
	/**
	 * The character used as a separator when returning the string representation of the path
	 * of this node via {@link #getPathAsString()}.  
	 */
	public static final char STRING_PATH_SEPARATOR = '.';
}