view java/src/uk/ac/qmul/eecs/ccmi/gui/PropertyTableModel.java @ 5:d66dd5880081

Added support for Falcon Haptic device and Tablet/Mouse as haptic device
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Tue, 10 Jul 2012 22:39:37 +0100
parents 9418ab7b7f3f
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.gui;

import java.util.HashSet;
import java.util.List;
import java.util.ArrayList;
import java.util.Set;

import javax.swing.table.AbstractTableModel;

import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;


/**
 * A table model containing the property values currently edited in a table of a {@code PropertyEditorDialog} 
 * and the modifiers assigned to them
 * in the form of an array of indexes pointing to the modifiers types  
 */
@SuppressWarnings("serial")
public class PropertyTableModel extends AbstractTableModel {
	
	/**
	 * Construct a new {@code PropertyTableModel}.
	 * 
	 * @param propertyType the type of the node property related to this table model 
	 * @param values the list of values for this node property
	 * @param modifiers the modifiers for this node property 
	 */
	public PropertyTableModel(String propertyType, List<String> values, Modifiers modifiers ){
		data = new ArrayList<ModifierString>();
		for(int i = 0; i< values.size();i++){
			String value = values.get(i);
			data.add(new ModifierString(value, (modifiers == null) ? null : modifiers.getIndexes(i)));
		}
		data.add(new ModifierString(null));
		columnName = propertyType;
	}
	
	@Override
	public int getColumnCount() {
		return 1;
	}

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

	@Override
	public Object getValueAt(int rowIndex, int columnIndex) {
		return data.get(rowIndex);
	}
	
	@Override
	public String getColumnName(int column){
		 return columnName;
	}
	
	@Override
	public void setValueAt(Object value, int rowIndex, int columnIndex){
		/* we filled up the last row, create another  one */
		if((rowIndex == data.size()-1)&&(!value.toString().equals(""))){
			data.add(new ModifierString(null));
			data.get(rowIndex).value = value.toString();
			fireTableRowsInserted(data.size()-1, data.size()-1);
			fireTableCellUpdated(rowIndex,columnIndex);
		}else if((rowIndex != data.size()-1)&&(value.toString().equals(""))){
			data.remove(rowIndex);
			fireTableRowsDeleted(rowIndex,rowIndex);
		}else {
			data.get(rowIndex).value = value.toString();
			fireTableCellUpdated(rowIndex,columnIndex);
		}
	}
	
	@Override
	public boolean isCellEditable(int rowIndex, int columnIndex){
		return true;
	}
	
	/**
	 * Returns the indexes (pointing to the modifiers types) of the property value at the specified row in the table
	 * with this model.
	 * 
	 * @param row the row of the property value 
	 * @return the modifiers type indexes for the specified property value 
	 */
	public Set<Integer> getIndexesAt(int row){
		return data.get(row).modifierIndexes;
	}
	
	/**
	 * Set the the indexes (pointing to the modifiers types) of the property value at the specified row in the table
	 * with this model.
	 * 
	 * @param row he row of the property value 
	 * @param indexes  the modifiers type indexes for the specified property value 
	 */
	public void setIndexesAt(int row, Set<Integer> indexes){
		data.get(row).modifierIndexes = new HashSet<Integer>();
		data.get(row).modifierIndexes.addAll(indexes);
	}
	
	/**
	 * Set the the indexes (pointing to the modifiers types) of the property value at the specified row in the table
	 * with this model.
	 * 
	 * @param row he row of the property value 
	 * @param indexes  the modifiers type indexes for the specified property value 
	 */
	public void setIndexesAt(int row, Integer[] indexes){
		data.get(row).modifierIndexes = new HashSet<Integer>();
		for(int i=0; i<indexes.length; i++)
			data.get(row).modifierIndexes.add(indexes[i]);
	}
	
	private List<ModifierString> data;
	private String columnName;
	
	private class ModifierString  {
		ModifierString(String value, Set<Integer> s){
			this.value = value;
			modifierIndexes = new HashSet<Integer>();
			if(s != null){
					modifierIndexes.addAll(s);
			}
		}
		
		ModifierString(Set<Integer> s){
			this("",s);
		}
		
		@Override
		public boolean equals(Object o){
			return value.equals(o);
		}
		
		@Override
		public int hashCode(){
			return value.hashCode();
		}
		
		@Override
		public String toString(){
			return value;
		}
		
		private String value;
		private Set<Integer> modifierIndexes;
	}

}