| 1 | // Copyright 2004-2007 Jean-Francois Poilpret |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package net.sourceforge.hiveboard.dialog; |
| 16 | |
| 17 | import javax.swing.JLabel; |
| 18 | import javax.swing.JPasswordField; |
| 19 | |
| 20 | import org.jdesktop.application.Action; |
| 21 | import org.jdesktop.application.Task; |
| 22 | |
| 23 | import net.sourceforge.hiveboard.WhiteBoardUserService; |
| 24 | import net.sourceforge.hiveboard.command.AbstractTask; |
| 25 | import net.sourceforge.hivegui.component.TextListener; |
| 26 | import net.sourceforge.hivegui.dialog.AbstractDialogPanel; |
| 27 | |
| 28 | import zappini.designgridlayout.DesignGridLayout; |
| 29 | |
| 30 | public class ChangePasswordPanel extends AbstractDialogPanel |
| 31 | { |
| 32 | static private final String NAME = "password"; |
| 33 | |
| 34 | public ChangePasswordPanel(WhiteBoardUserService service) |
| 35 | { |
| 36 | super(NAME); |
| 37 | _service = service; |
| 38 | |
| 39 | _lblPassword1.setName(NAME + "-new-password-1-label"); |
| 40 | _pwfPassword1.setName(NAME + "-new-password-1"); |
| 41 | _lblPassword2.setName(NAME + "-new-password-2-label"); |
| 42 | _pwfPassword2.setName(NAME + "-new-password-2"); |
| 43 | |
| 44 | _lblPassword1.setLabelFor(_pwfPassword1); |
| 45 | _lblPassword2.setLabelFor(_pwfPassword2); |
| 46 | |
| 47 | DesignGridLayout layout = new DesignGridLayout(this); |
| 48 | setLayout(layout); |
| 49 | |
| 50 | layout.row().label(_lblPassword1).add(_pwfPassword1); |
| 51 | layout.row().label(_lblPassword2).add(_pwfPassword2); |
| 52 | |
| 53 | TextListener listener = new TextListener() |
| 54 | { |
| 55 | @Override protected void contentUpdated() |
| 56 | { |
| 57 | setAcceptEnabled( _pwfPassword1.getPassword().length != 0 |
| 58 | && _pwfPassword2.getPassword().length != 0); |
| 59 | } |
| 60 | }; |
| 61 | _pwfPassword1.getDocument().addDocumentListener(listener); |
| 62 | _pwfPassword2.getDocument().addDocumentListener(listener); |
| 63 | } |
| 64 | |
| 65 | @Override public void reset() |
| 66 | { |
| 67 | _pwfPassword1.setText(""); |
| 68 | _pwfPassword2.setText(""); |
| 69 | setAcceptEnabled(false); |
| 70 | } |
| 71 | |
| 72 | @Action(enabledProperty = "acceptEnabled", block = Task.BlockingScope.WINDOW) |
| 73 | public Task accept() |
| 74 | { |
| 75 | final String pwd1 = new String(_pwfPassword1.getPassword()); |
| 76 | String pwd2 = new String(_pwfPassword2.getPassword()); |
| 77 | if (!pwd1.equals(pwd2)) |
| 78 | { |
| 79 | // Show an error message |
| 80 | _application.showMessage("change-password-different-passwords"); |
| 81 | return null; |
| 82 | } |
| 83 | return new AbstractTask<Void, Void>(_application, _parent) |
| 84 | { |
| 85 | @Override protected Void doInBackground() throws Exception |
| 86 | { |
| 87 | _service.changePassword(pwd1); |
| 88 | return null; |
| 89 | } |
| 90 | }; |
| 91 | } |
| 92 | |
| 93 | final private WhiteBoardUserService _service; |
| 94 | final private JLabel _lblPassword1 = new JLabel(); |
| 95 | final private JPasswordField _pwfPassword1 = new JPasswordField(20); |
| 96 | final private JLabel _lblPassword2 = new JLabel(); |
| 97 | final private JPasswordField _pwfPassword2 = new JPasswordField(20); |
| 98 | } |