]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/DefaultUserMainPage.java
Introduce aggregating node user admin
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / DefaultUserMainPage.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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 commonName.addModifyListener(new FormPartML(part));
130 firstName.addModifyListener(new FormPartML(part));
131 lastName.addModifyListener(new FormPartML(part));
132 email.addModifyListener(new FormPartML(part));
133 description.addModifyListener(new FormPartML(part));
134 getManagedForm().addPart(part);
135 }
136
137 private void refreshFormTitle(ScrolledForm form) throws RepositoryException {
138 form.setText(getProperty(Property.JCR_TITLE)
139 + (userProfile.getProperty(ARGEO_ENABLED).getBoolean() ? ""
140 : " [DISABLED]"));
141 }
142
143 /** @return the property, or the empty string if not set */
144 protected String getProperty(String name) throws RepositoryException {
145 return userProfile.hasProperty(name) ? userProfile.getProperty(name)
146 .getString() : "";
147 }
148
149 /** Creates the password section */
150 protected void createPassworPart(Composite parent) {
151 FormToolkit tk = getManagedForm().getToolkit();
152 Section section = tk.createSection(parent, Section.TITLE_BAR);
153 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
154 section.setText("Password");
155
156 Composite body = tk.createComposite(section, SWT.WRAP);
157 section.setClient(body);
158 GridLayout layout = new GridLayout(2, false);
159 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
160 body.setLayout(layout);
161
162 // add widgets (view)
163 final Text password1 = createLP(body, "New password", "");
164 final Text password2 = createLP(body, "Repeat password", "");
165 // create form part (controller)
166 AbstractFormPart part = new SectionPart(section) {
167
168 public void commit(boolean onSave) {
169 if (!password1.getText().equals("")
170 || !password2.getText().equals("")) {
171 if (password1.getText().equals(password2.getText())) {
172 newPassword = password1.getText().toCharArray();
173 password1.setText("");
174 password2.setText("");
175 super.commit(onSave);
176 } else {
177 password1.setText("");
178 password2.setText("");
179 throw new ArgeoException("Passwords are not equals");
180 }
181 }
182 }
183
184 };
185 password1.addModifyListener(new FormPartML(part));
186 password2.addModifyListener(new FormPartML(part));
187 getManagedForm().addPart(part);
188 }
189
190 /** Creates label and text. */
191 protected Text createLT(Composite body, String label, String value) {
192 FormToolkit toolkit = getManagedForm().getToolkit();
193 Label lbl = toolkit.createLabel(body, label);
194 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
195 Text text = toolkit.createText(body, value, SWT.BORDER);
196 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
197 return text;
198 }
199
200 /** Creates label and multiline text. */
201 protected Text createLMT(Composite body, String label, String value) {
202 FormToolkit toolkit = getManagedForm().getToolkit();
203 Label lbl = toolkit.createLabel(body, label);
204 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
205 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.MULTI);
206 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, true));
207 return text;
208 }
209
210 /** Creates label and password. */
211 protected Text createLP(Composite body, String label, String value) {
212 FormToolkit toolkit = getManagedForm().getToolkit();
213 Label lbl = toolkit.createLabel(body, label);
214 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
215 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.PASSWORD);
216 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
217 return text;
218 }
219
220 private class FormPartML implements ModifyListener {
221 private static final long serialVersionUID = 6299808129505381333L;
222 private AbstractFormPart formPart;
223
224 public FormPartML(AbstractFormPart generalPart) {
225 this.formPart = generalPart;
226 }
227
228 public void modifyText(ModifyEvent e) {
229 formPart.markDirty();
230 }
231
232 }
233
234 public String getNewPassword() {
235 if (newPassword != null)
236 return new String(newPassword);
237 else
238 return null;
239 }
240
241 public void resetNewPassword() {
242 if (newPassword != null)
243 Arrays.fill(newPassword, 'x');
244 newPassword = null;
245 }
246 }