]> git.argeo.org Git - lgpl/argeo-commons.git/blobdiff - org.argeo.security.ui.admin/src/org/argeo/security/ui/admin/editors/UserMainPage.java
Work on user and group editors. Comment out legacy code that use old userAdminService
[lgpl/argeo-commons.git] / org.argeo.security.ui.admin / src / org / argeo / security / ui / admin / editors / UserMainPage.java
index 745bbff53f7f52bd5eff77e6e8c4804a9cf6efb7..d4ca071270fc8db97c7e5d89939a182e96e343dc 100644 (file)
  */
 package org.argeo.security.ui.admin.editors;
 
+import java.util.ArrayList;
 import java.util.Arrays;
-
-import javax.jcr.RepositoryException;
+import java.util.List;
 
 import org.argeo.ArgeoException;
+import org.argeo.eclipse.ui.EclipseUiUtils;
 import org.argeo.jcr.ArgeoNames;
+import org.argeo.security.ui.admin.internal.ColumnDefinition;
+import org.argeo.security.ui.admin.internal.CommonNameLP;
+import org.argeo.security.ui.admin.internal.RoleIconLP;
 import org.argeo.security.ui.admin.internal.UserAdminConstants;
+import org.argeo.security.ui.admin.internal.UserNameLP;
+import org.argeo.security.ui.admin.internal.UserTableDefaultDClickListener;
+import org.argeo.security.ui.admin.internal.UserTableViewer;
+import org.eclipse.jface.viewers.TableViewer;
 import org.eclipse.swt.SWT;
 import org.eclipse.swt.events.ModifyEvent;
 import org.eclipse.swt.events.ModifyListener;
@@ -38,6 +46,8 @@ import org.eclipse.ui.forms.editor.FormPage;
 import org.eclipse.ui.forms.widgets.FormToolkit;
 import org.eclipse.ui.forms.widgets.ScrolledForm;
 import org.eclipse.ui.forms.widgets.Section;
