| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
package ca.spaz.cron.targets; |
| 5 |
|
|
| 6 |
|
import java.util.List; |
| 7 |
|
|
| 8 |
|
import javax.swing.table.AbstractTableModel; |
| 9 |
|
|
| 10 |
|
import ca.spaz.cron.database.NutrientInfo; |
| 11 |
|
import ca.spaz.cron.user.User; |
| 12 |
|
|
| 13 |
|
public class TargetEditorTableModel extends AbstractTableModel { |
| 14 |
|
|
| 15 |
|
private static final int NAME_COLUMN = 0; |
| 16 |
|
private static final int TARGET_MIN = 1; |
| 17 |
|
private static final int TARGET_MAX = 2; |
| 18 |
|
private static final int UNIT_COLUMN = 3; |
| 19 |
|
|
| 20 |
0 |
private String[] columnNames = { "Nutrient", "Minimum", "Maximum", "Units"}; |
| 21 |
|
List nutrients; |
| 22 |
|
User user; |
| 23 |
|
|
| 24 |
|
|
| 25 |
|
|
| 26 |
|
|
| 27 |
|
|
| 28 |
0 |
public TargetEditorTableModel(User user, List nutrientInfo) { |
| 29 |
0 |
this.user = user; |
| 30 |
0 |
this.nutrients = nutrientInfo; |
| 31 |
0 |
} |
| 32 |
|
|
| 33 |
|
public Class getColumnClass(int col) { |
| 34 |
0 |
Object o = getValueAt(0, col); |
| 35 |
0 |
if (o != null) { |
| 36 |
0 |
return o.getClass(); |
| 37 |
|
} |
| 38 |
0 |
return String.class; |
| 39 |
|
} |
| 40 |
|
|
| 41 |
|
public int getColumnCount() { |
| 42 |
0 |
return columnNames.length; |
| 43 |
|
} |
| 44 |
|
|
| 45 |
|
public String getColumnName(int col) { |
| 46 |
0 |
return columnNames[col].toString(); |
| 47 |
|
} |
| 48 |
|
|
| 49 |
|
public NutrientInfo getNutrientInfo(int i) { |
| 50 |
0 |
return (NutrientInfo) nutrients.get(i); |
| 51 |
|
} |
| 52 |
|
|
| 53 |
|
public int getRowCount() { |
| 54 |
0 |
return nutrients.size(); |
| 55 |
|
} |
| 56 |
|
|
| 57 |
|
public Object getValueAt(int row, class="keyword">int col) { |
| 58 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
| 59 |
0 |
if (ni != null) { |
| 60 |
0 |
Target target = user.getTarget(ni); |
| 61 |
0 |
switch (col) { |
| 62 |
|
case NAME_COLUMN: |
| 63 |
0 |
return ni.getName(); |
| 64 |
|
case TARGET_MIN: |
| 65 |
0 |
return new Double(target.getMin()); |
| 66 |
|
|
| 67 |
|
case TARGET_MAX: |
| 68 |
0 |
return new Double(target.getMax()); |
| 69 |
|
|
| 70 |
|
case UNIT_COLUMN: |
| 71 |
0 |
return ni.getUnits(); |
| 72 |
|
} |
| 73 |
|
} |
| 74 |
0 |
return ""; |
| 75 |
|
} |
| 76 |
|
|
| 77 |
|
public boolean isCellEditable(int row, class="keyword">int col) { |
| 78 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
| 79 |
0 |
if (ni != null) { |
| 80 |
0 |
if (col == TARGET_MIN || col == TARGET_MAX) { |
| 81 |
0 |
return true; |
| 82 |
|
} |
| 83 |
|
} |
| 84 |
0 |
return false; |
| 85 |
|
} |
| 86 |
|
|
| 87 |
|
public void setValueAt(Object value, int row, class="keyword">int col) { |
| 88 |
0 |
if (col == TARGET_MIN && value != null) { |
| 89 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
| 90 |
0 |
if (ni != null) { |
| 91 |
0 |
double val = ((Double) value).class="keyword">doubleValue(); |
| 92 |
0 |
Target target = user.getTarget(ni); |
| 93 |
0 |
target.setMin(val); |
| 94 |
0 |
user.setTarget(ni, target); |
| 95 |
0 |
fireTableCellUpdated(row, col); |
| 96 |
|
} |
| 97 |
|
} |
| 98 |
|
|
| 99 |
0 |
if (col == TARGET_MAX && value != null) { |
| 100 |
0 |
NutrientInfo ni = getNutrientInfo(row); |
| 101 |
0 |
if (ni != null) { |
| 102 |
0 |
double val = ((Double) value).class="keyword">doubleValue(); |
| 103 |
0 |
Target target = user.getTarget(ni); |
| 104 |
0 |
target.setMax(val); |
| 105 |
0 |
user.setTarget(ni, target); |
| 106 |
0 |
fireTableCellUpdated(row, col); |
| 107 |
|
} |
| 108 |
|
} |
| 109 |
0 |
} |
| 110 |
|
|
| 111 |
|
} |