View Javadoc
1   package fr.ifremer.reefdb.ui.swing.content.manage.referential.user.local.password;
2   
3   /*
4    * #%L
5    * Reef DB :: UI
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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.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   * Created on 1/29/14.
44   */
45  public class PasswordUIHandler implements UIHandler<PasswordUI> {
46  
47      protected PasswordUI ui;
48      protected ApplicationUIContext context;
49  
50      protected String newPassword;
51  
52      /** {@inheritDoc} */
53      @Override
54      public void beforeInit(PasswordUI ui) {
55          this.ui = ui;
56          this.context = ReefDbUIContext.getInstance();
57      }
58  
59      /** {@inheritDoc} */
60      @Override
61      public void afterInit(PasswordUI ui) {
62          addAutoSelectOnFocus(ui.getPasswordField());
63          addAutoSelectOnFocus(ui.getPassword2Field());
64      }
65  
66      /**
67       * <p>open.</p>
68       *
69       * @param login a {@link java.lang.String} object.
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          // set modality to Toolkit : cause ReefDbMainUI to block until this Dialog is closed
81          ui.setModalityType(Dialog.ModalityType.TOOLKIT_MODAL);
82          ui.setAlwaysOnTop(true);
83          ui.setVisible(true);
84      }
85  
86      /**
87       * <p>cancel.</p>
88       */
89      public void cancel() {
90          newPassword = null;
91          ui.dispose();
92      }
93  
94      /**
95       * <p>accept.</p>
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      * <p>Getter for the field <code>newPassword</code>.</p>
116      *
117      * @return a {@link java.lang.String} object.
118      */
119     protected String getNewPassword() {
120         return newPassword;
121     }
122 
123     /**
124      * <p>addAutoSelectOnFocus.</p>
125      *
126      * @param jTextField a {@link javax.swing.JTextField} object.
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 }