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 * @return the column count
32 public int getColumnCount()
39 * @param inColNum column number
40 * @return name of the column
42 public String getColumnName(int inColNum)
44 if (inColNum == 0) return I18nManager.getText("dialog.load.table.field");
45 else if (inColNum == 1) return I18nManager.getText("dialog.load.table.datatype");
46 return I18nManager.getText("dialog.load.table.description");
51 * @return the row count
53 public int getRowCount()
55 if (_fieldArray == null)
62 * @param inRowIndex row index
63 * @param inColumnIndex column index
64 * @return the value of the specified cell
66 public Object getValueAt(int inRowIndex, int inColumnIndex)
68 if (_fieldArray == null) return "";
69 if (inColumnIndex == 0) return ("" + (inRowIndex+1));
70 Field field = _fieldArray[inRowIndex];
71 if (inColumnIndex == 1)
73 // Field name - take name from built-in fields
74 if (field.isBuiltIn())
75 return field.getName();
76 // Otherwise take custom name
79 // description column - builtin fields don't have one
80 if (field.isBuiltIn()) return "";
81 return field.getName();
86 * Make sure only second and third columns are editable
87 * @param inRowIndex row index
88 * @param inColumnIndex column index
89 * @return true if cell editable
91 public boolean isCellEditable(int inRowIndex, int inColumnIndex)
93 if (inColumnIndex <= 1)
94 return (inColumnIndex == 1);
95 // Column is 2 so only edit non-builtin field names
96 Field field = _fieldArray[inRowIndex];
97 return !field.isBuiltIn();
103 * @param inData 2-dimensional Object array containing the data
105 public void updateData(Field[] inData)
107 _fieldArray = inData;
108 if (_fieldArray != null)
110 _numRows = _fieldArray.length;
112 fireTableStructureChanged();
117 * React to edits to the table data
118 * @param inValue value to set
119 * @param inRowIndex row index
120 * @param inColumnIndex column index
122 public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
124 super.setValueAt(inValue, inRowIndex, inColumnIndex);
125 if (inColumnIndex == 1)
127 Field field = _fieldArray[inRowIndex];
128 if (!field.getName().equals(inValue.toString()))
130 manageFieldChange(inRowIndex, inValue.toString());
133 else if (inColumnIndex == 2)
135 // change description if it's custom
136 Field field = _fieldArray[inRowIndex];
137 if (!field.isBuiltIn())
138 field.setName(inValue.toString());
144 * Move the selected item up one place
145 * @param inIndex index of item to move
147 public void moveUp(int inIndex)
151 swapItems(inIndex-1, inIndex);
157 * Move the selected item down one place
158 * @param inIndex index of item to move
160 public void moveDown(int inIndex)
162 if (inIndex > -1 && inIndex < (_numRows - 1))
164 swapItems(inIndex, inIndex+1);
170 * Swap the specified items in the array
171 * @param inIndex1 index of first item
172 * @param inIndex2 index of second item (higher than inIndex1)
174 private void swapItems(int inIndex1, int inIndex2)
176 Field temp = _fieldArray[inIndex1];
177 _fieldArray[inIndex1] = _fieldArray[inIndex2];
178 _fieldArray[inIndex2] = temp;
179 fireTableRowsUpdated(inIndex1, inIndex2);
184 * React to a requested change to one of the fields
185 * @param inRow row number of change
186 * @param inValue new string value
188 private void manageFieldChange(int inRow, String inValue)
190 // check if it's lat or long - don't allow changes to these fields
191 Field field = _fieldArray[inRow];
192 if (field == Field.LATITUDE || field == Field.LONGITUDE)
194 if (inValue.equals(I18nManager.getText("fieldname.latitude"))
195 || inValue.equals(I18nManager.getText("fieldname.longitude")))
198 // Changes to custom field need to be handled differently
199 boolean changeToCustom = inValue.equals(I18nManager.getText("fieldname.custom"));
202 if (field.isBuiltIn())
204 String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
205 int index = inRow + 1;
206 while (hasField(customPrefix + index))
208 _fieldArray[inRow] = new Field(customPrefix + index);
210 // ignore custom to custom changes
214 // Change to a fixed field - check we've not already got it
215 if (!hasField(inValue))
217 // Change is ok - find new Field object corresponding to text
218 _fieldArray[inRow] = Field.getField(inValue);
222 fireTableRowsUpdated(inRow, inRow);
227 * @return array of Field objects
229 public Field[] getFieldArray()
236 * @param inName Name of field to find
237 * @return true if this field is already present
239 private boolean hasField(String inName)
241 if (_fieldArray == null || inName == null) return false;
242 for (int i=0; i<_numRows; i++)
243 if (_fieldArray[i].getName().equals(inName))