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