]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DefaultUserMainPage.java
3581354197e4fc205e60ddd154343e45b1dd9d27
[lgpl/argeo-commons.git] / DefaultUserMainPage.java
1 package org.argeo.security.ui.admin.editors;
2
3 import java.util.Arrays;
4
5 import javax.jcr.Node;
6 import javax.jcr.Property;
7 import javax.jcr.RepositoryException;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.ArgeoException;
12 import org.argeo.jcr.ArgeoNames;
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.events.ModifyEvent;
15 import org.eclipse.swt.events.ModifyListener;
16 import org.eclipse.swt.layout.GridData;
17 import org.eclipse.swt.layout.GridLayout;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Label;
20 import org.eclipse.swt.widgets.Text;
21 import org.eclipse.ui.forms.AbstractFormPart;
22 import org.eclipse.ui.forms.IManagedForm;
23 import org.eclipse.ui.forms.SectionPart;
24 import org.eclipse.ui.forms.editor.FormEditor;
25 import org.eclipse.ui.forms.editor.FormPage;
26 import org.eclipse.ui.forms.widgets.FormToolkit;
27 import org.eclipse.ui.forms.widgets.ScrolledForm;
28 import org.eclipse.ui.forms.widgets.Section;
29
30 /**
31 * Display/edit the properties common to all Argeo users
32 */
33 public class DefaultUserMainPage extends FormPage implements ArgeoNames {
34 final static String ID = "argeoUserEditor.mainPage";
35
36 private final static Log log = LogFactory.getLog(DefaultUserMainPage.class);
37 private Node userProfile;
38
39 private char[] newPassword;
40
41 public DefaultUserMainPage(FormEditor editor, Node userProfile) {
42 super(editor, ID, "Main");
43 this.userProfile = userProfile;
44 }
45
46 protected void createFormContent(final IManagedForm mf) {
47 try {
48 ScrolledForm form = mf.getForm();
49 form.setText(getProperty(ARGEO_FIRST_NAME) + " "
50 + getProperty(ARGEO_LAST_NAME));
51 GridLayout mainLayout = new GridLayout(1, true);
52 form.getBody().setLayout(mainLayout);
53
54 createGeneralPart(form.getBody());
55 createPassworPart(form.getBody());
56 } catch (RepositoryException e) {
57 throw new ArgeoException("Cannot create form content", e);
58 }
59 }
60
61 /** Creates the general section */
62 protected void createGeneralPart(Composite parent)
63 throws RepositoryException {
64 FormToolkit tk = getManagedForm().getToolkit();
65 Section section = tk.createSection(parent, Section.TITLE_BAR);
66 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
67 section.setText("General");
68 Composite body = tk.createComposite(section, SWT.WRAP);
69 section.setClient(body);
70 GridLayout layout = new GridLayout(2, false);
71 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
72 body.setLayout(layout);
73
74 final Text firstName = createLT(body, "First name",
75 getProperty(ARGEO_FIRST_NAME));
76 final Text lastName = createLT(body, "Last name",
77 getProperty(ARGEO_LAST_NAME));
78 final Text email = createLT(body, "Email",
79 getProperty(ARGEO_PRIMARY_EMAIL));
80 final Text description = createLT(body, "Description",
81 getProperty(Property.JCR_DESCRIPTION));
82
83 // create form part (controller)
84 AbstractFormPart part = new SectionPart(section) {
85 public void commit(boolean onSave) {
86 try {
87 userProfile.getSession().getWorkspace().getVersionManager()
88 .checkout(userProfile.getPath());
89 userProfile.setProperty(ARGEO_FIRST_NAME,
90 firstName.getText());
91 userProfile
92 .setProperty(ARGEO_LAST_NAME, lastName.getText());
93 userProfile.setProperty(ARGEO_PRIMARY_EMAIL,
94 email.getText());
95 userProfile.setProperty(Property.JCR_DESCRIPTION,
96 description.getText());
97 userProfile.getSession().save();
98 userProfile.getSession().getWorkspace().getVersionManager()
99 .checkin(userProfile.getPath());
100 super.commit(onSave);
101 if (log.isTraceEnabled())
102 log.trace("General part committed");
103 } catch (RepositoryException e) {
104 throw new ArgeoException("Cannot commit", e);
105 }
106 }
107 };
108 // if (username != null)
109 // username.addModifyListener(new FormPartML(part));
110 firstName.addModifyListener(new FormPartML(part));
111 lastName.addModifyListener(new FormPartML(part));
112 email.addModifyListener(new FormPartML(part));
113 description.addModifyListener(new FormPartML(part));
114 getManagedForm().addPart(part);
115 }
116
117 /** @return the property, or teh empty string if not set */
118 protected String getProperty(String name) throws RepositoryException {
119 return userProfile.hasProperty(name) ? userProfile.getProperty(name)
120 .getString() : "";
121 }
122
123 /** Creates the password section */
124 protected void createPassworPart(Composite parent) {
125 FormToolkit tk = getManagedForm().getToolkit();
126 Section section = tk.createSection(parent, Section.TITLE_BAR);
127 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
128 section.setText("Password");
129
130 Composite body = tk.createComposite(section, SWT.WRAP);
131 section.setClient(body);
132 GridLayout layout = new GridLayout(2, false);
133 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
134 body.setLayout(layout);
135
136 // add widgets (view)
137 final Text password1 = createLP(body, "New password", "");
138 final Text password2 = createLP(body, "Repeat password", "");
139 // create form part (controller)
140 AbstractFormPart part = new SectionPart(section) {
141
142 public void commit(boolean onSave) {
143 if (!password1.getText().equals("")
144 || !password2.getText().equals("")) {
145 if (password1.getText().equals(password2.getText())) {
146 newPassword = password1.getText().toCharArray();
147 password1.setText("");
148 password2.setText("");
149 super.commit(onSave);
150 } else {
151 password1.setText("");
152 password2.setText("");
153 throw new ArgeoException("Passwords are not equals");
154 }
155 }
156 }
157
158 };
159 password1.addModifyListener(new FormPartML(part));
160 password2.addModifyListener(new FormPartML(part));
161 getManagedForm().addPart(part);
162 }
163
164 /** Creates label and text. */
165 protected Text createLT(Composite body, String label, String value) {
166 FormToolkit toolkit = getManagedForm().getToolkit();
167 Label lbl = toolkit.createLabel(body, label);
168 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
169 Text text = toolkit.createText(body, value, SWT.BORDER);
170 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
171 return text;
172 }
173
174 /** Creates label and password. */
175 protected Text createLP(Composite body, String label, String value) {
176 FormToolkit toolkit = getManagedForm().getToolkit();
177 Label lbl = toolkit.createLabel(body, label);
178 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
179 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.PASSWORD);
180 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
181 return text;
182 }
183
184 private class FormPartML implements ModifyListener {
185 private AbstractFormPart formPart;
186
187 public FormPartML(AbstractFormPart generalPart) {
188 this.formPart = generalPart;
189 }
190
191 public void modifyText(ModifyEvent e) {
192 formPart.markDirty();
193 }
194
195 }
196
197 public String getNewPassword() {
198 if (newPassword != null)
199 return new String(newPassword);
200 else
201 return null;
202 }
203
204 public void resetNewPassword() {
205 if (newPassword != null)
206 Arrays.fill(newPassword, 'x');
207 newPassword = null;
208 }
209 }