1 /* JChessBoard -- a chess game 2 * Copyright (C) 2000-2004 Claus Divossen <claus.divossen@gmx.de> 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation; either version 2 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program; if not, write to the Free Software 16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 17 */ 18 19 /* $Id: GameTable.java,v 1.5 2004/12/26 23:12:14 cdivossen Exp $ */ 20 package jchessboard; 21 22 import java.awt.Color; 23 import java.awt.Component; 24 import java.awt.Font; 25 import java.util.Vector; 26 27 import javax.swing.JTable; 28 import javax.swing.ListSelectionModel; 29 import javax.swing.event.TableModelEvent; 30 31 /** 32 * @author cd 33 * 34 * To change the template for this generated type comment go to 35 * Window>Preferences>Java>Code Generation>Code and Comments 36 */ 37 class GameTable extends javax.swing.JScrollPane { 38 private Vector pgnList = new Vector(); 39 private Vector strList = new Vector(); 40 private GameTableModel tableModel; 41 boolean tableChanged = false; 42 private JTable jTable; 43 private int currentGameIndex = 0; // Defines which game is highligthed in the list. 44 private int networkGameIndex = -1; // Defines which game is the connected one. 45 46 class GameTableModel extends javax.swing.table.AbstractTableModel { getColumnCount()47 public int getColumnCount() { 48 return 8; 49 } getRowCount()50 public int getRowCount() { 51 return strList.size(); 52 } getGameCount()53 public int getGameCount() { 54 return strList.size(); 55 } getColumnName(int col)56 public String getColumnName(int col) { 57 if (col == 0) 58 return "#"; 59 /* if (col == 1) 60 return "Description";*/ 61 if (col == 1) 62 return "White"; 63 if (col == 2) 64 return "Black"; 65 if (col == 3) 66 return "Res."; 67 if (col == 4) 68 return "Date"; 69 if (col == 5) 70 return "Event"; 71 if (col == 6) 72 return "Site"; 73 if (col == 7) 74 return "Rnd."; 75 return null; 76 } getValueAt(int row, int col)77 public Object getValueAt(int row, int col) { 78 if (col == 0) { 79 if (row == networkGameIndex) 80 return "Net"; 81 else 82 return row + 1 + ""; 83 } 84 PGN.STR str = (PGN.STR) strList.get(row); 85 // if (col == 1) 86 // return str.getTag("White")+" - "+str.getTag("Black")+", "+str.getTag("Result"); 87 // ", Date: " + str.getTag("Date")+" Round: "+str.getTag("Round"); 88 if (col == 1) 89 return str.getTag("White"); 90 if (col == 2) 91 return str.getTag("Black"); 92 if (col == 3) { 93 if (str.getTag("Result").equals("1/2-1/2")) 94 return "\u00BD-\u00BD"; 95 else 96 return str.getTag("Result"); 97 } 98 if (col == 4) 99 return str.getTag("Date"); 100 if (col == 5) 101 return str.getTag("Event"); 102 if (col == 6) 103 return str.getTag("Site"); 104 if (col == 7) 105 return str.getTag("Round"); 106 return null; 107 } 108 } addGame(String pgnData, PGN.STR str)109 public int addGame(String pgnData, PGN.STR str) { 110 pgnList.add(pgnData); 111 strList.add(str); 112 tableModel.fireTableChanged(new TableModelEvent(tableModel)); 113 int index = pgnList.size() - 1; 114 jTable.setRowSelectionInterval(index, index); 115 setColumnWidths(); 116 tableChanged = true; 117 return index; 118 } getPGN(int index)119 public String getPGN(int index) { 120 return (String) pgnList.get(index); 121 } setPGN(int index, String pgnData)122 public void setPGN(int index, String pgnData) { 123 pgnList.set(index, pgnData); 124 tableChanged = true; 125 } getSTR(int index)126 public PGN.STR getSTR(int index) { 127 return (PGN.STR) strList.get(index); 128 } setSTR(int index, PGN.STR str)129 public void setSTR(int index, PGN.STR str) { 130 strList.set(index, str); 131 tableModel.fireTableChanged(new TableModelEvent(tableModel)); 132 tableChanged = true; 133 } clear()134 public void clear() { 135 synchronized (this) { 136 pgnList.clear(); 137 strList.clear(); 138 tableModel.fireTableChanged(new TableModelEvent(tableModel)); 139 tableChanged = false; 140 } 141 setColumnWidths(); 142 } getGameCount()143 public int getGameCount() { 144 return pgnList.size(); 145 } getSelectedIndex()146 public int getSelectedIndex() { 147 return jTable.getSelectedRow(); 148 } getSelectionCount()149 public int getSelectionCount() { 150 return jTable.getSelectedRowCount(); 151 } setSelectedIndex(int index)152 public void setSelectedIndex(int index) { 153 jTable.setRowSelectionInterval(index, index); 154 jTable.scrollRectToVisible(jTable.getCellRect(index, 0, true)); 155 } updateRow(int row)156 public void updateRow(int row) { 157 tableModel.fireTableChanged(new TableModelEvent(tableModel, row)); 158 } removeGame(int index)159 public void removeGame(int index) { 160 pgnList.remove(index); 161 strList.remove(index); 162 tableModel.fireTableChanged(new TableModelEvent(tableModel)); 163 tableChanged = true; 164 } removeSelectedGames()165 public int removeSelectedGames() { 166 int removeIndex = getSelectedIndex(); 167 int count = getSelectionCount(); 168 int newGameIndex = currentGameIndex; 169 for (int n = 0; n < count; n++) { 170 removeGame(removeIndex); 171 if (newGameIndex >= removeIndex) 172 newGameIndex--; 173 } 174 if (getGameCount() == 0) { 175 addGame("", new PGN.STR()); 176 return 0; 177 } else { 178 if (newGameIndex < 0) 179 newGameIndex = 0; 180 return newGameIndex; 181 } 182 } 183 GameTable()184 public GameTable() { 185 super(); 186 tableModel = new GameTableModel(); 187 jTable = new JTable(tableModel); 188 jTable.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION); 189 setViewportView(jTable); 190 jTable.setFont(new Font("SansSerrif", Font.PLAIN, 10)); 191 javax.swing.table.TableCellRenderer cellRenderer = new javax.swing.table.DefaultTableCellRenderer() { 192 193 public Component getTableCellRendererComponent( 194 JTable table, 195 Object value, 196 boolean isSelected, 197 boolean hasFocus, 198 int row, 199 int column) { 200 if (row == currentGameIndex) 201 setBackground(new Color(200, 200, 200)); 202 else if (row == networkGameIndex) { 203 setBackground(new Color(200, 20, 20)); 204 isSelected=false; 205 } 206 else 207 setBackground(new Color(255, 255, 255)); 208 super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column); 209 return this; 210 } 211 }; 212 jTable.setDefaultRenderer((new Object()).getClass(), cellRenderer); 213 jTable.setAutoResizeMode(JTable.AUTO_RESIZE_LAST_COLUMN); 214 setColumnWidths(); 215 } 216 setColumnWidths()217 private void setColumnWidths() { 218 jTable.getColumnModel().getColumn(0).setMaxWidth(30); 219 jTable.getColumnModel().getColumn(1).setPreferredWidth(100); 220 jTable.getColumnModel().getColumn(2).setPreferredWidth(100); 221 jTable.getColumnModel().getColumn(3).setMaxWidth(50); 222 // jTable.getColumnModel().getColumn(4).setMaxWidth(60); 223 jTable.getColumnModel().getColumn(4).setPreferredWidth(80); 224 jTable.getColumnModel().getColumn(7).setMaxWidth(40); 225 } 226 227 /** 228 * @return 229 */ getCurrentGameIndex()230 public int getCurrentGameIndex() { 231 return currentGameIndex; 232 } 233 234 /** 235 * @param i 236 */ setCurrentGameIndex(int i)237 public void setCurrentGameIndex(int i) { 238 currentGameIndex = i; 239 } 240 241 /** 242 * @return The JTable used to display the games. 243 */ getJTable()244 public JTable getJTable() { 245 return jTable; 246 } 247 setEnabled(boolean enable)248 public void setEnabled(boolean enable) { 249 super.setEnabled(enable); 250 jTable.setEnabled(enable); 251 } 252 253 /** 254 * @return 255 */ getNetworkGameIndex()256 public int getNetworkGameIndex() { 257 return networkGameIndex; 258 } 259 260 /** 261 * @param i 262 */ setNetworkGameIndex(int i)263 public void setNetworkGameIndex(int i) { 264 networkGameIndex = i; 265 } 266 267 } 268