<commit_before>/**
 * 
 */
package wota.graphics;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

import java.awt.*;
import java.util.Map;
import java.util.Vector;

import wota.gamemaster.StatisticsLogger;
import wota.gameobjects.*;
import wota.gameobjects.GameWorld.Player;

/**
 *  View class for the statistics.
 *  Draws an JTable and puts in StatisticsView.StatisticsTableModel.
 */
public class StatisticsView implements Runnable {
	
	public JFrame frame;
	private GameWorld gameWorld;
	private StatisticsLogger logger;
	private StatisticsTableModel statisticsTableModel;
	
	public StatisticsView(GameWorld gameWorld, StatisticsLogger logger) {
		this.gameWorld = gameWorld;
		this.logger    = logger;
	}
	
	public void run() {
		frame = new JFrame("Wota Statistics");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new FlowLayout());

		statisticsTableModel = new StatisticsTableModel(logger);
		JTable table = new JTable(statisticsTableModel);
		table.setDefaultRenderer(Object.class, new CellRenderer());
		
	//	table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);
		
		JScrollPane scrollPane = new JScrollPane(table);
		frame.add(scrollPane);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	/**
	 * The StatisticTableModel gets passed to the JTable.
	 * It implements basic methods like getValueAt() which 
	 * are used by the JTable to obtain the cell values.
	 */
	public class StatisticsTableModel extends AbstractTableModel {
		
		private StatisticsLogger          logger;
		private final Vector<String>      playerNames;
		private final String[]			  rowNames;
		
		public StatisticsTableModel(StatisticsLogger logger) {
			this.logger = logger;
			playerNames = new Vector<String>();
			for (Player player : gameWorld.getPlayers()) {
				playerNames.add(player.name);
			}
			rowNames = new String[] {
					"", "# Ants", "# Gatherer", "# Soldiers", "# Scouts",
					"# created ants", "# lost ants",
					"collected food"
			};
		}
		
		@Override
		public int getRowCount() {
			return rowNames.length;
		}

		@Override
		public int getColumnCount() {
			return playerNames.size() + 1;
		}

		@Override
		public String getColumnName(int column) {
			if (column == 0) {
				return "";
			}
			return playerNames.get(column - 1);
		}
		
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			if (columnIndex == 0) {
				return rowNames[rowIndex];
			}
			int playerId = columnIndex - 1;
			Player player = gameWorld.getPlayers().get(playerId);
			switch(rowIndex) {
			case 0:
				return GameView.playerColors[player.id()];
			case 1:
				return player.antObjects.size();
			case 2:
				return player.numAnts(Caste.Gatherer);
			case 3:
				return player.numAnts(Caste.Soldier);
			case 4:
				return player.numAnts(Caste.Scout);
			case 5:
				return logger.createdAnts()[playerId];
			case 6:
				return logger.diedAnts()[playerId];
			case 7:
				return logger.collectedFood()[playerId];
			default:
				return null;
			}
		}
		
	}

	/** call this when the table should grep the information */
	public void refresh() {
		statisticsTableModel.fireTableDataChanged(); // tells the JTable to update graphics 
	}
	
	public class CellRenderer extends JLabel
							   implements TableCellRenderer {
		DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();<commit_msg>statistics buffers data in  	Object[][] data.  fixes #117. <commit_after>/**
 * 
 */
package wota.graphics;

import javax.swing.*;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;
import javax.swing.text.html.HTMLDocument.HTMLReader.IsindexAction;

import java.awt.*;
import java.util.Map;
import java.util.Vector;

import wota.gamemaster.StatisticsLogger;
import wota.gameobjects.*;
import wota.gameobjects.GameWorld.Player;

/**
 *  View class for the statistics.
 *  Draws an JTable and puts in StatisticsView.StatisticsTableModel.
 */
public class StatisticsView implements Runnable {
	
	public JFrame frame;
	private GameWorld gameWorld;
	private StatisticsLogger logger;
	private StatisticsTableModel statisticsTableModel;
	
	public StatisticsView(GameWorld gameWorld, StatisticsLogger logger) {
		this.gameWorld = gameWorld;
		this.logger    = logger;
	}
	
	public void run() {
		frame = new JFrame("Wota Statistics");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLayout(new FlowLayout());

		statisticsTableModel = new StatisticsTableModel(logger);
		JTable table = new JTable(statisticsTableModel);
		table.setDefaultRenderer(Object.class, new CellRenderer());
		
	//	table.setPreferredScrollableViewportSize(new Dimension(500, 70));
		table.setFillsViewportHeight(true);
		
		JScrollPane scrollPane = new JScrollPane(table);
		frame.add(scrollPane);
		
		frame.pack();
		frame.setVisible(true);
	}
	
	/**
	 * The StatisticTableModel gets passed to the JTable.
	 * It implements basic methods like getValueAt() which 
	 * are used by the JTable to obtain the cell values.
	 */
	@SuppressWarnings("synthetic-access")
	public class StatisticsTableModel extends AbstractTableModel {
		
		private StatisticsLogger          logger;
		private final String[]			  playerNames;
		/** representing the table data, 1st index = row, 2nd = column, 0 = row name, 1.. players */
		private Object[][]				  data; 
		
		public StatisticsTableModel(StatisticsLogger logger) {
			this.logger = logger;
			String[] rowNames = new String[] {
					"", "# Ants", "# Gatherer", "# Soldiers", "# Scouts",
					"# created ants", "# lost ants",
					"collected food"
			};
			data = new Object[rowNames.length][gameWorld.getPlayers().size() + 1];
			for (int i=0; i<rowNames.length; i++) {
				data[i][0] = rowNames[i]; 
			}
			
			playerNames = new String[gameWorld.getPlayers().size()];
			for (Player player : gameWorld.getPlayers()) {
				playerNames[player.id()] = player.name;
			}

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

		@Override
		public int getColumnCount() {
			return data[0].length;
		}

		@Override
		public String getColumnName(int column) {
			if (column == 0) {
				return "";
			}
			return playerNames[column - 1];
		}
		
		@Override
		public Object getValueAt(int rowIndex, int columnIndex) {
			return data[rowIndex][columnIndex];
		}
		
		/** updates all of the data */
		public void refresh() {
			for (int column = 1; column < getColumnCount(); column++) {
				int playerId = column - 1;
				Player player = gameWorld.getPlayers().get(playerId);
				
				data[0][column] = GameView.playerColors[player.id()];
				data[1][column] = player.antObjects.size();
				data[2][column] = player.numAnts(Caste.Gatherer);
				data[3][column] = player.numAnts(Caste.Soldier);
				data[4][column] = player.numAnts(Caste.Scout);
				data[5][column] = logger.createdAnts()[playerId];
				data[6][column] = logger.diedAnts()[playerId];
				data[7][column] = logger.collectedFood()[playerId];
			}
		}
		
	}

	/** call this when the table should grep the information */
	public void refresh() {
		statisticsTableModel.refresh();		
		statisticsTableModel.fireTableDataChanged(); // tells the JTable to update graphics 
	}
	
	public class CellRenderer extends JLabel
							   implements TableCellRenderer {
		DefaultTableCellRenderer defaultTableCellRenderer = new DefaultTableCellRenderer();
{'fraction_non_alphanumeric': 0.11229174865538502, 'fraction_numerical': 0.006034369670733307, 'mean_word_length': 3.9314359637774903, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7471314', 'n_tokens_mistral': 2663, 'n_tokens_neox': 2249, 'n_words': 566}