f@0: /*
f@0: CCmI Editor - A Collaborative Cross-Modal Diagram Editing Tool
f@0:
f@0: Copyright (C) 2011 Queen Mary University of London (http://ccmi.eecs.qmul.ac.uk/)
f@0:
f@0: This program is free software: you can redistribute it and/or modify
f@0: it under the terms of the GNU General Public License as published by
f@0: the Free Software Foundation, either version 3 of the License, or
f@0: (at your option) any later version.
f@0:
f@0: This program is distributed in the hope that it will be useful,
f@0: but WITHOUT ANY WARRANTY; without even the implied warranty of
f@0: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f@0: GNU General Public License for more details.
f@0:
f@0: You should have received a copy of the GNU General Public License
f@0: along with this program. If not, see .
f@0: */
f@0: package uk.ac.qmul.eecs.ccmi.gui;
f@0:
f@0: import java.util.HashSet;
f@0: import java.util.List;
f@0: import java.util.ArrayList;
f@0: import java.util.Set;
f@0:
f@0: import javax.swing.table.AbstractTableModel;
f@0:
f@0: import uk.ac.qmul.eecs.ccmi.diagrammodel.NodeProperties.Modifiers;
f@0:
f@0:
f@0: /**
f@0: * A table model containing the property values currently edited in a table of a {@code PropertyEditorDialog}
f@0: * and the modifiers assigned to them
f@0: * in the form of an array of indexes pointing to the modifiers types
f@0: */
f@0: @SuppressWarnings("serial")
f@0: public class PropertyTableModel extends AbstractTableModel {
f@0:
f@0: /**
f@0: * Construct a new {@code PropertyTableModel}.
f@0: *
f@0: * @param propertyType the type of the node property related to this table model
f@0: * @param values the list of values for this node property
f@0: * @param modifiers the modifiers for this node property
f@0: */
f@0: public PropertyTableModel(String propertyType, List values, Modifiers modifiers ){
f@0: data = new ArrayList();
f@0: for(int i = 0; i< values.size();i++){
f@0: String value = values.get(i);
f@0: data.add(new ModifierString(value, (modifiers == null) ? null : modifiers.getIndexes(i)));
f@0: }
f@0: data.add(new ModifierString(null));
f@0: columnName = propertyType;
f@0: }
f@0:
f@0: @Override
f@0: public int getColumnCount() {
f@0: return 1;
f@0: }
f@0:
f@0: @Override
f@0: public int getRowCount() {
f@0: return data.size();
f@0: }
f@0:
f@0: @Override
f@0: public Object getValueAt(int rowIndex, int columnIndex) {
f@0: return data.get(rowIndex);
f@0: }
f@0:
f@0: @Override
f@0: public String getColumnName(int column){
f@0: return columnName;
f@0: }
f@0:
f@0: @Override
f@0: public void setValueAt(Object value, int rowIndex, int columnIndex){
f@0: /* we filled up the last row, create another one */
f@0: if((rowIndex == data.size()-1)&&(!value.toString().equals(""))){
f@0: data.add(new ModifierString(null));
f@0: data.get(rowIndex).value = value.toString();
f@0: fireTableRowsInserted(data.size()-1, data.size()-1);
f@0: fireTableCellUpdated(rowIndex,columnIndex);
f@0: }else if((rowIndex != data.size()-1)&&(value.toString().equals(""))){
f@0: data.remove(rowIndex);
f@0: fireTableRowsDeleted(rowIndex,rowIndex);
f@0: }else {
f@0: data.get(rowIndex).value = value.toString();
f@0: fireTableCellUpdated(rowIndex,columnIndex);
f@0: }
f@0: }
f@0:
f@0: @Override
f@0: public boolean isCellEditable(int rowIndex, int columnIndex){
f@0: return true;
f@0: }
f@0:
f@0: /**
f@0: * Returns the indexes (pointing to the modifiers types) of the property value at the specified row in the table
f@0: * with this model.
f@0: *
f@0: * @param row the row of the property value
f@0: * @return the modifiers type indexes for the specified property value
f@0: */
f@0: public Set getIndexesAt(int row){
f@0: return data.get(row).modifierIndexes;
f@0: }
f@0:
f@0: /**
f@0: * Set the the indexes (pointing to the modifiers types) of the property value at the specified row in the table
f@0: * with this model.
f@0: *
f@0: * @param row he row of the property value
f@0: * @param indexes the modifiers type indexes for the specified property value
f@0: */
f@0: public void setIndexesAt(int row, Set indexes){
f@0: data.get(row).modifierIndexes = new HashSet();
f@0: data.get(row).modifierIndexes.addAll(indexes);
f@0: }
f@0:
f@0: /**
f@0: * Set the the indexes (pointing to the modifiers types) of the property value at the specified row in the table
f@0: * with this model.
f@0: *
f@0: * @param row he row of the property value
f@0: * @param indexes the modifiers type indexes for the specified property value
f@0: */
f@0: public void setIndexesAt(int row, Integer[] indexes){
f@0: data.get(row).modifierIndexes = new HashSet();
f@0: for(int i=0; i data;
f@0: private String columnName;
f@0:
f@0: private class ModifierString {
f@0: ModifierString(String value, Set s){
f@0: this.value = value;
f@0: modifierIndexes = new HashSet();
f@0: if(s != null){
f@0: modifierIndexes.addAll(s);
f@0: }
f@0: }
f@0:
f@0: ModifierString(Set s){
f@0: this("",s);
f@0: }
f@0:
f@0: @Override
f@0: public boolean equals(Object o){
f@0: return value.equals(o);
f@0: }
f@0:
f@0: @Override
f@0: public int hashCode(){
f@0: return value.hashCode();
f@0: }
f@0:
f@0: @Override
f@0: public String toString(){
f@0: return value;
f@0: }
f@0:
f@0: private String value;
f@0: private Set modifierIndexes;
f@0: }
f@0:
f@0: }