]> gitweb.fperrin.net Git - GpsPrune.git/blob - src/tim/prune/function/edit/EditFieldsTableModel.java
Moved source into separate src directory due to popular request
[GpsPrune.git] / src / tim / prune / function / edit / EditFieldsTableModel.java
1 package tim.prune.function.edit;
2
3 import javax.swing.table.AbstractTableModel;
4
5 import tim.prune.I18nManager;
6
7 /**
8  * Class to hold table model information for edit dialog
9  */
10 public class EditFieldsTableModel extends AbstractTableModel
11 {
12         private String[] _fieldNames = null;
13         private String[] _originalValues = null;
14         private String[] _fieldValues = null;
15         private boolean[] _valueChanged = null;
16
17
18         /**
19          * Constructor giving list size
20          * @param inSize number of fields
21          */
22         public EditFieldsTableModel(int inSize)
23         {
24                 _fieldNames     = new String[inSize];
25                 _originalValues = new String[inSize];
26                 _fieldValues    = new String[inSize];
27                 _valueChanged   = new boolean[inSize];
28         }
29
30
31         /**
32          * Set the given data in the array
33          * @param inName field name
34          * @param inValue field value
35          * @param inIndex index to place in array
36          */
37         public void addFieldInfo(String inName, String inValue, int inIndex)
38         {
39                 _fieldNames[inIndex] = inName;
40                 _originalValues[inIndex] = inValue;
41                 _fieldValues[inIndex] = inValue;
42                 _valueChanged[inIndex] = false;
43         }
44
45
46         /**
47          * @see javax.swing.table.TableModel#getColumnCount()
48          */
49         public int getColumnCount()
50         {
51                 return 2;
52         }
53
54
55         /**
56          * @see javax.swing.table.TableModel#getRowCount()
57          */
58         public int getRowCount()
59         {
60                 return _fieldNames.length;
61         }
62
63
64         /**
65          * @see javax.swing.table.TableModel#getValueAt(int, int)
66          */
67         public Object getValueAt(int inRowIndex, int inColumnIndex)
68         {
69                 if (inColumnIndex == 0)
70                 {
71                         return _fieldNames[inRowIndex];
72                 }
73                 return _fieldValues[inRowIndex];
74         }
75
76
77         /**
78          * @return true if cell is editable
79          */
80         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
81         {
82                 // no
83                 return false;
84         }
85
86
87         /**
88          * Set the given cell value
89          * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
90          */
91         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
92         {
93                 // ignore edits
94         }
95
96
97         /**
98          * @return Class of cell data
99          */
100         public Class<?> getColumnClass(int inColumnIndex)
101         {
102                 if (inColumnIndex <= 1) return String.class;
103                 return Boolean.class;
104         }
105
106
107         /**
108          * Get the name of the column
109          */
110         public String getColumnName(int inColNum)
111         {
112                 if (inColNum == 0) return I18nManager.getText("dialog.pointedit.table.field");
113                 return I18nManager.getText("dialog.pointedit.table.value");
114         }
115
116
117         /**
118          * Update the value of the given row
119          * @param inRowNum number of row, starting at 0
120          * @param inValue new value
121          */
122         public void updateValue(int inRowNum, String inValue)
123         {
124                 String origValue = _originalValues[inRowNum];
125                 String currValue = _fieldValues[inRowNum];
126                 // Update model if changed from original value
127                 _valueChanged[inRowNum] = areStringsDifferent(origValue, inValue);
128                 // Update model if changed from current value
129                 if (areStringsDifferent(currValue, inValue))
130                 {
131                         _fieldValues[inRowNum] = inValue;
132                         fireTableRowsUpdated(inRowNum, inRowNum);
133                 }
134         }
135
136         /**
137          * Compare two strings to see if they're equal or not (nulls treated the same as empty strings)
138          * @param inString1 first string
139          * @param inString2 second string
140          * @return true if the strings are different
141          */
142         private static boolean areStringsDifferent(String inString1, String inString2)
143         {
144                 // if both empty then same
145                 if ((inString1 == null || inString1.equals("")) && (inString2 == null || inString2.equals("")))
146                 {
147                         return false;
148                 }
149                 return (inString1 == null || inString2 == null || !inString1.equals(inString2));
150         }
151
152         /**
153          * Get the value at the given index
154          * @param inIndex index of field, starting at 0
155          * @return string value
156          */
157         public String getValue(int inIndex)
158         {
159                 return _fieldValues[inIndex];
160         }
161
162         /**
163          * Get the changed flag at the given index
164          * @param inIndex index of field, starting at 0
165          * @return true if field changed
166          */
167         public boolean getChanged(int inIndex)
168         {
169                 return _valueChanged[inIndex];
170         }
171 }