/** * Copyright 1999-2009 The Pegadi Team * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /** * List of scores. Implements TableModel for table display. * * @author HÃ¥vard Wigtil * @version $Revision$, $Date$ */ package org.pegadi.games; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.table.AbstractTableModel; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; public class ScoreList extends AbstractTableModel implements java.io.Serializable { protected List<Score> scores; /** The maximum number of scores to hold. */ protected int maxScores; private final Logger log = LoggerFactory.getLogger(getClass()); /** * Create a new list. The array of scores must be sorted for * {@link #insertScore} to operate properly. * * @param scores A sorted array of Score objects. */ public ScoreList(Score[] scores) { this.scores = new ArrayList<Score>(scores.length); this.scores.addAll(Arrays.asList(scores)); maxScores = Integer.MAX_VALUE; } public int getColumnCount() { return 3; } public Object getValueAt(int rowIndex, int colIndex) { Score s; try { s = scores.get(rowIndex); } catch (IndexOutOfBoundsException ie) { log.error("No score at index " + rowIndex, ie); return null; } catch (ClassCastException e) { log.error("Element can not be cast to Score", e); return null; } // If the rank is 0, return a null object if (colIndex == 0) { long r = s.getRank(); if (r == 0) { return null; } else { return r; } } switch (colIndex) { case 1: return s.getUserName(); case 2: return s.getScore(); default: log.error("No value for column " + colIndex); return null; } } public int getRowCount() { return scores.size(); } public String getColumnName(int column) { switch (column) { case 0: return "Rank"; case 1: return "Name"; case 2: return "Score"; default: log.error("No value for column " + column); return null; } } public Class getColumnClass(int column) { switch (column) { case 1: return String.class; case 0: case 2: return Long.class; default: log.error("No value for column " + column); return Object.class; } } /** * Sets the maximum number of scores that the list will hold. * If a new score is inserted, the lowes score will be removed from * the list. * * @param max Max number of scores to hold. * @see #insertScore */ public void setMaxScores(int max) { this.maxScores = max; // Trim the list down to size while (maxScores < scores.size()) { scores.remove(scores.size()-1); log.info("Scores trimmed to size " + scores.size()); } } /** * Inserts a new score into the list. The score will be inserted sorted * on the 'score' value. If a score with this ID is allready in the list * it will be replaced, if the ID isn't in the list and the list's size * is equal to <code>maxScores</code> the lowest score will be removed * from the list. * * @param score The new score. * @see #setMaxScores */ public void insertScore(Score score) { // Check to see if this ID is allready in the list, and remove it int size = scores.size(); boolean newScore = true; for (int i = 0; i < size; i++) { Score s = scores.get(i); if (s.getID() == score.getID()) { scores.set(i, score); newScore = false; } } if (newScore) { scores.add(score); //this.fireTableStructureChanged(); } Collections.sort(scores, new ScoreComparator()); while (maxScores < scores.size()) { scores.remove(scores.size()-1); } this.fireTableStructureChanged(); } }