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
.internal
.parts
;
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
.cms
.CmsException
;
27 import org
.argeo
.node
.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
;
45 /** Display/edit the properties common to all Argeo users */
46 public class DefaultUserMainPage
extends FormPage
implements ArgeoNames
{
47 final static String ID
= "argeoUserEditor.mainPage";
49 private final static Log log
= LogFactory
.getLog(DefaultUserMainPage
.class);
50 private Node userProfile
;
52 private char[] newPassword
;
54 public DefaultUserMainPage(FormEditor editor
, Node userProfile
) {
55 super(editor
, ID
, "Main");
56 this.userProfile
= userProfile
;
59 protected void createFormContent(final IManagedForm mf
) {
61 ScrolledForm form
= mf
.getForm();
62 refreshFormTitle(form
);
63 GridLayout mainLayout
= new GridLayout(1, true);
64 form
.getBody().setLayout(mainLayout
);
66 createGeneralPart(form
.getBody());
67 createPassworPart(form
.getBody());
68 } catch (RepositoryException e
) {
69 throw new CmsException("Cannot create form content", e
);
73 /** Creates the general section */
74 protected void createGeneralPart(Composite parent
)
75 throws RepositoryException
{
76 FormToolkit tk
= getManagedForm().getToolkit();
77 Section section
= tk
.createSection(parent
, Section
.TITLE_BAR
);
78 section
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
79 section
.setText("General");
80 Composite body
= tk
.createComposite(section
, SWT
.WRAP
);
81 section
.setClient(body
);
82 GridLayout layout
= new GridLayout(2, false);
83 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
84 body
.setLayout(layout
);
86 final Text commonName
= createLT(body
, "Displayed Name",
87 getProperty(Property
.JCR_TITLE
));
88 final Text firstName
= createLT(body
, "First name",
89 getProperty(ARGEO_FIRST_NAME
));
90 final Text lastName
= createLT(body
, "Last name",
91 getProperty(ARGEO_LAST_NAME
));
92 final Text email
= createLT(body
, "Email",
93 getProperty(ARGEO_PRIMARY_EMAIL
));
94 final Text description
= createLMT(body
, "Description",
95 getProperty(Property
.JCR_DESCRIPTION
));
97 // create form part (controller)
98 AbstractFormPart part
= new SectionPart(section
) {
99 public void commit(boolean onSave
) {
101 userProfile
.getSession().getWorkspace().getVersionManager()
102 .checkout(userProfile
.getPath());
103 userProfile
.setProperty(Property
.JCR_TITLE
,
104 commonName
.getText());
105 userProfile
.setProperty(ARGEO_FIRST_NAME
,
106 firstName
.getText());
108 .setProperty(ARGEO_LAST_NAME
, lastName
.getText());
109 userProfile
.setProperty(ARGEO_PRIMARY_EMAIL
,
111 userProfile
.setProperty(Property
.JCR_DESCRIPTION
,
112 description
.getText());
113 userProfile
.getSession().save();
114 userProfile
.getSession().getWorkspace().getVersionManager()
115 .checkin(userProfile
.getPath());
116 super.commit(onSave
);
117 refreshFormTitle(getManagedForm().getForm());
118 if (log
.isTraceEnabled())
119 log
.trace("General part committed");
120 } catch (RepositoryException e
) {
121 throw new CmsException("Cannot commit", e
);
125 // if (username != null)
126 // username.addModifyListener(new FormPartML(part));
127 commonName
.addModifyListener(new FormPartML(part
));
128 firstName
.addModifyListener(new FormPartML(part
));
129 lastName
.addModifyListener(new FormPartML(part
));
130 email
.addModifyListener(new FormPartML(part
));
131 description
.addModifyListener(new FormPartML(part
));
132 getManagedForm().addPart(part
);
135 private void refreshFormTitle(ScrolledForm form
) throws RepositoryException
{
136 form
.setText(getProperty(Property
.JCR_TITLE
)
137 + (userProfile
.getProperty(ARGEO_ENABLED
).getBoolean() ?
""
141 /** @return the property, or the empty string if not set */
142 protected String
getProperty(String name
) throws RepositoryException
{
143 return userProfile
.hasProperty(name
) ? userProfile
.getProperty(name
)
147 /** Creates the password section */
148 protected void createPassworPart(Composite parent
) {
149 FormToolkit tk
= getManagedForm().getToolkit();
150 Section section
= tk
.createSection(parent
, Section
.TITLE_BAR
);
151 section
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
152 section
.setText("Password");
154 Composite body
= tk
.createComposite(section
, SWT
.WRAP
);
155 section
.setClient(body
);
156 GridLayout layout
= new GridLayout(2, false);
157 body
.setLayoutData(new GridData(SWT
.FILL
, SWT
.FILL
, true, false));
158 body
.setLayout(layout
);
160 // add widgets (view)
161 final Text password1
= createLP(body
, "New password", "");
162 final Text password2
= createLP(body
, "Repeat password", "");
163 // create form part (controller)
164 AbstractFormPart part
= new SectionPart(section
) {
166 public void commit(boolean onSave
) {
167 if (!password1
.getText().equals("")
168 || !password2
.getText().equals("")) {
169 if (password1
.getText().equals(password2
.getText())) {
170 newPassword
= password1
.getText().toCharArray();
171 password1
.setText("");
172 password2
.setText("");
173 super.commit(onSave
);
175 password1
.setText("");
176 password2
.setText("");
177 throw new CmsException("Passwords are not equals");
183 password1
.addModifyListener(new FormPartML(part
));
184 password2
.addModifyListener(new FormPartML(part
));
185 getManagedForm().addPart(part
);
188 /** Creates label and text. */
189 protected Text
createLT(Composite body
, String label
, String value
) {
190 FormToolkit toolkit
= getManagedForm().getToolkit();
191 Label lbl
= toolkit
.createLabel(body
, label
);
192 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
193 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
);
194 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
198 /** Creates label and multiline text. */
199 protected Text
createLMT(Composite body
, String label
, String value
) {
200 FormToolkit toolkit
= getManagedForm().getToolkit();
201 Label lbl
= toolkit
.createLabel(body
, label
);
202 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
203 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
| SWT
.MULTI
);
204 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, true));
208 /** Creates label and password. */
209 protected Text
createLP(Composite body
, String label
, String value
) {
210 FormToolkit toolkit
= getManagedForm().getToolkit();
211 Label lbl
= toolkit
.createLabel(body
, label
);
212 lbl
.setLayoutData(new GridData(SWT
.RIGHT
, SWT
.CENTER
, false, false));
213 Text text
= toolkit
.createText(body
, value
, SWT
.BORDER
| SWT
.PASSWORD
);
214 text
.setLayoutData(new GridData(SWT
.FILL
, SWT
.CENTER
, true, false));
218 private class FormPartML
implements ModifyListener
{
219 private static final long serialVersionUID
= 6299808129505381333L;
220 private AbstractFormPart formPart
;
222 public FormPartML(AbstractFormPart generalPart
) {
223 this.formPart
= generalPart
;
226 public void modifyText(ModifyEvent e
) {
227 formPart
.markDirty();
231 public String
getNewPassword() {
232 if (newPassword
!= null)
233 return new String(newPassword
);
238 public void resetNewPassword() {
239 if (newPassword
!= null)
240 Arrays
.fill(newPassword
, 'x');