/*
* 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.Craft;
import net.rubikscomplex.ufosge.data.Location;
import net.rubikscomplex.ufosge.data.SavedGame;
/**
*
* @author Chris Davoren
*/
public class CraftTableModel extends AbstractTableModel {
public static final int COLUMN_NAME = 0;
public static final int COLUMN_TYPE = 1;
public static final int COLUMN_BASE = 2;
public static final int COLUMN_STATUS = 3;
public static final int COLUMN_DAMAGE = 4;
public static final int COLUMN_LEFT_WEAPON = 5;
public static final int COLUMN_LEFT_AMMO = 6;
public static final int COLUMN_RIGHT_WEAPON = 7;
public static final int COLUMN_RIGHT_AMMO = 8;
public static final int COLUMN_FUEL = 9;
protected ArrayList<Craft> cache;
protected SavedGame game;
public CraftTableModel(SavedGame game) {
super();
this.game = game;
updateCache();
}
public void updateCache() {
cache = new ArrayList<Craft>();
Iterator<Craft> ci = game.crafts.iterator();
while (ci.hasNext()) {
Craft c = ci.next();
if (c.isHuman()) {
cache.add(new Craft(c));
}
}
}
public Object getValueAt(int row, int column) {
switch(column) {
case COLUMN_NAME:
//System.out.println("Row: " + row + ", Column: " + column + ", locationIndex: " + Location.getLocationForCraft(game, cache.get(row).index).index);
return Craft.typeToString(cache.get(row).type) + "-" + Location.getLocationForCraft(game, cache.get(row).index).countSuffix;
case COLUMN_TYPE: return Craft.typeToString(cache.get(row).type);
case COLUMN_BASE: return game.bases.get(game.locations.get(cache.get(row).baseIndex).reference).name;
case COLUMN_STATUS: return Craft.statusToString(cache.get(row).status);
case COLUMN_DAMAGE: return cache.get(row).damage;
case COLUMN_LEFT_WEAPON: return Craft.weaponTypeToString(cache.get(row).leftType);
case COLUMN_LEFT_AMMO: return cache.get(row).leftAmmo;
case COLUMN_RIGHT_WEAPON: return Craft.weaponTypeToString(cache.get(row).rightType);
case COLUMN_RIGHT_AMMO: return cache.get(row).rightAmmo;
case COLUMN_FUEL: return cache.get(row).fuel;
default: return null;
}
}
@Override
public Class<?> getColumnClass(int column) {
switch(column) {
case COLUMN_NAME: return String.class;
case COLUMN_TYPE: return Byte.class;
case COLUMN_BASE: return String.class;
case COLUMN_STATUS: return String.class;
case COLUMN_DAMAGE: return Short.class;
case COLUMN_LEFT_WEAPON: return String.class;
case COLUMN_LEFT_AMMO: return Short.class;
case COLUMN_RIGHT_WEAPON: return String.class;
case COLUMN_RIGHT_AMMO: return Short.class;
case COLUMN_FUEL: return Short.class;
default: return Object.class;
}
}
@Override
public String getColumnName(int column) {
switch(column) {
case COLUMN_NAME: return "Name";
case COLUMN_TYPE: return "Type";
case COLUMN_BASE: return "Base";
case COLUMN_STATUS: return "Status";
case COLUMN_DAMAGE: return "Damage";
case COLUMN_LEFT_WEAPON: return "Left Weapon";
case COLUMN_LEFT_AMMO: return "Left Ammo";
case COLUMN_RIGHT_WEAPON: return "Right Weapon";
case COLUMN_RIGHT_AMMO: return "Right Ammo";
case COLUMN_FUEL: return "Fuel";
default: return "Unknown";
}
}
@Override
public void setValueAt(Object value, int row, int column) {
// System.out.println("setValueAt: (" + row + ", " + column + ")");
Craft c = cache.get(row);
switch (column) {
case COLUMN_TYPE:
double dmgProportion = (c.damage * 1.0) / Craft.MAX_DAMAGE_LIST[c.type];
double fuelProportion = (c.fuel * 1.0) / Craft.MAX_FUEL_LIST[c.type];
c.type = Craft.stringToType(value.toString());
c.damage = (short)(Craft.MAX_DAMAGE_LIST[c.type] * dmgProportion);
c.fuel = (short)(Craft.MAX_FUEL_LIST[c.type] * fuelProportion);
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
case COLUMN_DAMAGE:
c.damage = ((Short)value).shortValue();
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
case COLUMN_LEFT_WEAPON:
c.leftType = Craft.stringToWeaponType(value.toString());
c.setMaxAmmo(true);
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
case COLUMN_LEFT_AMMO:
c.leftAmmo = ((Short)value).shortValue();
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
case COLUMN_RIGHT_WEAPON:
c.rightType = Craft.stringToWeaponType(value.toString());
c.setMaxAmmo(false);
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
case COLUMN_RIGHT_AMMO:
c.rightAmmo = ((Short)value).shortValue();
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
case COLUMN_STATUS:
short status = Craft.stringToStatus(value.toString());
if (status == Craft.STATUS_READY) {
c.setMaxAmmo();
c.setMaxFuel();
c.damage = 0;
}
else if (status == Craft.STATUS_REARMING) {
c.damage = 0;
c.setMaxFuel();
}
else if (status == Craft.STATUS_REFUELING) {
c.damage = 0;
}
c.status = status;
fireTableRowsUpdated(row, row);
break;
case COLUMN_FUEL:
c.fuel = ((Short)value).shortValue();
c.setCorrectStatus();
fireTableRowsUpdated(row, row);
break;
default: return;
}
}
public int getColumnCount() {
return 10;
}
public int getRowCount() {
return cache.size();
}
public void update() {
Iterator<Craft> ci = cache.iterator();
while(ci.hasNext()) {
Craft c = ci.next();
game.crafts.get(c.index).copy(c);
}
}
@Override
public boolean isCellEditable(int row, int column) {
return (column != COLUMN_NAME && column != COLUMN_BASE);
}
}