]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/edit/EditFieldsTableModel.java
575b0aa9ba90e2161ab91840e3b2a8204c918c58
[GpsPrune.git] / tim / prune / edit / EditFieldsTableModel.java
1 package tim.prune.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[] _fieldValues = null;
14         private boolean[] _valueChanged = null;
15
16
17         /**
18          * Constructor giving list size
19          */
20         public EditFieldsTableModel(int inSize)
21         {
22                 _fieldNames = new String[inSize];
23                 _fieldValues = new String[inSize];
24                 _valueChanged = new boolean[inSize];
25         }
26
27
28         /**
29          * Set the given data in the array
30          * @param inName field name
31          * @param inValue field value
32          * @param inIndex index to place in array
33          */
34         public void addFieldInfo(String inName, String inValue, int inIndex)
35         {
36                 _fieldNames[inIndex] = inName;
37                 _fieldValues[inIndex] = inValue;
38                 _valueChanged[inIndex] = false;
39         }
40
41
42         /**
43          * @see javax.swing.table.TableModel#getColumnCount()
44          */
45         public int getColumnCount()
46         {
47                 return 3;
48         }
49
50
51         /**
52          * @see javax.swing.table.TableModel#getRowCount()
53          */
54         public int getRowCount()
55         {
56                 return _fieldNames.length;
57         }
58
59
60         /**
61          * @see javax.swing.table.TableModel#getValueAt(int, int)
62          */
63         public Object getValueAt(int inRowIndex, int inColumnIndex)
64         {
65                 if (inColumnIndex == 0)
66                 {
67                         return _fieldNames[inRowIndex];
68                 }
69                 else if (inColumnIndex == 1)
70                 {
71                         return _fieldValues[inRowIndex];
72                 }
73                 return new Boolean(_valueChanged[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                 else if (inColNum == 1) return I18nManager.getText("dialog.pointedit.table.value");
114                 return I18nManager.getText("dialog.pointedit.table.changed");
115         }
116
117
118         /**
119          * Update the value of the given row
120          * @param inRowNum number of row, starting at 0
121          * @param inValue new value
122          * @return true if data updated
123          */
124         public boolean updateValue(int inRowNum, String inValue)
125         {
126                 String currValue = _fieldValues[inRowNum];
127                 // ignore empty-to-empty changes
128                 if ((currValue == null || currValue.equals("")) && (inValue == null || inValue.equals("")))
129                 {
130                         return false;
131                 }
132                 // ignore changes when strings equal
133                 if (currValue == null || inValue == null || !currValue.equals(inValue))
134                 {
135                         // really changed
136                         _fieldValues[inRowNum] = inValue;
137                         _valueChanged[inRowNum] = true;
138                         fireTableRowsUpdated(inRowNum, inRowNum);
139                         return true;
140                 }
141                 return false;
142         }
143
144
145         /**
146          * Get the value at the given index
147          * @param inIndex index of field, starting at 0
148          * @return string value
149          */
150         public String getValue(int inIndex)
151         {
152                 return _fieldValues[inIndex];
153         }
154
155         /**
156          * Get the changed flag at the given index
157          * @param inIndex index of field, starting at 0
158          * @return true if field changed
159          */
160         public boolean getChanged(int inIndex)
161         {
162                 return _valueChanged[inIndex];
163         }
164 }