View Javadoc

1   package org.ximtec.igesture.tool.view.devicemanager.action;
2   
3   import java.awt.event.ActionEvent;
4   import java.util.ArrayList;
5   
6   import javax.swing.event.ListSelectionEvent;
7   import javax.swing.event.ListSelectionListener;
8   
9   import org.sigtec.graphix.widget.BasicAction;
10  import org.ximtec.igesture.io.DeviceUserAssociation;
11  import org.ximtec.igesture.tool.GestureConstants;
12  import org.ximtec.igesture.tool.service.GuiBundleService;
13  import org.ximtec.igesture.tool.view.devicemanager.DeviceManagerController;
14  import org.ximtec.igesture.tool.view.devicemanager.DeviceManagerView;
15  
16  /**
17   * Action to reconnect a device. It extends {@link org.sigtec.graphix.widget.BasicAction} and implements the {@link javax.swing.event.ListSelectionListener} interface.
18   * @author Bjorn Puype, bpuype@gmail.com
19   *
20   */
21  public class ReconnectDeviceAction extends BasicAction implements ListSelectionListener{
22  
23  	private static final long serialVersionUID = 1L;
24  	private DeviceManagerView view;
25  	private ArrayList<BasicAction> actions;
26  	
27  	public ReconnectDeviceAction(DeviceManagerController controller, DeviceManagerView view)
28  	{
29  		this(controller,view,false);
30  	}
31  	
32  	public ReconnectDeviceAction(DeviceManagerController controller, DeviceManagerView view, boolean enabled)
33  	{
34  		super(GestureConstants.RECONNECT_DEVICE, controller.getLocator().getService(
35  	            GuiBundleService.IDENTIFIER, GuiBundleService.class));
36  		this.view = view;
37  		setEnabled(enabled);
38  		
39  		actions = new ArrayList<BasicAction>();
40  	}
41  	
42  	public void addAction(BasicAction action)
43  	{
44  		actions.add(action);
45  	}
46  	
47  	@Override
48  	public void actionPerformed(ActionEvent e) {
49  		//connect device
50  		view.getSelectedDevice().connect();
51  		if(view.getSelectedDevice().isConnected())
52  		{
53  			//update model
54  			view.updateDevice(true, DeviceManagerView.COL_DEVICE_CONNECTED, null);
55  			setEnabled(false);
56  			for(BasicAction action : actions)
57  				action.setEnabled(true);
58  		}
59  	}
60  	
61  	@Override
62  	public void valueChanged(ListSelectionEvent e) {
63  		
64  			DeviceUserAssociation device = view.getSelectedDevice();
65  			if(device != null && !device.isConnected())
66  			{
67  				//enable this action when the device is not connected
68  				setEnabled(true);
69  			}
70  			else
71  			{
72  				//otherwise disable it.
73  				setEnabled(false);
74  			}
75  	}
76  
77  }