/** * 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. */ /** * A panel which holds (and can update) various score lists. * * @author H?vard Wigtil <havardw at pvv.org> * @version $Revision$, $Date$ */ package org.pegadi.games.tetris; // UI imports import org.pegadi.games.Score; import org.pegadi.games.ScoreList; import org.pegadi.games.tetris.model.TetrisScoreList; import org.pegadi.model.LoginContext; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import javax.swing.*; import javax.swing.table.TableModel; import java.awt.*; import java.util.Date; import java.util.List; import java.util.ResourceBundle; public class ScorePanel extends JPanel { /** The domain to show scores for. */ protected static String DOMAIN = "tetris"; /** Translatable strings. */ protected ResourceBundle strings; /** Lenght of personal score list. */ protected int personalLength; /** Lenght of today's score list. */ protected int todayLength; /** Lenght of high score list. */ protected int hiscoreLength; protected JLabel personalLabel; protected JTable personalTable; protected JLabel todayLabel; protected JTable todayTable; protected JLabel hiscoreLabel; protected JTable hiscoreTable; private final Logger log = LoggerFactory.getLogger(getClass()); public ScorePanel() { personalLength = 5; todayLength = 5; hiscoreLength = 5; createUI(); } /** * Set the number of personal scores shown. Set to <code>0</code> to * hide this list. This result will be applied with the first call * to {@link #updateScores updateScores}. * * @param length The length of the list. */ public void setPersonalLenght(int length) { personalLength = length; } /** * Return the lenght of the personal score list. * * @return The length. */ public int getPersonalLenght() { return personalLength; } /** * Set the number of today's best scores shown. Set to <code>0</code> to * hide this list. This result will be applied with the first call * to {@link #updateScores updateScores}. * * @param length The length of the list. */ public void setTodayLenght(int length) { todayLength = length; } /** * Return the lenght of today's score list. * * @return The length. */ public int getTodayLenght() { return todayLength; } /** * Set the number of high scores shown. Set to <code>0</code> to * hide this list. This result will be applied with the first call * to {@link #updateScores updateScores}. * * @param length The length of the list. */ public void setHighLenght(int length) { hiscoreLength = length; } /** * Return the lenght of the high score list. * * @return The length. */ public int getHighLenght() { return hiscoreLength; } /** * Updates the score tables in this component with data from the server. * If the {@link LoginContext.server} object is <code>null</code>, nothing will be done. * * @param activesOnly Only show active players on the highscore list * @see org.pegadi.server.Server */ public void updateScores(boolean activesOnly) { updateHighScoreTable(activesOnly); updateUserScoreTable(); updateTodayScoreTable(); this.validate(); } private void updateHighScoreTable(boolean activesOnly) { TetrisScoreList highscores; if (hiscoreLength > 0) { try { List<? extends Score> highScore = LoginContext.server.getHighScore(DOMAIN, hiscoreLength, activesOnly, LoginContext.sessionKey); if (highScore == null) { log.warn("hiscore list is NULL!"); return; } if (highScore.size() > 0 && highScore.get(0) instanceof TetrisScore) { highscores = new TetrisScoreList(highScore.toArray(new TetrisScore[highScore.size()])); } else { log.warn("No scores (list size="+highScore.size()+"), or ScoreList from getHighScores is NOT a TetrisScoreList"); return; } highscores.setMaxScores(hiscoreLength); hiscoreTable.setModel(highscores); setTableProperties(hiscoreTable); } catch (Exception e) { log.error("Unable to get new hiscore list", e); } } else { // Hide this list hiscoreLabel.setVisible(false); hiscoreTable.setVisible(false); } } private void updateUserScoreTable() { if (personalLength > 0) { if (LoginContext.sessionKey != null) { try { List<? extends Score> userScore = LoginContext.server.getUserScore(DOMAIN, personalLength, LoginContext.sessionKey); if (userScore != null) { if (userScore.size() > 0 && userScore.get(0) instanceof TetrisScore) { TetrisScoreList l = new TetrisScoreList(userScore.toArray(new TetrisScore[userScore.size()])); l.setMaxScores(personalLength); personalTable.setModel(l); setTableProperties(personalTable); } else { log.error("Scores returned from server are not TetrisScore[]"); } } else { log.error("getUserScore returns NULL, check server log for errors"); } } catch (Exception e) { log.error("Exception getting user scores", e); } } // if session != null else { log.warn("No session key, skipping user scores."); } } else { // Hide personal scores personalLabel.setVisible(false); personalTable.setVisible(false); } } private void updateTodayScoreTable() { if (todayLength > 0) { try { List<? extends Score> todayScores = LoginContext.server.getDayScore(DOMAIN, new Date(), todayLength, LoginContext.sessionKey); if (todayScores != null) { if (todayScores.size() > 0 && todayScores.get(0) instanceof TetrisScore) { TetrisScoreList todayList = new TetrisScoreList(todayScores.toArray(new TetrisScore[todayScores.size()])); todayList.setMaxScores(todayLength); todayTable.setModel(todayList); setTableProperties(todayTable); } else { log.warn("No scores found, or scores returned for day score are not TetrisScore[]"); } } else { log.warn("getUserScore returns NULL, check server log for errors"); } } catch (Exception e) { log.error("Exception getting today's scores", e); } } else { // Hide today's scores todayLabel.setVisible(false); todayTable.setVisible(false); } } /** * Helper method used by the updateScoreTable methods * @param scoreTable The table to be edited */ private void setTableProperties(JTable scoreTable) { scoreTable.getColumnModel().getColumn(0).setPreferredWidth(40); scoreTable.getColumnModel().getColumn(1).setPreferredWidth(185); scoreTable.getColumnModel().getColumn(2).setPreferredWidth(50); scoreTable.getColumnModel().getColumn(3).setPreferredWidth(50); scoreTable.getColumnModel().getColumn(4).setPreferredWidth(50); hiscoreTable.setAutoCreateColumnsFromModel(false); hiscoreLabel.setVisible(true); hiscoreTable.setVisible(true); } /** * Inserts a new score into each list. * * @param score The score to insert. * @see org.pegadi.games.ScoreList#insertScore */ public void insertScore(Score score) { TableModel[] models = { hiscoreTable.getModel(), todayTable.getModel(), personalTable.getModel() }; for (TableModel model : models) { if (model instanceof ScoreList) { ((ScoreList) model).insertScore(score); } } validate(); } /** * This method will set up the interface. Use {@link #updateScores} * to fill the tables with data. */ protected void createUI() { strings = ResourceBundle.getBundle("org.pegadi.games.tetris.ScorePanelStrings"); hiscoreLabel = new JLabel(strings.getString("hiscore")); todayLabel = new JLabel(strings.getString("today")); personalLabel = new JLabel(strings.getString("personal")); hiscoreTable = new JTable(); todayTable = new JTable(); personalTable = new JTable(); Box b = new Box(BoxLayout.Y_AXIS); JScrollPane scroll = new JScrollPane(b); JPanel hiscorePanel = new JPanel(); hiscorePanel.setLayout(new BorderLayout()); hiscorePanel.add(hiscoreTable.getTableHeader(), BorderLayout.NORTH); hiscorePanel.add(hiscoreTable, BorderLayout.CENTER); JPanel todayPanel = new JPanel(); todayPanel.setLayout(new BorderLayout()); todayPanel.add(todayTable.getTableHeader(), BorderLayout.NORTH); todayPanel.add(todayTable, BorderLayout.CENTER); JPanel personalPanel = new JPanel(); personalPanel.setLayout(new BorderLayout()); personalPanel.add(personalTable.getTableHeader(), BorderLayout.NORTH); personalPanel.add(personalTable, BorderLayout.CENTER); b.add(hiscoreLabel, null); b.add(hiscorePanel, null); b.add(Box.createVerticalStrut(20)); b.add(todayLabel, null); b.add(todayPanel, null); b.add(Box.createVerticalStrut(20)); b.add(personalLabel, null); b.add(personalPanel, null); this.add(scroll); } }