2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.security
.ui
.admin
.editors
;
18 import java
.util
.Arrays
;
20 import javax
.jcr
.Node
;
21 import javax
.jcr
.Property
;
22 import javax
.jcr
.RepositoryException
;
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
;
46 * Display/edit the properties common to all Argeo users
48 public class DefaultUserMainPage
extends FormPage
implements ArgeoNames
{
49 final static String ID
= "argeoUserEditor.mainPage";
51 private final static Log log
= LogFactory
.getLog(DefaultUserMainPage
.class);
52 private Node userProfile
;
54 private char[] newPassword
;
56 public DefaultUserMainPage(FormEditor editor
, Node userProfile
) {
57 super(editor
, ID
, "Main");
58 this.userProfile
= userProfile
;
61 protected void createFormContent(final IManagedForm mf
) {
63 ScrolledForm form
= mf
.getForm();
64 refreshFormTitle(form
);
65 GridLayout mainLayout
= new GridLayout(1, true);
66 form
.getBody().setLayout(mainLayout
);
68 createGeneralPart(form
.getBody());
69 createPassworPart(form
.getBody());
70 } catch (RepositoryException e
) {
71 throw new ArgeoException("Cannot create form content", e
);
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
);
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
));
99 // create form part (controller)
100 AbstractFormPart part
= new SectionPart(section
) {
101 public void commit(boolean onSave
) {
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());
110 .setProperty(ARGEO_LAST_NAME
, lastName
.getText());
111 userProfile
.setProperty(ARGEO_PRIMARY_EMAIL
,
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
);
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
);
137 private void refreshFormTitle(ScrolledForm form
) throws RepositoryException
{
138 form
.setText(getProperty(Property
.JCR_TITLE
)
139 + (userProfile
.getProperty(ARGEO_ENABLED
).getBoolean() ?
""
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
)
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");
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
);
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
) {
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
);
177 password1
.setText("");
178 password2
.setText("");
179 throw new ArgeoException("Passwords are not equals");
185 password1
.addModifyListener(new FormPartML(part
));
186 password2
.addModifyListener(new FormPartML(part
));
187 getManagedForm().addPart(part
);
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));
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));
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));
220 private class FormPartML
implements ModifyListener
{
221 private AbstractFormPart formPart
;
223 public FormPartML(AbstractFormPart generalPart
) {
224 this.formPart
= generalPart
;
227 public void modifyText(ModifyEvent e
) {
228 formPart
.markDirty();
233 public String
getNewPassword() {
234 if (newPassword
!= null)
235 return new String(newPassword
);
240 public void resetNewPassword() {
241 if (newPassword
!= null)
242 Arrays
.fill(newPassword
, 'x');