view java/src/uk/ac/qmul/eecs/ccmi/gui/PropertyTableModel.java @ 0:9418ab7b7f3f

Initial import
author Fiore Martin <fiore@eecs.qmul.ac.uk>
date Fri, 16 Dec 2011 17:35:51 +0000
parents
children d66dd5880081
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 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 {

	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;
	}
	
	public Set<Integer> getIndexesAt(int row){
		return data.get(row).modifierIndexes;
	}
	
	public void setIndexesAt(int row, Set<Integer> indexes){
		data.get(row).modifierIndexes = new HashSet<Integer>();
		data.get(row).modifierIndexes.addAll(indexes);
	}
	
	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]);
	}
	
	List<ModifierString> data;
	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;
		}
		
		String value;
		Set<Integer> modifierIndexes;
	}

}