visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java
changeset 255 9ecd02d694cd
parent 254 1273bfb0e2e7
child 264 d60370059c3c
     1.1 --- a/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sun Sep 05 17:58:45 2010 +0200
     1.2 +++ b/visidor/src/main/java/cz/xelfi/quoridor/visidor/Viewer.java	Sun Sep 05 22:34:43 2010 +0200
     1.3 @@ -55,6 +55,7 @@
     1.4      private int xdelta;
     1.5      private int ydelta;
     1.6      private Orientation lastOrientation;
     1.7 +    private BoardPane pane;
     1.8  
     1.9      public Viewer() {
    1.10          this(Board.empty());
    1.11 @@ -62,8 +63,6 @@
    1.12  
    1.13      private Viewer(Board b) {
    1.14          this.board = b;
    1.15 -        addMouseMotionListener(this);
    1.16 -        addMouseListener(this);
    1.17      }
    1.18  
    1.19  
    1.20 @@ -71,10 +70,24 @@
    1.21       * @param args the command line arguments
    1.22       */
    1.23      public static void main(String[] args) throws IllegalPositionException {
    1.24 -        Board b = Board.empty();
    1.25          JFrame f = new JFrame();
    1.26          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    1.27 -        f.add(new Viewer(b));
    1.28 +        final BoardPane bp = new BoardPane();
    1.29 +        class ApplyMove implements BoardListener {
    1.30 +            public void boardChanged(BoardEvent ev) {
    1.31 +                Move m = ev.getMove();
    1.32 +                if (m != null) {
    1.33 +                    try {
    1.34 +                        bp.setBoard(bp.getBoard().apply(m));
    1.35 +                    } catch (IllegalPositionException ex) {
    1.36 +                        // no new move
    1.37 +                    }
    1.38 +                }
    1.39 +            }
    1.40 +        }
    1.41 +        bp.addBoardListener(new ApplyMove());
    1.42 +        bp.setEditable(true);
    1.43 +        f.add(bp);
    1.44          f.pack();
    1.45          f.setVisible(true);
    1.46      }
    1.47 @@ -259,13 +272,9 @@
    1.48  
    1.49      public void mousePressed(MouseEvent e) {
    1.50          Move m = redrawCurrentMove(e);
    1.51 -        if (m != null) {
    1.52 -            try {
    1.53 -                board = board.apply(m);
    1.54 -            } catch (IllegalPositionException ex) {
    1.55 -                // no new move
    1.56 -            }
    1.57 -            repaint();
    1.58 +        BoardPane bp = pane;
    1.59 +        if (bp != null) {
    1.60 +            bp.moveHappened(m);
    1.61          }
    1.62      }
    1.63  
    1.64 @@ -278,4 +287,29 @@
    1.65      public void mouseExited(MouseEvent e) {
    1.66      }
    1.67  
    1.68 +    final void setBoard(Board b) {
    1.69 +        board = b;
    1.70 +        if (isShowing()) {
    1.71 +            repaint();
    1.72 +        }
    1.73 +    }
    1.74 +
    1.75 +    final Board getBoard() {
    1.76 +        return board;
    1.77 +    }
    1.78 +
    1.79 +    final void enableListeners(boolean editable) {
    1.80 +        if (editable) {
    1.81 +            addMouseMotionListener(this);
    1.82 +            addMouseListener(this);
    1.83 +        } else {
    1.84 +            removeMouseListener(this);
    1.85 +            removeMouseMotionListener(this);
    1.86 +        }
    1.87 +    }
    1.88 +
    1.89 +    final void moveListener(BoardPane pane) {
    1.90 +        this.pane = pane;
    1.91 +    }
    1.92 +
    1.93  }