From 513c0077e0dfe61280c5afd923dff502f5904dd0 Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 24 Aug 2019 16:04:09 +0200 Subject: [PATCH] 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. --- src/dev/kske/chess/ui/FENDropTarget.java | 21 ++++++++++----------- src/dev/kske/chess/ui/GamePane.java | 11 +---------- src/dev/kske/chess/ui/MainWindow.java | 5 ++--- 3 files changed, 13 insertions(+), 24 deletions(-) diff --git a/src/dev/kske/chess/ui/FENDropTarget.java b/src/dev/kske/chess/ui/FENDropTarget.java index ccb6526..dfbd39a 100644 --- a/src/dev/kske/chess/ui/FENDropTarget.java +++ b/src/dev/kske/chess/ui/FENDropTarget.java @@ -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 droppedFiles = (List) 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); } diff --git a/src/dev/kske/chess/ui/GamePane.java b/src/dev/kske/chess/ui/GamePane.java index 7dc2bcc..2e83c07 100644 --- a/src/dev/kske/chess/ui/GamePane.java +++ b/src/dev/kske/chess/ui/GamePane.java @@ -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; } diff --git a/src/dev/kske/chess/ui/MainWindow.java b/src/dev/kske/chess/ui/MainWindow.java index bd5b2b4..d95ae8a 100644 --- a/src/dev/kske/chess/ui/MainWindow.java +++ b/src/dev/kske/chess/ui/MainWindow.java @@ -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();