1 package tim.prune.load;
3 import javax.swing.table.AbstractTableModel;
5 import tim.prune.I18nManager;
6 import tim.prune.data.Field;
9 * Class to hold the table model for the field selection table
11 public class FieldSelectionTableModel extends AbstractTableModel
14 private int _numRows = 0;
15 private Field[] _fieldArray = null;
16 private String _customText = null;
21 public FieldSelectionTableModel()
23 // Cache the custom text for the table so it doesn't
24 // have to be looked up so often
25 _customText = I18nManager.getText("fieldname.custom");
30 * Get the column count
32 public int getColumnCount()
39 * Get the name of the column
41 public String getColumnName(int inColNum)
43 if (inColNum == 0) return I18nManager.getText("dialog.load.table.field");
44 else if (inColNum == 1) return I18nManager.getText("dialog.load.table.datatype");
45 return I18nManager.getText("dialog.load.table.description");
52 public int getRowCount()
54 if (_fieldArray == null)
61 * Get the value of the specified cell
63 public Object getValueAt(int rowIndex, int columnIndex)
65 if (_fieldArray == null) return "";
66 if (columnIndex == 0) return ("" + (rowIndex+1));
67 Field field = _fieldArray[rowIndex];
70 // Field name - take name from built-in fields
71 if (field.isBuiltIn())
72 return field.getName();
73 // Otherwise take custom name
76 // description column - builtin fields don't have one
77 if (field.isBuiltIn()) return "";
78 return field.getName();
83 * Make sure only second and third columns are editable
85 public boolean isCellEditable(int rowIndex, int columnIndex)
88 return (columnIndex == 1);
89 // Column is 2 so only edit non-builtin field names
90 Field field = _fieldArray[rowIndex];
91 return !field.isBuiltIn();
97 * @param inData 2-dimensional Object array containing the data
99 public void updateData(Field[] inData)
101 _fieldArray = inData;
102 if (_fieldArray != null)
104 _numRows = _fieldArray.length;
106 fireTableStructureChanged();
111 * React to edits to the table data
113 public void setValueAt(Object aValue, int rowIndex, int columnIndex)
115 super.setValueAt(aValue, rowIndex, columnIndex);
116 if (columnIndex == 1)
118 Field field = _fieldArray[rowIndex];
119 if (!field.getName().equals(aValue.toString()))
121 manageFieldChange(rowIndex, aValue.toString());
124 else if (columnIndex == 2)
126 // change description if it's custom
127 Field field = _fieldArray[rowIndex];
128 if (!field.isBuiltIn())
129 field.setName(aValue.toString());
135 * Move the selected item up one place
136 * @param inIndex index of item to move
138 public void moveUp(int inIndex)
142 swapItems(inIndex-1, inIndex);
148 * Move the selected item down one place
149 * @param inIndex index of item to move
151 public void moveDown(int inIndex)
153 if (inIndex > -1 && inIndex < (_numRows - 1))
155 swapItems(inIndex, inIndex+1);
161 * Swap the specified items in the array
162 * @param inIndex1 index of first item
163 * @param inIndex2 index of second item (higher than inIndex1)
165 private void swapItems(int inIndex1, int inIndex2)
167 Field temp = _fieldArray[inIndex1];
168 _fieldArray[inIndex1] = _fieldArray[inIndex2];
169 _fieldArray[inIndex2] = temp;
170 fireTableRowsUpdated(inIndex1, inIndex2);
175 * React to a requested change to one of the fields
176 * @param inRow row number of change
177 * @param inValue new string value
179 private void manageFieldChange(int inRow, String inValue)
181 // check if it's lat or long - don't allow changes to these fields
182 Field field = _fieldArray[inRow];
183 if (field == Field.LATITUDE || field == Field.LONGITUDE)
185 if (inValue.equals(I18nManager.getText("fieldname.latitude"))
186 || inValue.equals(I18nManager.getText("fieldname.longitude")))
189 // Changes to custom field need to be handled differently
190 boolean changeToCustom = inValue.equals(I18nManager.getText("fieldname.custom"));
193 if (field.isBuiltIn())
195 String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
196 int index = inRow + 1;
197 while (hasField(customPrefix + index))
199 _fieldArray[inRow] = new Field(customPrefix + index);
201 // ignore custom to custom changes
205 // Change to a fixed field - check we've not already got it
206 if (!hasField(inValue))
208 // Change is ok - find new Field object corresponding to text
209 for (int i=0; i<Field.ALL_AVAILABLE_FIELDS.length; i++)
210 if (Field.ALL_AVAILABLE_FIELDS[i].getName().equals(inValue))
211 _fieldArray[inRow] = Field.ALL_AVAILABLE_FIELDS[i];
215 fireTableRowsUpdated(inRow, inRow);
220 * @return array of Field objects
222 public Field[] getFieldArray()
229 * @param inName Name of field to find
230 * @return true if this field is already present
232 private boolean hasField(String inName)
234 if (_fieldArray == null || inName == null) return false;
235 for (int i=0; i<_numRows; i++)
236 if (_fieldArray[i].getName().equals(inName))