001package edu.pdx.cs410J.grader.poa.ui; 002 003import com.google.inject.Inject; 004import com.google.inject.Singleton; 005import edu.pdx.cs410J.grader.poa.EmailCredentialsView; 006 007import javax.swing.*; 008import java.awt.*; 009import java.awt.event.ActionEvent; 010import java.awt.event.FocusEvent; 011import java.awt.event.FocusListener; 012import java.util.ArrayList; 013import java.util.Arrays; 014import java.util.List; 015 016@Singleton 017public class EmailCredentialsDialog extends JDialog implements EmailCredentialsView { 018 019 private final JTextField emailAddressField = new JTextField(30); 020 private final JPasswordField passwordField = new JPasswordField(30); 021 private final JButton okButton = new JButton("OK"); 022 private final JButton cancelButton = new JButton("Cancel"); 023 private final List<SubmitCredentialsListener> submitCredentialsListeners = new ArrayList<>(); 024 025 @Inject 026 public EmailCredentialsDialog(TopLevelJFrame parent) { 027 super(parent, "Enter Email Credentials", true); 028 this.setVisible(false); 029 030 Container rootPane1 = this.getContentPane(); 031 rootPane1.setLayout(new BorderLayout()); 032 rootPane1.add(createCredentialsPanel(), BorderLayout.CENTER); 033 rootPane1.add(createOkCancelPanel(), BorderLayout.SOUTH); 034 035 this.cancelButton.addActionListener(e -> hideDialog()); 036 this.okButton.addActionListener(e -> submitCredentials()); 037 038 this.pack(); 039 040 Dimension dimension = Toolkit.getDefaultToolkit().getScreenSize(); 041 int x = (int) ((dimension.getWidth() - this.getWidth()) / 2); 042 int y = (int) ((dimension.getHeight() - this.getHeight()) / 2); 043 this.setLocation(x, y); 044 045 } 046 047 private JPanel createOkCancelPanel() { 048 JPanel panel = new JPanel(); 049 BoxLayout layout = new BoxLayout(panel, BoxLayout.LINE_AXIS); 050 panel.setLayout(layout); 051 052 panel.add(Box.createHorizontalGlue()); 053 panel.add(okButton); 054 panel.add(cancelButton); 055 056 return panel; 057 } 058 059 private JPanel createCredentialsPanel() { 060 JPanel credentialsPanel = new JPanel(new GridBagLayout()); 061 addComponentOnGrid(new JLabel("Email Address:"), credentialsPanel, 0, 0); 062 addComponentOnGrid(this.emailAddressField, credentialsPanel, 0, 1); 063 addComponentOnGrid(new JLabel("Password:"), credentialsPanel, 1, 0); 064 addComponentOnGrid(this.passwordField, credentialsPanel, 1, 1); 065 066 return credentialsPanel; 067 } 068 069 private void addComponentOnGrid(JComponent component, JPanel container, int row, int column) { 070 GridBagConstraints constraints = new GridBagConstraints(); 071 constraints.fill = GridBagConstraints.HORIZONTAL; 072 constraints.gridx = column; 073 constraints.gridy = row; 074 075 container.add(component, constraints); 076 } 077 078 079 @Override 080 public void setIsVisible(boolean isVisible) { 081 this.pack(); 082 this.setVisible(isVisible); 083 } 084 085 @Override 086 public void addEmailAddressValueListener(EmailAddressValueListener listener) { 087 this.emailAddressField.addFocusListener(new FocusListener() { 088 @Override 089 public void focusGained(FocusEvent e) { 090 091 } 092 093 @Override 094 public void focusLost(FocusEvent e) { 095 listener.setEmailAddress(emailAddressField.getText()); 096 } 097 }); 098 099 this.emailAddressField.addActionListener(e -> { 100 listener.setEmailAddress(emailAddressField.getText()); 101 submitCredentials(); 102 }); 103 } 104 105 @Override 106 public void addPasswordValueListener(PasswordValueListener listener) { 107 this.passwordField.addFocusListener(new FocusListener() { 108 @Override 109 public void focusGained(FocusEvent e) { 110 111 } 112 113 @Override 114 public void focusLost(FocusEvent e) { 115 listener.setPassword(getPasswordFromWidget()); 116 } 117 }); 118 119 this.passwordField.addActionListener(e -> { 120 listener.setPassword(getPasswordFromWidget()); 121 submitCredentials(); 122 }); 123 } 124 125 private void submitCredentials() { 126 this.submitCredentialsListeners.forEach(this::submitCredentials); 127 } 128 129 private String getPasswordFromWidget() { 130 return new String(passwordField.getPassword()); 131 } 132 133 @Override 134 public void addSubmitCredentialsListener(SubmitCredentialsListener listener) { 135 this.submitCredentialsListeners.add(listener); 136 } 137 138 @Override 139 public void setEmailAddress(String emailAddress) { 140 setFieldText(this.emailAddressField, emailAddress); 141 } 142 143 private void setFieldText(JTextField field, String emailAddress) { 144 field.setText(emailAddress); 145 Arrays.asList(field.getActionListeners()).forEach(actionListener -> actionListener.actionPerformed(new ActionEvent(field, 42, "update"))); 146 } 147 148 @Override 149 public void setPassword(String password) { 150 setFieldText(this.passwordField, password); 151 } 152 153 private void submitCredentials(SubmitCredentialsListener listener) { 154 listener.submitCredentials(); 155 hideDialog(); 156 } 157 158 private void hideDialog() { 159 this.setVisible(false); 160 } 161 162}