]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/load/FieldSelectionTableModel.java
Version 4, January 2008
[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          * @return the column count
31          */
32         public int getColumnCount()
33         {
34                 return 3;
35         }
36
37
38         /**
39          * @param inColNum column number
40          * @return name of the column
41          */
42         public String getColumnName(int inColNum)
43         {
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");
47         }
48
49
50         /**
51          * @return the row count
52          */
53         public int getRowCount()
54         {
55                 if (_fieldArray == null)
56                         return 2;
57                 return _numRows;
58         }
59
60
61         /**
62          * @param inRowIndex row index
63          * @param inColumnIndex column index
64          * @return the value of the specified cell
65          */
66         public Object getValueAt(int inRowIndex, int inColumnIndex)
67         {
68                 if (_fieldArray == null) return "";
69                 if (inColumnIndex == 0) return ("" + (inRowIndex+1));
70                 Field field = _fieldArray[inRowIndex];
71                 if (inColumnIndex == 1)
72                 {
73                         // Field name - take name from built-in fields
74                         if (field.isBuiltIn())
75                                 return field.getName();
76                         // Otherwise take custom name
77                         return _customText;
78                 }
79                 // description column - builtin fields don't have one
80                 if (field.isBuiltIn()) return "";
81                 return field.getName();
82         }
83
84
85         /**
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
90          */
91         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
92         {
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();
98         }
99
100
101         /**
102          * Update the data
103          * @param inData 2-dimensional Object array containing the data
104          */
105         public void updateData(Field[] inData)
106         {
107                 _fieldArray = inData;
108                 if (_fieldArray != null)
109                 {
110                         _numRows = _fieldArray.length;
111                 }
112                 fireTableStructureChanged();
113         }
114
115
116         /**
117          * React to edits to the table data
118          * @param inValue value to set
119          * @param inRowIndex row index
120          * @param inColumnIndex column index
121          */
122         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
123         {
124                 super.setValueAt(inValue, inRowIndex, inColumnIndex);
125                 if (inColumnIndex == 1)
126                 {
127                         Field field = _fieldArray[inRowIndex];
128                         if (!field.getName().equals(inValue.toString()))
129                         {
130                                 manageFieldChange(inRowIndex, inValue.toString());
131                         }
132                 }
133                 else if (inColumnIndex == 2)
134                 {
135                         // change description if it's custom
136                         Field field = _fieldArray[inRowIndex];
137                         if (!field.isBuiltIn())
138                                 field.setName(inValue.toString());
139                 }
140         }
141
142
143         /**
144          * Move the selected item up one place
145          * @param inIndex index of item to move
146          */
147         public void moveUp(int inIndex)
148         {
149                 if (inIndex > 0)
150                 {
151                         swapItems(inIndex-1, inIndex);
152                 }
153         }
154
155
156         /**
157          * Move the selected item down one place
158          * @param inIndex index of item to move
159          */
160         public void moveDown(int inIndex)
161         {
162                 if (inIndex > -1 && inIndex < (_numRows - 1))
163                 {
164                         swapItems(inIndex, inIndex+1);
165                 }
166         }
167
168
169         /**
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)
173          */
174         private void swapItems(int inIndex1, int inIndex2)
175         {
176                 Field temp = _fieldArray[inIndex1];
177                 _fieldArray[inIndex1] = _fieldArray[inIndex2];
178                 _fieldArray[inIndex2] = temp;
179                 fireTableRowsUpdated(inIndex1, inIndex2);
180         }
181
182
183         /**
184          * React to a requested change to one of the fields
185          * @param inRow row number of change
186          * @param inValue new string value
187          */
188         private void manageFieldChange(int inRow, String inValue)
189         {
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)
193                         return;
194                 if (inValue.equals(I18nManager.getText("fieldname.latitude"))
195                   || inValue.equals(I18nManager.getText("fieldname.longitude")))
196                         return;
197
198                 // Changes to custom field need to be handled differently
199                 boolean changeToCustom = inValue.equals(I18nManager.getText("fieldname.custom"));
200                 if (changeToCustom)
201                 {
202                         if (field.isBuiltIn())
203                         {
204                                 String customPrefix = I18nManager.getText("fieldname.prefix") + " ";
205                                 int index = inRow + 1;
206                                 while (hasField(customPrefix + index))
207                                         index++;
208                                 _fieldArray[inRow] = new Field(customPrefix + index);
209                         }
210                         // ignore custom to custom changes
211                 }
212                 else
213                 {
214                         // Change to a fixed field - check we've not already got it
215                         if (!hasField(inValue))
216                         {
217                                 // Change is ok - find new Field object corresponding to text
218                                 for (int i=0; i<Field.ALL_AVAILABLE_FIELDS.length; i++)
219                                         if (Field.ALL_AVAILABLE_FIELDS[i].getName().equals(inValue))
220                                                 _fieldArray[inRow] = Field.ALL_AVAILABLE_FIELDS[i];
221                         }
222                 }
223                 // fire change
224                 fireTableRowsUpdated(inRow, inRow);
225         }
226
227
228         /**
229          * @return array of Field objects
230          */
231         public Field[] getFieldArray()
232         {
233                 return _fieldArray;
234         }
235
236
237         /**
238          * @param inName Name of field to find
239          * @return true if this field is already present
240          */
241         private boolean hasField(String inName)
242         {
243                 if (_fieldArray == null || inName == null) return false;
244                 for (int i=0; i<_numRows; i++)
245                         if (_fieldArray[i].getName().equals(inName))
246                                 return true;
247                 return false;
248         }
249 }