1 package fr.ifremer.reefdb.ui.swing.content.manage.referential.user.local.password;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 import fr.ifremer.quadrige3.ui.swing.ApplicationUIContext;
27 import fr.ifremer.reefdb.ui.swing.ReefDbUIContext;
28 import jaxx.runtime.SwingUtil;
29 import jaxx.runtime.spi.UIHandler;
30
31 import javax.swing.JOptionPane;
32 import javax.swing.JTextField;
33 import javax.swing.SwingUtilities;
34 import java.awt.Container;
35 import java.awt.Dialog;
36 import java.awt.event.FocusAdapter;
37 import java.awt.event.FocusEvent;
38 import java.util.Arrays;
39
40 import static org.nuiton.i18n.I18n.t;
41
42
43
44
45 public class PasswordUIHandler implements UIHandler<PasswordUI> {
46
47 protected PasswordUI ui;
48 protected ApplicationUIContext context;
49
50 protected String newPassword;
51
52
53 @Override
54 public void beforeInit(PasswordUI ui) {
55 this.ui = ui;
56 this.context = ReefDbUIContext.getInstance();
57 }
58
59
60 @Override
61 public void afterInit(PasswordUI ui) {
62 addAutoSelectOnFocus(ui.getPasswordField());
63 addAutoSelectOnFocus(ui.getPassword2Field());
64 }
65
66
67
68
69
70
71 public void open(String login) {
72
73 ui.getInfoMessage().setText(t("reefdb.user.password.infoMessage", login));
74 ui.pack();
75
76 Container parent = context.getMainUI();
77 if (parent != null) {
78 SwingUtil.center(parent, ui);
79 }
80
81 ui.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
82 ui.setAlwaysOnTop(true);
83 ui.setVisible(true);
84 }
85
86
87
88
89 public void cancel() {
90 newPassword = null;
91 ui.dispose();
92 }
93
94
95
96
97 public void accept() {
98
99 if (!Arrays.equals(ui.getPasswordField().getPassword(), ui.getPassword2Field().getPassword())) {
100 return;
101 }
102
103 if (ui.getPasswordField().getPassword().length == 0) {
104 if (context.getDialogHelper().showConfirmDialog(ui, t("reefdb.user.password.reset"), JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) {
105 return;
106 }
107 newPassword = "";
108 } else {
109 newPassword = String.copyValueOf(ui.getPasswordField().getPassword());
110 }
111 ui.dispose();
112 }
113
114
115
116
117
118
119 protected String getNewPassword() {
120 return newPassword;
121 }
122
123
124
125
126
127
128 protected void addAutoSelectOnFocus(JTextField jTextField) {
129 jTextField.addFocusListener(new FocusAdapter() {
130 @Override
131 public void focusGained(final FocusEvent e) {
132 SwingUtilities.invokeLater(() -> {
133 JTextField source = (JTextField) e.getSource();
134 source.selectAll();
135 });
136
137 }
138 });
139 }
140 }