]> gitweb.fperrin.net Git - GpsPrune.git/blob - tim/prune/edit/EditFieldsTableModel.java
Version 4, January 2008
[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          * @param inSize number of fields
20          */
21         public EditFieldsTableModel(int inSize)
22         {
23                 _fieldNames = new String[inSize];
24                 _fieldValues = new String[inSize];
25                 _valueChanged = new boolean[inSize];
26         }
27
28
29         /**
30          * Set the given data in the array
31          * @param inName field name
32          * @param inValue field value
33          * @param inIndex index to place in array
34          */
35         public void addFieldInfo(String inName, String inValue, int inIndex)
36         {
37                 _fieldNames[inIndex] = inName;
38                 _fieldValues[inIndex] = inValue;
39                 _valueChanged[inIndex] = false;
40         }
41
42
43         /**
44          * @see javax.swing.table.TableModel#getColumnCount()
45          */
46         public int getColumnCount()
47         {
48                 return 3;
49         }
50
51
52         /**
53          * @see javax.swing.table.TableModel#getRowCount()
54          */
55         public int getRowCount()
56         {
57                 return _fieldNames.length;
58         }
59
60
61         /**
62          * @see javax.swing.table.TableModel#getValueAt(int, int)
63          */
64         public Object getValueAt(int inRowIndex, int inColumnIndex)
65         {
66                 if (inColumnIndex == 0)
67                 {
68                         return _fieldNames[inRowIndex];
69                 }
70                 else if (inColumnIndex == 1)
71                 {
72                         return _fieldValues[inRowIndex];
73                 }
74                 return Boolean.valueOf(_valueChanged[inRowIndex]);
75         }
76
77
78         /**
79          * @return true if cell is editable
80          */
81         public boolean isCellEditable(int inRowIndex, int inColumnIndex)
82         {
83                 // no
84                 return false;
85         }
86
87
88         /**
89          * Set the given cell value
90          * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
91          */
92         public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
93         {
94                 // ignore edits
95         }
96
97
98         /**
99          * @return Class of cell data
100          */
101         public Class getColumnClass(int inColumnIndex)
102         {
103                 if (inColumnIndex <= 1) return String.class;
104                 return Boolean.class;
105         }
106
107
108         /**
109          * Get the name of the column
110          */
111         public String getColumnName(int inColNum)
112         {
113                 if (inColNum == 0) return I18nManager.getText("dialog.pointedit.table.field");
114                 else if (inColNum == 1) return I18nManager.getText("dialog.pointedit.table.value");
115                 return I18nManager.getText("dialog.pointedit.table.changed");
116         }
117
118
119         /**
120          * Update the value of the given row
121          * @param inRowNum number of row, starting at 0
122          * @param inValue new value
123          * @return true if data updated
124          */
125         public boolean updateValue(int inRowNum, String inValue)
126         {
127                 String currValue = _fieldValues[inRowNum];
128                 // ignore empty-to-empty changes
129                 if ((currValue == null || currValue.equals("")) && (inValue == null || inValue.equals("")))
130                 {
131                         return false;
132                 }
133                 // ignore changes when strings equal
134                 if (currValue == null || inValue == null || !currValue.equals(inValue))
135                 {
136                         // really changed
137                         _fieldValues[inRowNum] = inValue;
138                         _valueChanged[inRowNum] = true;
139                         fireTableRowsUpdated(inRowNum, inRowNum);
140                         return true;
141                 }
142                 return false;
143         }
144
145
146         /**
147          * Get the value at the given index
148          * @param inIndex index of field, starting at 0
149          * @return string value
150          */
151         public String getValue(int inIndex)
152         {
153                 return _fieldValues[inIndex];
154         }
155
156         /**
157          * Get the changed flag at the given index
158          * @param inIndex index of field, starting at 0
159          * @return true if field changed
160          */
161         public boolean getChanged(int inIndex)
162         {
163                 return _valueChanged[inIndex];
164         }
165 }