1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.sf.magicproject.ui;
20
21 import javax.swing.table.AbstractTableModel;
22
23 import net.sf.magicproject.tools.MCardCompare;
24
25 /***
26 * @author <a href="mailto:fabdouglas@users.sourceforge.net">Fabrice Daugan </a>
27 * @since 0.94 create a TableModel with a list of MCardCompare
28 */
29 public class MCardTableModel extends AbstractTableModel {
30
31 private MListModel<MCardCompare> mCardList;
32
33 /***
34 * Create a new TableModel with an MCardCompare list as source for cells.
35 *
36 * @param mCardList
37 */
38 public MCardTableModel(MListModel<MCardCompare> mCardList) {
39 this.mCardList = mCardList;
40 }
41
42 /***
43 * number of columns of the table
44 *
45 * @return 2 columns
46 */
47 public int getColumnCount() {
48 return 2;
49 }
50
51 /***
52 * number of rowns of the table
53 *
54 * @return number of cards in deck
55 */
56 public int getRowCount() {
57 return mCardList.getSize();
58 }
59
60 /***
61 * only the quantities column may be edited
62 */
63 @Override
64 public boolean isCellEditable(int rowIndex, int columnIndex) {
65 if (columnIndex == 0)
66 return false;
67 return true;
68 }
69
70 /***
71 * get the value in cell [rowIndex; columnIndex]
72 *
73 * @param rowIndex
74 * @param columnIndex
75 * @return cell value
76 */
77 public Object getValueAt(int rowIndex, int columnIndex) {
78 MCardCompare card = mCardList.elementAt(rowIndex);
79 if (columnIndex == 0)
80 return card.getName();
81 return card.getAmount();
82 }
83
84 /***
85 * set value at cell [rowIndex; columnIndex]
86 */
87 @Override
88 public void setValueAt(Object aValue, int rowIndex, int columnIndex) {
89 int i = Integer.parseInt(aValue.toString());
90 mCardList.elementAt(rowIndex).setAmount(i);
91
92 if (i == 0) {
93 fireTableCellUpdated(rowIndex, columnIndex);
94 mCardList.remove(rowIndex);
95 return;
96 }
97 fireTableCellUpdated(rowIndex, columnIndex);
98 }
99
100 /***
101 * @return mCardList -> list of cards
102 */
103 public MListModel<MCardCompare> getCards() {
104 return mCardList;
105 }
106 }