2022-12-02 09:10:30 +01:00
|
|
|
package Test;
|
|
|
|
|
|
|
|
import javax.swing.*;
|
|
|
|
import java.awt.*;
|
|
|
|
import java.awt.event.ActionEvent;
|
|
|
|
|
|
|
|
public class MainMenu extends JFrame {
|
|
|
|
private final static Dimension MINIMUM_SIZE = new Dimension(960, 600);
|
|
|
|
private final CardLayout cards = new CardLayout(), adminCards = new CardLayout(), profCards = new CardLayout(), studentCards = new CardLayout();
|
|
|
|
private final JPanel adminPanel = new JPanel(), profPanel = new JPanel(), studentPanel = new JPanel();
|
2022-12-02 12:08:10 +01:00
|
|
|
private final Color blue = new Color(53, 242, 242);
|
2022-12-02 11:33:37 +01:00
|
|
|
private JMenuBar menuBar;
|
2022-12-02 09:10:30 +01:00
|
|
|
private Controller listener;
|
|
|
|
private AdminView av;
|
|
|
|
private ProfView pv;
|
|
|
|
private StudentView sv;
|
|
|
|
private JTable table;
|
|
|
|
private int cardIndex = 0;
|
2022-12-02 09:12:33 +01:00
|
|
|
private String[] viewName = {"Administrateur", "Professeur", "Etudiant"};
|
2022-12-02 09:10:30 +01:00
|
|
|
private final JButton[] buttonTab = {
|
|
|
|
new JButton(viewName[0]),
|
|
|
|
new JButton(viewName[1]),
|
|
|
|
new JButton(viewName[2])
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
public MainMenu(Controller listener) {
|
|
|
|
super();
|
|
|
|
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
|
|
|
|
this.setExtendedState(MAXIMIZED_BOTH);
|
|
|
|
this.setMinimumSize(MINIMUM_SIZE);
|
|
|
|
init(listener);
|
|
|
|
this.setVisible(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void init(Controller listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
|
|
|
|
this.av = this.listener.getAdminView();
|
|
|
|
this.pv = this.listener.getProfView();
|
|
|
|
this.sv = this.listener.getStudentView();
|
|
|
|
|
|
|
|
this.table = this.listener.initTable();
|
|
|
|
|
|
|
|
this.setLayout(cards);
|
|
|
|
|
|
|
|
this.add(firstCard());
|
|
|
|
this.add(adminPanel);
|
|
|
|
this.add(profPanel);
|
2022-12-02 11:33:37 +01:00
|
|
|
this.add(studentPanel);
|
2022-12-02 09:10:30 +01:00
|
|
|
|
|
|
|
cards.first(this.getContentPane());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void createJMenuBar() {
|
|
|
|
JMenu menu = new JMenu("Menu"), file = new JMenu("File"), edit = new JMenu("Edit"), view = new JMenu("View");
|
|
|
|
|
|
|
|
JMenuItem adminMenuItem = new JMenuItem("Vu "+viewName[0]);
|
|
|
|
adminMenuItem.addActionListener((event) -> changeView(1));
|
|
|
|
|
2022-12-02 09:14:01 +01:00
|
|
|
JMenuItem profMenuItem = new JMenuItem("Vu "+viewName[1]);
|
2022-12-02 09:10:30 +01:00
|
|
|
profMenuItem.addActionListener((event) -> changeView(2));
|
|
|
|
|
2022-12-02 09:14:01 +01:00
|
|
|
JMenuItem studentMenuItem = new JMenuItem("Vu "+viewName[2]);
|
2022-12-02 09:10:30 +01:00
|
|
|
studentMenuItem.addActionListener((event) -> changeView(3));
|
|
|
|
|
2022-12-02 11:33:37 +01:00
|
|
|
JMenuItem mainMenuItem = new JMenuItem("Retourner au menu principal");
|
|
|
|
mainMenuItem.addActionListener(this::backtoMainMenu);
|
|
|
|
|
2022-12-02 09:10:30 +01:00
|
|
|
JMenuItem quitMenuItem = new JMenuItem("Quitter");
|
|
|
|
quitMenuItem.addActionListener((event) -> System.exit(0));
|
|
|
|
|
|
|
|
menu.add(adminMenuItem);
|
|
|
|
menu.add(profMenuItem);
|
|
|
|
menu.add(studentMenuItem);
|
2022-12-02 11:33:37 +01:00
|
|
|
menu.add(mainMenuItem);
|
2022-12-02 09:10:30 +01:00
|
|
|
menu.add(quitMenuItem);
|
|
|
|
|
|
|
|
file.add(new JMenuItem("Ca c'est juste du style"));
|
|
|
|
edit.add(new JMenuItem("Ca aussi c'est du style"));
|
|
|
|
view.add(new JMenuItem("Encore du style"));
|
|
|
|
|
|
|
|
menuBar.add(menu);
|
|
|
|
menuBar.add(file);
|
|
|
|
menuBar.add(edit);
|
|
|
|
menuBar.add(view);
|
|
|
|
|
|
|
|
this.setJMenuBar(menuBar);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 11:33:37 +01:00
|
|
|
private void backtoMainMenu(ActionEvent e) {
|
|
|
|
this.setJMenuBar(null);
|
|
|
|
cards.first(this.getContentPane());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-12-02 09:10:30 +01:00
|
|
|
private JPanel cardWithTable(JPanel sidePanel) {
|
|
|
|
JPanel panel = new JPanel();
|
|
|
|
JScrollPane scrollPane = new JScrollPane();
|
|
|
|
|
|
|
|
panel.setLayout(new GridLayout(1, 2));
|
|
|
|
|
|
|
|
panel.add(sidePanel);
|
|
|
|
panel.add(this.table);
|
|
|
|
panel.add(scrollPane);
|
|
|
|
scrollPane.setViewportView(this.table);
|
|
|
|
|
|
|
|
return panel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void changeView(int index) {
|
|
|
|
table = this.listener.initTable();
|
|
|
|
|
|
|
|
if (index == 1) {
|
|
|
|
cardIndex = index;
|
|
|
|
this.setTitle(viewName[0]);
|
|
|
|
adminPanel.setLayout(adminCards);
|
|
|
|
adminPanel.add(cardWithTable(adminView()));
|
|
|
|
adminCards.next(adminPanel);
|
|
|
|
menuBar.setBackground(Color.RED);
|
|
|
|
|
|
|
|
} else if (index == 2) {
|
|
|
|
cardIndex = index;
|
|
|
|
this.setTitle(viewName[1]);
|
|
|
|
profPanel.setLayout(profCards);
|
|
|
|
profPanel.add(cardWithTable(profView()));
|
|
|
|
profCards.next(profPanel);
|
|
|
|
menuBar.setBackground(Color.MAGENTA);
|
|
|
|
|
|
|
|
} else if (index == 3){
|
|
|
|
cardIndex = index;
|
|
|
|
this.setTitle(viewName[2]);
|
|
|
|
studentPanel.setLayout(studentCards);
|
|
|
|
studentPanel.add(cardWithTable(studentView()));
|
|
|
|
studentCards.next(studentPanel);
|
2022-12-02 12:08:10 +01:00
|
|
|
menuBar.setBackground(this.blue);
|
2022-12-02 09:10:30 +01:00
|
|
|
}
|
|
|
|
travelThroughCards(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public void updateTable(JTable table) {
|
|
|
|
if (cardIndex == 1) {
|
|
|
|
this.table = table;
|
|
|
|
adminPanel.add(cardWithTable(adminView()));
|
|
|
|
adminCards.next(adminPanel);
|
|
|
|
} else if (cardIndex == 2) {
|
|
|
|
this.table = table;
|
|
|
|
profPanel.add(cardWithTable(profView()));
|
|
|
|
profCards.next(profPanel);
|
|
|
|
} else {
|
|
|
|
this.table = table;
|
|
|
|
studentPanel.add(cardWithTable(profView()));
|
|
|
|
studentCards.next(studentPanel);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel firstCard() {
|
|
|
|
JPanel mainPanel = new JPanel(), centerPanel = new JPanel();
|
|
|
|
Dimension buttonDimension = new Dimension(300, 50);
|
|
|
|
|
|
|
|
mainPanel.setLayout(new BorderLayout());
|
|
|
|
centerPanel.setLayout(new GridBagLayout());
|
|
|
|
|
|
|
|
Insets insets = new Insets(10, 10, 10, 10);
|
|
|
|
GridBagConstraints gbc = new GridBagConstraints();
|
|
|
|
gbc.insets = insets;
|
|
|
|
gbc.gridwidth = 1;
|
|
|
|
gbc.gridx = 0;
|
|
|
|
|
|
|
|
for (int i = 0; i < buttonTab.length; i++) {
|
|
|
|
gbc.gridy = i;
|
|
|
|
buttonTab[i].setPreferredSize(buttonDimension);
|
|
|
|
buttonTab[i].addActionListener(this::action);
|
|
|
|
centerPanel.add(buttonTab[i], gbc);
|
|
|
|
}
|
|
|
|
|
|
|
|
mainPanel.add(centerPanel, BorderLayout.CENTER);
|
|
|
|
|
|
|
|
return mainPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel adminView() {
|
|
|
|
JPanel mainPanel = new JPanel();
|
|
|
|
mainPanel.setLayout(new BorderLayout());
|
|
|
|
mainPanel.add(this.av, BorderLayout.CENTER);
|
|
|
|
return mainPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel profView() {
|
|
|
|
JPanel mainPanel = new JPanel();
|
|
|
|
mainPanel.setLayout(new BorderLayout());
|
|
|
|
mainPanel.add(this.pv, BorderLayout.CENTER);
|
|
|
|
return mainPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private JPanel studentView() {
|
|
|
|
JPanel mainPanel = new JPanel();
|
|
|
|
mainPanel.setLayout(new BorderLayout());
|
|
|
|
mainPanel.add(this.sv, BorderLayout.CENTER);
|
|
|
|
return mainPanel;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void action(ActionEvent e) {
|
|
|
|
JButton origin = (JButton) e.getSource();
|
2022-12-02 11:33:37 +01:00
|
|
|
|
|
|
|
if (menuBar == null) {
|
|
|
|
menuBar = new JMenuBar();
|
|
|
|
createJMenuBar();
|
|
|
|
} else {
|
|
|
|
this.setJMenuBar(menuBar);
|
|
|
|
}
|
2022-12-02 09:10:30 +01:00
|
|
|
|
2022-12-02 09:16:02 +01:00
|
|
|
if (origin.getText().equals(viewName[0])) {
|
2022-12-02 09:10:30 +01:00
|
|
|
this.setTitle(viewName[0]);
|
|
|
|
menuBar.setBackground(Color.RED);
|
|
|
|
adminPanel.setLayout(adminCards);
|
|
|
|
adminPanel.add(cardWithTable(adminView()));
|
2022-12-02 11:33:37 +01:00
|
|
|
adminCards.next(adminPanel);
|
2022-12-02 09:10:30 +01:00
|
|
|
cardIndex = 1;
|
|
|
|
|
2022-12-02 09:16:02 +01:00
|
|
|
} else if (origin.getText().equals(viewName[1])) {
|
2022-12-02 09:10:30 +01:00
|
|
|
this.setTitle(viewName[1]);
|
|
|
|
menuBar.setBackground(Color.MAGENTA);
|
|
|
|
profPanel.setLayout(profCards);
|
|
|
|
profPanel.add(cardWithTable(profView()));
|
2022-12-02 11:33:37 +01:00
|
|
|
profCards.next(profPanel);
|
2022-12-02 09:10:30 +01:00
|
|
|
cardIndex = 2;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
this.setTitle(viewName[2]);
|
2022-12-02 12:08:10 +01:00
|
|
|
menuBar.setBackground(this.blue);
|
2022-12-02 09:10:30 +01:00
|
|
|
studentPanel.setLayout(studentCards);
|
|
|
|
studentPanel.add(cardWithTable(studentView()));
|
2022-12-02 11:33:37 +01:00
|
|
|
studentCards.next(studentPanel);
|
2022-12-02 09:10:30 +01:00
|
|
|
cardIndex = 3;
|
|
|
|
}
|
|
|
|
|
|
|
|
travelThroughCards(cardIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private void travelThroughCards(int index) {
|
|
|
|
cards.first(this.getContentPane());
|
|
|
|
|
|
|
|
for (int i = 0; i < index; i++) {
|
|
|
|
cards.next(this.getContentPane());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|