Skip to content

Commit

Permalink
Merge pull request #2093 from Windchild292/dev_Windchild_LootTableModel
Browse files Browse the repository at this point in the history
Loot Table: Fixing Money Capitalization and Adding Basic Unit/Part Tooltips
  • Loading branch information
Windchild292 authored Oct 4, 2020
2 parents 67c76b3 + 269787d commit 9f12201
Showing 1 changed file with 81 additions and 73 deletions.
154 changes: 81 additions & 73 deletions MekHQ/src/mekhq/gui/model/LootTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* LootTableModel.java
*
* Copyright (c) 2009 Jay Lawson <jaylawson39 at yahoo.com>. All rights reserved.
* Copyright (c) 2020 - The MegaMek Team. All Rights Reserved.
*
* This file is part of MekHQ.
*
Expand All @@ -12,60 +13,64 @@
*
* MekHQ 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
* 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 MekHQ. If not, see <http://www.gnu.org/licenses/>.
* along with MekHQ. If not, see <http://www.gnu.org/licenses/>.
*/
package mekhq.gui.model;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;

import megamek.common.Entity;
import mekhq.campaign.mission.Loot;
import mekhq.campaign.parts.Part;

/**
* A table model for displaying loot for scenarios and missions
*/
/**
* A table model for displaying loot for scenarios and missions
*/
public class LootTableModel extends AbstractTableModel {
/**
*
*/
//region Variable Declarations
private static final long serialVersionUID = -58915479895694545L;
protected String[] columnNames;
protected ArrayList<Loot> data;
protected List<Loot> data;

public final static int COL_NAME = 0;
public final static int COL_MONEY = 1;
public final static int COL_MECHS = 2;
public final static int COL_PARTS = 3;
public final static int N_COL = 4;
//endregion Variable Declarations

public LootTableModel(ArrayList<Loot> entries) {
public LootTableModel(List<Loot> entries) {
data = entries;
}

@Override
public int getRowCount() {
return data.size();
}

@Override
public int getColumnCount() {
return N_COL;
}

@Override
public String getColumnName(int column) {
switch(column) {
switch (column) {
case COL_NAME:
return "Name";
case COL_MONEY:
return "money";
return "Money";
case COL_MECHS:
return "# Units";
case COL_PARTS:
Expand All @@ -75,26 +80,27 @@ public String getColumnName(int column) {
}
}

@Override
public Object getValueAt(int row, int col) {
Loot loot;
if(data.isEmpty()) {
if (data.isEmpty()) {
return "";
} else {
loot = getLootAt(row);
}
if(col == COL_NAME) {
return loot.getName();
}
if(col == COL_MONEY) {
return loot.getCash().toAmountAndSymbolString();
}
if(col == COL_MECHS) {
return loot.getUnits().size();
}
if(col == COL_PARTS) {
return loot.getParts().size();

switch (col) {
case COL_NAME:
return loot.getName();
case COL_MONEY:
return loot.getCash().toAmountAndSymbolString();
case COL_MECHS:
return loot.getUnits().size();
case COL_PARTS:
return loot.getParts().size();
default:
return "?";
}
return "?";
}

@Override
Expand All @@ -103,66 +109,68 @@ public boolean isCellEditable(int row, int col) {
}

@Override
public Class<? extends Object> getColumnClass(int c) {
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}

public Loot getLootAt(int row) {
return (Loot) data.get(row);
return data.get(row);
}

public void addLoot(Loot loot) {
data.add(loot);
fireTableDataChanged();
}

public ArrayList<Loot> getAllLoot() {
public List<Loot> getAllLoot() {
return data;
}

public int getColumnWidth(int c) {
switch(c) {
case COL_MONEY:
case COL_NAME:
return 100;
default:
return 20;
}
}

public int getAlignment(int col) {
return SwingConstants.LEFT;
}

public String getTooltip(int row, int col) {
switch(col) {
default:
return null;
}
}

//fill table with values
public void setData(ArrayList<Loot> loot) {
data = loot;
fireTableDataChanged();
}

public LootTableModel.Renderer getRenderer() {
return new LootTableModel.Renderer();
}

public class Renderer extends DefaultTableCellRenderer {

/**
*
*/
private static final long serialVersionUID = -2888173457152182907L;

public Component getTableCellRendererComponent(JTable table,
Object value, boolean isSelected, boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected,
hasFocus, row, column);
public int getColumnWidth(int c) {
switch (c) {
case COL_MONEY:
case COL_NAME:
return 100;
default:
return 20;
}
}

public int getAlignment(int col) {
return SwingConstants.LEFT;
}

public String getTooltip(int row, int col) {
switch (col) {
case COL_MECHS:
return getLootAt(row).getUnits().stream().map(Entity::getDisplayName)
.collect(Collectors.joining(", "));
case COL_PARTS:
return getLootAt(row).getParts().stream().map(Part::getPartName)
.collect(Collectors.joining(", "));
default:
return null;
}
}

//fill table with values
public void setData(List<Loot> loot) {
data = loot;
fireTableDataChanged();
}

public LootTableModel.Renderer getRenderer() {
return new LootTableModel.Renderer();
}

public class Renderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = -2888173457152182907L;

@Override
public Component getTableCellRendererComponent(JTable table, Object value,
boolean isSelected, boolean hasFocus,
int row, int column) {
super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
setOpaque(true);
int actualCol = table.convertColumnIndexToModel(column);
int actualRow = table.convertRowIndexToModel(row);
Expand All @@ -171,5 +179,5 @@ public Component getTableCellRendererComponent(JTable table,

return this;
}
}
}
}

0 comments on commit 9f12201

Please sign in to comment.