package org.ggp.base.apps.server.leaderboard;

import java.awt.BorderLayout;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableModel;

import org.ggp.base.server.event.ServerMatchUpdatedEvent;
import org.ggp.base.util.match.Match;
import org.ggp.base.util.observer.Event;
import org.ggp.base.util.observer.Observer;
import org.ggp.base.util.ui.JLabelBold;

@SuppressWarnings("serial")
public final class LeaderboardPanel extends JPanel implements Observer
{
	private final JTable leaderTable;
	
	public LeaderboardPanel()
	{
		super(new BorderLayout());
		
        DefaultTableModel model = new DefaultTableModel();		
        model.addColumn("Player");
        model.addColumn("Total Score");
        
		leaderTable = new JTable(model)
		{
			@Override
			public boolean isCellEditable(int rowIndex, int colIndex)
			{
				return false;
			}
		};
		leaderTable.setShowHorizontalLines(true);
		leaderTable.setShowVerticalLines(true);
		leaderTable.getColumnModel().getColumn(1).setPreferredWidth(1);

		add(new JLabelBold("Leaderboard"), BorderLayout.NORTH);
		add(new JScrollPane(leaderTable, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED), BorderLayout.CENTER);
	}

	public void observe(Event event)
	{
		if (!(event instanceof ServerMatchUpdatedEvent)) return;
		Match match = ((ServerMatchUpdatedEvent) event).getMatch();
		
		if (!match.isCompleted()) return;
		if (match.getMatchId().startsWith("Test")) return;
		
		List<Integer> goals = match.getGoalValues();
		List<String> players = match.getPlayerNamesFromHost();
		for (int i = 0; i < players.size(); i++) { if (players.get(i)==null) { players.set(i, "?"); } }

		Set<String> playersToAdd = new HashSet<String>(players);
		DefaultTableModel model = (DefaultTableModel) leaderTable.getModel();
		for (int i = 0; i < model.getRowCount(); i++) {
			String rowPlayer = model.getValueAt(i, 0).toString();
			int playerIndex = players.indexOf(rowPlayer);
			if (playerIndex != -1) {
				int oldScore = (Integer)model.getValueAt(i, 1);
				model.setValueAt(oldScore + goals.get(playerIndex), i, 1);
				playersToAdd.remove(rowPlayer);
			}
		}
		for (String playerToAdd : playersToAdd) {
			model.addRow(new Object[]{playerToAdd, goals.get(players.indexOf(playerToAdd))});
		}
	}
}

@@ -70,16 +70,70 @@
 Layout;%0A
+import java.util.Arrays;%0Aimport java.util.Comparator;%0A
 import j
@@ -282,16 +282,54 @@
 JTable;%0A
+import javax.swing.RowSorter.SortKey;%0A
 import j
@@ -360,16 +360,46 @@
 stants;%0A
+import javax.swing.SortOrder;%0A
 import j
@@ -433,16 +433,94 @@
 leModel;
+%0Aimport javax.swing.table.TableModel;%0Aimport javax.swing.table.TableRowSorter;
 %0A%0Aimport
@@ -871,16 +871,66 @@
 rTable;%0A
+%09private final TableRowSorter%3CTableModel%3E sorter;%0A
 %09%0A%09publi
@@ -1276,160 +1276,645 @@
 %7D%0A%09%09
-%7D;%0A%09%09leaderTable.setShowHorizontalLines(true);%0A%09%09leaderTable.setShowVerticalLines(true);%0A%09%09leaderTable.getColumnModel().getColumn(1).setPreferredWidth(1
+%09@Override%0A%09%09%09public Class%3C?%3E getColumnClass(int colIndex) %7B%0A%09%09%09%09if (colIndex == 0) return String.class;%0A%09%09%09%09if (colIndex == 1) return Integer.class;%0A%09%09%09%09return Object.class;%0A%09%09%09%7D%0A%09%09%7D;%0A%09%09leaderTable.setShowHorizontalLines(true);%0A%09%09leaderTable.setShowVerticalLines(true);%0A%09%09leaderTable.getColumnModel().getColumn(1).setPreferredWidth(1);%0A%09%09sorter = new TableRowSorter%3CTableModel%3E(model);%0A%09%09sorter.setComparator(1, new Comparator%3CInteger%3E() %7B%0A%09%09%09public int compare(Integer a, Integer b) %7B%0A%09%09%09%09return a-b;%0A%09%09%09%7D%0A%09%09%7D);%0A%09%09sorter.setSortKeys(Arrays.asList(new SortKey%5B%5D%7Bnew SortKey(1, SortOrder.DESCENDING)%7D));%0A%09%09leaderTable.setRowSorter(sorter
 );%0A%0A
@@ -3199,13 +3199,30 @@
 %7D);%0A%09%09%7D%0A
+%09%09sorter.sort();%0A
 %09%7D%0A%7D%0A

Automatically sort the leaderboard as new match results come in.