ace-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From j...@apache.org
Subject svn commit: r1537894 - in /ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui: editor/ editor/impl/ osgi/ vaadin/
Date Fri, 01 Nov 2013 10:55:09 GMT
Author: jawi
Date: Fri Nov  1 10:55:08 2013
New Revision: 1537894

URL: http://svn.apache.org/r1537894
Log:
Usability updates:

- split out the "manage users" functionality from the "my info" functionality
  as to make the UI more heterogenous for various roles;
- take the current User from the Application instead of let it being passed
  around through the context;
- made some improvements in the layout and usability of the various forms, by
  making fields required and showing notifications when something is done.


Added:
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoButton.java   (with props)
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoWindow.java   (with props)
Modified:
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserDTO.java
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserEditor.java
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/impl/UserEditorImpl.java
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/osgi/Activator.java
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminButton.java
    ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminWindow.java

Modified: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserDTO.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserDTO.java?rev=1537894&r1=1537893&r2=1537894&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserDTO.java (original)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserDTO.java Fri Nov  1 10:55:08 2013
@@ -25,24 +25,21 @@ import org.osgi.service.useradmin.User;
  * UserDTO contains the information of the user and the group.
  */
 public class UserDTO implements Comparable<UserDTO> {
-
-    private User m_user;
     private String m_previousUsername;
     private String m_username;
     private String m_password;
+    private String m_previousGroupname;
     private String m_groupname;
-    private Group m_group;
     private boolean m_usernameChanged;
     private boolean m_passwordChanged;
     private boolean m_groupChanged;
 
     public UserDTO(User user, Group group) {
-        m_user = user;
-        m_group = group;
         m_username = (String) user.getProperties().get("username");
         m_previousUsername = m_username;
         m_password = (String) user.getCredentials().get("password");
         m_groupname = group.getName();
+        m_previousGroupname = m_groupname;
     }
 
     public UserDTO(String username, String password, String groupname) {
@@ -50,6 +47,7 @@ public class UserDTO implements Comparab
         m_password = password;
         m_previousUsername = username;
         m_groupname = groupname;
+        m_previousGroupname = groupname;
     }
 
     /**
@@ -107,6 +105,13 @@ public class UserDTO implements Comparab
     }
 
     /**
+     * @return the previousGroupname
+     */
+    public String getPreviousGroupname() {
+        return m_previousGroupname;
+    }
+    
+    /**
      * Returns the current username that is stored in the useradminService.
      * 
      * @return username
@@ -116,15 +121,6 @@ public class UserDTO implements Comparab
     }
 
     /**
-     * Returns the user object.
-     * 
-     * @return user
-     */
-    public User getUser() {
-        return m_user;
-    }
-
-    /**
      * Returns the current username. This can be either the one that is stored in userAdminService, or the
      * current username that is store in this object.
      * 
@@ -141,6 +137,7 @@ public class UserDTO implements Comparab
      */
     public void setUsername(String username) {
         m_usernameChanged = true;
+        m_previousUsername = m_username;
         m_username = username;
     }
 
@@ -179,18 +176,10 @@ public class UserDTO implements Comparab
      */
     public void setGroupname(String groupname) {
         m_groupChanged = true;
+        m_previousGroupname = m_groupname;
         m_groupname = groupname;
     }
 
-    /**
-     * Returns the Group object
-     * 
-     * @return Group object
-     */
-    public Group getGroup() {
-        return m_group;
-    }
-
     public String toString() {
         return m_username;
     }

Modified: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserEditor.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserEditor.java?rev=1537894&r1=1537893&r2=1537894&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserEditor.java (original)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/UserEditor.java Fri Nov  1 10:55:08 2013
@@ -46,7 +46,7 @@ public interface UserEditor {
      * @throws GroupNotFoundException Will be thrown when the new group can't be found
      * @throws UserAlreadyExistsException Will be thrown when a user with the username already exist
      */
-    void storeUserDTO(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException, UserAlreadyExistsException;
+    void updateUser(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException, UserAlreadyExistsException;
 
     /**
      * editUser can be called to edit the username of an user. It will add a new Role with the information of the old

Modified: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/impl/UserEditorImpl.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/impl/UserEditorImpl.java?rev=1537894&r1=1537893&r2=1537894&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/impl/UserEditorImpl.java (original)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/editor/impl/UserEditorImpl.java Fri Nov  1 10:55:08 2013
@@ -37,7 +37,7 @@ import org.osgi.service.useradmin.UserAd
  * An implementation of UserEditor. Can be used to execute operations on users in the useradminService;
  */
 public class UserEditorImpl implements UserEditor {
-
+    // Injected by Felix DM...
     private volatile UserAdmin m_useradmin;
 
     @Override
@@ -45,108 +45,104 @@ public class UserEditorImpl implements U
         String username = userDTO.getUsername();
         String password = userDTO.getPassword();
         String groupname = userDTO.getGroupname();
+
         if (username == null || "".equals(username) || password == null || "".equals(password) || groupname == null || "".equals(groupname)) {
             throw new IllegalArgumentException("Username, password and groupname cannot be null or \"\"");
         }
+
         Group group = (Group) m_useradmin.getRole(groupname);
         if (group == null) {
             throw new GroupNotFoundException(groupname);
         }
+
         Role newRole = m_useradmin.createRole(username, Role.USER);
         if (newRole == null) {
             throw new UserAlreadyExistsException(username);
         }
+
         User newUser = (User) newRole;
         newUser.getProperties().put("username", username);
         newUser.getCredentials().put("password", password);
         group.addMember(newUser);
     }
 
-    public void storeUserDTO(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException, UserAlreadyExistsException {
-        if (userDTO.isUsernameChanged()) {
-            editUsername(userDTO);
-            return;
-        }
-        if (userDTO.isPasswordChanged()) {
-            editPassword(userDTO);
-        }
-        if (userDTO.isGroupChanged()) {
-            editGroup(userDTO);
-        }
-    }
-
     @Override
-    public void editUsername(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException, UserAlreadyExistsException {
-        String oldUsername = userDTO.getPreviousUsername();
-        String newUsername = userDTO.getUsername();
-        if (oldUsername == null || newUsername == null || "".equals(newUsername)) {
-            throw new IllegalArgumentException("oldUsername and newUsername cannot be null or \"\" ");
-        }
-        if (newUsername.equals(oldUsername)) {
-            throw new UserAlreadyExistsException(newUsername);
-        }
-        if (getUser(newUsername) != null) {
-            throw new UserAlreadyExistsException(newUsername);
+    public void editGroup(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException {
+        String username = userDTO.getUsername();
+        String group = userDTO.getGroupname();
+
+        if (username == null || group == null) {
+            throw new IllegalArgumentException("Username and group cannot be null or \"\" ");
         }
-        User user = getUser(oldUsername);
+
+        User user = getUser(username);
         if (user == null) {
-            throw new UserNotFoundException(oldUsername);
+            throw new UserNotFoundException(username);
         }
-        String group = getGroup(user).getName();
-        if (group == null) {
-            throw new GroupNotFoundException(null);
+
+        Group oldGroup = (Group) m_useradmin.getRole(userDTO.getPreviousGroupname());
+        if (oldGroup == null) {
+            throw new GroupNotFoundException(group);
         }
-        addUser(userDTO);
-        removeUser(userDTO);
+
+        Group newGroup = (Group) m_useradmin.getRole(group);
+        if (newGroup == null) {
+            throw new GroupNotFoundException(group);
+        }
+
+        oldGroup.removeMember(user);
+        newGroup.addMember(user);
     }
 
     @Override
     public void editPassword(UserDTO userDTO) throws UserNotFoundException {
         String username = userDTO.getUsername();
         String password = userDTO.getPassword();
+
         if (username == null || password == null || "".equals(password)) {
             throw new IllegalArgumentException("Username or Password cannot be null or \"\" ");
         }
+
         User user = getUser(username);
         if (user == null) {
             throw new UserNotFoundException(username);
-
         }
+
         user.getCredentials().put("password", password);
     }
 
     @Override
-    public void editGroup(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException {
-        String username = userDTO.getUsername();
-        String group = userDTO.getGroupname();
-        if (username == null || group == null) {
-            throw new IllegalArgumentException("Username and group cannot be null or \"\" ");
-        }
-        User user = getUser(username);
-        Group newGroup = (Group) m_useradmin.getRole(group);
-        if (newGroup == null) {
-            throw new GroupNotFoundException(group);
+    public void editUsername(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException, UserAlreadyExistsException {
+        String oldUsername = userDTO.getPreviousUsername();
+        String newUsername = userDTO.getUsername();
+
+        if (oldUsername == null || newUsername == null || "".equals(newUsername)) {
+            throw new IllegalArgumentException("oldUsername and newUsername cannot be null or \"\" ");
         }
-        if (user == null) {
-            throw new UserNotFoundException(username);
+        if (newUsername.equals(oldUsername)) {
+            // Nothing needs to be done...
+            return;
         }
-        getGroup(user).removeMember(user);
-        newGroup.addMember(user);
-    }
-
-    @Override
-    public void removeUser(UserDTO userDTO) throws UserNotFoundException {
-        String username = userDTO.getPreviousUsername();
-        if (username == null) {
-            throw new IllegalArgumentException("Username cannot be null or \"\" ");
+        if (getUser(newUsername) != null) {
+            throw new UserAlreadyExistsException(newUsername);
         }
-        User user = getUser(username);
+        User user = getUser(oldUsername);
         if (user == null) {
-            throw new UserNotFoundException(username);
+            throw new UserNotFoundException(oldUsername);
         }
         Group group = getGroup(user);
+        if (group == null) {
+            throw new GroupNotFoundException(null);
+        }
+
         group.removeMember(user);
         m_useradmin.removeRole(user.getName());
+
+        User newUser = (User) m_useradmin.createRole(newUsername, Role.USER);
+        newUser.getProperties().put("username", newUsername);
+        newUser.getCredentials().put("password", userDTO.getPassword());
+
+        group.addMember(newUser);
     }
 
     @Override
@@ -170,6 +166,29 @@ public class UserEditorImpl implements U
     }
 
     @Override
+    public Group getGroup(User user) {
+        Authorization auth = m_useradmin.getAuthorization(user);
+        String[] roles = auth.getRoles();
+        if (roles != null) {
+            for (String role : roles) {
+                Role result = m_useradmin.getRole(role);
+                if (result.getType() == Role.GROUP) {
+                    Group group = (Group) result;
+                    Role[] members = group.getMembers();
+                    if (members != null) {
+                        for (Role r : members) {
+                            if (r.getType() == Role.USER && r.getName().equals(user.getName())) {
+                                return group;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+        return null;
+    }
+
+    @Override
     public List<Group> getGroups() {
         List<Group> tempGroups = new ArrayList<Group>();
         try {
@@ -189,6 +208,11 @@ public class UserEditorImpl implements U
     }
 
     @Override
+    public User getUser(String username) {
+        return m_useradmin.getUser("username", username);
+    }
+
+    @Override
     public List<User> getUsers() {
         List<User> tempUsers = new ArrayList<User>();
         try {
@@ -208,36 +232,37 @@ public class UserEditorImpl implements U
     }
 
     @Override
-    public Group getGroup(User user) {
-        Authorization auth = m_useradmin.getAuthorization(user);
-        String[] roles = auth.getRoles();
-        if (roles != null) {
-            for (String role : roles) {
-                Role result = m_useradmin.getRole(role);
-                if (result.getType() == Role.GROUP) {
-                    Group group = (Group) result;
-                    Role[] members = group.getMembers();
-                    if (members != null) {
-                        for (Role r : members) {
-                            if (r.getType() == Role.USER && r.getName().equals(user.getName())) {
-                                return group;
-                            }
-                        }
-                    }
-                }
-            }
-        }
-        return null;
+    public boolean hasRole(User user, String role) {
+        Authorization authorization = m_useradmin.getAuthorization(user);
+        return authorization.hasRole(role);
     }
 
     @Override
-    public User getUser(String username) {
-        return m_useradmin.getUser("username", username);
+    public void removeUser(UserDTO userDTO) throws UserNotFoundException {
+        String username = userDTO.getUsername();
+        if (username == null) {
+            throw new IllegalArgumentException("Username cannot be null or \"\" ");
+        }
+        User user = getUser(username);
+        if (user == null) {
+            throw new UserNotFoundException(username);
+        }
+        Group group = getGroup(user);
+        group.removeMember(user);
+        m_useradmin.removeRole(user.getName());
     }
 
-    @Override
-    public boolean hasRole(User user, String role) {
-        Authorization authorization = m_useradmin.getAuthorization(user);
-        return authorization.hasRole(role);
+    public void updateUser(UserDTO userDTO) throws UserNotFoundException, GroupNotFoundException, UserAlreadyExistsException {
+        if (userDTO.isUsernameChanged()) {
+            editUsername(userDTO);
+        }
+        else {
+            if (userDTO.isPasswordChanged()) {
+                editPassword(userDTO);
+            }
+            if (userDTO.isGroupChanged()) {
+                editGroup(userDTO);
+            }
+        }
     }
 }

Modified: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/osgi/Activator.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/osgi/Activator.java?rev=1537894&r1=1537893&r2=1537894&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/osgi/Activator.java (original)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/osgi/Activator.java Fri Nov  1 10:55:08 2013
@@ -23,15 +23,16 @@ import java.util.Properties;
 
 import org.apache.ace.useradmin.ui.editor.UserEditor;
 import org.apache.ace.useradmin.ui.editor.impl.UserEditorImpl;
+import org.apache.ace.useradmin.ui.vaadin.EditUserInfoButton;
 import org.apache.ace.useradmin.ui.vaadin.UserAdminButton;
 import org.apache.ace.webui.UIExtensionFactory;
 import org.apache.felix.dm.DependencyActivatorBase;
 import org.apache.felix.dm.DependencyManager;
 import org.osgi.framework.BundleContext;
-import org.osgi.service.useradmin.User;
+import org.osgi.framework.Constants;
+import org.osgi.service.log.LogService;
 import org.osgi.service.useradmin.UserAdmin;
 
-import com.vaadin.ui.Button;
 import com.vaadin.ui.Component;
 
 public class Activator extends DependencyActivatorBase {
@@ -45,22 +46,53 @@ public class Activator extends Dependenc
                 .setRequired(true)
             )
         );
+
         Properties properties = new Properties();
         properties.put(UIExtensionFactory.EXTENSION_POINT_KEY, UIExtensionFactory.EXTENSION_POINT_VALUE_MENU);
+        properties.put(Constants.SERVICE_RANKING, 102);
+
+        manager.add(createComponent()
+            .setInterface(UIExtensionFactory.class.getName(), properties)
+            .setImplementation(new UIExtensionFactory() {
+                    @Override
+                    public Component create(Map<String, Object> context) {
+                        EditUserInfoButton b = new EditUserInfoButton();
+                        manager.add(createComponent()
+                            .setImplementation(b)
+                            .setComposition("getComposition")
+                            .add(createServiceDependency()
+                                .setService(UserEditor.class)
+                                .setRequired(true))
+                            .add(createServiceDependency()
+                                .setService(LogService.class)
+                                .setRequired(false)
+                            )
+                        );
+                        return b;
+                    }
+                }
+            )
+        );
+
+        properties.put(Constants.SERVICE_RANKING, 101);
+
         manager.add(createComponent()
             .setInterface(UIExtensionFactory.class.getName(), properties)
             .setImplementation(new UIExtensionFactory() {
                     @Override
                     public Component create(Map<String, Object> context) {
-                        Button b = new UserAdminButton((User) context.get("user"));
+                        UserAdminButton b = new UserAdminButton();
                         manager.add(createComponent()
                             .setImplementation(b)
+                            .setComposition("getComposition")
                             .add(createServiceDependency()
                                 .setService(UserEditor.class)
-                                .setRequired(true)
+                                .setRequired(true))
+                            .add(createServiceDependency()
+                                .setService(LogService.class)
+                                .setRequired(false)
                             )
                         );
-                        b.setDescription("This button opens a window to manage users");
                         return b;
                     }
                 }

Added: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoButton.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoButton.java?rev=1537894&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoButton.java (added)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoButton.java Fri Nov  1 10:55:08 2013
@@ -0,0 +1,54 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.useradmin.ui.vaadin;
+
+import org.apache.felix.dm.Component;
+
+import com.vaadin.ui.Button;
+
+public class EditUserInfoButton extends Button {
+    private final EditUserInfoWindow m_window;
+
+    public EditUserInfoButton() {
+        m_window = new EditUserInfoWindow();
+
+        setCaption("My Info");
+        setEnabled(false);
+
+        addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                m_window.open(getWindow());
+            }
+        });
+    }
+
+    protected Object[] getComposition() {
+        return new Object[] { this, m_window };
+    }
+
+    protected void start(Component component) {
+        setEnabled(true);
+    }
+
+    protected void stop(Component component) {
+        setEnabled(false);
+        setDescription("This service seems to be unavailable at this moment...");
+    }
+}

Propchange: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoButton.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoWindow.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoWindow.java?rev=1537894&view=auto
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoWindow.java (added)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoWindow.java Fri Nov  1 10:55:08 2013
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.ace.useradmin.ui.vaadin;
+
+import org.apache.ace.useradmin.ui.editor.UserDTO;
+import org.apache.ace.useradmin.ui.editor.UserEditor;
+import org.osgi.service.useradmin.User;
+
+import com.vaadin.data.Property.ValueChangeEvent;
+import com.vaadin.data.Property.ValueChangeListener;
+import com.vaadin.ui.Button;
+import com.vaadin.ui.Button.ClickEvent;
+import com.vaadin.ui.Button.ClickListener;
+import com.vaadin.ui.FormLayout;
+import com.vaadin.ui.PasswordField;
+import com.vaadin.ui.TextField;
+import com.vaadin.ui.Window;
+
+/**
+ * Provides a simple editor for changing the password of the current user.
+ */
+public class EditUserInfoWindow extends Window {
+    private final TextField m_groupField;
+    private final TextField m_usernameTextField;
+    private final PasswordField m_passwordTextField;
+    private final Button m_applyButton;
+
+    private volatile UserDTO m_userDTO;
+    private volatile UserEditor m_userUtil;
+
+    /**
+     * Creates a new {@link EditUserInfoWindow} instance.
+     */
+    public EditUserInfoWindow() {
+        setCaption("My info");
+        setWidth("20%");
+
+        m_usernameTextField = new TextField();
+        m_usernameTextField.setEnabled(false);
+        m_usernameTextField.setCaption("Username");
+
+        m_passwordTextField = new PasswordField();
+        m_passwordTextField.setCaption("Password");
+        m_passwordTextField.setImmediate(true);
+        m_passwordTextField.addListener(new ValueChangeListener() {
+            @Override
+            public void valueChange(ValueChangeEvent event) {
+                boolean changed = passwordChanged();
+                m_applyButton.setEnabled(changed);
+            }
+        });
+
+        m_groupField = new TextField();
+        m_groupField.setEnabled(false);
+        m_groupField.setCaption("Role");
+
+        m_applyButton = new Button();
+        m_applyButton.setCaption("Apply changes");
+        m_applyButton.addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                storeUserInfo();
+            }
+        });
+
+        FormLayout formLayout = new FormLayout();
+        formLayout.setMargin(false, false, false, true);
+        formLayout.addComponent(m_usernameTextField);
+        formLayout.addComponent(m_passwordTextField);
+        formLayout.addComponent(m_groupField);
+        formLayout.addComponent(m_applyButton);
+
+        addComponent(formLayout);
+    }
+
+    @Override
+    public void attach() {
+        try {
+            // Take the logged in user from the main application...
+            initializeUserDTO((User) getApplication().getUser());
+        }
+        finally {
+            super.attach();
+        }
+    }
+
+    /**
+     * @param parent
+     *            the parent window of this editor, cannot be <code>null</code>.
+     */
+    public void open(Window parent) {
+        // In case this window is already open, close it first...
+        close();
+
+        // will show this window on screen, and call attach() above...
+        parent.addWindow(this);
+        center();
+
+        m_applyButton.setEnabled(false);
+    }
+
+    @Override
+    protected void close() {
+        try {
+            m_userDTO = null;
+        }
+        finally {
+            super.close();
+        }
+    }
+
+    /**
+     * Called for each change of the password field.
+     * 
+     * @return <code>true</code> if the password is valid, <code>false</code> otherwise.
+     */
+    protected boolean passwordChanged() {
+        return isPasswordValid((String) m_passwordTextField.getValue());
+    }
+
+    /**
+     * Will be called by Felix DM when all dependency are available
+     */
+    protected void start(org.apache.felix.dm.Component component) {
+        close();
+    }
+
+    /**
+     * Will be called by Felix DM when a dependency isn't available
+     */
+    protected void stop(org.apache.felix.dm.Component component) {
+        close();
+    }
+
+    /**
+     * @return <code>true</code> iff the user information is successfully stored, <code>false</code> otherwise.
+     */
+    protected boolean storeUserInfo() {
+        String pwd = (String) m_passwordTextField.getValue();
+        if (!isPasswordValid(pwd)) {
+            showNotification("Password cannot be empty", Notification.TYPE_ERROR_MESSAGE);
+            return false;
+        }
+
+        try {
+            m_userDTO.setPassword(pwd);
+            m_userUtil.updateUser(m_userDTO);
+
+            showNotification(String.format("Password for '%s' updated!", m_userDTO.getUsername()), Notification.TYPE_TRAY_NOTIFICATION);
+
+            return true;
+        }
+        catch (Exception e) {
+            showNotification("Failed to store user changes!", Notification.TYPE_ERROR_MESSAGE);
+        }
+
+        return false;
+    }
+
+    private void initializeUserDTO(User user) {
+        m_userDTO = new UserDTO((String) user.getProperties().get("username"), (String) user.getCredentials().get("password"), m_userUtil.getGroup(user).getName());
+
+        m_usernameTextField.setValue(m_userDTO.getUsername());
+        m_passwordTextField.setValue(m_userDTO.getPassword());
+        m_groupField.setValue(m_userDTO.getGroupname());
+    }
+
+    private boolean isPasswordValid(String newPassword) {
+        if ((newPassword == null) || "".equals(newPassword.trim())) {
+            return false;
+        }
+        return true;
+    }
+}

Propchange: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/EditUserInfoWindow.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminButton.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminButton.java?rev=1537894&r1=1537893&r2=1537894&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminButton.java (original)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminButton.java Fri Nov  1 10:55:08 2013
@@ -20,56 +20,49 @@ package org.apache.ace.useradmin.ui.vaad
 
 import org.apache.ace.useradmin.ui.editor.UserEditor;
 import org.apache.felix.dm.Component;
-import org.apache.felix.dm.DependencyManager;
 import org.osgi.service.useradmin.User;
 
 import com.vaadin.ui.Button;
-import com.vaadin.ui.Window;
 
 public class UserAdminButton extends Button {
+    private final UserAdminWindow m_window;
 
-    private final User m_currentUser;
     private volatile UserEditor m_userUtil;
-    private volatile Component m_window;
-    private volatile ClickListener m_click;
 
-    public UserAdminButton(User currentUser) {
-        m_currentUser = currentUser;
+    public UserAdminButton() {
+        setCaption("Manage Users");
         setEnabled(false);
+
+        m_window = new UserAdminWindow();
+
+        addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                m_window.open(getWindow());
+            }
+        });
     }
 
-    public void init(Component component) {
-        final DependencyManager dm = component.getDependencyManager();
-        setEnabled(true);
-        if (m_userUtil.hasRole(m_currentUser, "editUsers")) {
-            setCaption("Manage Users");
+    @Override
+    public void attach() {
+        User user = (User) getApplication().getUser();
+        if (m_userUtil.hasRole(user, "editUsers")) {
+            setEnabled(true);
         }
         else {
-            setCaption("My Info");
+            // no edit user karma, so no use to show this button at all...
+            setVisible(false);
         }
-        final Window window = new UserAdminWindow(m_currentUser);
-        window.setModal(true);
-        // create a new dependency for the window
-        m_window = dm.createComponent()
-            .setImplementation(window)
-            .add(dm.createServiceDependency()
-                .setService(UserEditor.class)
-                .setRequired(true)
-            );
-        dm.add(m_window);
-        m_click = new ClickListener() {
-            @Override
-            public void buttonClick(ClickEvent event) {
-                getWindow().addWindow(window);
-            }
-        };
-        addListener(m_click);
+
+        super.attach();
+    }
+
+    protected Object[] getComposition() {
+        return new Object[] { this, m_window };
     }
 
-    public void destroy(Component component) {
+    protected void stop(Component component) {
         setEnabled(false);
-        component.getDependencyManager().remove(m_window);
-        removeListener(m_click);
         setDescription("This service seems to be unavailable at this moment...");
     }
 }

Modified: ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminWindow.java
URL: http://svn.apache.org/viewvc/ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminWindow.java?rev=1537894&r1=1537893&r2=1537894&view=diff
==============================================================================
--- ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminWindow.java (original)
+++ ace/trunk/org.apache.ace.useradmin.ui/src/org/apache/ace/useradmin/ui/vaadin/UserAdminWindow.java Fri Nov  1 10:55:08 2013
@@ -23,20 +23,16 @@ import java.util.List;
 import org.apache.ace.useradmin.ui.editor.GroupNotFoundException;
 import org.apache.ace.useradmin.ui.editor.UserAlreadyExistsException;
 import org.apache.ace.useradmin.ui.editor.UserDTO;
-import org.apache.ace.useradmin.ui.editor.UserNotFoundException;
 import org.apache.ace.useradmin.ui.editor.UserEditor;
+import org.apache.ace.useradmin.ui.editor.UserNotFoundException;
 import org.osgi.service.useradmin.Group;
 import org.osgi.service.useradmin.User;
 
-import com.vaadin.Application;
 import com.vaadin.data.Property.ValueChangeEvent;
 import com.vaadin.data.Property.ValueChangeListener;
-import com.vaadin.event.ItemClickEvent;
-import com.vaadin.event.ItemClickEvent.ItemClickListener;
 import com.vaadin.ui.Button;
 import com.vaadin.ui.Button.ClickEvent;
 import com.vaadin.ui.Button.ClickListener;
-import com.vaadin.ui.Component;
 import com.vaadin.ui.FormLayout;
 import com.vaadin.ui.HorizontalLayout;
 import com.vaadin.ui.PasswordField;
@@ -45,599 +41,353 @@ import com.vaadin.ui.Table;
 import com.vaadin.ui.TextField;
 import com.vaadin.ui.VerticalLayout;
 import com.vaadin.ui.Window;
+import com.vaadin.ui.themes.Reindeer;
 
 /**
- * UserAdminWindow is the user management window which handles changes made to ACE users. It provides an option to
- * perform CRUD operations on ACE users. The changes are made by calls to userUtilImpl which calls Felix's UserAdmin.
- * 
+ * Provides a more advanced CRUD-functionality for adding, removing or updating users in ACE.
  */
-@SuppressWarnings("serial")
 public class UserAdminWindow extends Window {
-
     private final Table m_userTable;
     private final Select m_groupSelect;
     private final TextField m_usernameTextField;
     private final PasswordField m_passwordTextField;
-    private final Button m_addNewUserButton;
-    private final Button m_cancelNewUserButton;
     private final Button m_applyButton;
+    private final Button m_cancelButton;
     private final Button m_removeUserButton;
-    private ItemClickListener m_itemClickListener;
-    private UserDTO m_userDTO;
-    private volatile UserEditor m_userUtil;
-    private final User m_currentUser;
-    private boolean adminMode;
 
-    public UserAdminWindow(User currentUser) {
-        m_userTable = new Table();
-        m_groupSelect = new Select();
-        m_usernameTextField = new TextField();
-        m_passwordTextField = new PasswordField();
-        m_addNewUserButton = new Button();
-        m_cancelNewUserButton = new Button();
-        m_applyButton = new Button();
-        m_removeUserButton = new Button();
-        m_currentUser = currentUser;
-    }
+    private volatile UserEditor m_userUtil;
 
-    public void init() {
+    /**
+     * Creates a new {@link UserAdminWindow} instance.
+     */
+    public UserAdminWindow() {
         setCaption("Manage users");
-        if (m_userUtil.hasRole(m_currentUser, "editUsers")) {
-            adminMode = true;
+        setWidth("30%");
 
-            setSizeFull();
-            addComponent(createAdminWindowLayout());
-            populateUserTableAndSelect();
-        }
-        else {
-            setCaption("My info");
-            addComponent(createUserWindowLayout());
-            populateSelect();
-            showApplyButton();
-            initializeUserDTO();
-        }
-    }
+        m_userTable = new Table();
+        m_userTable.setSizeFull();
+        m_userTable.setImmediate(true);
+        m_userTable.setSelectable(true);
+        m_userTable.setSortDisabled(false);
+        m_userTable.addContainerProperty("User", UserDTO.class, null);
+        m_userTable.addListener(new ValueChangeListener() {
+            @Override
+            public void valueChange(ValueChangeEvent event) {
+                selectUser((UserDTO) m_userTable.getValue());
+            }
+        });
 
-    /**
-     * Will be called when a dependency isn't available
-     */
-    public void destroy() {
-        close();
+        VerticalLayout usersList = new VerticalLayout();
+        usersList.setSizeFull();
+        usersList.addComponent(m_userTable);
+
+        Button addUserButton = new Button("+");
+        addUserButton.setStyleName(Reindeer.BUTTON_SMALL);
+        addUserButton.addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                prepareForNewUser();
+            }
+        });
 
-        Application application = getApplication();
-        if (application != null) {
-            Window mainWindow = application.getMainWindow();
-            if (mainWindow != null) {
-                if (adminMode) {
-                    mainWindow.showNotification("Oops", "Manage Users function has been disabled", Notification.TYPE_ERROR_MESSAGE);
-                }
-                else {
-                    mainWindow.showNotification("Oops", "My info function has been disabled", Notification.TYPE_ERROR_MESSAGE);
-                }
+        m_removeUserButton = new Button();
+        m_removeUserButton.setStyleName(Reindeer.BUTTON_SMALL);
+        m_removeUserButton.setCaption("-");
+        m_removeUserButton.addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                removeSelectedUser((UserDTO) m_userTable.getValue());
             }
-        }
-    }
+        });
 
-    private void initializeUserDTO() {
-        m_userDTO = new UserDTO((String) m_currentUser.getProperties().get("username"), (String) m_currentUser.getCredentials().get("password"), m_userUtil.getGroup(m_currentUser).getName());
-        m_usernameTextField.setValue(m_userDTO.getUsername());
-        m_passwordTextField.setValue(m_userDTO.getPassword());
-        m_groupSelect.setValue(m_userDTO.getGroupname());
-        disableUsernameAndGroup();
-    }
+        HorizontalLayout addRemoveUserButtons = new HorizontalLayout();
+        addRemoveUserButtons.setMargin(true, false, false, false);
+        addRemoveUserButtons.setSpacing(true);
+        addRemoveUserButtons.addComponent(addUserButton);
+        addRemoveUserButtons.addComponent(m_removeUserButton);
+        usersList.addComponent(addRemoveUserButtons);
 
-    /**
-     * Creates a new Layout containing the user table and edit form
-     * 
-     * @returns the manage users window
-     */
-    private HorizontalLayout createAdminWindowLayout() {
-        getWindow().setWidth("40%");
-        HorizontalLayout horizontalLayout = new HorizontalLayout();
-        horizontalLayout.setSizeFull();
-        horizontalLayout.addComponent(createFirstColumn());
-        horizontalLayout.addComponent(createFormLayout());
-        return horizontalLayout;
-    }
+        usersList.setExpandRatio(m_userTable, 1.0f);
+        usersList.setExpandRatio(addRemoveUserButtons, 0.0f);
 
-    /**
-     * Create a new Layout containing with only the information of the logged user
-     * 
-     * @return
-     */
-    private HorizontalLayout createUserWindowLayout() {
-        getWindow().setHeight("25%");
-        getWindow().setWidth("20%");
-        HorizontalLayout horizontalLayout = new HorizontalLayout();
-        horizontalLayout.addComponent(createFormLayout());
-        return horizontalLayout;
-    }
+        ValueChangeListener changeListener = new ValueChangeListener() {
+            @Override
+            public void valueChange(ValueChangeEvent event) {
+                m_applyButton.setEnabled(isCurrentFormValid());
+                m_cancelButton.setEnabled(true);
+            }
+        };
 
-    /**
-     * Creates the left column containing the user table
-     * 
-     * @returns the user table column
-     */
-    private VerticalLayout createFirstColumn() {
-        VerticalLayout verticalLayout = new VerticalLayout();
-        verticalLayout.setSizeFull();
-        verticalLayout.setMargin(false, true, false, false);
-        Table table = initTable();
-        verticalLayout.addComponent(table);
-        HorizontalLayout buttons = createHorizontalButtonLayout();
-        verticalLayout.addComponent(buttons);
-        verticalLayout.setExpandRatio(table, 1.0f);
-        verticalLayout.setExpandRatio(buttons, 0.0f);
-        return verticalLayout;
-    }
+        m_usernameTextField = new TextField();
+        m_usernameTextField.setCaption("Username");
+        m_usernameTextField.setImmediate(true);
+        m_usernameTextField.setRequired(true);
+        m_usernameTextField.addListener(changeListener);
+
+        m_passwordTextField = new PasswordField();
+        m_passwordTextField.setCaption("Password");
+        m_passwordTextField.setImmediate(true);
+        m_passwordTextField.setRequired(true);
+        m_passwordTextField.addListener(changeListener);
+
+        m_groupSelect = new Select();
+        m_groupSelect.setCaption("Role");
+        m_groupSelect.setImmediate(true);
+        m_groupSelect.setNullSelectionAllowed(false);
+        m_groupSelect.setRequired(true);
+        m_groupSelect.addListener(changeListener);
 
-    /**
-     * Creates the form containing fields to edit user data
-     * 
-     * @returns the user edit fields
-     */
-    private FormLayout createFormLayout() {
         FormLayout formLayout = new FormLayout();
-        formLayout.setMargin(false, false, false, true);
-        formLayout.addComponent(initUsernameTextField());
-        formLayout.addComponent(initPasswordField());
-        formLayout.addComponent(initSelect());
+        formLayout.addComponent(m_usernameTextField);
+        formLayout.addComponent(m_passwordTextField);
+        formLayout.addComponent(m_groupSelect);
+
+        m_applyButton = new Button();
+        m_applyButton.setCaption("Apply changes");
+        m_applyButton.addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                storeUserInfo();
+            }
+        });
+
+        m_cancelButton = new Button();
+        m_cancelButton.setEnabled(false);
+        m_cancelButton.setCaption("Cancel");
+        m_cancelButton.addListener(new ClickListener() {
+            @Override
+            public void buttonClick(ClickEvent event) {
+                selectUser((UserDTO) m_userTable.getValue());
+            }
+        });
+
         HorizontalLayout addUserButtons = new HorizontalLayout();
-        addUserButtons.addComponent(initApplyButton());
-        Button initializeAddNewUserButton = initializeAddNewUserButton();
-        addUserButtons.addComponent(initializeAddNewUserButton);
-        // addUserButtons.setComponentAlignment(initializeAddNewUserButton, Alignment.MIDDLE_CENTER);
+        addUserButtons.setMargin(true, false, false, false);
         addUserButtons.setSpacing(true);
-        addUserButtons.addComponent(initCancelNewUserButton());
+        addUserButtons.addComponent(m_applyButton);
+        addUserButtons.addComponent(m_cancelButton);
 
         formLayout.addComponent(addUserButtons);
-        hideNewUserButtons();
-        disableTextFieldsAndSelect();
-        return formLayout;
-    }
 
-    /**
-     * Creates a Layout containing the + and - button for addition and removal of users
-     * 
-     * @returns the button layout containing + and - buttons
-     */
-    private HorizontalLayout createHorizontalButtonLayout() {
         HorizontalLayout horizontalLayout = new HorizontalLayout();
-        horizontalLayout.setMargin(true, false, true, false);
+        horizontalLayout.setSizeFull();
         horizontalLayout.setSpacing(true);
-        horizontalLayout.addComponent(createAddButton());
-        horizontalLayout.addComponent(createRemoveButton());
-        return horizontalLayout;
-    }
+        horizontalLayout.addComponent(usersList);
+        horizontalLayout.addComponent(formLayout);
 
-    /**
-     * Initializes the user table
-     * 
-     * @returns the usertable
-     */
-    private Table initTable() {
-        m_userTable.setSizeFull();
-        m_userTable.setSelectable(true);
-        m_userTable.setPageLength(0);
-        m_userTable.addContainerProperty("User", UserDTO.class, null);
-        m_userTable.setSortDisabled(false);
-        m_userTable.addListener(createUserTableSelectListener());
-        return m_userTable;
-    }
+        horizontalLayout.setExpandRatio(usersList, 0.35f);
+        horizontalLayout.setExpandRatio(formLayout, 0.65f);
 
-    /**
-     * Creates a ClickListener to update the table selection
-     * 
-     * @returns a user table selection listener
-     */
-    private ItemClickListener createUserTableSelectListener() {
-        m_itemClickListener = new ItemClickListener() {
+        addComponent(horizontalLayout);
 
-            @Override
-            public void itemClick(ItemClickEvent event) {
-                enableTextFieldsAndSelect();
-                hideNewUserButtons();
-                showApplyButton();
-                UserDTO user = (UserDTO) event.getItem().getItemProperty("User").getValue();
-                m_userTable.select(user);
-                m_userDTO = new UserDTO(user.getUsername(), user.getPassword(), user.getGroupname());
-                m_usernameTextField.setValue(user.getUsername());
-                m_passwordTextField.setValue(user.getPassword());
-                m_groupSelect.setValue(user.getGroupname());
-                checkSameUser(m_userDTO.getUsername());
-            }
-        };
-        return m_itemClickListener;
+        updateState(null, false /* editAllowed */);
     }
 
-    /**
-     * Inserts inital user data into the user table
-     */
-    private void populateUserTableAndSelect() {
-        m_userTable.removeAllItems();
-        List<UserDTO> data = m_userUtil.getData();
-        for (UserDTO userDTO : data) {
-            m_userTable.addItem(new Object[] { userDTO }, userDTO);
+    @Override
+    public void attach() {
+        try {
+            populateUserTable();
+            populateSelect();
+        }
+        finally {
+            super.attach();
         }
-        populateSelect();
     }
 
-    private void populateSelect() {
-        List<Group> grouplist = m_userUtil.getGroups();
-        for (Group g : grouplist) {
-            m_groupSelect.addItem(g.getName());
-        }
-        m_userTable.sort(new Object[] { "User" }, new boolean[] { true });
+    public void open(Window parent) {
+        parent.removeWindow(this);
+        parent.addWindow(this);
+        center();
     }
 
     /**
-     * Creates the + button for creation of new users
-     * 
-     * @returns the + button
+     * @return <code>true</code> if the current form is valid, <code>false</code> otherwise.
      */
-    private Button createAddButton() {
-        Button b = new Button("+");
-        b.setWidth("4em");
-        b.addListener(new ClickListener() {
-
-            @Override
-            public void buttonClick(final ClickEvent event) {
-                enableTextFieldsAndSelect();
-                m_usernameTextField.setValue("");
-                m_passwordTextField.setValue("");
-                m_groupSelect.setValue(null);
-                m_userTable.select(null);
-                m_usernameTextField.focus();
-                showNewUserButtons();
-            }
-        });
-        return b;
+    protected boolean isCurrentFormValid() {
+        String username = (String) m_usernameTextField.getValue();
+        if (username == null || "".equals(username.trim())) {
+            return false;
+        }
+        String password = (String) m_passwordTextField.getValue();
+        if (password == null || "".equals(password.trim())) {
+            return false;
+        }
+        String groupName = (String) m_groupSelect.getValue();
+        if (groupName == null || "".equals(groupName.trim())) {
+            return false;
+        }
+        return true;
     }
 
     /**
-     * Initializes the - button for removal of existing users
-     * 
-     * @returns the - button
+     * Prepares everything for adding a new user.
      */
-    private Button createRemoveButton() {
-        m_removeUserButton.setCaption("-");
-        m_removeUserButton.setWidth("4em");
-        m_removeUserButton.addListener(new ClickListener() {
+    protected void prepareForNewUser() {
+        m_userTable.setValue(null);
+        m_usernameTextField.focus();
 
-            @Override
-            public void buttonClick(ClickEvent event) {
-                try {
-                    if (m_userTable.getValue() == null) {
-                        return;
-                    }
-                    m_userUtil.removeUser(m_userDTO);
-                    m_userTable.removeItem(m_userTable.getValue());
-                    m_usernameTextField.setValue("");
-                    m_passwordTextField.setValue("");
-                    m_groupSelect.select(null);
-                    disableTextFieldsAndSelect();
-                }
-                catch (UserNotFoundException e) {
-                    showUserNotFoundWarning();
-                }
-            }
-        });
-        return m_removeUserButton;
+        updateState(null, true /* editAllowed */);
     }
 
     /**
-     * Initializes the apply button to save changes after editing
-     * 
-     * @returns the apply button
+     * Removes the given user.
      */
-    private Button initApplyButton() {
-        m_applyButton.setCaption("Apply");
-        m_applyButton.addListener(new ClickListener() {
-
-            @Override
-            public void buttonClick(ClickEvent event) {
-                String usernameTextFieldInput = (String) m_usernameTextField.getValue();
-                if (usernameTextFieldInput == null || "".equals(usernameTextFieldInput)) {
-                    getWindow().showNotification("Username cannot be empty", Window.Notification.TYPE_WARNING_MESSAGE);
-                    return;
-                }
-                String groupName = (String) m_groupSelect.getValue();
-                if (groupName == null || "".equals(groupName)) {
-
-                    getWindow().showNotification("Role cannot be empty", Window.Notification.TYPE_WARNING_MESSAGE);
-                    return;
-                }
-                String passwordTextFieldInput = (String) m_passwordTextField.getValue();
-                if (passwordTextFieldInput == null || "".equals(passwordTextFieldInput)) {
-                    getWindow().showNotification("Password cannot be empty", Window.Notification.TYPE_WARNING_MESSAGE);
-                    return;
-                }
-                try {
+    protected void removeSelectedUser(UserDTO user) {
+        if (user == null) {
+            return;
+        }
 
-                    if (m_userDTO.isUpdated()) {
-                        m_userUtil.storeUserDTO(m_userDTO);
-                        if (adminMode) {
-                            UserDTO userDTO = new UserDTO(m_userDTO.getUsername(), m_userDTO.getPassword(), m_userDTO.getGroupname());
-                            m_userTable.removeItem(m_userTable.getValue());
-                            m_userTable.addItem(new Object[] { userDTO }, userDTO);
-                            m_userTable.sort(new Object[] { "User" }, new boolean[] { true });
-                            m_itemClickListener.itemClick(new ItemClickEvent((Component) event.getSource(), m_userTable.getItem(userDTO), null, null, null));
-                        }
-                        getWindow().showNotification("User updated");
+        try {
+            if (m_userTable.removeItem(user)) {
+                m_userUtil.removeUser(user);
 
-                    }
-                }
-                catch (UserNotFoundException e) {
-                    showUserNotFoundWarning();
-                }
-                catch (GroupNotFoundException e) {
-                    showGroupNotFoundWarning();
-                }
-                catch (UserAlreadyExistsException e) {
-                    showUserAlreadyExistsWarning();
-                }
+                showNotification(String.format("User '%s' removed!", user.getUsername()), Notification.TYPE_TRAY_NOTIFICATION);
             }
-        });
-        return m_applyButton;
+        }
+        catch (UserNotFoundException e) {
+            showNotification("Cannot store changes!", "<br>User not found, please refresh.", Notification.TYPE_ERROR_MESSAGE);
+        }
+
+        updateState(null, false /* editAllowed */);
     }
 
     /**
-     * Initializes the cancel button which cancels the addition of a new user
+     * Called when the selected user is changed.
      * 
-     * @returns the cancel button
+     * @param user
+     *            the selected user, can be <code>null</code> in case no user is selected.
      */
-    private Button initCancelNewUserButton() {
-        m_cancelNewUserButton.setCaption("Cancel");
-        m_cancelNewUserButton.addListener(new ClickListener() {
+    protected void selectUser(UserDTO user) {
+        if (user != null) {
+            m_usernameTextField.setValue(user.getUsername());
+            m_passwordTextField.setValue(user.getPassword());
+            m_groupSelect.setValue(user.getGroupname());
+        }
+        else {
+            m_usernameTextField.setValue("");
+            m_passwordTextField.setValue("");
+            m_groupSelect.setValue(null);
+        }
 
-            @Override
-            public void buttonClick(ClickEvent event) {
-                hideNewUserButtons();
-                m_userTable.select(null);
-                disableTextFieldsAndSelect();
-            }
-        });
-        return m_cancelNewUserButton;
+        updateState(user, user != null /* editAllowed */);
+
+        m_cancelButton.setEnabled(false);
     }
 
     /**
-     * Initializes the add button which checks if the input is valid, and then saves a new user to UserAdmin
-     * 
-     * @returns the add button
+     * Will be called by Felix DM when all dependencies become available.
      */
-    private Button initializeAddNewUserButton() {
-        m_addNewUserButton.setCaption("Add");
-        m_addNewUserButton.setWidth("5em");
-        m_addNewUserButton.addListener(new ClickListener() {
-
-            @Override
-            public void buttonClick(ClickEvent event) {
-                String username = (String) m_usernameTextField.getValue();
-                String password = (String) m_passwordTextField.getValue();
-                String group = (String) m_groupSelect.getValue();
-                if (username == null || "".equals(username) || password == null || "".equals(password) || group == null || "".equals(group)) {
-                    showEmptyInputWarning();
-                }
-                else {
-                    try {
-                        UserDTO userDTO = new UserDTO(username, password, group);
-                        m_userUtil.addUser(userDTO);
-                        m_userTable.addItem(new Object[] { userDTO }, userDTO);
-                        m_userTable.sort(new Object[] { "User" }, new boolean[] { true });
-                        m_userTable.select(username);
-                        m_itemClickListener.itemClick(new ItemClickEvent((Component) event.getSource(), m_userTable.getItem(userDTO), null, null, null));
-                    }
-                    catch (GroupNotFoundException e) {
-                        showGroupNotFoundWarning();
-                    }
-                    catch (UserAlreadyExistsException e) {
-                        showUserAlreadyExistsWarning();
-                    }
-                }
-            }
-        });
-        return m_addNewUserButton;
+    protected void start(org.apache.felix.dm.Component component) {
+        close();
     }
 
     /**
-     * Initializes the username text field
-     * 
-     * @returns the username text field
+     * Will be called by Felix DM when a dependency isn't available.
      */
-    private TextField initUsernameTextField() {
-        m_usernameTextField.setCaption("Username: ");
-        m_usernameTextField.setImmediate(true);
-        m_usernameTextField.addListener(new ValueChangeListener() {
-
-            @Override
-            public void valueChange(ValueChangeEvent event) {
-                UserDTO user = (UserDTO) m_userTable.getValue();
-                if (user == null) {
-                    return;
-                }
-                if (m_userUtil.getUser(user.getUsername()) == null) {
-                    return;
-                }
-                String usernameTextFieldInput = (String) m_usernameTextField.getValue();
-                if (user.getUsername().equals(usernameTextFieldInput)) {
-                    if (!m_userDTO.getUsername().equals(usernameTextFieldInput)) {
-                        m_userDTO.setUsername(usernameTextFieldInput);
-                        m_userDTO.setUsernameChanged(false);
-                    }
-                    return;
-                }
-                m_userDTO.setUsername(usernameTextFieldInput);
-            }
-        });
-        return m_usernameTextField;
+    protected void stop(org.apache.felix.dm.Component component) {
+        close();
     }
 
     /**
-     * Initializes the password field
      * 
-     * @returns the password field
      */
-    private PasswordField initPasswordField() {
-        m_passwordTextField.setCaption("Password: ");
-        m_passwordTextField.setImmediate(true);
-        m_passwordTextField.addListener(new ValueChangeListener() {
+    protected void storeUserInfo() {
+        try {
+            String username = (String) m_usernameTextField.getValue();
+            String password = (String) m_passwordTextField.getValue();
+            String groupName = (String) m_groupSelect.getValue();
 
-            @Override
-            public void valueChange(ValueChangeEvent event) {
-                UserDTO user = m_userDTO;
-                if (user == null) {
-                    return;
-                }
-                User result = m_userUtil.getUser(user.getPreviousUsername());
-                if (result == null) {
-                    return;
-                }
-                String password = (String) result.getCredentials().get("password");
-                String passwordTextFieldInput = (String) m_passwordTextField.getValue();
-                if (password.equals(passwordTextFieldInput)) {
-                    if (!m_userDTO.getPassword().equals(passwordTextFieldInput)) {
-                        m_userDTO.setPassword(passwordTextFieldInput);
-                        m_userDTO.setPasswordChanged(false);
-                    }
-                    return;
-                }
-                m_userDTO.setPassword(passwordTextFieldInput);
-            }
-        });
-        return m_passwordTextField;
-    }
+            String notification;
+            Object itemID;
 
-    /**
-     * Initializes the group select box
-     * 
-     * @returns the group select
-     */
-    private Select initSelect() {
-        m_groupSelect.setCaption("Role: ");
-        m_groupSelect.setImmediate(true);
-        m_groupSelect.setSizeFull();
-        m_groupSelect.setNullSelectionAllowed(false);
-        m_groupSelect.addListener(new ValueChangeListener() {
+            UserDTO user = (UserDTO) m_userTable.getValue();
+            if (user == null) {
+                user = new UserDTO(username, password, groupName);
 
-            @Override
-            public void valueChange(ValueChangeEvent event) {
-                UserDTO user = (UserDTO) m_userTable.getValue();
-                if (user == null) {
-                    return;
+                m_userUtil.addUser(user);
+
+                notification = String.format("User '%s' created!", user.getUsername());
+
+                itemID = m_userTable.addItem(new Object[] { user }, user);
+            }
+            else {
+                if (!groupName.equals(user.getGroupname())) {
+                    user.setGroupname(groupName);
                 }
-                User result = m_userUtil.getUser(user.getUsername());
-                if (result == null) {
-                    return;
+                if (!username.equals(user.getUsername())) {
+                    user.setUsername(username);
                 }
-                Group group = m_userUtil.getGroup(result);
-                String groupName = (String) m_groupSelect.getValue();
-                if (group.getName().equals(groupName)) {
-                    if (!m_userDTO.getGroupname().equals(groupName)) {
-                        m_userDTO.setGroupname(groupName);
-                        m_userDTO.setGroupChanged(false);
-                    }
-                    return;
+                if (!password.equals(user.getPassword())) {
+                    user.setPassword(password);
                 }
-                m_userDTO.setGroupname(groupName);
-            }
-        });
-        return m_groupSelect;
-    }
 
-    /**
-     * Enables the username, password and group select form
-     */
-    private void enableTextFieldsAndSelect() {
-        m_usernameTextField.setEnabled(true);
-        m_passwordTextField.setEnabled(true);
-        m_groupSelect.setEnabled(true);
-        m_removeUserButton.setEnabled(true);
-    }
-
-    /**
-     * Disables the username, password and group select form
-     */
-    private void disableTextFieldsAndSelect() {
-        m_usernameTextField.setEnabled(false);
-        m_passwordTextField.setEnabled(false);
-        m_groupSelect.setEnabled(false);
-        m_removeUserButton.setEnabled(false);
-        hideApplyButton();
-    }
-
-    private void disableUsernameAndGroup() {
-        m_usernameTextField.setEnabled(false);
-        m_passwordTextField.setEnabled(true);
-        m_groupSelect.setEnabled(false);
-        m_removeUserButton.setEnabled(false);
-    }
-
-    private void checkSameUser(String currentUser) {
-        if (m_currentUser.getProperties().get("username").equals(m_userUtil.getUser(currentUser).getProperties().get("username"))) {
-            disableUsernameAndGroup();
-        }
-        else {
-            enableTextFieldsAndSelect();
-        }
-    }
+                m_userUtil.updateUser(user);
 
-    /**
-     * Shows the add and cancel buttons when adding a new user
-     */
-    private void showNewUserButtons() {
-        m_addNewUserButton.setVisible(true);
-        m_cancelNewUserButton.setVisible(true);
-        m_usernameTextField.setValue("");
-        m_passwordTextField.setValue("");
-        m_groupSelect.setValue(null);
-        m_removeUserButton.setEnabled(false);
-        hideApplyButton();
-    }
+                notification = String.format("User '%s' changed!", user.getUsername());
 
-    /**
-     * Hides the add and cancel buttons when adding a new user
-     */
-    private void hideNewUserButtons() {
-        m_addNewUserButton.setVisible(false);
-        m_cancelNewUserButton.setVisible(false);
-        m_usernameTextField.setValue("");
-        m_passwordTextField.setValue("");
-        m_groupSelect.setValue(null);
-    }
+                itemID = user;
+            }
 
-    private void showApplyButton() {
-        m_applyButton.setVisible(true);
-    }
+            m_userTable.sort(new Object[] { "User" }, new boolean[] { true });
+            m_userTable.setValue(itemID);
 
-    private void hideApplyButton() {
-        m_applyButton.setVisible(false);
+            showNotification(notification, Notification.TYPE_TRAY_NOTIFICATION);
+            
+            updateState(user, true /* editAllowed */);
+        }
+        catch (UserNotFoundException e) {
+            showNotification("Cannot store changes!", "<br>User not found, please refresh.", Notification.TYPE_ERROR_MESSAGE);
+            m_usernameTextField.focus();
+        }
+        catch (GroupNotFoundException e) {
+            showNotification("Cannot store changes!", "<br>Group was not found.", Notification.TYPE_ERROR_MESSAGE);
+            m_groupSelect.focus();
+        }
+        catch (UserAlreadyExistsException e) {
+            showNotification("Cannot store changes!", "<br>Username already in use.", Notification.TYPE_ERROR_MESSAGE);
+            m_usernameTextField.focus();
+        }
     }
 
-    /**
-     * Displays an error when the requested user is not found
-     */
-    private void showUserNotFoundWarning() {
-        getWindow().showNotification("Oops:", "<br>User not found, please refresh</br>", Window.Notification.TYPE_WARNING_MESSAGE);
+    private boolean isCurrentUser(UserDTO user) {
+        if (user == null) {
+            return false;
+        }
+        User currentUser = (User) getApplication().getUser();
+        return currentUser.getProperties().get("username").equals(user.getUsername());
     }
 
-    /**
-     * Displays an error that the given username is already in use
-     */
-    private void showUserAlreadyExistsWarning() {
-        getWindow().showNotification("Oops:", "<br>Username already in use</br>", Window.Notification.TYPE_WARNING_MESSAGE);
+    private void populateSelect() {
+        List<Group> grouplist = m_userUtil.getGroups();
+        for (Group g : grouplist) {
+            m_groupSelect.addItem(g.getName());
+        }
+        m_userTable.sort(new Object[] { "User" }, new boolean[] { true });
     }
 
     /**
-     * Displays an error that the selected group is not found
+     * Inserts inital user data into the user table
      */
-    private void showGroupNotFoundWarning() {
-        getWindow().showNotification("Error:", "<br>Group not found, please refresh</br>", Window.Notification.TYPE_WARNING_MESSAGE);
+    private void populateUserTable() {
+        m_userTable.removeAllItems();
+        List<UserDTO> data = m_userUtil.getData();
+        for (UserDTO userDTO : data) {
+            m_userTable.addItem(new Object[] { userDTO }, userDTO);
+        }
     }
 
     /**
-     * Displays an error message regarding empty input
+     * @param user
      */
-    private void showEmptyInputWarning() {
-        getWindow().showNotification("Oops:", "<br>Username, password & group cannot be empty</br>", Window.Notification.TYPE_WARNING_MESSAGE);
+    private void updateState(UserDTO user, boolean editAllowed) {
+        boolean userSelected = (user != null);
+        m_applyButton.setEnabled(false);
+        m_cancelButton.setEnabled(!userSelected && editAllowed);
+
+        boolean currentUser = isCurrentUser(user);
+        m_removeUserButton.setEnabled(userSelected && !currentUser);
+        m_usernameTextField.setEnabled(editAllowed && !currentUser);
+        m_passwordTextField.setEnabled(editAllowed);
+        m_groupSelect.setEnabled(editAllowed && !currentUser);
     }
 }



Mime
View raw message