]> git.argeo.org Git - lgpl/argeo-commons.git/blob - DefaultUserMainPage.java
cfaf6e4fb6aa47c0d7b571f4ffa53f4cb2b46883
[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(userProfile.getProperty(ARGEO_FIRST_NAME).getString()
50 + " "
51 + userProfile.getProperty(ARGEO_LAST_NAME).getString());
52 GridLayout mainLayout = new GridLayout(1, true);
53 // ColumnLayout mainLayout = new ColumnLayout();
54 // mainLayout.minNumColumns = 1;
55 // mainLayout.maxNumColumns = 4;
56 // mainLayout.topMargin = 0;
57 // mainLayout.bottomMargin = 5;
58 // mainLayout.leftMargin = mainLayout.rightMargin =
59 // mainLayout.horizontalSpacing = mainLayout.verticalSpacing = 10;
60 form.getBody().setLayout(mainLayout);
61
62 createGeneralPart(form.getBody());
63 createPassworPart(form.getBody());
64 } catch (RepositoryException e) {
65 throw new ArgeoException("Cannot create form content", e);
66 }
67 }
68
69 /** Creates the general section */
70 protected void createGeneralPart(Composite parent)
71 throws RepositoryException {
72 FormToolkit tk = getManagedForm().getToolkit();
73 Section section = tk.createSection(parent, Section.TITLE_BAR);
74 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
75 section.setText("General");
76 Composite body = tk.createComposite(section, SWT.WRAP);
77 section.setClient(body);
78 GridLayout layout = new GridLayout(2, false);
79 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
80 body.setLayout(layout);
81
82 // add widgets (view)
83 // final Text username;
84 // if (user.getUsername() != null) {
85 // tk.createLabel(body, "Username");
86 // tk.createLabel(body, user.getUsername());
87 // username = null;
88 // } else {
89 // username = createLT(body, "Username", "");
90 // }
91 final Text firstName = createLT(body, "First name",
92 userProfile.getProperty(ARGEO_FIRST_NAME));
93 final Text lastName = createLT(body, "Last name",
94 userProfile.getProperty(ARGEO_LAST_NAME));
95 final Text email = createLT(body, "Email",
96 userProfile.getProperty(ARGEO_PRIMARY_EMAIL));
97 final Text description = createLT(body, "Description",
98 userProfile.getProperty(Property.JCR_DESCRIPTION));
99
100 // create form part (controller)
101 AbstractFormPart part = new SectionPart(section) {
102 public void commit(boolean onSave) {
103 // if (username != null) {
104 // ((SimpleArgeoUser) user).setUsername(username.getText());
105 // username.setEditable(false);
106 // username.setEnabled(false);
107 // }
108 // simpleNature.setFirstName(firstName.getText());
109 // simpleNature.setLastName(lastName.getText());
110 // simpleNature.setEmail(email.getText());
111 // simpleNature.setDescription(description.getText());
112 try {
113 userProfile.setProperty(ARGEO_FIRST_NAME,
114 firstName.getText());
115 userProfile
116 .setProperty(ARGEO_LAST_NAME, lastName.getText());
117 userProfile.setProperty(ARGEO_PRIMARY_EMAIL,
118 email.getText());
119 userProfile.setProperty(Property.JCR_DESCRIPTION,
120 description.getText());
121 super.commit(onSave);
122 if (log.isTraceEnabled())
123 log.trace("General part committed");
124 } catch (RepositoryException e) {
125 throw new ArgeoException("Cannot commit", e);
126 }
127 }
128 };
129 // if (username != null)
130 // username.addModifyListener(new FormPartML(part));
131 firstName.addModifyListener(new FormPartML(part));
132 lastName.addModifyListener(new FormPartML(part));
133 email.addModifyListener(new FormPartML(part));
134 description.addModifyListener(new FormPartML(part));
135 getManagedForm().addPart(part);
136 }
137
138 /** Creates the password section */
139 protected void createPassworPart(Composite parent) {
140 FormToolkit tk = getManagedForm().getToolkit();
141 Section section = tk.createSection(parent, Section.TITLE_BAR);
142 section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
143 section.setText("Password");
144
145 Composite body = tk.createComposite(section, SWT.WRAP);
146 section.setClient(body);
147 GridLayout layout = new GridLayout(2, false);
148 body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
149 body.setLayout(layout);
150
151 // add widgets (view)
152 final Text password1 = createLP(body, "New password", "");
153 final Text password2 = createLP(body, "Repeat password", "");
154 // create form part (controller)
155 AbstractFormPart part = new SectionPart(section) {
156
157 public void commit(boolean onSave) {
158 if (!password1.getText().equals("")
159 || !password2.getText().equals("")) {
160 if (password1.getText().equals(password2.getText())) {
161 newPassword = password1.getText().toCharArray();
162 password1.setText("");
163 password2.setText("");
164 super.commit(onSave);
165 } else {
166 password1.setText("");
167 password2.setText("");
168 throw new ArgeoException("Passwords are not equals");
169 }
170 }
171 }
172
173 };
174 password1.addModifyListener(new FormPartML(part));
175 password2.addModifyListener(new FormPartML(part));
176 getManagedForm().addPart(part);
177 }
178
179 /** Creates label and text. */
180 protected Text createLT(Composite body, String label, String value) {
181 FormToolkit toolkit = getManagedForm().getToolkit();
182 Label lbl = toolkit.createLabel(body, label);
183 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
184 Text text = toolkit.createText(body, value, SWT.BORDER);
185 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
186 return text;
187 }
188
189 protected Text createLT(Composite body, String label, Property value)
190 throws RepositoryException {
191 return createLT(body, label, value.getString());
192 }
193
194 /** Creates label and password. */
195 protected Text createLP(Composite body, String label, String value) {
196 FormToolkit toolkit = getManagedForm().getToolkit();
197 Label lbl = toolkit.createLabel(body, label);
198 lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
199 Text text = toolkit.createText(body, value, SWT.BORDER | SWT.PASSWORD);
200 text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
201 return text;
202 }
203
204 private class FormPartML implements ModifyListener {
205 private AbstractFormPart formPart;
206
207 public FormPartML(AbstractFormPart generalPart) {
208 this.formPart = generalPart;
209 }
210
211 public void modifyText(ModifyEvent e) {
212 formPart.markDirty();
213 }
214
215 }
216
217 public String getNewPassword() {
218 if (newPassword != null)
219 return new String(newPassword);
220 else
221 return null;
222 }
223
224 public void resetNewPassword() {
225 if (newPassword != null)
226 Arrays.fill(newPassword, 'x');
227 newPassword = null;
228 }
229 }