HowTo's - Display: User Management

Define a UserFilter

Description:
Once you have a huge amount of registered Users in your application, you may want to display only a certain amount of them. This can be accomplished by the UserFilter.
UserFilter is an interface, but don't worry, there's not much to implement. You only have to define the method match(User user), which shall return true when the User matches the criteria you want him to be displayed for.

Used classes:

Related topics:

ToDo:

  1. Create a class implementing UserFilter.
  2. Add the constructor and invoke the super constructor.
  3. Implement the public boolean match(User user) method.
    Therein decide which user shall be filtered and which not.

Example Source Code:

1
public class DisplayUserFilter implements UserFilter
{
			
    2
    public DisplayUserFilter()
    {
        super();
    }
			
    3
    public boolean match(User user)
    {
        return user.getCapability("AdminRights").isGranted();
    }
}
		

Back to:


Display Users in a JUserList

Description:
Like TableFormSheets display the content of CountingStocks or Catalogs, a JUserList displays the Users managed by a UserManager.
The JUserList is a JComponent and thereby can easily be displayed by a FormSheet. In this example we do not only display the Users, but use the method User.getCapabilityCheckBox(), too, so we can also edit the Capabilities of a User.
With the UserManager determined as the GlobalUM, the JUserList can be initialized with its default constructor and will use the GlobalUM. Otherwise you have to initialize it with the UserManager you want to display the content of.
The JUserList being a JComponent makes it possible to add other components to it, like in this case the JCheckBox. It is disabled at first, because initially no User is being selected in the JUserList.
In order to enable the CheckBox after a selection, we added a ListSelectionListener to the JUserList and let the valueChanged(ListSelectionEvent) method update the CheckBox.
In this example we put all this into a SaleProcess, which is the easiest way, but not the only one. Be careful to initialize the UIGate with the new FormSheet and then add the JComponent to the Formsheet, otherwise the Gate won't display the JUserList.
For more information about processes, please refer to the section Processes.

Used classes:

Related topics:

ToDo:

  1. Add a JUserList to the attributes of the SaleProcess in order to make it available all over the process. Do the same for a JCheckBox, Transition and UIGate. All four will be needed.
  2. In your Gate initialize the JUserList. The default constructor will do, because we use only one UserManager and declared it GlobalUM in the Shop instance.
  3. Before we add the JCheckBox that later on will be the CapabilityCheckBox, we set the Layout of the JUserList and after that add the blank JCheckBox.
  4. In order to update the state of the JCheckBox, add a ListSelectionListener to the JUserList. This will enable the JCheckBox as soon as a User is selected in the List and even update the value of the JCheckBox referring to the selected User.
  5. Create a FormSheet (with buttons and so on, set by FormSheetContentCreator) where the JUserList will be displayed in.
  6. Create a new UIGate containing that FormSheet.
  7. Call FormSheet.setComponent at the FormSheet above to display the JUserList and the UIGate is ready to be displayed.
  8. Instantiate the Transition that updates the JCheckbox. Therein remove the old Box, create a new from the JUserList, add it to the JUserList and return the UIGate from the perform method.

The order of the last two steps is quite important, because the other way around (first setting the Component to the FormSheet and then adding it to the UIGate), the Gate won't display the JUserList!

Example Source Code:

public class DisplaySaleProcess extends SaleProcess
{
    1
    private JUserList userList;
    private JCheckBox checkBox;
    private Transition transitionSelect;
    private UIGate uig_user;
			
    public Gate getUserGate()
    {
        2
        userList = new JUserList();
			
        3
        userList.setLayout(new GridLayout());
        checkBox = new JCheckBox();
        userList.add(checkBox);
			
        4
        userList.addListSelectionListener(new ListSelectionListener()
                {
                    public void valueChanged(ListSelectionEvent listSelectionEvent)
                    {
                        uig_user.setNextTransition((Transition) transitionSelect);
                    }
                }
        );
			
        5
        FormSheet formSheet = new FormSheet("UserEditor", null);
        formSheet.addContentCreator(
                new DisplayFormSheetContentCreator());
			
        6
        uig_user = new UIGate(formSheet, null);
			
        7
        formSheet.setComponent(userList);
			
        8
        transitionSelect = new Transition()
            {
                public Gate perform(SaleProcess process, User user)
                {
                    userList.remove(checkBox);
                    checkBox = userList.getSelectedUser().getCapabilityCheckBox("Office");
                    userList.add(checkBox);
                    return uig_user;
                }
            };
			
        return uig_user;
    }
}
		

Back to:


Define a User's Capability

Description:
To display the status of a user's Capability, the class User has the method getCapabilityCheckBox(String nameOfCap). This will return a JCheckBox with the DisplayName of the Capability on it and it will be checked or not, depending on wether the user is granted that capability.
The tricky thing about this is the CapabilityDisplayName which is used as the caption of the JCheckBox. You have to extend the java.util.ResourceBundle at first and put the DisplayName (the + caption matching to the locale) into the keyList. For more information on ResourceBundles go to ResourceBundle of the Java API and setDisplayNameResourceBundleName of the Framework API.
For the complete example refer to Display Users in a JUserList.

Used classes:

Related topics:

ToDo:

  1. Resolve the User you want to display the Capability of and call the getCapabilityCheckBox(String nameOfCap) method in order to get the JCheckBox.
  2. Add the JCheckBox to the Container you want it to be displayed in.

You don't have to take care about any action triggered by the CheckBox. The status of the Capability will be updated automatically at the User by the Framework, so it can also be used to edit the Capabilites.

Example Source Code:

 
                    1
                    checkBox = userList.getSelectedUser().getCapabilityCheckBox("Office");
			
                    2
                    userList.add(checkBox);
		

Back to:


Use a LogOnForm

Description:
A LogOnForm is a special FormSheet with a ComboBox and an optional PasswordField.
It can be used where ever you want a User to be identified. The ComboBox will show the Users stored in the UserManager and in the PasswordField you can enter the password.
If the identification has been successful, the LogOnForm will return the User that was identified, otherwise null.

Used classes:

Related topics:

ToDo:

  1. Instantiate the LogOnForm with
    • its title
    • the prompt of the ComboBox
    • the prompt of the PasswordField
    • boolean wether password is needed or not
    • the UserManager storing the User to be identified
    • a Comparator for the appearance of the Users.
      (null -> Users will be ordered by their names)
    • a Filter in order to display only certain Users.
      (null -> all Users will be displayed)
  2. Display the FormSheet.

The method getResult() will return the User if the identification was successful, else null.

Example Source Code:

 
        1
        LogOnForm formLogOn = new LogOnForm(
                "Please identify yourself",
                "Username: ",
                "Password: ",
                true,
                UserManager.getGlobalUM(),
                null,
                null);
			
        2
        uig_logon.setFormSheet(formLogOn);
		

Back to:


previous Display: LogFile ManagementDisplay: Tables next


Valid HTML 4.01!