View Javadoc

1   /*
2    *   Magic-Project is a turn based strategy simulator
3    *   Copyright (C) 2003-2007 Fabrice Daugan
4    *
5    *   This program is free software; you can redistribute it and/or modify it 
6    * under the terms of the GNU General Public License as published by the Free 
7    * Software Foundation; either version 2 of the License, or (at your option) any
8    * later version.
9    *
10   *   This program is distributed in the hope that it will be useful, but WITHOUT 
11   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12   * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more 
13   * details.
14   *
15   *   You should have received a copy of the GNU General Public License along  
16   * with this program; if not, write to the Free Software Foundation, Inc., 
17   * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
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 }