View Javadoc

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