1 package tim.prune.function.edit;
3 import javax.swing.table.AbstractTableModel;
5 import tim.prune.I18nManager;
8 * Class to hold table model information for edit dialog
10 public class EditFieldsTableModel extends AbstractTableModel
12 private String[] _fieldNames = null;
13 private String[] _originalValues = null;
14 private String[] _fieldValues = null;
15 private boolean[] _valueChanged = null;
19 * Constructor giving list size
20 * @param inSize number of fields
22 public EditFieldsTableModel(int inSize)
24 _fieldNames = new String[inSize];
25 _originalValues = new String[inSize];
26 _fieldValues = new String[inSize];
27 _valueChanged = new boolean[inSize];
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
37 public void addFieldInfo(String inName, String inValue, int inIndex)
39 _fieldNames[inIndex] = inName;
40 _originalValues[inIndex] = inValue;
41 _fieldValues[inIndex] = inValue;
42 _valueChanged[inIndex] = false;
47 * @see javax.swing.table.TableModel#getColumnCount()
49 public int getColumnCount()
56 * @see javax.swing.table.TableModel#getRowCount()
58 public int getRowCount()
60 return _fieldNames.length;
65 * @see javax.swing.table.TableModel#getValueAt(int, int)
67 public Object getValueAt(int inRowIndex, int inColumnIndex)
69 if (inColumnIndex == 0)
71 return _fieldNames[inRowIndex];
73 return _fieldValues[inRowIndex];
78 * @return true if cell is editable
80 public boolean isCellEditable(int inRowIndex, int inColumnIndex)
88 * Set the given cell value
89 * @see javax.swing.table.TableModel#setValueAt(java.lang.Object, int, int)
91 public void setValueAt(Object inValue, int inRowIndex, int inColumnIndex)
98 * @return Class of cell data
100 public Class<?> getColumnClass(int inColumnIndex)
102 if (inColumnIndex <= 1) return String.class;
103 return Boolean.class;
108 * Get the name of the column
110 public String getColumnName(int inColNum)
112 if (inColNum == 0) return I18nManager.getText("dialog.pointedit.table.field");
113 return I18nManager.getText("dialog.pointedit.table.value");
118 * Update the value of the given row
119 * @param inRowNum number of row, starting at 0
120 * @param inValue new value
122 public void updateValue(int inRowNum, String inValue)
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))
131 _fieldValues[inRowNum] = inValue;
132 fireTableRowsUpdated(inRowNum, inRowNum);
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
142 private static boolean areStringsDifferent(String inString1, String inString2)
144 // if both empty then same
145 if ((inString1 == null || inString1.equals("")) && (inString2 == null || inString2.equals("")))
149 return (inString1 == null || inString2 == null || !inString1.equals(inString2));
153 * Get the value at the given index
154 * @param inIndex index of field, starting at 0
155 * @return string value
157 public String getValue(int inIndex)
159 return _fieldValues[inIndex];
163 * Get the changed flag at the given index
164 * @param inIndex index of field, starting at 0
165 * @return true if field changed
167 public boolean getChanged(int inIndex)
169 return _valueChanged[inIndex];