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