package com.duesk; import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import com.duesk.api.ApiClient; import com.duesk.api.SessionManager; import com.duesk.api.models.LoginResponse; public class LoginDialog extends JDialog { private JTextField usernameField; private JPasswordField passwordField; private JButton loginButton; private JButton cancelButton; public LoginDialog() { setTitle("DÜSK - Anmeldung"); setModal(true); setSize(400, 250); setLocationRelativeTo(null); setDefaultCloseOperation(DISPOSE_ON_CLOSE); initComponents(); } private void initComponents() { setLayout(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(5, 5, 5, 5); // Titel JLabel titleLabel = new JLabel("DÜSK - Düsseldorfer Schülerinventar"); titleLabel.setFont(new Font("Arial", Font.BOLD, 16)); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; add(titleLabel, gbc); // Benutzername gbc.gridwidth = 1; gbc.gridy = 1; gbc.gridx = 0; add(new JLabel("Benutzername:"), gbc); usernameField = new JTextField("gast", 15); gbc.gridx = 1; add(usernameField, gbc); // Passwort gbc.gridy = 2; gbc.gridx = 0; add(new JLabel("Passwort:"), gbc); passwordField = new JPasswordField("gast", 15); gbc.gridx = 1; add(passwordField, gbc); // Buttons JPanel buttonPanel = new JPanel(new FlowLayout()); loginButton = new JButton("Anmelden"); cancelButton = new JButton("Abbrechen"); loginButton.addActionListener(e -> doLogin()); cancelButton.addActionListener(e -> System.exit(0)); buttonPanel.add(loginButton); buttonPanel.add(cancelButton); gbc.gridy = 3; gbc.gridx = 0; gbc.gridwidth = 2; add(buttonPanel, gbc); // Info JLabel infoLabel = new JLabel("Server: paul-koop.org | Benutzung mit gast/gast möglich"); infoLabel.setFont(new Font("Arial", Font.PLAIN, 10)); infoLabel.setForeground(Color.GRAY); gbc.gridy = 4; add(infoLabel, gbc); } private void doLogin() { String username = usernameField.getText().trim(); String password = new String(passwordField.getPassword()).trim(); if (username.isEmpty() || password.isEmpty()) { JOptionPane.showMessageDialog(this, "Bitte Benutzername und Passwort eingeben!"); return; } loginButton.setEnabled(false); SwingWorker worker = new SwingWorker() { @Override protected LoginResponse doInBackground() throws Exception { return ApiClient.login(username, password); } @Override protected void done() { try { LoginResponse response = get(); if (response.isSuccess() || response.getUserId() != null) { SessionManager.getInstance().setSession(response.getUserId(), response.getSession()); openMainFrame(); dispose(); } else { JOptionPane.showMessageDialog(LoginDialog.this, "Anmeldung fehlgeschlagen: " + response.getError()); } } catch (Exception e) { JOptionPane.showMessageDialog(LoginDialog.this, "Verbindungsfehler: " + e.getMessage()); } finally { loginButton.setEnabled(true); } } }; worker.execute(); } private void openMainFrame() { SwingUtilities.invokeLater(() -> { new MainFrame().setVisible(true); }); } }