Amélioration

This commit is contained in:
2024-12-05 16:02:08 +01:00
parent 7d1dbf29da
commit d7a23c63d4
4 changed files with 349 additions and 80 deletions

View File

@@ -1,65 +1,214 @@
package fr.monkhanny.dorfromantik.gui;
import fr.monkhanny.dorfromantik.components.Title;
import fr.monkhanny.dorfromantik.utils.Database;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;
import java.util.Calendar;
public class GameModeSelectionPanel extends JPanel {
private JLabel titleLabel;
private JButton mode1Button;
private JButton mode2Button;
private JButton mode3Button;
private JButton mode4Button;
private JTextField seedField;
private JButton startButton;
private List<JButton> seriesButtons;
private Database database;
private JPanel modePanel;
private JButton prevButton;
private JButton nextButton;
private int currentPage = 1;
private int itemsPerPage = 15;
private JLabel pageLabel;
private JSpinner startDateSpinner;
private JSpinner endDateSpinner;
public GameModeSelectionPanel(ActionListener buttonListener, JFrame gameModeFrame, MainMenu mainMenu) {
// Initialize database
try {
database = new Database();
} catch (SQLException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(null, "Erreur de connexion à la base de données", "Erreur", JOptionPane.ERROR_MESSAGE);
return;
}
setLayout(new BorderLayout());
// Ajouter l'image de fond
// Background setup
JLabel background = new JLabel(new ImageIcon("./ressources/images/MainMenu/backgroundBlured.jpg"));
background.setLayout(new BorderLayout()); // Utilisation de BorderLayout ici
background.setLayout(new BorderLayout());
this.add(background);
// Créer un topPanel avec le bouton de retour
// Top panel setup
JPanel topPanel = createTopPanel(gameModeFrame, mainMenu);
background.add(topPanel, BorderLayout.NORTH); // Placer topPanel en haut à gauche
background.add(topPanel, BorderLayout.NORTH);
// Panel principal (au centre)
// Main panel setup
JPanel mainPanel = createMainPanel();
background.add(mainPanel, BorderLayout.CENTER); // Placer le contenu principal sous le bouton
background.add(mainPanel, BorderLayout.CENTER);
// Title
titleLabel = new Title("Choisissez un mode de jeu", 60f, Color.WHITE);
titleLabel = new Title("Choisissez un série", 60f, Color.WHITE);
mainPanel.add(titleLabel, createGridBagConstraints(0, 0, 2));
mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 1, 1));
// Mode buttons - now horizontally aligned
JPanel modePanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 20, 10)); // Centered with horizontal spacing
modePanel.setOpaque(false);
mode1Button = createGameModeButton("Série 1", buttonListener);
mode2Button = createGameModeButton("Série 2", buttonListener);
mode3Button = createGameModeButton("Série 3", buttonListener);
mode4Button = createGameModeButton("Série 4", buttonListener);
modePanel.add(mode1Button);
modePanel.add(mode2Button);
modePanel.add(mode3Button);
modePanel.add(mode4Button);
mainPanel.add(modePanel, createGridBagConstraints(0, 2, 2)); // Span across 2 columns
// Date range filter panel
JPanel dateFilterPanel = createDateFilterPanel();
mainPanel.add(dateFilterPanel, createGridBagConstraints(0, 2, 2));
mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 3, 1));
// Series buttons panel with pagination
modePanel = new JPanel();
modePanel.setOpaque(false);
seriesButtons = new ArrayList<>();
// Pagination panel
JPanel paginationPanel = createPaginationPanel(buttonListener);
// Load initial series
loadSeriesForCurrentPage();
mainPanel.add(modePanel, createGridBagConstraints(0, 3, 2));
mainPanel.add(paginationPanel, createGridBagConstraints(0, 4, 2));
mainPanel.add(Box.createVerticalStrut(30), createGridBagConstraints(0, 5, 1));
// Seed input and start button
JPanel seedPanel = createSeedPanel(buttonListener);
mainPanel.add(seedPanel, createGridBagConstraints(0, 4, 2));
mainPanel.add(seedPanel, createGridBagConstraints(0, 6, 2));
}
private void loadSeriesForCurrentPage() {
// Clear existing buttons
modePanel.removeAll();
seriesButtons.clear();
// Récupérer les dates des spinners
Date startDate = (Date) startDateSpinner.getValue();
Date endDate = (Date) endDateSpinner.getValue();
// Get paginated series
List<String> series = database.getSeriesByDateRangePaginated(startDate, endDate,
currentPage,
itemsPerPage);
// Si aucune série n'est trouvée pour la période donnée, afficher un message
if (series.isEmpty()) {
JOptionPane.showMessageDialog(this, "Aucune série pour la période donnée",
"Avertissement", JOptionPane.WARNING_MESSAGE);
return;
}
// Calculate grid layout dimensions
int buttonCount = series.size();
int columns = Math.min(buttonCount, 5); // Maximum 5 buttons per row
int rows = (int) Math.ceil((double) buttonCount / columns);
modePanel.setLayout(new GridLayout(rows, columns, 20, 10)); // Add spacing between buttons
for (String seriesName : series) {
JButton seriesButton = createGameModeButton(seriesName, null);
modePanel.add(seriesButton);
seriesButtons.add(seriesButton);
}
// Update page label
int totalSeries = database.countSeriesByDateRange(startDate, endDate);
int totalPages = (int) Math.ceil((double) totalSeries / itemsPerPage);
pageLabel.setText("Page " + currentPage + " / " + totalPages);
// Enable/disable pagination buttons
prevButton.setEnabled(currentPage > 1);
nextButton.setEnabled(currentPage < totalPages);
// Refresh the panel
modePanel.revalidate();
modePanel.repaint();
}
private JPanel createDateFilterPanel() {
JPanel datePanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
datePanel.setOpaque(false);
JLabel startLabel = new JLabel("Date de début:");
startLabel.setForeground(Color.WHITE);
JLabel endLabel = new JLabel("Date de fin:");
endLabel.setForeground(Color.WHITE);
// Créer des spinners pour les dates de début et de fin
Calendar cal = Calendar.getInstance();
Date endDate = cal.getTime();
cal.add(Calendar.DAY_OF_YEAR, -30);
Date startDate = cal.getTime();
SpinnerDateModel startDateModel = new SpinnerDateModel(
startDate, // Date par défaut (30 jours avant aujourd'hui)
null, // Pas de date minimale
endDate, // Date maximale = aujourd'hui
Calendar.DAY_OF_MONTH // Unité de changement
);
startDateSpinner = new JSpinner(startDateModel);
startDateSpinner.setEditor(new JSpinner.DateEditor(startDateSpinner, "yyyy-MM-dd"));
SpinnerDateModel endDateModel = new SpinnerDateModel(
endDate, // Date par défaut = aujourd'hui
null, // Pas de date minimale
endDate, // Date maximale = aujourd'hui
Calendar.DAY_OF_MONTH // Unité de changement
);
endDateSpinner = new JSpinner(endDateModel);
endDateSpinner.setEditor(new JSpinner.DateEditor(endDateSpinner, "yyyy-MM-dd"));
JButton filterButton = new JButton("Filtrer");
filterButton.addActionListener(e -> {
// Réinitialiser à la première page lors du filtrage
currentPage = 1;
loadSeriesForCurrentPage();
});
datePanel.add(startLabel);
datePanel.add(startDateSpinner);
datePanel.add(endLabel);
datePanel.add(endDateSpinner);
datePanel.add(filterButton);
return datePanel;
}
private JPanel createPaginationPanel(ActionListener buttonListener) {
JPanel paginationPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
paginationPanel.setOpaque(false);
prevButton = new JButton("Précédent");
prevButton.addActionListener(e -> {
if (currentPage > 1) {
currentPage--;
loadSeriesForCurrentPage();
}
});
nextButton = new JButton("Suivant");
nextButton.addActionListener(e -> {
currentPage++;
loadSeriesForCurrentPage();
});
pageLabel = new JLabel("Page 1");
pageLabel.setForeground(Color.WHITE);
paginationPanel.add(prevButton);
paginationPanel.add(pageLabel);
paginationPanel.add(nextButton);
return paginationPanel;
}
private JPanel createTopPanel(JFrame gameModeFrame, MainMenu mainMenu) {
@@ -113,13 +262,22 @@ public class GameModeSelectionPanel extends JPanel {
private JButton createGameModeButton(String modeName, ActionListener buttonListener) {
JButton button = new JButton(modeName);
button.setFont(new Font("Arial", Font.BOLD, 24));
button.setFont(new Font("Arial", Font.BOLD, 18)); // Slightly smaller font to accommodate longer text
button.setBackground(new Color(0, 122, 255));
button.setForeground(Color.WHITE);
button.setBorder(BorderFactory.createLineBorder(Color.WHITE, 2));
button.setFocusPainted(false);
button.addActionListener(buttonListener);
button.setPreferredSize(new Dimension(150, 50)); // Adjust the size of the buttons
// Calculate button width dynamically based on text length
FontMetrics metrics = button.getFontMetrics(button.getFont());
int textWidth = metrics.stringWidth(modeName);
int buttonWidth = Math.max(200, textWidth + 40); // Minimum 200, with 20px padding on each side
button.setPreferredSize(new Dimension(buttonWidth, 50));
button.setMinimumSize(new Dimension(buttonWidth, 50));
button.setMaximumSize(new Dimension(buttonWidth, 50));
return button;
}