+import org.osgi.service.useradmin.User;
+import org.osgi.service.useradmin.UserAdmin;
 
 /** Display/edit the properties common to all users */
 public class UserMainPage extends FormPage implements ArgeoNames {
@@ -46,148 +56,222 @@ public class UserMainPage extends FormPage implements ArgeoNames {
        // private final static Log log = LogFactory.getLog(UserMainPage.class);
 
        private final UserEditor editor;
+       private UserAdmin userAdmin;
+
        private char[] newPassword;
 
-       public UserMainPage(FormEditor editor) {
+       // Local configuration
+       private final int PRE_TITLE_INDENT = 10;
+
+       public UserMainPage(FormEditor editor, UserAdmin userAdmin) {
                super(editor, ID, "Main");
                this.editor = (UserEditor) editor;
+               this.userAdmin = userAdmin;
+       }
+
+       public String getNewPassword() {
+               if (newPassword != null)
+                       return new String(newPassword);
+               else
+                       return null;
+       }
+
+       public void resetNewPassword() {
+               if (newPassword != null)
+                       Arrays.fill(newPassword, 'x');
+               newPassword = null;
        }
 
        protected void createFormContent(final IManagedForm mf) {
-               try {
-                       ScrolledForm form = mf.getForm();
-                       refreshFormTitle(form);
-                       GridLayout mainLayout = new GridLayout(1, true);
-                       form.getBody().setLayout(mainLayout);
-
-                       createGeneralPart(form.getBody());
-                       // createPassworPart(form.getBody());
-               } catch (RepositoryException e) {
-                       throw new ArgeoException("Cannot create form content", e);
-               }
+               ScrolledForm form = mf.getForm();
+               // Form page main title
+               form.setText(editor.getProperty(UserAdminConstants.KEY_CN));
+
+               // Body
+               Composite body = form.getBody();
+               GridLayout mainLayout = new GridLayout(1, true);
+               body.setLayout(mainLayout);
+               appendOverviewPart(body);
+               appendPasswordPart(body);
+               appendMemberOfPart(body);
        }
 
        /** Creates the general section */
-       protected void createGeneralPart(Composite parent)
-                       throws RepositoryException {
+       protected void appendOverviewPart(Composite parent) {
                FormToolkit tk = getManagedForm().getToolkit();
-               Section section = tk.createSection(parent, Section.TITLE_BAR);
-               section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-               section.setText("General");
-               Composite body = tk.createComposite(section, SWT.WRAP);
-               section.setClient(body);
-               GridLayout layout = new GridLayout(2, false);
-               body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-               body.setLayout(layout);
+               Composite body = addSection(tk, parent, "Main information");
+               body.setLayout(new GridLayout(2, false));
+
+               Text distinguishedName = createLT(body, "User Name",
+                               editor.getProperty(UserAdminConstants.KEY_UID));
+               distinguishedName.setEnabled(false);
 
                final Text commonName = createLT(body, "Common Name",
                                editor.getProperty(UserAdminConstants.KEY_CN));
                commonName.setEnabled(false);
 
-               // final Text firstName = createLT(body, "First name",
-               // getProperty(ARGEO_FIRST_NAME));
-               // final Text lastName = createLT(body, "Last name",
-               // getProperty(ARGEO_LAST_NAME));
+               final Text firstName = createLT(body, "First name",
+                               editor.getProperty(UserAdminConstants.KEY_FIRSTNAME));
+
+               final Text lastName = createLT(body, "Last name",
+                               editor.getProperty(UserAdminConstants.KEY_LASTNAME));
+
                final Text email = createLT(body, "Email",
                                editor.getProperty(UserAdminConstants.KEY_MAIL));
-               // final Text description = createLMT(body, "Description",
-               // getProperty(Property.JCR_DESCRIPTION));
 
                // create form part (controller)
-               AbstractFormPart part = new SectionPart(section) {
+               AbstractFormPart part = new SectionPart((Section) body.getParent()) {
                        public void commit(boolean onSave) {
+                               // TODO check changed ?
+                               // TODO Sanity checks
+
+                               editor.setProperty(UserAdminConstants.KEY_FIRSTNAME,
+                                               firstName.getText());
+                               editor.setProperty(UserAdminConstants.KEY_LASTNAME,
+                                               lastName.getText());
+                               editor.setProperty(UserAdminConstants.KEY_CN,
+                                               commonName.getText());
                                // TODO check mail validity
                                editor.setProperty(UserAdminConstants.KEY_MAIL, email.getText());
 
-                               // userProfile.getSession().getWorkspace().getVersionManager()
-                               // .checkout(userProfile.getPath());
-                               // userProfile.setProperty(Property.JCR_TITLE,
-                               // commonName.getText());
-                               // userProfile.setProperty(ARGEO_FIRST_NAME,
-                               // firstName.getText());
-                               // userProfile
-                               // .setProperty(ARGEO_LAST_NAME, lastName.getText());
-                               // userProfile.setProperty(ARGEO_PRIMARY_EMAIL,
+                               // Enable common name ?
+                               // editor.setProperty(UserAdminConstants.KEY_CN,
                                // email.getText());
-                               // userProfile.setProperty(Property.JCR_DESCRIPTION,
-                               // description.getText());
-                               // userProfile.getSession().save();
-                               // userProfile.getSession().getWorkspace().getVersionManager()
-                               // .checkin(userProfile.getPath());
                                super.commit(onSave);
                        }
                };
-               // if (username != null)
-               // username.addModifyListener(new FormPartML(part));
-               // commonName.addModifyListener(new FormPartML(part));
-               // firstName.addModifyListener(new FormPartML(part));
-               // lastName.addModifyListener(new FormPartML(part));
 
+               ModifyListener cnML = new ModifyListener() {
+                       private static final long serialVersionUID = 4298649222869835486L;
+
+                       @Override
+                       public void modifyText(ModifyEvent event) {
+                               String first = firstName.getText();
+                               String last = lastName.getText();
+                               String cn = first.trim() + " " + last.trim() + " ";
+                               cn = cn.trim();
+                               commonName.setText(cn);
+                               getManagedForm().getForm().setText(cn);
+                               editor.updateEditorTitle(cn);
+                       }
+               };
+               firstName.addModifyListener(cnML);
+               lastName.addModifyListener(cnML);
+               firstName.addModifyListener(new FormPartML(part));
+               lastName.addModifyListener(new FormPartML(part));
                email.addModifyListener(new FormPartML(part));
                getManagedForm().addPart(part);
        }
 
-       private void refreshFormTitle(ScrolledForm form) throws RepositoryException {
-               // form.setText(getProperty(Property.JCR_TITLE)
-               // + (userProfile.getProperty(ARGEO_ENABLED).getBoolean() ? ""
-               // : " [DISABLED]"));
+       /** Creates the password section */
+       protected void appendPasswordPart(Composite parent) {
+               FormToolkit tk = getManagedForm().getToolkit();
+               Composite body = addSection(tk, parent, "Password");
+
+               // Section section = tk.createSection(parent, Section.TITLE_BAR);
+               // section.setLayoutData(EclipseUiUtils.fillWidth());
+               // section.setText("Password");
+               // Composite body = tk.createComposite(section, SWT.NO_FOCUS);
+               // section.setClient(body);
+               // body.setLayoutData(EclipseUiUtils.fillWidth());
+
+               body.setLayout(new GridLayout(2, false));
+               // add widgets (view)
+               final Text password1 = createLP(body, "New password", "");
+               final Text password2 = createLP(body, "Repeat password", "");
+               // create form part (controller)
+               AbstractFormPart part = new SectionPart((Section) body.getParent()) {
+
+                       public void commit(boolean onSave) {
+                               if (!password1.getText().equals("")
+                                               || !password2.getText().equals("")) {
+                                       if (password1.getText().equals(password2.getText())) {
+                                               newPassword = password1.getText().toCharArray();
+                                               // TODO real set password
+                                               password1.setText("");
+                                               password2.setText("");
+                                               super.commit(onSave);
+                                       } else {
+                                               password1.setText("");
+                                               password2.setText("");
+                                               throw new ArgeoException("Passwords are not equals");
+                                       }
+                               }
+                       }
+
+               };
+               password1.addModifyListener(new FormPartML(part));
+               password2.addModifyListener(new FormPartML(part));
+               getManagedForm().addPart(part);
        }
 
-       // /** @return the property, or the empty string if not set */
-       // protected String getProperty(String name) throws RepositoryException {
-       // return userProfile.hasProperty(name) ? userProfile.getProperty(name)
-       // .getString() : "";
-       // }
+       private UserTableViewer userTableViewerCmp;
+       private TableViewer userViewer;
 
-       /** Creates the password section */
-       // protected void createPassworPart(Composite parent) {
-       // FormToolkit tk = getManagedForm().getToolkit();
-       // Section section = tk.createSection(parent, Section.TITLE_BAR);
-       // section.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-       // section.setText("Password");
-       //
-       // Composite body = tk.createComposite(section, SWT.WRAP);
-       // section.setClient(body);
-       // GridLayout layout = new GridLayout(2, false);
-       // body.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, false));
-       // body.setLayout(layout);
-       //
-       // // add widgets (view)
-       // final Text password1 = createLP(body, "New password", "");
-       // final Text password2 = createLP(body, "Repeat password", "");
-       // // create form part (controller)
-       // AbstractFormPart part = new SectionPart(section) {
-       //
-       // public void commit(boolean onSave) {
-       // if (!password1.getText().equals("")
-       // || !password2.getText().equals("")) {
-       // if (password1.getText().equals(password2.getText())) {
-       // newPassword = password1.getText().toCharArray();
-       // password1.setText("");
-       // password2.setText("");
-       // super.commit(onSave);
-       // } else {
-       // password1.setText("");
-       // password2.setText("");
-       // throw new ArgeoException("Passwords are not equals");
-       // }
-       // }
-       // }
-       //
-       // };
-       // password1.addModifyListener(new FormPartML(part));
-       // password2.addModifyListener(new FormPartML(part));
-       // getManagedForm().addPart(part);
-       // }
+       public void appendMemberOfPart(Composite parent) {
+               FormToolkit tk = getManagedForm().getToolkit();
+               Composite body = addSection(tk, parent, "Groups");
+               body.setLayout(EclipseUiUtils.noSpaceGridLayout());
 
-       /** Creates label and text. */
-       protected Text createLT(Composite body, String label, String value) {
-               FormToolkit toolkit = getManagedForm().getToolkit();
-               Label lbl = toolkit.createLabel(body, label);
-               lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
-               Text text = toolkit.createText(body, value, SWT.BORDER);
-               text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
-               return text;
+               // Define the displayed columns
+               List<ColumnDefinition> columnDefs = new ArrayList<ColumnDefinition>();
+               columnDefs.add(new ColumnDefinition(new RoleIconLP(), "", 0, 24));
+               columnDefs.add(new ColumnDefinition(new UserNameLP(),
+                               "Distinguished Name", 240));
+               columnDefs.add(new ColumnDefinition(new CommonNameLP(), "Common Name",
+                               150));
+
+               // Create and configure the table
+               userTableViewerCmp = new MyUserTableViewer(body, SWT.MULTI
+                               | SWT.H_SCROLL | SWT.V_SCROLL, userAdmin);
+
+               userTableViewerCmp.setColumnDefinitions(columnDefs);
+               userTableViewerCmp.populate(true, false);
+               GridData gd = EclipseUiUtils.fillAll();
+               gd.heightHint = 300;
+               userTableViewerCmp.setLayoutData(gd);
+
+               // Links
+               userViewer = userTableViewerCmp.getTableViewer();
+               userViewer.addDoubleClickListener(new UserTableDefaultDClickListener());
+               // Really?
+               userTableViewerCmp.refresh();
+       }
+
+       private class MyUserTableViewer extends UserTableViewer {
+               private static final long serialVersionUID = 8467999509931900367L;
+
+               public MyUserTableViewer(Composite parent, int style,
+                               UserAdmin userAdmin) {
+                       super(parent, style, userAdmin, true);
+               }
+
+               @Override
+               protected List<User> listFilteredElements(String filter) {
+                       List<User> users = new ArrayList<User>();
+
+                       // "member of" method?
+                       // Group group = (Group) editor.getDisplayedUser();
+                       // Role[] roles = group.getMembers();
+                       // List<User> users = new ArrayList<User>();
+                       // for (Role role : roles)
+                       // // if (role.getType() == Role.GROUP)
+                       // users.add((User) role);
+                       // return users;
+                       return users;
+               }
+       }
+
+       private Composite addSection(FormToolkit tk, Composite parent, String title) {
+               Section section = tk.createSection(parent, Section.TITLE_BAR);
+               GridData gd = EclipseUiUtils.fillWidth();
+               gd.verticalAlignment = PRE_TITLE_INDENT;
+               section.setLayoutData(gd);
+               section.setText(title);
+               Composite body = tk.createComposite(section, SWT.WRAP);
+               body.setLayoutData(EclipseUiUtils.fillAll());
+               section.setClient(body);
+               return body;
        }
 
        /** Creates label and multiline text. */
@@ -210,6 +294,16 @@ public class UserMainPage extends FormPage implements ArgeoNames {
                return text;
        }
 
+       /** Creates label and text. */
+       protected Text createLT(Composite body, String label, String value) {
+               FormToolkit toolkit = getManagedForm().getToolkit();
+               Label lbl = toolkit.createLabel(body, label);
+               lbl.setLayoutData(new GridData(SWT.RIGHT, SWT.CENTER, false, false));
+               Text text = toolkit.createText(body, value, SWT.BORDER);
+               text.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false));
+               return text;
+       }
+       
        private class FormPartML implements ModifyListener {
                private static final long serialVersionUID = 6299808129505381333L;
                private AbstractFormPart formPart;
@@ -221,19 +315,5 @@ public class UserMainPage extends FormPage implements ArgeoNames {
                public void modifyText(ModifyEvent e) {
                        formPart.markDirty();
                }
-
-       }
-
-       public String getNewPassword() {
-               if (newPassword != null)
-                       return new String(newPassword);
-               else
-                       return null;
-       }
-
-       public void resetNewPassword() {
-               if (newPassword != null)
-                       Arrays.fill(newPassword, 'x');
-               newPassword = null;
        }
 }
\ No newline at end of file