/*
* MegaMekLab - Copyright (C) 2009
*
* Original author - jtighe (torren@users.sourceforge.net)
*
* 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 2 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.
*/
package megameklab.com.ui.Vehicle.views;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Collections;
import java.util.Vector;
import javax.swing.BoxLayout;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import megamek.common.AmmoType;
import megamek.common.Entity;
import megamek.common.MiscType;
import megamek.common.Mounted;
import megamek.common.weapons.Weapon;
import megameklab.com.ui.EntitySource;
import megameklab.com.ui.Vehicle.tabs.BuildTab;
import megameklab.com.util.CriticalTableModel;
import megameklab.com.util.CriticalTransferHandler;
import megameklab.com.util.IView;
import megameklab.com.util.RefreshListener;
import megameklab.com.util.StringUtils;
import megameklab.com.util.UnitUtil;
public class BuildView extends IView implements ActionListener, MouseListener {
/**
*
*/
private static final long serialVersionUID = 799195356642563937L;
private JPanel mainPanel = new JPanel();
private CriticalTableModel equipmentList;
private Vector<Mounted> masterEquipmentList = new Vector<Mounted>(10, 1);
private JTable equipmentTable = new JTable();
private JScrollPane equipmentScroll = new JScrollPane();
CriticalTransferHandler cth;
public BuildView(EntitySource eSource, RefreshListener refresh) {
super(eSource);
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
equipmentList = new CriticalTableModel(getTank(), CriticalTableModel.BUILDTABLE);
equipmentTable.setModel(equipmentList);
equipmentTable.setDragEnabled(true);
cth = new CriticalTransferHandler(eSource, refresh);
equipmentTable.setTransferHandler(cth);
equipmentList.initColumnSizes(equipmentTable);
for (int i = 0; i < equipmentList.getColumnCount(); i++) {
equipmentTable.getColumnModel().getColumn(i).setCellRenderer(equipmentList.getRenderer());
}
equipmentTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
// equipmentScroll.setToolTipText("");
//equipmentScroll.setPreferredSize(new Dimension(getWidth(), getHeight()));
equipmentTable.setDoubleBuffered(true);
equipmentScroll.setViewportView(equipmentTable);
equipmentScroll.setTransferHandler(cth);
mainPanel.add(equipmentScroll);
equipmentTable.addMouseListener(this);
this.add(mainPanel);
// loadEquipmentTable();
}
public void addRefreshedListener(RefreshListener l) {
cth.addRefreshListener(l);
}
private void loadEquipmentTable() {
equipmentList.removeAllCrits();
masterEquipmentList.clear();
for (Mounted mount : getTank().getMisc()) {
if (mount.getLocation() == Entity.LOC_NONE) {
masterEquipmentList.add(mount);
}
}
for (Mounted mount : getTank().getWeaponList()) {
if (mount.getLocation() == Entity.LOC_NONE) {
masterEquipmentList.add(mount);
}
}
for (Mounted mount : getTank().getAmmo()) {
int ammoType = ((AmmoType)mount.getType()).getAmmoType();
if ((mount.getLocation() == Entity.LOC_NONE) &&
(mount.getUsableShotsLeft() > 1
|| ammoType == AmmoType.T_CRUISE_MISSILE )) {
masterEquipmentList.add(mount);
}
}
Collections.sort(masterEquipmentList, StringUtils.mountedComparator());
// Time to Sort
// HeatSinks first
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
if (UnitUtil.isHeatSink(masterEquipmentList.get(pos))) {
equipmentList.addCrit(masterEquipmentList.get(pos));
masterEquipmentList.remove(pos);
pos--;
}
}
// Jump Jets
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
if (UnitUtil.isJumpJet(masterEquipmentList.get(pos).getType())) {
equipmentList.addCrit(masterEquipmentList.get(pos));
masterEquipmentList.remove(pos);
pos--;
}
}
// weapons and ammo
Vector<Mounted> weaponsNAmmoList = new Vector<Mounted>(10, 1);
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
if ((masterEquipmentList.get(pos).getType() instanceof Weapon) || (masterEquipmentList.get(pos).getType() instanceof AmmoType)) {
weaponsNAmmoList.add(masterEquipmentList.get(pos));
masterEquipmentList.remove(pos);
pos--;
}
}
Collections.sort(weaponsNAmmoList, StringUtils.mountedComparator());
for (Mounted mount : weaponsNAmmoList) {
equipmentList.addCrit(mount);
}
// Equipment
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
if ((masterEquipmentList.get(pos).getType() instanceof MiscType) && UnitUtil.isArmor(masterEquipmentList.get(pos).getType())) {
equipmentList.addCrit(masterEquipmentList.get(pos));
masterEquipmentList.remove(pos);
pos--;
}
}
// structure
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
if ((masterEquipmentList.get(pos).getType() instanceof MiscType) && masterEquipmentList.get(pos).getType().hasFlag(MiscType.F_ENDO_STEEL)) {
equipmentList.addCrit(masterEquipmentList.get(pos));
masterEquipmentList.remove(pos);
pos--;
}
}
// armor
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
if (UnitUtil.isArmor(masterEquipmentList.get(pos).getType())) {
equipmentList.addCrit(masterEquipmentList.get(pos));
masterEquipmentList.remove(pos);
pos--;
}
}
// everything else
for (int pos = 0; pos < masterEquipmentList.size(); pos++) {
equipmentList.addCrit(masterEquipmentList.get(pos));
}
}
public void refresh() {
removeAllListeners();
loadEquipmentTable();
fireTableRefresh();
addAllListeners();
}
private void removeAllListeners() {
}
private void addAllListeners() {
}
public void actionPerformed(ActionEvent e) {
fireTableRefresh();
}
private void fireTableRefresh() {
equipmentList.updateUnit(getTank());
equipmentList.refreshModel();
//equipmentScroll.setPreferredSize(new Dimension(getWidth() * 90 / 100, getHeight() * 90 / 100));
//equipmentScroll.repaint();
}
public CriticalTableModel getTableModel() {
return equipmentList;
}
public JTable getTable() {
return equipmentTable;
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
if (e.getButton() == MouseEvent.BUTTON3) {
JPopupMenu popup = new JPopupMenu();
JMenuItem item;
final int selectedRow = equipmentTable.rowAtPoint(e.getPoint());
Mounted mount = (Mounted)equipmentTable.getModel().getValueAt(selectedRow, CriticalTableModel.EQUIPMENT);
String[] locations;
locations = getTank().getLocationNames();
for (int location = 0; location < getTank().locations(); location++) {
if (UnitUtil.isValidLocation(getTank(), mount.getType(), location)) {
item = new JMenuItem("Add to " + locations[location]);
final int loc = location;
item.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
jMenuLoadComponent_actionPerformed(loc, selectedRow);
}
});
popup.add(item);
}
}
popup.show(this, e.getX(), e.getY());
}
}
public void mouseReleased(MouseEvent e) {
}
private void jMenuLoadComponent_actionPerformed(int location, int selectedRow) {
Mounted eq = (Mounted)equipmentTable.getModel().getValueAt(selectedRow, CriticalTableModel.EQUIPMENT);
UnitUtil.changeMountStatus(getTank(), eq, location, -1, false);
try {
getTank().addEquipment(eq, location, false);
} catch (Exception ex) {
ex.printStackTrace();
}
// go back up to grandparent build tab and fire a full refresh.
((BuildTab) getParent().getParent()).refreshAll();
}
}