/*
* UFO Saved Game Editor
* Copyright (C) 2010 Christopher Davoren
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.rubikscomplex.ufosge.gui.table;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.table.AbstractTableModel;
import net.rubikscomplex.ufosge.data.SavedGame;
import net.rubikscomplex.ufosge.data.Soldier;
/**
*
* @author Chris Davoren
*/
public class SoldierListTableModel extends AbstractTableModel {
protected SavedGame game;
protected ArrayList<Soldier> soldiersInList;
public static final int COLUMN_RANK = 0;
public static final int COLUMN_NAME = 1;
public static final int COLUMN_BASE = 2;
public static final int COLUMN_ARMOR = 3;
public SoldierListTableModel(SavedGame game) {
super();
this.game = game;
soldiersInList = new ArrayList<Soldier>();
updateList(false);
}
public void updateList(boolean fireChangedEvent) {
Iterator<Soldier> si = game.soldiers.iterator();
while(si.hasNext()) {
Soldier s = si.next();
if (s.isUsed()) {
soldiersInList.add(s);
}
}
if (fireChangedEvent) {
fireTableDataChanged();
}
}
public int getRowCount() {
return soldiersInList.size();
}
public int getColumnCount() {
return 4;
}
public Object getValueAt(int row, int column) {
Soldier s = soldiersInList.get(row);
switch(column) {
case COLUMN_RANK: return Soldier.rankToString(s.rank);
case COLUMN_NAME: return s.name;
case COLUMN_BASE:
if(s.hostBase != Soldier.HOST_BASE_TRANSFERRING) {
return game.bases.get(game.locations.get(s.hostBase).reference).name;
}
else {
return game.bases.get(game.locations.get(s.transferDestination).reference).name + " (transferring)";
}
case COLUMN_ARMOR: return Soldier.armorToString(s.armor);
default: return null;
}
}
public Soldier getSoldierAt(int row) {
return soldiersInList.get(row);
}
@Override
public String getColumnName(int column) {
switch(column) {
case COLUMN_RANK: return "Rank";
case COLUMN_NAME: return "Name";
case COLUMN_BASE: return "Base";
case COLUMN_ARMOR: return "Armor";
default: return "Unknown";
}
}
@Override
public Class<?> getColumnClass(int column) {
switch(column) {
case COLUMN_RANK: return String.class;
case COLUMN_NAME: return String.class;
case COLUMN_BASE: return String.class;
case COLUMN_ARMOR: return String.class;
default: return Object.class;
}
}
}