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