SOURCECODE

How to... display Users in a JUserListBox


Description:
Like TableFormSheets display the content of CountingStocks or Catalogs, a JUserListBox displays the Users managed by a UserManager.
The JUserListBox 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 Capabilites of a User.
With the UserManager determined as the GlobalUM, the JUserListBox 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 JUserListBox 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.
Take also a closer look at the imports, they are quite various.
For more information about processes, please refer to the section Processes

ToDo's:
  1. Add a JUserList to the attributes of the SaleProcess in order to make it available all over the process
  2. Initialize the JUserList while setting up the SaleProcess. 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
  4. In order to update the state of the CheckBox, add a ListSelectionListener to the JUserList. This will enable the CheckBox as soon as a User is selected in the List and even update the value of the CheckBox referring to the selected User
  5. Create a FormSheet (with it's buttons and so on) 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

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!

Uses:
SalesProcess  UIGate  FormSheet  User  JUserList  



SourceCode

import sale.*;
import users.*;
import users.swing.*; //for the JUserList
import javax.swing.*; //for the JCheckBox
import javax.swing.event.*; //for the ListSelectionListener
import java.awt.*; //for the GridLayout


public class EditCustomersProcess extends SaleProcess {

     1
    //The JUserList that will display all the users
    private JUserList jul;
    //The CheckBox that will indicate the Capability
    private JCheckBox jcb;
    
    private UIGate uig;
    private Transition selection;
    
    public EditCustomersProcess() {
        super("Edit Customers");
    }

    //setup the Gate and Transition
    public void setupMachine() {

         2
        //In this case the GlobalUM will be used as UserManager
        jul = new JUserList();
        
         3
        jul.setLayout(new GridLayout());
        //first we add a blank JCheckBox, because primarily there´s no user selected
        jcb = new JCheckBox();
        jul.add(jcb);
        
         4
        //the ListSelectionListener leads to the selection Transition
        //in order to update the JCheckBox according to the User selected
        jul.addListSelectionListener(new ListSelectionListener() {
            public void valueChanged(ListSelectionEvent lse) {
                uig.setNextTransition(selection);
            }
        });
        
         5
        FormSheet fs = new FormSheet("UserEditor", null);
        
        // Add the buttons with a FormSheetContentCreator
        fs.addContentCreator(new FormSheetContentCreator() {
            protected void createFormSheetContent(FormSheet fs) {
                fs.removeButton(fs.BTNID_OK);
                
                // add a new "Ok"-button
                fs.addButton ("Ok", 102, new sale.Action() { //the behaviour is defined rigth here
                    public void doAction (SaleProcess p, SalesPoint sp) {
                        uig.setNextTransition(GateChangeTransition.CHANGE_TO_COMMIT_GATE);
                    }
                });
                // add a new action to the CANCEL button
                fs.getButton(fs.BTNID_CANCEL).setAction(new sale.Action() {//the behaviour is defined rigth here
                    public void doAction (SaleProcess p, SalesPoint sp) {
                        uig.setNextTransition(GateChangeTransition.CHANGE_TO_ROLLBACK_GATE);
                    }
                });
            }
        });
        
         6
        //initiate the UIGate
        uig = new UIGate(fs, null);
        
         7
        //set the Component here, otherwise it won't be displayed!
        fs.setComponent(jul);
        
        //the Transition that updates the JCheckBox
        selection = new Transition() {
            public Gate perform(SaleProcess pOwner, User usr) {
                //remove the old CheckBox from the JUserList
                jul.remove(jcb);
                //create the new CheckBox
                jcb = jul.getSelectedUser().getCapabilityCheckBox("Office");
                //add it to the JUserList
                jul.add(jcb);
                //and return the Gate
                return uig;
            }
        };
    }
    
    
    public Gate getInitialGate() {
        setupMachine();
        //uig is setup in setupMachine()
        return uig;
    }
}