View Javadoc

1   package org.ximtec.igesture.tool.view.devicemanager.action;
2   
3   import java.awt.GridBagConstraints;
4   import java.awt.GridBagLayout;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   import java.util.HashMap;
8   import java.util.Map;
9   import java.util.Set;
10  
11  import javax.swing.JButton;
12  import javax.swing.JComponent;
13  import javax.swing.JDialog;
14  import javax.swing.JLabel;
15  import javax.swing.JList;
16  import javax.swing.JOptionPane;
17  import javax.swing.event.ListSelectionEvent;
18  import javax.swing.event.ListSelectionListener;
19  
20  import org.sigtec.graphix.widget.BasicAction;
21  import org.ximtec.igesture.io.DeviceUserAssociation;
22  import org.ximtec.igesture.io.IUser;
23  import org.ximtec.igesture.tool.GestureConstants;
24  import org.ximtec.igesture.tool.service.GuiBundleService;
25  import org.ximtec.igesture.tool.view.devicemanager.DeviceManagerController;
26  import org.ximtec.igesture.tool.view.devicemanager.DeviceManagerView;
27  
28  /**
29   * Action to associate a user with a device. It extends {@link org.sigtec.graphix.widget.BasicAction} and implements the {@link javax.swing.event.ListSelectionListener} interface.
30   * @author Bjorn Puype, bpuype@gmail.com
31   *
32   */
33  public class AssociateUserAction extends BasicAction implements ListSelectionListener{
34  
35  	private static final long serialVersionUID = 1L;
36  	private DeviceManagerController controller;
37  	private DeviceManagerView view;
38  	
39  	
40  	public AssociateUserAction(DeviceManagerController controller, DeviceManagerView view)
41  	{
42  		this(controller,view,false);
43  	}
44  	
45  	public AssociateUserAction(DeviceManagerController controller, DeviceManagerView view, boolean enabled)
46  	{
47  		super(GestureConstants.ASSOCIATE_USER, controller.getLocator().getService(
48  	            GuiBundleService.IDENTIFIER, GuiBundleService.class));
49  		this.controller = controller;
50  		this.view = view;
51  		setEnabled(enabled);
52  	}
53  	
54  	@Override
55  	public void actionPerformed(ActionEvent e) {
56  		//creat a dialog
57  		final JDialog dlg = new JDialog();
58  		dlg.setTitle("Associate User");
59  		dlg.setLayout(new GridBagLayout());
60  
61  		// label
62  		add(new JLabel("Select a user:"),dlg,0,0,1);
63  		
64  		// user list
65  		Set<IUser> users = controller.getUsers();
66  		final Map<String, IUser> map = new HashMap<String, IUser>();
67  		for(IUser user : users)
68  		{
69  			map.put(user.toString(), user);
70  		}
71  		
72  		//add users to the list.
73  		final JList list = new JList(map.keySet().toArray());
74  		list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
75  		add(list, dlg, 0, 1, 2);
76  		
77  		// buttons
78  		JButton btnOK = new JButton("OK");
79  		final DeviceUserAssociation device = view.getSelectedDevice();
80  		btnOK.addActionListener(new ActionListener(){
81  
82  			@Override
83  			public void actionPerformed(ActionEvent e) {
84  					if(list.getSelectionModel().isSelectionEmpty())
85  						JOptionPane.showMessageDialog(dlg, "Please select a user.", "Warning", JOptionPane.WARNING_MESSAGE);
86  					else
87  					{
88  						IUser user = map.get(list.getSelectedValues()[0]);
89  						controller.associateUser(device.getDeviceItem(),user);
90  						dlg.setVisible(false);
91  						dlg.dispose();
92  					}
93  			}
94  			
95  		});
96  		add(btnOK,dlg,0,2,1);
97  		
98  		JButton btnCancel = new JButton("Cancel");
99  		btnCancel.addActionListener(new ActionListener(){
100 
101 			@Override
102 			public void actionPerformed(ActionEvent e) {
103 				dlg.setVisible(false);
104 				dlg.dispose();				
105 			}
106 			
107 		});
108 		add(btnCancel,dlg,1,2,1);
109 		
110 		dlg.pack();
111 		dlg.setVisible(true);		
112 		
113 	}
114 	
115 	/**
116 	 * Add a component to a dialog with a GridBagLayout.
117 	 * @param component	The component to add.
118 	 * @param dlg		The dialog.
119 	 * @param gridx		The x position.
120 	 * @param gridy		The y position.
121 	 * @param gridwidth	The width of the component expressed in number of columns.
122 	 */
123 	private void add(JComponent component, JDialog dlg, int gridx, int gridy, int gridwidth)
124 	{
125 		GridBagConstraints c = new GridBagConstraints();
126 		c.fill = GridBagConstraints.HORIZONTAL;
127 		c.gridx = gridx;
128 		c.gridy = gridy;
129 		c.gridwidth = gridwidth;
130 		
131 		dlg.add(component,c);
132 	}
133 
134 	@Override
135 	public void valueChanged(ListSelectionEvent e) {
136 		setEnabled(true);
137 		//enable the action when something was selected.
138 	}
139 
140 }