/** * 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 Tetris scores. This class overrides {@link org.pegadi.games.ScoreList} * to provide for display of the extra two attributes in * {@link org.pegadi.games.tetris.TetrisScore}. These attributes * are added to the end, and all calculations will use the size * of the base <code>ScoreList</code> class, so <code>ScoreList</code> * can change without the need for updates to this class. * * @author H?vard Wigtil * @version $Revision$, $Date$ */ package org.pegadi.games.tetris.model; import org.pegadi.games.ScoreList; import org.pegadi.games.tetris.TetrisScore; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TetrisScoreList extends ScoreList implements java.io.Serializable { /** Number of columns in the super class. */ protected int superCol; private final Logger log = LoggerFactory.getLogger(getClass()); /** * Creates a new score list. * * @param scores An list of scores. */ public TetrisScoreList(TetrisScore[] scores) { super(scores); superCol = super.getColumnCount(); } /** * Returs the column count for the table model. * * @return Number of columns. * @see javax.swing.table.TableModel#getColumnCount */ public int getColumnCount() { return superCol + 2; } public Object getValueAt(int rowIndex, int colIndex) { // Let the super class handle it's own columns, // and any values out of range if (colIndex < superCol || colIndex >= this.getColumnCount()) { return super.getValueAt(rowIndex, colIndex); } TetrisScore s; try { s = (TetrisScore)scores.get(rowIndex); } catch (IndexOutOfBoundsException ie) { log.error("No score at index " + rowIndex, ie); return null; } catch (ClassCastException ce) { log.error("Object at row " + rowIndex + " is not an instance of TetrisScore", ce); return null; } if (colIndex == superCol) { return s.getLevel(); } else if (colIndex == superCol+1) { return s.getLines(); } else { // This should never happen, as the // super class handles out-of-range cases return null; } } public String getColumnName(int column) { if (column < superCol || column >= this.getColumnCount()) { return super.getColumnName(column); } if (column == superCol) { return "Level"; } else if (column == superCol+1) { return "Lines"; } else { // This should never happen return null; } } public Class getColumnClass(int column) { if (column < superCol || column >= this.getColumnCount()) { return super.getColumnClass(column); } return Integer.class; } }