1 /** 2 * Jin - a chess client for internet chess servers. 3 * More information is available at http://www.jinchess.com/. 4 * Copyright (C) 2004 Alexander Maryanovsky. 5 * All rights reserved. 6 * 7 * This program is free software; you can redistribute it and/or 8 * modify it under the terms of the GNU General Public License 9 * as published by the Free Software Foundation; either version 2 10 * of the License, or (at your option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License 18 * along with this program; if not, write to the Free Software 19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 20 */ 21 22 package free.jin.board.prefs; 23 24 import java.awt.BorderLayout; 25 import java.awt.Container; 26 import java.awt.Dimension; 27 import java.util.Arrays; 28 import java.util.Collections; 29 import java.util.Comparator; 30 import java.util.Map; 31 32 import javax.swing.*; 33 import javax.swing.event.ChangeEvent; 34 import javax.swing.event.ChangeListener; 35 import javax.swing.event.ListSelectionEvent; 36 import javax.swing.event.ListSelectionListener; 37 38 import free.chess.BoardPainter; 39 import free.chess.ColoredBoardPainter; 40 import free.chess.ColoredPiecePainter; 41 import free.chess.PiecePainter; 42 import free.jin.BadChangesException; 43 import free.jin.I18n; 44 import free.jin.Jin; 45 import free.jin.board.BoardManager; 46 import free.jin.board.BoardPattern; 47 import free.jin.board.JinBoard; 48 import free.jin.board.PieceSet; 49 import free.util.AWTUtilities; 50 import free.util.TableLayout; 51 import free.util.swing.ColorChooser; 52 import free.util.swing.PreferredSizedPanel; 53 import free.util.swing.UrlDisplayingAction; 54 55 56 /** 57 * A preferences panel allowing the user to modify the looks of the board. 58 */ 59 60 public class BoardLooksPanel extends BoardModifyingPrefsPanel{ 61 62 63 64 /** 65 * The list holding all the available piece set names. 66 */ 67 68 protected final JList pieceSets; 69 70 71 72 /** 73 * The color chooser for the white piece's color. 74 */ 75 76 protected final ColorChooser whiteColor; 77 78 79 80 /** 81 * The color chooser for the black piece's color. 82 */ 83 84 protected final ColorChooser blackColor; 85 86 87 88 /** 89 * The color chooser for the white piece's outline color. 90 */ 91 92 protected final ColorChooser whiteOutline; 93 94 95 96 /** 97 * The color chooser for the black piece's outline color. 98 */ 99 100 protected final ColorChooser blackOutline; 101 102 103 104 /** 105 * The list holding all the available board pattern names. 106 */ 107 108 protected final JList boardPatterns; 109 110 111 112 /** 113 * The color chooser for the board's dark squares' color. 114 */ 115 116 protected final ColorChooser darkSquares; 117 118 119 120 /** 121 * The color chooser for the board's light squares' color. 122 */ 123 124 protected final ColorChooser lightSquares; 125 126 127 128 /** 129 * The panel containing all of the piece color selection controls. This is 130 * needed so that they can be disabled when the selected piece painter is 131 * not a ColoredPiecePainter. 132 */ 133 134 private Container pieceColorsPanel; 135 136 137 138 /** 139 * The panel containing all of the board color selection controls. This is 140 * needed so that they can be disabled when the selected board painter is 141 * not a ColoredBoardPainter. 142 */ 143 144 private Container boardColorsPanel; 145 146 147 148 /** 149 * Creates a new <code>BoardLooksPanel</code> for the specified 150 * <code>BoardManager</code> and with the specified preview board. 151 */ 152 BoardLooksPanel(BoardManager boardManager, JinBoard previewBoard)153 public BoardLooksPanel(BoardManager boardManager, JinBoard previewBoard){ 154 super(boardManager, previewBoard); 155 156 I18n i18n = I18n.get(BoardLooksPanel.class); 157 158 pieceSets = new JList(getPieceSets()); 159 boardPatterns = new JList(getBoardPatterns()); 160 whiteColor = i18n.createColorChooser("whiteColorChooser"); 161 blackColor = i18n.createColorChooser("blackColorChooser"); 162 whiteOutline = i18n.createColorChooser("whiteOutlineChooser"); 163 blackOutline = i18n.createColorChooser("blackOutlineChooser"); 164 darkSquares = i18n.createColorChooser("darkSquaresChooser"); 165 lightSquares = i18n.createColorChooser("lightSquaresChooser"); 166 167 whiteColor.setColor(boardManager.getWhitePieceColor()); 168 blackColor.setColor(boardManager.getBlackPieceColor()); 169 whiteOutline.setColor(boardManager.getWhiteOutlineColor()); 170 blackOutline.setColor(boardManager.getBlackOutlineColor()); 171 darkSquares.setColor(boardManager.getDarkSquareColor()); 172 lightSquares.setColor(boardManager.getLightSquareColor()); 173 174 JComponent piecesPanel = createPiecesUI(); 175 JComponent boardPanel = createBoardUI(); 176 177 pieceSets.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 178 boardPatterns.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); 179 180 pieceSets.addListSelectionListener(new ListSelectionListener(){ 181 public void valueChanged(ListSelectionEvent evt){ 182 PieceSet set = (PieceSet)pieceSets.getSelectedValue(); 183 PiecePainter painter = set.getPiecePainter(); 184 185 if (painter instanceof ColoredPiecePainter){ 186 ColoredPiecePainter cPainter = (ColoredPiecePainter)painter; 187 cPainter.setWhiteColor(whiteColor.getColor()); 188 cPainter.setBlackColor(blackColor.getColor()); 189 cPainter.setWhiteOutline(whiteOutline.getColor()); 190 cPainter.setBlackOutline(blackOutline.getColor()); 191 } 192 193 BoardLooksPanel.this.previewBoard.setPiecePainter(painter); 194 AWTUtilities.setContainerEnabled(pieceColorsPanel, painter instanceof ColoredPiecePainter); 195 fireStateChanged(); 196 } 197 }); 198 199 boardPatterns.addListSelectionListener(new ListSelectionListener(){ 200 public void valueChanged(ListSelectionEvent evt){ 201 BoardPattern pattern = (BoardPattern)boardPatterns.getSelectedValue(); 202 BoardPainter painter = pattern.getBoardPainter(); 203 204 if (painter instanceof ColoredBoardPainter){ 205 ColoredBoardPainter cPainter = (ColoredBoardPainter)painter; 206 cPainter.setDarkColor(darkSquares.getColor()); 207 cPainter.setLightColor(lightSquares.getColor()); 208 } 209 210 BoardLooksPanel.this.previewBoard.setBoardPainter(painter); 211 AWTUtilities.setContainerEnabled(boardColorsPanel, painter instanceof ColoredBoardPainter); 212 fireStateChanged(); 213 } 214 }); 215 216 pieceSets.setSelectedValue(boardManager.getPieceSet(), true); 217 boardPatterns.setSelectedValue(boardManager.getBoardPattern(), true); 218 219 ChangeListener pieceColorChangeListener = new ChangeListener(){ 220 public void stateChanged(ChangeEvent evt){ 221 ColoredPiecePainter painter = 222 (ColoredPiecePainter)(BoardLooksPanel.this.previewBoard.getPiecePainter()); 223 painter.setWhiteColor(whiteColor.getColor()); 224 painter.setBlackColor(blackColor.getColor()); 225 painter.setWhiteOutline(whiteOutline.getColor()); 226 painter.setBlackOutline(blackOutline.getColor()); 227 228 BoardLooksPanel.this.previewBoard.repaint(); 229 fireStateChanged(); 230 } 231 }; 232 whiteColor.addChangeListener(pieceColorChangeListener); 233 blackColor.addChangeListener(pieceColorChangeListener); 234 whiteOutline.addChangeListener(pieceColorChangeListener); 235 blackOutline.addChangeListener(pieceColorChangeListener); 236 237 ChangeListener boardColorChangeListener = new ChangeListener(){ 238 public void stateChanged(ChangeEvent evt){ 239 // The controls should be disabled when the painter isn't colored 240 ColoredBoardPainter painter = 241 (ColoredBoardPainter)(BoardLooksPanel.this.previewBoard.getBoardPainter()); 242 painter.setDarkColor(darkSquares.getColor()); 243 painter.setLightColor(lightSquares.getColor()); 244 245 BoardLooksPanel.this.previewBoard.repaint(); 246 fireStateChanged(); 247 } 248 }; 249 darkSquares.addChangeListener(boardColorChangeListener); 250 lightSquares.addChangeListener(boardColorChangeListener); 251 252 253 JButton downloadExtras = null; 254 if (Jin.getInstance().isUserExtensible()){ 255 downloadExtras = i18n.createButton("downloadExtrasButton"); 256 downloadExtras.addActionListener(new UrlDisplayingAction("http://www.jinchess.com/extras/")); 257 } 258 259 JPanel boardAndExtraPanel = new JPanel(); 260 boardAndExtraPanel.setLayout(new BoxLayout(boardAndExtraPanel, BoxLayout.Y_AXIS)); 261 boardPanel.setAlignmentX(JComponent.CENTER_ALIGNMENT); 262 if (downloadExtras != null) 263 downloadExtras.setAlignmentX(JComponent.CENTER_ALIGNMENT); 264 boardAndExtraPanel.add(boardPanel); 265 if (downloadExtras != null){ 266 boardAndExtraPanel.add(Box.createVerticalStrut(15)); 267 boardAndExtraPanel.add(downloadExtras); 268 boardAndExtraPanel.add(Box.createVerticalStrut(15)); 269 } 270 271 272 JPanel contentPanel = new JPanel(new BorderLayout()); 273 contentPanel.add(BorderLayout.WEST, piecesPanel); 274 contentPanel.add(BorderLayout.CENTER, Box.createHorizontalStrut(10)); 275 contentPanel.add(BorderLayout.EAST, boardAndExtraPanel); 276 277 JLabel noticeLabel = new JLabel(i18n.getString("colorsNotice")); 278 279 contentPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 280 noticeLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 281 282 setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); 283 setBorder(BorderFactory.createEmptyBorder(15, 10, 15, 10)); 284 add(contentPanel); 285 add(Box.createVerticalStrut(10)); 286 add(noticeLabel); 287 } 288 289 290 291 /** 292 * Gets the list of piece sets from the board manager. 293 */ 294 getPieceSets()295 private PieceSet [] getPieceSets(){ 296 Map resources = boardManager.getResources("pieces"); 297 PieceSet [] pieceSets = (PieceSet [])resources.values().toArray(new PieceSet[0]); 298 299 Collections.sort(Arrays.asList(pieceSets), new Comparator(){ 300 public int compare(Object arg1, Object arg2){ 301 PieceSet set1 = (PieceSet)arg1; 302 PieceSet set2 = (PieceSet)arg2; 303 return set1.getName().compareTo(set2.getName()); 304 } 305 }); 306 307 return pieceSets; 308 } 309 310 311 312 /** 313 * Gets the list of board patterns from the board manager. 314 */ 315 getBoardPatterns()316 private BoardPattern [] getBoardPatterns(){ 317 Map resources = boardManager.getResources("boards"); 318 BoardPattern [] boardPatterns = (BoardPattern [])resources.values().toArray(new BoardPattern[0]); 319 320 Collections.sort(Arrays.asList(boardPatterns), new Comparator(){ 321 public int compare(Object arg1, Object arg2){ 322 BoardPattern pat1 = (BoardPattern)arg1; 323 BoardPattern pat2 = (BoardPattern)arg2; 324 return pat1.getName().compareTo(pat2.getName()); 325 } 326 }); 327 328 return boardPatterns; 329 } 330 331 332 333 /** 334 * Sets the initial properties of the preview board. 335 */ 336 initPreviewBoard()337 public void initPreviewBoard(){ 338 PieceSet pieceSet = (PieceSet)pieceSets.getSelectedValue(); 339 if (pieceSet != null){ 340 PiecePainter piecePainter = pieceSet.getPiecePainter(); 341 if (piecePainter instanceof ColoredPiecePainter){ 342 ColoredPiecePainter cPainter = (ColoredPiecePainter)piecePainter; 343 cPainter.setWhiteColor(whiteColor.getColor()); 344 cPainter.setBlackColor(blackColor.getColor()); 345 cPainter.setWhiteOutline(whiteOutline.getColor()); 346 cPainter.setBlackOutline(blackOutline.getColor()); 347 } 348 previewBoard.setPiecePainter(piecePainter); 349 } 350 351 BoardPattern boardPattern = (BoardPattern)boardPatterns.getSelectedValue(); 352 if (boardPattern != null){ 353 BoardPainter boardPainter = boardPattern.getBoardPainter(); 354 if (boardPainter instanceof ColoredBoardPainter){ 355 ColoredBoardPainter cPainter = (ColoredBoardPainter)boardPainter; 356 cPainter.setDarkColor(darkSquares.getColor()); 357 cPainter.setLightColor(lightSquares.getColor()); 358 } 359 previewBoard.setBoardPainter(boardPainter); 360 } 361 } 362 363 364 365 /** 366 * Creates the user interface for selecting the looks of the piece set. 367 */ 368 createPiecesUI()369 private JComponent createPiecesUI(){ 370 I18n i18n = I18n.get(BoardLooksPanel.class); 371 372 JComponent piecesPanel = new JPanel(); 373 piecesPanel.setLayout(new BoxLayout(piecesPanel, BoxLayout.Y_AXIS)); 374 piecesPanel.setBorder(BorderFactory.createCompoundBorder( 375 i18n.createTitledBorder("piecesPanel"), 376 BorderFactory.createEmptyBorder(0, 10, 10, 10))); 377 378 379 JLabel pieceSetLabel = i18n.createLabel("pieceSetLabel"); 380 pieceSetLabel.setLabelFor(pieceSets); 381 pieceSetLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 382 383 384 JScrollPane scrollPane = 385 new JScrollPane(pieceSets, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 386 scrollPane.setAlignmentX(JComponent.LEFT_ALIGNMENT); 387 scrollPane.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); 388 389 JPanel colorsPanel = new PreferredSizedPanel(new TableLayout(1, 0, 5)); 390 colorsPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 391 colorsPanel.add(whiteColor); 392 colorsPanel.add(blackColor); 393 colorsPanel.add(whiteOutline); 394 colorsPanel.add(blackOutline); 395 396 piecesPanel.add(pieceSetLabel); 397 piecesPanel.add(Box.createVerticalStrut(5)); 398 piecesPanel.add(scrollPane); 399 piecesPanel.add(Box.createVerticalStrut(10)); 400 piecesPanel.add(colorsPanel); 401 402 pieceColorsPanel = colorsPanel; 403 404 return piecesPanel; 405 } 406 407 408 409 /** 410 * Creates the user interface for selecting the looks of the piece set. 411 */ 412 createBoardUI()413 private JComponent createBoardUI(){ 414 I18n i18n = I18n.get(BoardLooksPanel.class); 415 416 JComponent boardPanel = new JPanel(); 417 boardPanel.setLayout(new BoxLayout(boardPanel, BoxLayout.Y_AXIS)); 418 boardPanel.setBorder(BorderFactory.createCompoundBorder( 419 i18n.createTitledBorder("boardPanel"), 420 BorderFactory.createEmptyBorder(0, 10, 10, 10))); 421 422 423 JLabel patternLabel = i18n.createLabel("boardPatternLabel"); 424 patternLabel.setLabelFor(boardPatterns); 425 patternLabel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 426 427 428 JScrollPane scrollPane = 429 new JScrollPane(boardPatterns, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_NEVER); 430 scrollPane.setAlignmentX(JComponent.LEFT_ALIGNMENT); 431 scrollPane.setMaximumSize(new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE)); 432 433 JPanel colorsPanel = new PreferredSizedPanel(new TableLayout(1, 0, 5)); 434 colorsPanel.setAlignmentX(JComponent.LEFT_ALIGNMENT); 435 colorsPanel.add(darkSquares); 436 colorsPanel.add(lightSquares); 437 438 boardPanel.add(patternLabel); 439 boardPanel.add(Box.createVerticalStrut(5)); 440 boardPanel.add(scrollPane); 441 boardPanel.add(Box.createVerticalStrut(10)); 442 boardPanel.add(colorsPanel); 443 444 boardColorsPanel = colorsPanel; 445 446 return boardPanel; 447 } 448 449 450 451 /** 452 * Applies any changes made by the user. 453 */ 454 applyChanges()455 public void applyChanges() throws BadChangesException{ 456 boardManager.setPieceSet((PieceSet)pieceSets.getSelectedValue()); 457 boardManager.setBoardPattern((BoardPattern)boardPatterns.getSelectedValue()); 458 boardManager.setWhitePieceColor(whiteColor.getColor()); 459 boardManager.setBlackPieceColor(blackColor.getColor()); 460 boardManager.setWhiteOutlineColor(whiteOutline.getColor()); 461 boardManager.setBlackOutlineColor(blackOutline.getColor()); 462 boardManager.setDarkSquareColor(darkSquares.getColor()); 463 boardManager.setLightSquareColor(lightSquares.getColor()); 464 } 465 466 467 468 } 469