author | Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
Tue, 14 Sep 2010 09:46:43 +0200 | |
changeset 266 | 15fcdfc4cd4a |
parent 255 | 9ecd02d694cd |
permissions | -rw-r--r-- |
jaroslav@266 | 1 |
/** |
jaroslav@266 | 2 |
* Quoridor server and related libraries |
jaroslav@266 | 3 |
* Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org> |
jaroslav@266 | 4 |
* |
jaroslav@266 | 5 |
* This program is free software: you can redistribute it and/or modify |
jaroslav@266 | 6 |
* it under the terms of the GNU General Public License as published by |
jaroslav@266 | 7 |
* the Free Software Foundation, either version 3 of the License. |
jaroslav@266 | 8 |
* |
jaroslav@266 | 9 |
* This program is distributed in the hope that it will be useful, |
jaroslav@266 | 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
jaroslav@266 | 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
jaroslav@266 | 12 |
* GNU General Public License for more details. |
jaroslav@266 | 13 |
* |
jaroslav@266 | 14 |
* You should have received a copy of the GNU General Public License |
jaroslav@266 | 15 |
* along with this program. Look for COPYING file in the top folder. |
jaroslav@266 | 16 |
* If not, see http://www.gnu.org/licenses/. |
jaroslav@255 | 17 |
*/ |
jaroslav@255 | 18 |
|
jaroslav@266 | 19 |
|
jaroslav@255 | 20 |
package cz.xelfi.quoridor.visidor; |
jaroslav@255 | 21 |
|
jaroslav@255 | 22 |
import cz.xelfi.quoridor.Board; |
jaroslav@255 | 23 |
import cz.xelfi.quoridor.Move; |
jaroslav@255 | 24 |
import java.awt.GridLayout; |
jaroslav@255 | 25 |
import java.util.List; |
jaroslav@255 | 26 |
import java.util.concurrent.CopyOnWriteArrayList; |
jaroslav@255 | 27 |
import javax.swing.JPanel; |
jaroslav@255 | 28 |
|
jaroslav@255 | 29 |
/** JavaBean class that displays and can manipulate with Quoridor {@link Board}. |
jaroslav@255 | 30 |
* |
jaroslav@255 | 31 |
* @author Jaroslav Tulach <jtulach@netbeans.org> |
jaroslav@255 | 32 |
*/ |
jaroslav@255 | 33 |
public final class BoardPane extends JPanel { |
jaroslav@255 | 34 |
private final Viewer viewer; |
jaroslav@255 | 35 |
private final List<BoardListener> listeners = new CopyOnWriteArrayList<BoardListener>(); |
jaroslav@255 | 36 |
|
jaroslav@255 | 37 |
public BoardPane() { |
jaroslav@255 | 38 |
setLayout(new GridLayout(1,1)); |
jaroslav@255 | 39 |
viewer = new Viewer(); |
jaroslav@255 | 40 |
viewer.moveListener(this); |
jaroslav@255 | 41 |
add(viewer); |
jaroslav@255 | 42 |
} |
jaroslav@255 | 43 |
|
jaroslav@255 | 44 |
public final void setBoard(Board b) { |
jaroslav@255 | 45 |
viewer.setBoard(b); |
jaroslav@255 | 46 |
} |
jaroslav@255 | 47 |
|
jaroslav@255 | 48 |
public final Board getBoard() { |
jaroslav@255 | 49 |
return viewer.getBoard(); |
jaroslav@255 | 50 |
} |
jaroslav@255 | 51 |
|
jaroslav@255 | 52 |
/** The board can either be in <q>view only</q> mode when showing state |
jaroslav@255 | 53 |
* of the {@link Board} during opponent's move, or it can be editable. |
jaroslav@255 | 54 |
* In editable mode, one shall listen on changes delivered to |
jaroslav@255 | 55 |
* BoardListener. |
jaroslav@255 | 56 |
* |
jaroslav@255 | 57 |
* @param editable read only or editable mode? |
jaroslav@255 | 58 |
*/ |
jaroslav@255 | 59 |
public final void setEditable(boolean editable) { |
jaroslav@255 | 60 |
viewer.enableListeners(editable); |
jaroslav@255 | 61 |
} |
jaroslav@255 | 62 |
|
jaroslav@255 | 63 |
public final void addBoardListener(BoardListener l) { |
jaroslav@255 | 64 |
listeners.add(l); |
jaroslav@255 | 65 |
} |
jaroslav@255 | 66 |
public final void removeBoardListener(BoardListener l) { |
jaroslav@255 | 67 |
listeners.remove(l); |
jaroslav@255 | 68 |
} |
jaroslav@255 | 69 |
|
jaroslav@255 | 70 |
final void moveHappened(Move m) { |
jaroslav@255 | 71 |
BoardEvent ev = null; |
jaroslav@255 | 72 |
for (BoardListener bl : listeners) { |
jaroslav@255 | 73 |
if (ev == null) { |
jaroslav@255 | 74 |
ev = new BoardEvent(this, m); |
jaroslav@255 | 75 |
} |
jaroslav@255 | 76 |
bl.boardChanged(ev); |
jaroslav@255 | 77 |
} |
jaroslav@255 | 78 |
} |
jaroslav@255 | 79 |
} |