View Javadoc
1   package fr.ifremer.quadrige3.ui.swing.content.login;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 UI Common
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  
26  import fr.ifremer.quadrige3.ui.swing.AbstractUIHandler;
27  import org.apache.commons.lang3.StringUtils;
28  import org.nuiton.jaxx.application.swing.util.Cancelable;
29  
30  import javax.swing.*;
31  import java.awt.event.ActionEvent;
32  
33  import static org.nuiton.i18n.I18n.t;
34  
35  /**
36   * Created on 1/29/14.
37   */
38  public class LoginUIHandler extends AbstractUIHandler<LoginUIModel, LoginUI> implements Cancelable {
39  
40      private long delay;
41  
42      /** {@inheritDoc} */
43      @Override
44      public void beforeInit(LoginUI ui) {
45          super.beforeInit(ui);
46  
47          ui.setContextValue(new LoginUIModel());
48      }
49  
50      /** {@inheritDoc} */
51      @Override
52      public void afterInit(LoginUI ui) {
53          initUI(ui);
54  
55          addAutoSelectOnFocus(ui.getLoginField());
56          addAutoSelectOnFocus(ui.getPasswordField());
57  
58          // add listener on enter key (Mantis #47698)
59          delay = System.currentTimeMillis() + 100;
60          KeyStroke enterKeyStroke = KeyStroke.getKeyStroke("pressed ENTER");
61          Action acceptAction = new AbstractAction() {
62              @Override
63              public void actionPerformed(ActionEvent e) {
64                  if (getUI().getAcceptButton().isEnabled()
65                          // add a small tempo to prevent too fast event (Mantis #48304)
66                          && System.currentTimeMillis() > delay)
67                      SwingUtilities.invokeLater(() -> accept());
68              }
69          };
70          ui.getLoginField().getKeymap().addActionForKeyStroke(enterKeyStroke, acceptAction);
71          ui.getPasswordField().getKeymap().addActionForKeyStroke(enterKeyStroke, acceptAction);
72  
73          getModel().addPropertyChangeListener(LoginUIModel.PROPERTY_URL, evt -> {
74              getUI().getInfoMessage().setVisible(StringUtils.isNotBlank(getModel().getUrl()));
75              getUI().getInfoMessage().setText(t("quadrige3.login.infoMessage", getModel().getUrl()));
76          });
77      }
78  
79      @Override
80      protected JComponent getComponentToFocus() {
81          return getModel().getLogin() == null ? getUI().getLoginField() : getUI().getPasswordField();
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public void cancel() {
87          getModel().setLogin(null);
88          getModel().setPassword(null);
89          closeDialog();
90      }
91  
92      /**
93       * <p>accept.</p>
94       */
95      public void accept() {
96          closeDialog();
97      }
98  
99      @Override
100     public void closeDialog() {
101 
102         // remove key bindings (Mantis #48378)
103         getUI().getLoginField().getKeymap().removeBindings();
104         getUI().getPasswordField().getKeymap().removeBindings();
105 
106         super.closeDialog();
107     }
108 }