Make a user interface for maintaining two lists, one list with groups, the other with persons. Every person can exist in several groups (for example, “member of the soccer team” and “member of the basketball team”).
The most important part of the user interface is two lists:
The user interface will also contain two pushbuttons:
The user interface will consist of only one window. New groups and new persons will be input by using methods in JOptionPane.
It is sufficient that the data be stored in the list's “models”.
We will need to store user selection by group. When user changes the selection in user selection list (List B) we will store the selection here. At the initial iteration, we use an ArrayList to store groups and we will create a UserGroup class to store user selection. Because the actual user and group data will be stored in lists' model, we will store only user selection for particular group. The following services will be available to client from UserGroup:
The user inteface will have a one main window (JFrame) in a separate class UserGroupForm which will contain two specialised panels - one for groups, GroupPanel and one for users, UserPanel. These two panels will encapsulate user and group management as well as user and group selection. Panels will not interact directly with each other and with group data, but will send messages to parent window to add group or user and to notify the window that group or user selection has changed.
The window will be able to send a message to the user panel to change the user selection.
/** * Simple user selection management. * @author Ivan Georgiev */ import java.util.Arrays; class UserGroup { private int[] theUserIndices = new int[0]; public int[] getUserIndices() { return Arrays.copyOf(theUserIndices, theUserIndices.length); } public void setUserIndicies(int[] newIndices) { theUserIndices = Arrays.copyOf(newIndices, newIndices.length); } public int numUsers() { return theUserIndices.length; } public int userIndex(int user) { return theUserIndices[user]; } }
The main window code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*; import java.util.ArrayList; class UserGroupsForm extends JFrame { private DefaultListModel groups = new DefaultListModel(); private DefaultListModel users = new DefaultListModel(); private ArrayList userGroups = new ArrayList(); private GroupPanel groupPanel; private UserPanel userPanel; public UserGroupsForm() { setTitle("Simple User Group Management"); Container content = getContentPane(); content.setLayout(new GridLayout(1,2)); groups.addElement("--- All ---"); groupPanel = new GroupPanel(); content.add(groupPanel); userPanel = new UserPanel(); content.add(userPanel); } public void addUser(String name) { users.addElement(name); } public void addGroup(String name) { groups.addElement(name); userGroups.add(new UserGroup()); } public void userSelectionChanged() { int groupIndex = groupPanel.getSelectedGroupIndex() - 1; if (groupIndex < 0) return; UserGroup group = (UserGroup) userGroups.get(groupIndex); group.setUserIndicies(userPanel.getUserSelection()); } public void groupSelectionChanged() { int groupIndex = groupPanel.getSelectedGroupIndex(); if (groupIndex < 0) return; groupIndex--; int[] userSelection; if (groupIndex<0) { int size = 0; for (int i=0; i<userGroups.size(); i++) { size += ((UserGroup) userGroups.get(i)).numUsers(); } userSelection = new int[size]; int index = 0; for (int i=0; i<userGroups.size(); i++) { UserGroup group = (UserGroup) userGroups.get(i); for (int j=0; j<group.numUsers(); j++) { userSelection[index] = group.userIndex(j); index++; } } } else { UserGroup group = (UserGroup) userGroups.get(groupIndex); userSelection = group.getUserIndices(); } userPanel.setUserSelection(userSelection); } private class GroupPanel extends JPanel { JList groupsList = new JList(groups); public GroupPanel() { setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); add(groupsList, BorderLayout.CENTER); groupsList.setSelectedIndex(0); groupsList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { if ( !groupsList.getValueIsAdjusting()) groupSelectionChanged(); } }); JButton addButton = new JButton("Add new group..."); buttonPanel.add(addButton); addButton.setMnemonic('G'); add(addButton, BorderLayout.SOUTH); addButton.addActionListener(new AddButtonListener()); } public int getSelectedGroupIndex() { return groupsList.getSelectedIndex(); } private class AddButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String userInput = JOptionPane.showInputDialog(UserGroupsForm.this, "Enter a group name:"); if (userInput != null && (userInput = userInput.trim()).length() > 0) { addGroup(userInput); } } } } private class UserPanel extends JPanel { private JList usersList = new JList(users); public UserPanel() { setLayout(new BorderLayout()); JPanel buttonPanel = new JPanel(); usersList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION); add(usersList, BorderLayout.CENTER); usersList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent event) { if ( !usersList.getValueIsAdjusting()) userSelectionChanged(); } }); JButton addButton = new JButton("Add new user..."); buttonPanel.add(addButton); addButton.setMnemonic('U'); add(addButton, BorderLayout.SOUTH); addButton.addActionListener(new AddButtonListener()); } public int[] getUserSelection() { return usersList.getSelectedIndices(); } public void setUserSelection(int[] userIndicies) { usersList.setSelectedIndices(userIndicies); } private class AddButtonListener implements ActionListener { public void actionPerformed(ActionEvent event) { String userInput = JOptionPane.showInputDialog(UserGroupsForm.this, "Enter a user name:"); if (userInput != null && (userInput = userInput.trim()).length() > 0) { addUser(userInput); } } } } }
Although the code presented does the job, it is not too flexible. The user and group entry and selection is not easy to use outside the main window. We can change the design of the application so that it is more flexible.
We can also think about data persistency.
Here is a simple Java application that creates and shows a user/group management form.
<code java> /** * @(#)Ch14_4_UserGroups.java * * Ch14_4_UserGroups application * * @author Ivan Georgiev * @version 1.00 2008/9/11 */
import javax.swing.JFrame; public class Ch14_4_UserGroups {
public static void main(String[] args) {
UserGroupsForm mainWindow = new UserGroupsForm();
mainWindow.setVisible(true);
mainWindow.setSize(450,300);
mainWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
} </java>
You can download and test the sample user group management application, packaged as a JAR file.