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:
parent
b3c2253c45
commit
73509f2dd1
@ -19,27 +19,26 @@ import java.util.List;
|
|||||||
*/
|
*/
|
||||||
public class FENDropTarget extends DropTargetAdapter {
|
public class FENDropTarget extends DropTargetAdapter {
|
||||||
|
|
||||||
private GamePane gamePane;
|
private MainWindow mainWindow;
|
||||||
|
|
||||||
public FENDropTarget(GamePane gamePane) {
|
public FENDropTarget(MainWindow mainWindow) {
|
||||||
this.gamePane = gamePane;
|
this.mainWindow = mainWindow;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void drop(DropTargetDropEvent evt) {
|
public void drop(DropTargetDropEvent evt) {
|
||||||
try {
|
try {
|
||||||
evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
|
evt.acceptDrop(DnDConstants.ACTION_COPY_OR_MOVE);
|
||||||
@SuppressWarnings(
|
@SuppressWarnings("unchecked")
|
||||||
"unchecked"
|
|
||||||
)
|
|
||||||
List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
|
List<File> droppedFiles = (List<File>) evt.getTransferable().getTransferData(DataFlavor.javaFileListFlavor);
|
||||||
try (BufferedReader br = new BufferedReader(new FileReader(droppedFiles.get(0)))) {
|
try (BufferedReader br = new BufferedReader(new FileReader(droppedFiles.get(0)))) {
|
||||||
gamePane.getGame().reset();
|
mainWindow.getSelectedGamePane().getGame().reset();
|
||||||
gamePane.getGame().getBoard().initFromFEN(br.readLine());
|
mainWindow.getSelectedGamePane().getGame().getBoard().initFromFEN(br.readLine());
|
||||||
gamePane.getBoardPane().getBoardComponent().repaint();
|
mainWindow.getSelectedGamePane().getBoardPane().getBoardComponent().repaint();
|
||||||
gamePane.getGame()
|
mainWindow.getSelectedGamePane()
|
||||||
|
.getGame()
|
||||||
.getPlayers()
|
.getPlayers()
|
||||||
.get(gamePane.getGame().getBoard().getLog().getActiveColor())
|
.get(mainWindow.getSelectedGamePane().getGame().getBoard().getLog().getActiveColor())
|
||||||
.requestMove();
|
.requestMove();
|
||||||
evt.dropComplete(true);
|
evt.dropComplete(true);
|
||||||
}
|
}
|
||||||
|
@ -2,7 +2,6 @@ package dev.kske.chess.ui;
|
|||||||
|
|
||||||
import java.awt.BorderLayout;
|
import java.awt.BorderLayout;
|
||||||
import java.awt.GridLayout;
|
import java.awt.GridLayout;
|
||||||
import java.awt.dnd.DropTarget;
|
|
||||||
|
|
||||||
import javax.swing.JButton;
|
import javax.swing.JButton;
|
||||||
import javax.swing.JComponent;
|
import javax.swing.JComponent;
|
||||||
@ -28,9 +27,6 @@ public class GamePane extends JComponent {
|
|||||||
private Game game;
|
private Game game;
|
||||||
private Color activeColor;
|
private Color activeColor;
|
||||||
|
|
||||||
@SuppressWarnings(
|
|
||||||
"unused"
|
|
||||||
)
|
|
||||||
public GamePane() {
|
public GamePane() {
|
||||||
setLayout(new BorderLayout());
|
setLayout(new BorderLayout());
|
||||||
boardPane = new BoardPane();
|
boardPane = new BoardPane();
|
||||||
@ -38,10 +34,7 @@ public class GamePane extends JComponent {
|
|||||||
|
|
||||||
JPanel toolPanel = new JPanel();
|
JPanel toolPanel = new JPanel();
|
||||||
btnRestart = new JButton("Restart");
|
btnRestart = new JButton("Restart");
|
||||||
btnRestart.addActionListener((evt) -> {
|
btnRestart.addActionListener((evt) -> { if (game != null) game.reset(); game.start(); });
|
||||||
if (game != null) game.reset();
|
|
||||||
game.start();
|
|
||||||
});
|
|
||||||
|
|
||||||
activeColor = Color.WHITE;
|
activeColor = Color.WHITE;
|
||||||
btnSwapColors = new JButton("Play as black");
|
btnSwapColors = new JButton("Play as black");
|
||||||
@ -67,8 +60,6 @@ public class GamePane extends JComponent {
|
|||||||
for (int i = 0; i < 8; i++)
|
for (int i = 0; i < 8; i++)
|
||||||
numberPanel.add(new JLabel(String.valueOf(8 - i)));
|
numberPanel.add(new JLabel(String.valueOf(8 - i)));
|
||||||
add(numberPanel, BorderLayout.EAST);
|
add(numberPanel, BorderLayout.EAST);
|
||||||
|
|
||||||
new DropTarget(this, new FENDropTarget(this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BoardPane getBoardPane() { return boardPane; }
|
public BoardPane getBoardPane() { return boardPane; }
|
||||||
|
@ -2,6 +2,7 @@ package dev.kske.chess.ui;
|
|||||||
|
|
||||||
import java.awt.EventQueue;
|
import java.awt.EventQueue;
|
||||||
import java.awt.Toolkit;
|
import java.awt.Toolkit;
|
||||||
|
import java.awt.dnd.DropTarget;
|
||||||
|
|
||||||
import javax.swing.JFrame;
|
import javax.swing.JFrame;
|
||||||
import javax.swing.JTabbedPane;
|
import javax.swing.JTabbedPane;
|
||||||
@ -20,9 +21,6 @@ public class MainWindow {
|
|||||||
/**
|
/**
|
||||||
* Launch the application.
|
* Launch the application.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings(
|
|
||||||
"unused"
|
|
||||||
)
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
EventQueue.invokeLater(() -> {
|
EventQueue.invokeLater(() -> {
|
||||||
try {
|
try {
|
||||||
@ -57,6 +55,7 @@ public class MainWindow {
|
|||||||
frame.getContentPane().add(tabbedPane);
|
frame.getContentPane().add(tabbedPane);
|
||||||
|
|
||||||
frame.setJMenuBar(new MenuBar(this));
|
frame.setJMenuBar(new MenuBar(this));
|
||||||
|
new DropTarget(frame, new FENDropTarget(this));
|
||||||
|
|
||||||
// Update position and dimensions
|
// Update position and dimensions
|
||||||
frame.pack();
|
frame.pack();
|
||||||
|
Reference in New Issue
Block a user