View Javadoc

1   package org.ximtec.igesture.tool.view.devicemanager;
2   
3   import java.awt.Color;
4   import java.awt.Dimension;
5   import java.awt.GridBagConstraints;
6   import java.awt.GridBagLayout;
7   import java.awt.event.ActionEvent;
8   import java.awt.event.ActionListener;
9   import java.util.Collection;
10  import java.util.logging.Level;
11  import java.util.logging.Logger;
12  
13  import javax.swing.BorderFactory;
14  import javax.swing.JButton;
15  import javax.swing.JScrollPane;
16  import javax.swing.JTable;
17  
18  import org.sigtec.graphix.GridBagLayouter;
19  import org.sigtec.graphix.GuiBundle;
20  import org.sigtec.graphix.widget.BasicAction;
21  import org.sigtec.graphix.widget.BasicButton;
22  import org.sigtec.graphix.widget.BasicDialog;
23  import org.ximtec.igesture.io.DeviceUserAssociation;
24  import org.ximtec.igesture.io.IDeviceManagerView;
25  import org.ximtec.igesture.io.IUser;
26  import org.ximtec.igesture.tool.GestureConstants;
27  import org.ximtec.igesture.tool.util.Formatter;
28  import org.ximtec.igesture.tool.view.devicemanager.action.AddDeviceAction;
29  import org.ximtec.igesture.tool.view.devicemanager.action.AddUserAction;
30  import org.ximtec.igesture.tool.view.devicemanager.action.AssociateUserAction;
31  import org.ximtec.igesture.tool.view.devicemanager.action.DisconnectDeviceAction;
32  import org.ximtec.igesture.tool.view.devicemanager.action.LoadDeviceConfigurationAction;
33  import org.ximtec.igesture.tool.view.devicemanager.action.ReconnectDeviceAction;
34  import org.ximtec.igesture.tool.view.devicemanager.action.RemoveDeviceAction;
35  import org.ximtec.igesture.tool.view.devicemanager.action.RemoveUserAction;
36  import org.ximtec.igesture.tool.view.devicemanager.action.SaveDeviceConfigurationAction;
37  
38  /**
39   * This class is the view for the Device Manager. It implements the {@link org.ximtec.igesture.io.devicemanage.IDeviceManagerView} interface and extends {@link import org.sigtec.graphix.widget.BasicDialog;}.
40   * @author Bjorn Puype, bpuype@gmail.com
41   *
42   */
43  public class DeviceManagerView extends BasicDialog implements IDeviceManagerView
44  {
45  
46  	private static final long serialVersionUID = 1L;
47  	
48  	//user table
49  	public static final int COL_USER_NAME = 0;
50  	public static final int COL_USER_INITIALS = 1;
51  	//device table
52  	public static final int COL_DEVICE_NAME = 0;
53  	public static final int COL_DEVICE_ID = 1;
54  	public static final int COL_DEVICE_TYPE = 2;
55  	public static final int COL_DEVICE_USER = 3;
56  	public static final int COL_DEVICE_CONNECTION = 4;
57  	public static final int COL_DEVICE_CONNECTED = 5;
58  	
59  	private static final Logger LOGGER = Logger.getLogger(DeviceManagerView.class.getName());
60  	
61  	private DeviceManagerController deviceManagerController;
62  	private static final int TABLE_HEIGHT = 200;
63  	private static final int BUTTON_WIDTH = 200;
64  	
65  	private BasicTable<IUser> userTable;
66  	private BasicTable<DeviceUserAssociation> deviceTable;
67  
68  	/**
69  	 * Constructs a new Device Manager.
70  	 * 
71  	 * @param controller
72  	 * 			  parent controller
73  	 * @param key
74  	 *            the key to be used for the lookup of information in the GUI
75  	 *            bundle.
76  	 * @param guiBundle
77  	 *            the GUI bundle to be used to create the about dialog.
78  	 */
79  	public DeviceManagerView(DeviceManagerController controller, String key, GuiBundle guiBundle) {
80  		super(key,guiBundle);
81  		deviceManagerController = controller;
82  		initUI(key, guiBundle);
83  	}
84  	
85  	/**
86  	 * Initialize UI
87  	 * @param key
88  	 * @param guiBundle
89  	 */
90  	private void initUI(String key, GuiBundle guiBundle) {
91  		setLayout(new GridBagLayout());
92  
93  		/********************************* User Panel *******************************/
94  		/////////////////////// user actions
95  		
96  		AddUserAction addUserAction = new AddUserAction(deviceManagerController);
97  		RemoveUserAction removeUserAction = new RemoveUserAction(deviceManagerController,this);
98  		
99  		/////////////////////// user table
100 		String[] headers = new String[]{GestureConstants.USER_NAME,GestureConstants.USER_INITIALS};
101 		userTable = new BasicTable<IUser>(headers);
102 		userTable.addListSelectionListener(removeUserAction);
103 		JScrollPane userPane = createScrollableTable(userTable, guiBundle.getWidth(key), TABLE_HEIGHT);
104 		userPane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY),"Users:"));
105 		
106 		/////////////////////// add components
107 		GridBagLayouter.addComponent(this, userPane, 
108 				0, 1, 5, 1, 0, 0, 0, 0, 0, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
109 		GridBagLayouter.addComponent(this, createButton(addUserAction,BUTTON_WIDTH), 
110 				0, 2, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
111 		GridBagLayouter.addComponent(this, createButton(removeUserAction,BUTTON_WIDTH), 
112 				1, 2, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
113 		
114 		
115 		/********************************* Device Panel *******************************/
116 		/////////////////////// device actions
117 		AddDeviceAction addDeviceAction = new AddDeviceAction(deviceManagerController);
118 		addDeviceAction.setEnabled(deviceManagerController.getEnableAddDevicesAction());
119 		AssociateUserAction associateUserAction = new AssociateUserAction(deviceManagerController,this);
120 		associateUserAction.setEnabled(false);
121 		RemoveDeviceAction removeDeviceAction = new RemoveDeviceAction(deviceManagerController,this);
122 		removeDeviceAction.setEnabled(false);		
123 		ReconnectDeviceAction reconnectDeviceAction = new ReconnectDeviceAction(deviceManagerController,this);
124 		reconnectDeviceAction.setEnabled(false);
125 		DisconnectDeviceAction disconnectDeviceAction = new DisconnectDeviceAction(deviceManagerController,this);
126 		disconnectDeviceAction.setEnabled(false);
127 		
128 		reconnectDeviceAction.addAction(disconnectDeviceAction);
129 		disconnectDeviceAction.addAction(reconnectDeviceAction);
130 		
131 		/////////////////////// device table
132 		headers = new String[]{GestureConstants.DEVICE_NAME, GestureConstants.DEVICE_ID, GestureConstants.DEVICE_TYPE,
133 									GestureConstants.DEVICE_USER, GestureConstants.DEVICE_CONNECTION, GestureConstants.DEVICE_CONNECTED};
134 		
135 		deviceTable = new BasicTable<DeviceUserAssociation>(headers);
136 		deviceTable.addListSelectionListener(associateUserAction);
137 		deviceTable.addListSelectionListener(removeDeviceAction);
138 		deviceTable.addListSelectionListener(reconnectDeviceAction);
139 		deviceTable.addListSelectionListener(disconnectDeviceAction);
140 		
141 		JScrollPane devicePane = createScrollableTable(deviceTable, guiBundle.getWidth(key), TABLE_HEIGHT);
142 		devicePane.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY),"Devices:"));
143 		
144 		/////////////////////// add components
145 		GridBagLayouter.addComponent(this, devicePane, 
146 				0, 4, 5, 1, 0, 0, 0, 0, 0, 0.5, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
147 		GridBagLayouter.addComponent(this, createButton(addDeviceAction,BUTTON_WIDTH), 
148 				0, 5, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
149 		GridBagLayouter.addComponent(this, createButton(associateUserAction,BUTTON_WIDTH), 
150 				1, 5, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
151 		GridBagLayouter.addComponent(this, createButton(removeDeviceAction,BUTTON_WIDTH), 
152 				2, 5, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
153 		GridBagLayouter.addComponent(this, createButton(reconnectDeviceAction,BUTTON_WIDTH), 
154 				3, 5, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
155 		GridBagLayouter.addComponent(this, createButton(disconnectDeviceAction,BUTTON_WIDTH), 
156 				4, 5, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
157 		
158 		/********************************* Bottom Panel *******************************/
159 		/////////////////////// actions
160 		SaveDeviceConfigurationAction saveDeviceConfigurationAction = new SaveDeviceConfigurationAction(deviceManagerController);
161 		LoadDeviceConfigurationAction loadDeviceConfigurationAction = new LoadDeviceConfigurationAction(deviceManagerController);
162 		
163 		GridBagLayouter.addComponent(this, createButton(loadDeviceConfigurationAction,BUTTON_WIDTH), 
164 				0, 6, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
165 		GridBagLayouter.addComponent(this, createButton(saveDeviceConfigurationAction,BUTTON_WIDTH), 
166 				1, 6, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
167 		
168 		//close button
169 		JButton closeButton = new JButton("Close");
170 		closeButton.addActionListener(new ActionListener(){
171 
172 			@Override
173 			public void actionPerformed(ActionEvent e) {
174 				closeDialog();
175 			}			
176 		});
177 		GridBagLayouter.addComponent(this, closeButton, 
178 				4, 6, 1, 1, 0, 0, 0, 0, 0.5, 0, GridBagConstraints.CENTER, GridBagConstraints.BOTH);
179 		
180 		pack();
181 		repaint();
182 	}
183 
184 	/**
185 	 * Initialises the dialogue.
186 	 * 
187 	 * @param key
188 	 *            the key to be used for the lookup of dialogue information in
189 	 *            the GUI bundle.
190 	 * @param guiBundle
191 	 *            the GUI bundle to be used to create the dialogue.
192 	 */
193 	protected void init(String key, GuiBundle guiBundle)
194 	{
195 		LOGGER.log(Level.FINER, "Init Device Manager");
196 		super.init(key, guiBundle);	
197 	}
198 	
199 	/**
200 	 * Create a scrollable table.
201 	 * @param table			The table to add.
202 	 * @param tableWidth	The desired width.
203 	 * @param tableHeight	The desired height.
204 	 * @return	A scrollable table.
205 	 */
206 	private JScrollPane createScrollableTable(JTable table, int tableWidth, int tableHeight)
207 	{
208 		JScrollPane pane = new JScrollPane(table);
209 		table.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
210 		table.setFillsViewportHeight(true);
211 		pane.setPreferredSize(new Dimension(tableWidth,tableHeight));
212 		return pane;
213 	}
214 	
215 	/**
216 	 * Create a BasicButton.
217 	 * @param action	The action that is coupled to the button.
218 	 * @param width		The width of the button
219 	 * @return
220 	 */
221 	private BasicButton createButton(BasicAction action, int width)
222 	{
223 		BasicButton btn = new BasicButton(action);
224 		Formatter.formatButton(btn);
225 //		btn.setHorizontalAlignment(SwingConstants.CENTER);
226 		return btn;		
227 	}
228 	
229 	private void closeDialog() {
230 		setVisible(false);
231 //		dispose();
232 	}
233 	
234 	@Override
235 	public void addUser(IUser user)
236 	{
237 		userTable.addItem(user);
238 	}
239 	
240 	@Override
241 	public void addDevice(DeviceUserAssociation association)
242 	{
243 		deviceTable.addItem(association);
244 	}
245 	
246 	@Override
247 	public void removeDevice()
248 	{
249 		deviceTable.removeItem();
250 	}
251 	
252 	@Override
253 	public void removeUser()
254 	{
255 		userTable.removeItem();
256 	}
257 	
258 	@Override
259 	public Collection<DeviceUserAssociation> getDevices()
260 	{
261 		return deviceTable.getItems();
262 	}
263 	
264 	/**
265 	 * Update a device-user association.
266 	 * @param value		The new value.
267 	 * @param column	The field that should be updated.
268 	 * @param object	The object that should be updated, when null only the table model is updated.
269 	 */
270 	@Override
271 	public void updateDevice(Object value, int column, DeviceUserAssociation object)
272 	{
273 		deviceTable.updateItem(value, column, object);
274 	}
275 	
276 	@Override
277 	public DeviceUserAssociation getSelectedDevice()
278 	{
279 		return deviceTable.getSelectedItem();
280 	}
281 	
282 	@Override
283 	public IUser getSelectedUser()
284 	{
285 		return userTable.getSelectedItem();
286 	}
287 	
288 	@Override
289 	public void clear()
290 	{
291 		userTable.removeAllItems();
292 		deviceTable.removeAllItems();
293 	}
294 }