Associated DropTarget with MainWindow

When a FEN file is dropped into the main window, the drop target
automatically loads it into the currently visible (=selected) game.
This commit is contained in:
Kai S. K. Engelbart 2019-08-24 16:04:09 +02:00
parent 961100f74c
commit 513c0077e0
3 changed files with 13 additions and 24 deletions

View File

@ -19,27 +19,26 @@ import java.util.List;
*/
public class FENDropTarget extends DropTargetAdapter {
private GamePane gamePane;
private MainWindow mainWindow;
public FENDropTarget(GamePane gamePane) {
this.gamePane = gamePane;
public FENDropTarget(MainWindow mainWindow) {
this.mainWindow = mainWindow;
}
@Override
public void drop(DropTargetDropEvent evt) {
try {
evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
@SuppressWarnings(
"unchecked"
)
@SuppressWarnings("unchecked")
List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
try (BufferedReader br = new BufferedReader(new FileReader(droppedFiles.get(0)))) {
gamePane.getGame().reset();
gamePane.getGame().getBoard().initFromFEN(br.readLine());
gamePane.getBoardPane().getBoardComponent().repaint();
gamePane.getGame()
mainWindow.getSelectedGamePane().getGame().reset();
mainWindow.getSelectedGamePane().getGame().getBoard().initFromFEN(br.readLine());
mainWindow.getSelectedGamePane().getBoardPane().getBoardComponent().repaint();
mainWindow.getSelectedGamePane()
.getGame()
.getPlayers()
.get(gamePane.getGame().getBoard().getLog().getActiveColor())
.get(mainWindow.getSelectedGamePane().getGame().getBoard().getLog().getActiveColor())
.requestMove();
evt.dropComplete(true);
}

View File

@ -2,7 +2,6 @@ package dev.kske.chess.ui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.dnd.DropTarget;
import javax.swing.JButton;
import javax.swing.JComponent;
@ -28,9 +27,6 @@ public class GamePane extends JComponent {
private Game game;
private Color activeColor;
@SuppressWarnings(
"unused"
)
public GamePane() {
setLayout(new BorderLayout());
boardPane = new BoardPane();
@ -38,10 +34,7 @@ public class GamePane extends JComponent {
JPanel toolPanel = new JPanel();
btnRestart = new JButton("Restart");
btnRestart.addActionListener((evt) -> {
if (game != null) game.reset();
game.start();
});
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
activeColor = Color.WHITE;
btnSwapColors = new JButton("Play as black");
@ -67,8 +60,6 @@ public class GamePane extends JComponent {
for (int i = 0; i < 8; i++)
numberPanel.add(new JLabel(String.valueOf(8 - i)));
add(numberPanel, BorderLayout.EAST);
new DropTarget(this, new FENDropTarget(this));
}
public BoardPane getBoardPane() { return boardPane; }

View File

@ -2,6 +2,7 @@ package dev.kske.chess.ui;
import java.awt.EventQueue;
import java.awt.Toolkit;
import java.awt.dnd.DropTarget;
import javax.swing.JFrame;
import javax.swing.JTabbedPane;
@ -20,9 +21,6 @@ public class MainWindow {
/**
* Launch the application.
*/
@SuppressWarnings(
"unused"
)
public static void main(String[] args) {
EventQueue.invokeLater(() -> {
try {
@ -57,6 +55,7 @@ public class MainWindow {
frame.getContentPane().add(tabbedPane);
frame.setJMenuBar(new MenuBar(this));
new DropTarget(frame, new FENDropTarget(this));
// Update position and dimensions
frame.pack();