]> git.argeo.org Git - lgpl/argeo-commons.git/blob - security/plugins/org.argeo.security.ui.admin/src/main/java/org/argeo/security/ui/admin/editors/DefaultUserMainPage.java
Update license headers
[lgpl/argeo-commons.git] / security / plugins / org.argeo.security.ui.admin / src / main / java / org / argeo / security / ui / admin / editors / DefaultUserMainPage.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.security.ui.admin.editors;
17
18 import java.util.Arrays;
19
20 import javax.jcr.Node;
21 import javax.jcr.Property;
22 import javax.jcr.RepositoryException;
23
24 import org.apache.commons.logging.Log;
25 import org.apache.commons.logging.LogFactory;
26 import org.argeo.ArgeoException;
27 import org.argeo.jcr.ArgeoNames;
28 import org.eclipse.swt.SWT;
29 import org.eclipse.swt.events.ModifyEvent;
30 import org.eclipse.swt.events.ModifyListener;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Label;
35 import org.eclipse.swt.widgets.Text;
36 import org.eclipse.ui.forms.AbstractFormPart;
37 import org.eclipse.ui.forms.IManagedForm;
38 import org.eclipse.ui.forms.SectionPart;
39 import org.eclipse.ui.forms.editor.FormEditor;
40 import org.eclipse.ui.forms.editor.FormPage;
41 import org.eclipse.ui.forms.widgets.FormToolkit;
42 import org.eclipse.ui.forms.widgets.ScrolledForm;
43 import org.eclipse.ui.forms.widgets.Section;
44
45 /**
46 * Display/edit the properties common to all Argeo users
47 */
48 public class DefaultUserMainPage extends FormPage implements ArgeoNames {
49 final static String ID = "argeoUserEditor.mainPage";
50
51 private final static Log log = LogFactory.getLog(DefaultUserMainPage.class);
52 private Node userProfile;
53
54 private char[] newPassword;
55
56 public DefaultUserMainPage(FormEditor editor, Node userProfile) {
57 super(editor, ID, "Main");
58 this.userProfile = userProfile;
59 }
60
61 protected void createFormContent(final IManagedForm mf) {
62 try {
63 ScrolledForm form = mf.getForm();
64 refreshFormTitle(form);
65 GridLayout mainLayout = new GridLayout(1, true);
66 form.getBody().setLayout(mainLayout);
67
68 createGeneralPart(form.getBody());
69 createPassworPart(form.getBody());
70 } catch (RepositoryException e) {
71 throw new ArgeoException("Cannot create form content", e);
72 }
73 }
74
75 /** Creates the general section */
76 protected void createGeneralPart(Composite parent)
77 throws RepositoryException {
78 FormToolkit tk = getManagedForm().getToolkit();
79 Section section = tk.createSection(parent, Section.TITLE_BAR);
80 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
81 section.setText("General");
82 Composite body = tk.createComposite(section, SWT.WRAP);
83 section.setClient(body);
84 GridLayout layout = new GridLayout(2, false);
85 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
86 body.setLayout(layout);
87
88 final Text commonName = createLT(body, "Displayed Name",
89 getProperty(Property.JCR_TITLE));
90 final Text firstName = createLT(body, "First name",
91 getProperty(ARGEO_FIRST_NAME));
92 final Text lastName = createLT(body, "Last name",
93 getProperty(ARGEO_LAST_NAME));
94 final Text email = createLT(body, "Email",
95 getProperty(ARGEO_PRIMARY_EMAIL));
96 final Text description = createLMT(body, "Description",
97 getProperty(Property.JCR_DESCRIPTION));
98
99 // create form part (controller)
100 AbstractFormPart part = new SectionPart(section) {
101 public void commit(boolean onSave) {
102 try {
103 userProfile.getSession().getWorkspace().getVersionManager()
104 .checkout(userProfile.getPath());
105 userProfile.setProperty(Property.JCR_TITLE,
106 commonName.getText());
107 userProfile.setProperty(ARGEO_FIRST_NAME,
108 firstName.getText());
109 userProfile
110 .setProperty(ARGEO_LAST_NAME, lastName.getText());
111 userProfile.setProperty(ARGEO_PRIMARY_EMAIL,
112 email.getText());
113 userProfile.setProperty(Property.JCR_DESCRIPTION,
114 description.getText());
115 userProfile.getSession().save();
116 userProfile.getSession().getWorkspace().getVersionManager()
117 .checkin(userProfile.getPath());
118 super.commit(onSave);
119 refreshFormTitle(getManagedForm().getForm());
120 if (log.isTraceEnabled())
121 log.trace("General part committed");
122 } catch (RepositoryException e) {
123 throw new ArgeoException("Cannot commit", e);
124 }
125 }
126 };
127 // if (username != null)
128 // username.addModifyListener(new FormPartML(part));
129 firstName.addModifyListener(new FormPartML(part));
130 lastName.addModifyListener(new FormPartML(part));
131 email.addModifyListener(new FormPartML(part));
132 description.addModifyListener(new FormPartML(part));
133 getManagedForm().addPart(part);
134 }
135
136 private void refreshFormTitle(ScrolledForm form) throws RepositoryException {
137 form.setText(getProperty(Property.JCR_TITLE)
138 + (userProfile.getProperty(ARGEO_ENABLED).getBoolean() ? ""
139 : " [DISABLED]"));
140 }
141
142 /** @return the property, or the empty string if not set */
143 protected String getProperty(String name) throws RepositoryException {
144 return userProfile.hasProperty(name) ? userProfile.getProperty(name)
145 .getString() : "";
146 }
147
148 /** Creates the password section */
149 protected void createPassworPart(Composite parent) {
150 FormToolkit tk = getManagedForm().getToolkit();
151 Section section = tk.createSection(parent, Section.TITLE_BAR);
152 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
153 section.setText("Password");
154
155 Composite body = tk.createComposite(section, SWT.WRAP);
156 section.setClient(body);
157 GridLayout layout = new GridLayout(2, false);
158 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
159 body.setLayout(layout);
160
161 // add widgets (view)
162 final Text password1 = createLP(body, "New password", "");
163 final Text password2 = createLP(body, "Repeat password", "");
164 // create form part (controller)
165 AbstractFormPart part = new SectionPart(section) {
166
167 public void commit(boolean onSave) {
168 if (!password1.getText().equals("")
169 || !password2.getText().equals("")) {
170 if (password1.getText().equals(password2.getText())) {
171 newPassword = password1.getText().toCharArray();
172 password1.setText("");
173 password2.setText("");
174 super.commit(onSave);
175 } else {
176 password1.setText("");
177 password2.setText("");
178 throw new ArgeoException("Passwords are not equals");
179 }
180 }
181 }
182
183 };
184 password1.addModifyListener(new FormPartML(part));
185 password2.addModifyListener(new FormPartML(part));
186 getManagedForm().addPart(part);
187 }
188
189 /** Creates label and text. */
190 protected Text createLT(Composite body, String label, String value) {
191 FormToolkit toolkit = getManagedForm().getToolkit();
192 Label lbl = toolkit.createLabel(body, label);
193 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
194 Text text = toolkit.createText(body, value, SWT.BORDER);
195 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
196 return text;
197 }
198
199 /** Creates label and multiline text. */
200 protected Text createLMT(Composite body, String label, String value) {
201 FormToolkit toolkit = getManagedForm().getToolkit();
202 Label lbl = toolkit.createLabel(body, label);
203 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
204 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.MULTI);
205 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
206 return text;
207 }
208
209 /** Creates label and password. */
210 protected Text createLP(Composite body, String label, String value) {
211 FormToolkit toolkit = getManagedForm().getToolkit();
212 Label lbl = toolkit.createLabel(body, label);
213 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
214 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.PASSWORD);
215 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
216 return text;
217 }
218
219 private class FormPartML implements ModifyListener {
220 private AbstractFormPart formPart;
221
222 public FormPartML(AbstractFormPart generalPart) {
223 this.formPart = generalPart;
224 }
225
226 public void modifyText(ModifyEvent e) {
227 formPart.markDirty();
228 }
229
230 }
231
232 public String getNewPassword() {
233 if (newPassword != null)
234 return new String(newPassword);
235 else
236 return null;
237 }
238
239 public void resetNewPassword() {
240 if (newPassword != null)
241 Arrays.fill(newPassword, 'x');
242 newPassword = null;
243 }
244 }