/** * Ensures that the field <code>row/column</code> shows <code>figure</code>. * * @param row the row of the field * @param column the column of the field * @param figure the figure to show, can be <code>null</code> if the field should be cleared. */ public void put(int row, int column, ChessFigure figure) { Field field = fields[row][column]; ChessFigure old = field.getFigure(); if (old != null) { for (DockStationListener listener : listListeners()) listener.dockableRemoving(this, old); field.set(null); old.setDockParent(null); for (DockStationListener listener : listListeners()) listener.dockableRemoved(this, old); } if (figure != null) { Field oldField = getFieldOf(figure); if (oldField == null) { for (DockStationListener listener : listListeners()) listener.dockableAdding(this, figure); field.set(figure); figure.setDockParent(this); for (DockStationListener listener : listListeners()) listener.dockableAdded(this, figure); } else { field.transfer(oldField); if (!board.isEmpty(oldField.getRow(), oldField.getColumn())) { board.move(oldField.getRow(), oldField.getColumn(), row, column); Figure pawn = board.pawnReplacement(); if (pawn != null) { pawn = pawnReplaceDialog.replace(pawn); board.put(pawn); figure.setFigure(pawn); } board.switchPlayer(); } } } }
/** * Gets the field which shows <code>figure</code>. * * @param figure the figure whose field is searched * @return the field or <code>null</code> if nothing was found */ private Field getFieldOf(Dockable figure) { for (Field field : usedFieldList) if (field.getFigure() == figure) return field; return null; }