add progessbar

pgnReader
dstuck 2025-06-25 01:46:15 +02:00
parent 2afe8d406c
commit cdf993d998
4 changed files with 62 additions and 8 deletions

View File

@ -27,16 +27,16 @@ public class ButtonMovePieceListener implements ActionListener {
else else
this.game.playMove(this.mv); this.game.playMove(this.mv);
this.game.setViewPointer(this.game.getMoveList().size() - 1);
if (this.game.isDraw()) { if (this.game.isDraw()) {
this.game.stopClock(); this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished); this.sf.setBoardMode(BoardMode.finished);
this.game.setViewPointer(this.game.getMoveList().size() - 1);
this.sf.enableControlPanelButtons(); this.sf.enableControlPanelButtons();
this.sf.showDraw(); this.sf.showDraw();
} else if (this.game.isMate()) { } else if (this.game.isMate()) {
this.game.stopClock(); this.game.stopClock();
this.sf.setBoardMode(BoardMode.finished); this.sf.setBoardMode(BoardMode.finished);
this.game.setViewPointer(this.game.getMoveList().size() - 1);
this.sf.enableControlPanelButtons(); this.sf.enableControlPanelButtons();
this.sf.showWin(game.getActivePlayer()); this.sf.showWin(game.getActivePlayer());
} else { } else {

View File

@ -114,7 +114,10 @@ public class MainFrame extends JFrame {
public void startGame() { public void startGame() {
if (this.game != null) { if (this.game != null) {
this.game.stopClock();
SpielFrame sf = new SpielFrame(this.game); SpielFrame sf = new SpielFrame(this.game);
sf.setMode(SpielFrame.BoardMode.finished);
sf.enableControlPanelButtons();
} }
} }

View File

@ -1,7 +1,6 @@
package de.mannheim.th.chess.ui; package de.mannheim.th.chess.ui;
import java.util.List; import java.util.List;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent; import java.awt.event.ActionEvent;
import java.awt.event.ActionListener; import java.awt.event.ActionListener;
import java.io.File; import java.io.File;
@ -13,11 +12,14 @@ import javax.swing.JFileChooser;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JList; import javax.swing.JList;
import javax.swing.JPanel; import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.JScrollPane; import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel; import javax.swing.ListSelectionModel;
import javax.swing.SwingWorker;
import com.github.bhlangonijr.chesslib.game.Game; import com.github.bhlangonijr.chesslib.game.Game;
import com.github.bhlangonijr.chesslib.pgn.PgnHolder; import com.github.bhlangonijr.chesslib.pgn.PgnHolder;
import com.github.bhlangonijr.chesslib.pgn.PgnLoadListener;
public class PGNLoaderFrame extends JFrame { public class PGNLoaderFrame extends JFrame {
private PgnHolder pgn; private PgnHolder pgn;
@ -26,11 +28,12 @@ public class PGNLoaderFrame extends JFrame {
private DefaultListModel<String> gameListModel; private DefaultListModel<String> gameListModel;
private JPanel contentPane; private JPanel contentPane;
private JList<String> gameList; private JList<String> gameList;
private JProgressBar progressBar;
public PGNLoaderFrame(MainFrame mf) { public PGNLoaderFrame(MainFrame mf) {
setResizable(true); setResizable(true);
setAlwaysOnTop(true); setAlwaysOnTop(true);
setTitle("Schach"); setTitle("PGNLoader");
setBounds(100, 100, 500, 500); setBounds(100, 100, 500, 500);
contentPane = new JPanel(); contentPane = new JPanel();
@ -65,6 +68,11 @@ public class PGNLoaderFrame extends JFrame {
contentPane.add(scrollPane); contentPane.add(scrollPane);
progressBar = new JProgressBar(0, 100);
progressBar.setValue(0);
progressBar.setStringPainted(true);
contentPane.add(progressBar);
JButton startGameButton = new JButton("Starte Spiel"); JButton startGameButton = new JButton("Starte Spiel");
startGameButton.addActionListener(e -> { startGameButton.addActionListener(e -> {
int index = gameList.getSelectedIndex(); int index = gameList.getSelectedIndex();
@ -82,18 +90,57 @@ public class PGNLoaderFrame extends JFrame {
private void loadFile() { private void loadFile() {
if (this.selectedFile != null) { if (this.selectedFile != null) {
pgn = new PgnHolder(this.selectedFile.getAbsolutePath()); pgn = new PgnHolder(this.selectedFile.getAbsolutePath());
LoadPGNWorker loadPGNWorker = new LoadPGNWorker();
loadPGNWorker.addPropertyChangeListener(e -> {
System.out.println(e.getNewValue());
});
progressBar.setIndeterminate(true);
pgn.getListener().add(loadPGNWorker);
loadPGNWorker.execute();
gameList.revalidate();
}
}
private class LoadPGNWorker extends SwingWorker<Integer, Integer> implements PgnLoadListener {
@Override
protected Integer doInBackground() throws Exception {
try { try {
pgn.loadPgn(); pgn.loadPgn();
games = pgn.getGames(); games = pgn.getGames();
int i = 0; int totalGames = games.size();
for (Game game : games) { for (int i = 0; i < totalGames; i++) {
gameListModel.addElement(i++ + ""); publish(i);
} }
gameList.revalidate();
} catch (Exception e) { } catch (Exception e) {
// TODO: handle exception // TODO: handle exception
} }
return pgn.getSize();
}
@Override
protected void process(List<Integer> chunks) {
for (Integer index : chunks) {
gameListModel.addElement("Game: " + index);
setProgress(Math.min(90, index * 100 / games.size()));
}
}
@Override
protected void done() {
setProgress(100);
progressBar.setValue(100);
progressBar.setIndeterminate(false);
}
@Override
public void notifyProgress(int games) {
setProgress(Math.min(90, games));
progressBar.setValue(Math.min(90, games));
} }
} }

View File

@ -583,6 +583,10 @@ public class SpielFrame extends JFrame {
return clock; return clock;
} }
public void setMode(BoardMode mode) {
this.mode = mode;
}
public void enableControlPanelButtons() { public void enableControlPanelButtons() {
for (Component c : this.controlPanel.getComponents()) { for (Component c : this.controlPanel.getComponents()) {
if (c instanceof JButton) { if (c instanceof JButton) {