Files
FIProjetIHM2022/src/Test/MainMenu.java

183 lines
5.2 KiB
Java
Raw Normal View History

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, 540);
private CardLayout cards = new CardLayout(), adminCards = new CardLayout(), profCards = new CardLayout();
private JPanel adminPanel = new JPanel(), profPanel = new JPanel();
private JMenuBar menuBar = new JMenuBar();
private AdminView av;
private ProfView pv;
private JTable table;
private int cardIndex = 0;
private JButton[] buttonTab = {
new JButton("Admin"),
new JButton("Prof"),
new JButton("Student")
};
public MainMenu() {
super();
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setExtendedState(MAXIMIZED_BOTH);
this.setMinimumSize(MINIMUM_SIZE);
init();
this.setVisible(true);
}
private void init() {
BDatabase db = new BDatabase();
Controller listener = new Controller(db, this);
this.av = new AdminView(listener.getEtudiants(), listener.getGroupes(), listener);
this.pv = new ProfView(listener.getEtudiants(), listener.getGroupes(), listener);
listener.setAv(av);
listener.setPv(pv);
this.table = listener.initTable();
createJMenuBar();
this.setLayout(cards);
this.add(first());
this.add(adminPanel);
this.add(profPanel);
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("Admin View");
JMenuItem profMenuItem = new JMenuItem("Professor View");
JMenuItem studentMenuItem = new JMenuItem("Student View");
JMenuItem quitMenuItem = new JMenuItem("Quit");
quitMenuItem.addActionListener(this::quit);
menu.add(adminMenuItem);
menu.add(profMenuItem);
menu.add(studentMenuItem);
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);
}
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;
}
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 {
}
}
private JPanel first() {
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.add(av);
return mainPanel;
}
private JPanel profView() {
JPanel mainPanel = new JPanel();
mainPanel.add(pv);
return mainPanel;
}
private void action(ActionEvent e) {
JButton origin = (JButton) e.getSource();
System.out.println(origin.getText());
if (origin.getText() == "Admin") {
adminPanel.setLayout(adminCards);
adminPanel.add(cardWithTable(adminView()));
cards.next(this.getContentPane());
cardIndex = 1;
} else if (origin.getText() == "Prof") {
profPanel.setLayout(profCards);
profPanel.add(cardWithTable(profView()));
cards.next(this.getContentPane());
cards.next(this.getContentPane());
cardIndex = 2;
} else {
JOptionPane.showMessageDialog(null, "En travaux");
//cardIndex = 3;
}
}
private void quit(ActionEvent e) {
System.exit(0);
}
}