package com.duesk; import javax.swing.*; import javax.swing.table.AbstractTableModel; import java.awt.*; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import com.duesk.api.ApiClient; import com.duesk.api.SessionManager; import com.duesk.api.models.Profile; public class MainFrame extends JFrame { private JTable profileTable; private ProfileTableModel tableModel; private List profiles = new ArrayList<>(); private JLabel statusLabel; public MainFrame() { setTitle("DÜSK - Düsseldorfer Schülerinventar"); setSize(900, 600); setLocationRelativeTo(null); setDefaultCloseOperation(EXIT_ON_CLOSE); initComponents(); loadProfiles(); } private void initComponents() { setLayout(new BorderLayout()); // Toolbar JToolBar toolbar = new JToolBar(); toolbar.setFloatable(false); JButton newBtn = new JButton("Neues Profil"); JButton refreshBtn = new JButton("Aktualisieren"); JButton groupsBtn = new JButton("Gruppen"); JButton logoutBtn = new JButton("Abmelden"); newBtn.addActionListener(e -> newProfile()); refreshBtn.addActionListener(e -> loadProfiles()); groupsBtn.addActionListener(e -> manageGroups()); logoutBtn.addActionListener(e -> logout()); toolbar.add(newBtn); toolbar.add(refreshBtn); toolbar.add(groupsBtn); toolbar.add(Box.createHorizontalGlue()); toolbar.add(logoutBtn); add(toolbar, BorderLayout.NORTH); // Tabelle tableModel = new ProfileTableModel(); profileTable = new JTable(tableModel); profileTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); profileTable.addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { if (e.getClickCount() == 2) { viewProfile(); } } }); // Kontextmenü JPopupMenu popupMenu = new JPopupMenu(); JMenuItem viewItem = new JMenuItem("Anzeigen"); JMenuItem editItem = new JMenuItem("Bearbeiten"); JMenuItem deleteItem = new JMenuItem("Löschen"); viewItem.addActionListener(e -> viewProfile()); editItem.addActionListener(e -> editProfile()); deleteItem.addActionListener(e -> deleteProfile()); popupMenu.add(viewItem); popupMenu.add(editItem); popupMenu.add(deleteItem); profileTable.setComponentPopupMenu(popupMenu); add(new JScrollPane(profileTable), BorderLayout.CENTER); // Statusleiste statusLabel = new JLabel("Bereit"); statusLabel.setBorder(BorderFactory.createEtchedBorder()); add(statusLabel, BorderLayout.SOUTH); } private void loadProfiles() { statusLabel.setText("Lade Profile..."); SwingWorker, Void> worker = new SwingWorker, Void>() { @Override protected List doInBackground() throws Exception { return ApiClient.getProfiles(); } @Override protected void done() { try { profiles = get(); if (profiles != null) { tableModel.setProfiles(profiles); statusLabel.setText(profiles.size() + " Profile geladen"); } else { statusLabel.setText("Keine Profile gefunden"); } } catch (Exception e) { statusLabel.setText("Fehler: " + e.getMessage()); JOptionPane.showMessageDialog(MainFrame.this, "Fehler beim Laden: " + e.getMessage()); } } }; worker.execute(); } private void newProfile() { ProfileEditDialog dialog = new ProfileEditDialog(this, null); dialog.setVisible(true); if (dialog.isSaved()) { loadProfiles(); } } private void editProfile() { int selectedRow = profileTable.getSelectedRow(); if (selectedRow >= 0 && selectedRow < profiles.size()) { Profile profile = profiles.get(selectedRow); ProfileEditDialog dialog = new ProfileEditDialog(this, profile); dialog.setVisible(true); if (dialog.isSaved()) { loadProfiles(); } } else { JOptionPane.showMessageDialog(this, "Bitte wählen Sie ein Profil aus."); } } private void viewProfile() { int selectedRow = profileTable.getSelectedRow(); if (selectedRow >= 0 && selectedRow < profiles.size()) { Profile profile = profiles.get(selectedRow); new ProfileDetailDialog(this, profile).setVisible(true); } else { JOptionPane.showMessageDialog(this, "Bitte wählen Sie ein Profil aus."); } } private void deleteProfile() { int selectedRow = profileTable.getSelectedRow(); if (selectedRow >= 0 && selectedRow < profiles.size()) { Profile profile = profiles.get(selectedRow); int confirm = JOptionPane.showConfirmDialog(this, "Möchten Sie das Profil '" + profile.getName() + "' wirklich löschen?", "Profil löschen", JOptionPane.YES_NO_OPTION); if (confirm == JOptionPane.YES_OPTION) { SwingWorker worker = new SwingWorker() { @Override protected Boolean doInBackground() throws Exception { return ApiClient.deleteProfile(profile.getProfileId()); } @Override protected void done() { try { if (get()) { loadProfiles(); } else { JOptionPane.showMessageDialog(MainFrame.this, "Fehler beim Löschen des Profils"); } } catch (Exception e) { JOptionPane.showMessageDialog(MainFrame.this, "Fehler: " + e.getMessage()); } } }; worker.execute(); } } else { JOptionPane.showMessageDialog(this, "Bitte wählen Sie ein Profil aus."); } } private void manageGroups() { GroupManagerDialog dialog = new GroupManagerDialog(this); dialog.setVisible(true); loadProfiles(); } private void logout() { SessionManager.getInstance().clear(); dispose(); new LoginDialog().setVisible(true); } private class ProfileTableModel extends AbstractTableModel { private final String[] columns = {"Name", "Gruppe", "ProfilID"}; private List data = new ArrayList<>(); public void setProfiles(List profiles) { this.data = profiles; fireTableDataChanged(); } @Override public int getRowCount() { return data.size(); } @Override public int getColumnCount() { return 3; } @Override public String getColumnName(int col) { return columns[col]; } @Override public Object getValueAt(int row, int col) { Profile p = data.get(row); switch (col) { case 0: return p.getName(); case 1: return p.getGroupName(); case 2: return p.getProfileId(); default: return null; } } } }