View Javadoc

1   package org.ximtec.igesture.tool.view.devicemanager.action;
2   
3   import java.awt.Dimension;
4   import java.awt.GridBagConstraints;
5   import java.awt.GridBagLayout;
6   import java.awt.event.ActionEvent;
7   import java.awt.event.ActionListener;
8   
9   import javax.swing.JButton;
10  import javax.swing.JComponent;
11  import javax.swing.JDialog;
12  import javax.swing.JLabel;
13  import javax.swing.JOptionPane;
14  import javax.swing.JPanel;
15  import javax.swing.JTextField;
16  
17  import org.sigtec.graphix.widget.BasicAction;
18  import org.ximtec.igesture.io.IUser;
19  import org.ximtec.igesture.io.User;
20  import org.ximtec.igesture.tool.GestureConstants;
21  import org.ximtec.igesture.tool.service.GuiBundleService;
22  import org.ximtec.igesture.tool.util.Formatter;
23  import org.ximtec.igesture.tool.view.devicemanager.DeviceManagerController;
24  
25  /**
26   * Action to add a user. It extends {@link org.sigtec.graphix.widget.BasicAction}.
27   * @author Bjorn Puype, bpuype@gmail.com
28   *
29   */
30  public class AddUserAction extends BasicAction {
31  
32  	private DeviceManagerController controller;
33  	
34  	public AddUserAction(DeviceManagerController controller)
35  	{
36  		super(GestureConstants.ADD_USER, controller.getLocator().getService(
37  	            GuiBundleService.IDENTIFIER, GuiBundleService.class));
38  		this.controller = controller;	
39  	}
40  	
41  	@Override
42  	public void actionPerformed(ActionEvent e) {
43  		final JDialog dlg = new JDialog();
44  		dlg.setTitle("Add User");
45  //		dlg.setLayout(new GridBagLayout());
46  		JPanel contentPanel = new JPanel();
47  		contentPanel.setLayout(new GridBagLayout());
48  		
49  		// labels
50  		add(new JLabel("Name:"),contentPanel,0,0,1);
51  		add(new JLabel("Initials:"),contentPanel,0,1,1);
52  		
53  		// text fields
54  		final JTextField txtName = new JTextField();
55  		Formatter.formatTextField(txtName);
56  		add(txtName,contentPanel,1,0,2);
57  		final JTextField txtInitials = new JTextField();
58  		Formatter.formatTextField(txtInitials);
59  		add(txtInitials,contentPanel,1,1,2);
60  		
61  		// buttons
62  		JButton btnOK = new JButton("OK");
63  		btnOK.addActionListener(new ActionListener(){
64  
65  			@Override
66  			public void actionPerformed(ActionEvent e) {
67  				if(txtName.getText().isEmpty() || txtInitials.getText().isEmpty())
68  					JOptionPane.showMessageDialog(dlg, "Please enter a username and initials.", "Warning", JOptionPane.WARNING_MESSAGE);
69  				else
70  				{
71  					if(controller.areInitialsValid(txtInitials.getText()))
72  					{
73  						controller.addUser(new User(txtName.getText(),txtInitials.getText()));
74  						dlg.setVisible(false);
75  						dlg.dispose();
76  					}
77  					else
78  					{
79  						JOptionPane.showMessageDialog(dlg, "The initials must be unique!\n" +
80  								"Please enter other initials.", "Warning", JOptionPane.WARNING_MESSAGE);
81  					}
82  				}
83  			}
84  			
85  		});
86  		Formatter.formatButton(btnOK);
87  		add(btnOK,contentPanel,1,2,1);
88  		
89  		JButton btnCancel = new JButton("Cancel");
90  		btnCancel.addActionListener(new ActionListener(){
91  
92  			@Override
93  			public void actionPerformed(ActionEvent e) {
94  				dlg.setVisible(false);
95  				dlg.dispose();				
96  			}
97  			
98  		});
99  		Formatter.formatButton(btnCancel);
100 		add(btnCancel,contentPanel,2,2,1);
101 		
102 		dlg.setContentPane(contentPanel);
103 		dlg.setPreferredSize(new Dimension(400,120));
104 		dlg.setResizable(false);
105 		dlg.pack();
106 		dlg.setVisible(true);
107 		
108 	}
109 
110 	/**
111 	 * Add a component to a dialog with a GridBagLayout.
112 	 * @param component	The component to add.
113 	 * @param dlg		The dialog.
114 	 * @param gridx		The x position.
115 	 * @param gridy		The y position.
116 	 * @param gridwidth	The width of the component expressed in number of columns.
117 	 */
118 	private void add(JComponent component, JPanel dlg, int gridx, int gridy, int gridwidth)
119 	{
120 		GridBagConstraints c = new GridBagConstraints();
121 		c.fill = GridBagConstraints.BOTH;
122 		c.gridx = gridx;
123 		c.gridy = gridy;
124 		c.gridwidth = gridwidth;
125 		
126 		dlg.add(component,c);
127 	}
128 }