1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
|
package tim.prune.load;
import javax.swing.table.AbstractTableModel;
import tim.prune.I18nManager;
import tim.prune.data.Field;
/**
* Class to hold the table model for the field selection table
*/
public class FieldSelectionTableModel extends AbstractTableModel
{
private int _numRows = 0;
private Field[] _fieldArray = null;
private String _customText = null;
/**
* Constructor
*/
public FieldSelectionTableModel()
{
// Cache the custom text for the table so it doesn't
// have to be looked up so often
_customText = I18nManager.getText("fieldname.custom");
}
/**
* @return the column count
*/
public int getColumnCount()
{
return 3;
}
/**
* @param inColNum column number
* @return name of the column
*/
public String getColumnName(int inColNum)
{
if (inColNum == 0) return I18nManager.getText("dialog.load.table.field");
else if (inColNum == 1) return I18nManager.getText("dialog.load.table.datatype");
return I18nManager.getText("dialog.load.table.description");
}
/**
* @return the row count
*/
public int getRowCount()
{
if (_fieldArray == null)
return 2;
return _numRows;
}
/**
* @param inRowIndex row index
* @param inColumnIndex column index
* @return the value of the specified cell
*/
public Object getValueAt(int inRowIndex, int inColumnIndex)
{
if (_fieldArray == null) return "";
if (inColumnIndex == 0) return ("" + (inRowIndex+1));
Field field = _fieldArray[inRowIndex];
if (inColumnIndex == 1)
{
// Field name - take name from built-in fields
if (field.isBuiltIn())
return field.getName();
// Otherwise take custom name
return _customText;
}
// description column - builtin fields don't have one
if (field.isBuiltIn()) return "";
return field.getName();
}
/**
* Make sure only second and third columns are editable
* @param inRowIndex row index
* @param inColumnIndex column index
* @return true if cell editable
*/
public boolean isCellEditable(int inRowIndex, int inColumnIndex)
{
if (inColumnIndex <= 1)
return (inColumnIndex == 1);
// Column is 2 so only edit non-builtin field names
Field field = _fieldArray[inRowIndex];
return !field.isBuiltIn();
}
/**
* Update the data
* @param inData 2-dimensional Object array containing the data
*/
public void updateData(Field[] inData)
{
_fieldArray = inData;
if (_fieldArray != null)
{
_numRows = _fieldArray.length;
}
fireTableStructureChanged();
}
/**
* React to edits to the table data
* @param inValue value to set
* @param inRowIndex row index
* @param inColumnIndex column index
*/
public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
{
super.setValueAt(inValue, inRowIndex, inColumnIndex);
if (inColumnIndex == 1)
{
Field field = _fieldArray[inRowIndex];
if (!field.getName().equals(inValue.toString()))
{
manageFieldChange(inRowIndex, inValue.toString());
}
}
else if (inColumnIndex == 2)
{
// change description if it's custom
Field field = _fieldArray[inRowIndex];
if (!field.isBuiltIn())
field.setName(inValue.toString());
}
}
/**
* Move the selected item up one place
* @param inIndex index of item to move
*/
public void moveUp(int inIndex)
{
if (inIndex > 0)
{
swapItems(inIndex-1, inIndex);
}
}
/**
* Move the selected item down one place
* @param inIndex index of item to move
*/
public void moveDown(int inIndex)
{
if (inIndex > -1 && inIndex < (_numRows - 1))
{
swapItems(inIndex, inIndex+1);
}
}
/**
* Swap the specified items in the array
* @param inIndex1 index of first item
* @param inIndex2 index of second item (higher than inIndex1)
*/
private void swapItems(int inIndex1, int inIndex2)
{
Field temp = _fieldArray[inIndex1];
_fieldArray[inIndex1] = _fieldArray[inIndex2];
_fieldArray[inIndex2] = temp;
fireTableRowsUpdated(inIndex1, inIndex2);
}
/**
* React to a requested change to one of the fields
* @param inRow row number of change
* @param inValue new string value
*/
private void manageFieldChange(int inRow, String inValue)
{
// check if it's lat or long - don't allow changes to these fields
Field field = _fieldArray[inRow];
if (field == Field.LATITUDE || field == Field.LONGITUDE)
return;
if (inValue.equals(I18nManager.getText("fieldname.latitude"))
|| inValue.equals(I18nManager.getText("fieldname.longitude")))
return;
// Changes to custom field need to be handled differently
boolean changeToCustom = inValue.equals(I18nManager.getText("fieldname.custom"));
if (changeToCustom)
{
if (field.isBuiltIn())
{
String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
int index = inRow + 1;
while (hasField(customPrefix + index))
index++;
_fieldArray[inRow] = new Field(customPrefix + index);
}
// ignore custom to custom changes
}
else
{
// Change to a fixed field - check we've not already got it
if (!hasField(inValue))
{
// Change is ok - find new Field object corresponding to text
_fieldArray[inRow] = Field.getField(inValue);
}
}
// fire change
fireTableRowsUpdated(inRow, inRow);
}
/**
* @return array of Field objects
*/
public Field[] getFieldArray()
{
return _fieldArray;
}
/**
* @param inName Name of field to find
* @return true if this field is already present
*/
private boolean hasField(String inName)
{
if (_fieldArray == null || inName == null) return false;
for (int i=0; i<_numRows; i++)
if (_fieldArray[i].getName().equals(inName))
return true;
return false;
}
}
|