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.ButtonGroup; |
18 | import javax.swing.JCheckBox; |
19 | import javax.swing.JLabel; |
20 | import javax.swing.JPasswordField; |
21 | import javax.swing.JRadioButton; |
22 | import javax.swing.JTextField; |
23 | |
24 | import org.jdesktop.application.Action; |
25 | import org.jdesktop.application.Task; |
26 | |
27 | import net.sourceforge.hiveboard.Account; |
28 | import net.sourceforge.hiveboard.AccountRole; |
29 | import net.sourceforge.hiveboard.WhiteBoardUserService; |
30 | import net.sourceforge.hiveboard.command.AbstractTask; |
31 | import net.sourceforge.hivegui.application.HiveGuiApplicationMain; |
32 | import net.sourceforge.hivegui.component.TextListener; |
33 | import net.sourceforge.hivegui.dialog.GenericDialog; |
34 | import net.sourceforge.hivetranse.exception.DuplicateKeyException; |
35 | |
36 | import zappini.designgridlayout.DesignGridLayout; |
37 | import zappini.designgridlayout.Row; |
38 | |
39 | public class AccountPanel extends AbstractAccountPanel |
40 | { |
41 | static private final String NAME_CREATE = "account-creation"; |
42 | static private final String NAME_MODIFY = "account-modification"; |
43 | |
44 | public AccountPanel(WhiteBoardUserService service) |
45 | { |
46 | this(service, null, true); |
47 | } |
48 | |
49 | public AccountPanel(WhiteBoardUserService service, Account account) |
50 | { |
51 | this(service, account.copy(), false); |
52 | } |
53 | |
54 | private AccountPanel( WhiteBoardUserService service, |
55 | Account account, |
56 | boolean create) |
57 | { |
58 | super(create ? NAME_CREATE : NAME_MODIFY); |
59 | _service = service; |
60 | _account = account; |
61 | _create = create; |
62 | |
63 | DesignGridLayout layout = new DesignGridLayout(this); |
64 | setLayout(layout); |
65 | |
66 | initComponentNames(); |
67 | |
68 | _lblVisa.setLabelFor(_txfVisa); |
69 | _lblPassword.setLabelFor(_txfPassword); |
70 | _lblName.setLabelFor(_txfName); |
71 | _lblEmail.setLabelFor(_txfEmail); |
72 | _lblRole.setLabelFor(_rbnRoleBasic); |
73 | |
74 | ButtonGroup group = new ButtonGroup(); |
75 | group.add(_rbnRoleBasic); |
76 | group.add(_rbnRoleInitiator); |
77 | |
78 | // Add widgets to the panel |
79 | layout.row().label(_lblVisa).add(_txfVisa).add(Row.EMPTY); |
80 | if (_create) |
81 | { |
82 | layout.row().label(_lblPassword).add(_txfPassword).add(Row.EMPTY); |
83 | } |
84 | layout.row().label(_lblName).add(_txfName); |
85 | layout.row().label(_lblEmail).add(_txfEmail); |
86 | layout.row().label(_lblRole).add(_rbnRoleBasic).add(_rbnRoleInitiator); |
87 | layout.row().add(_cbxAdmin); |
88 | |
89 | TextListener listener = new TextListener() |
90 | { |
91 | @Override protected void contentUpdated() |
92 | { |
93 | boolean enabled = true; |
94 | if ( _txfVisa.getText() == null |
95 | || _txfVisa.getText().length() == 0 |
96 | || _txfName.getText() == null |
97 | || _txfName.getText().length() == 0) |
98 | { |
99 | enabled = false; |
100 | } |
101 | if ( _create |
102 | && ( _txfPassword.getPassword() == null |
103 | || _txfPassword.getPassword().length == 0)) |
104 | { |
105 | enabled = false; |
106 | } |
107 | setAcceptEnabled(enabled); |
108 | } |
109 | }; |
110 | |
111 | _txfVisa.getDocument().addDocumentListener(listener); |
112 | _txfPassword.getDocument().addDocumentListener(listener); |
113 | _txfName.getDocument().addDocumentListener(listener); |
114 | |
115 | if (!_create) |
116 | { |
117 | _lblPassword.setVisible(false); |
118 | _txfPassword.setVisible(false); |
119 | } |
120 | } |
121 | |
122 | private void initComponentNames() |
123 | { |
124 | String name = getName(); |
125 | _lblVisa.setName(name + "-visa-label"); |
126 | _txfVisa.setName(name + "-visa"); |
127 | _lblPassword.setName(name + "-password-label"); |
128 | _txfPassword.setName(name + "-password"); |
129 | _lblName.setName(name + "-name-label"); |
130 | _txfName.setName(name + "-name"); |
131 | _lblEmail.setName(name + "-email-label"); |
132 | _txfEmail.setName(name + "-email"); |
133 | _cbxAdmin.setName(name + "-isadmin"); |
134 | _lblRole.setName(name + "-role-label"); |
135 | _rbnRoleBasic.setName(name + "-role-basic"); |
136 | _rbnRoleInitiator.setName(name + "-role-initiator"); |
137 | } |
138 | |
139 | @Override public void reset() |
140 | { |
141 | if (_create) |
142 | { |
143 | // If dialog for account creation, reinitialize a new Account |
144 | _account = new Account(); |
145 | setAcceptEnabled(false); |
146 | } |
147 | refresh(); |
148 | } |
149 | |
150 | @Action(enabledProperty = "acceptEnabled", block = Task.BlockingScope.WINDOW) |
151 | public Task accept() |
152 | { |
153 | updateAccount(); |
154 | return new ModifyAccountTask(_application, _parent); |
155 | } |
156 | |
157 | private void updateAccount() |
158 | { |
159 | _account.setVisa(_txfVisa.getText()); |
160 | _account.setPassword(new String(_txfPassword.getPassword())); |
161 | _account.setName(_txfName.getText()); |
162 | _account.setEmail(_txfEmail.getText()); |
163 | if (_rbnRoleInitiator.isSelected()) |
164 | { |
165 | _account.setRole(AccountRole.INITIATOR); |
166 | } |
167 | else |
168 | { |
169 | _account.setRole(AccountRole.BASIC); |
170 | } |
171 | _account.setAdmin(_cbxAdmin.isSelected()); |
172 | } |
173 | |
174 | private void refresh() |
175 | { |
176 | _txfVisa.setText(_account.getVisa()); |
177 | _txfPassword.setText(""); |
178 | _txfName.setText(_account.getName()); |
179 | _txfEmail.setText(_account.getEmail()); |
180 | if (_account.getRole() == AccountRole.BASIC) |
181 | { |
182 | _rbnRoleBasic.setSelected(true); |
183 | } |
184 | else |
185 | { |
186 | _rbnRoleInitiator.setSelected(true); |
187 | } |
188 | _cbxAdmin.setSelected(_account.isAdmin()); |
189 | } |
190 | |
191 | private class ModifyAccountTask extends AbstractTask<Void, Void> |
192 | { |
193 | public ModifyAccountTask(HiveGuiApplicationMain application, GenericDialog parent) |
194 | { |
195 | super(application, parent); |
196 | } |
197 | |
198 | @Override protected Void doInBackground() throws Exception |
199 | { |
200 | if (_account.getId() == 0) |
201 | { |
202 | _service.createAccount(_account); |
203 | } |
204 | else |
205 | { |
206 | _service.modifyAccount(_account); |
207 | } |
208 | return null; |
209 | } |
210 | |
211 | @Override protected void failed(Throwable cause) |
212 | { |
213 | if (cause instanceof DuplicateKeyException) |
214 | { |
215 | // Display Message |
216 | _application.showMessage("duplicate-account-visa", _account.getVisa()); |
217 | } |
218 | else |
219 | { |
220 | super.failed(cause); |
221 | } |
222 | } |
223 | } |
224 | |
225 | private final WhiteBoardUserService _service; |
226 | private final boolean _create; |
227 | private final JLabel _lblVisa = new JLabel(); |
228 | private final JTextField _txfVisa = new JTextField(); |
229 | private final JLabel _lblPassword = new JLabel(); |
230 | private final JPasswordField _txfPassword = new JPasswordField(); |
231 | private final JLabel _lblName = new JLabel(); |
232 | private final JTextField _txfName = new JTextField(); |
233 | private final JLabel _lblEmail = new JLabel(); |
234 | private final JTextField _txfEmail = new JTextField(); |
235 | private final JLabel _lblRole = new JLabel(); |
236 | private final JRadioButton _rbnRoleBasic = new JRadioButton(); |
237 | private final JRadioButton _rbnRoleInitiator = new JRadioButton(); |
238 | private final JCheckBox _cbxAdmin = new JCheckBox(); |
239 | } |