View Javadoc

1   package org.ximtec.igesture.tool.view.devicemanager.wizard;
2   
3   import java.awt.BorderLayout;
4   import java.awt.Color;
5   import java.awt.event.ActionEvent;
6   import java.awt.event.ActionListener;
7   import java.util.ArrayList;
8   import java.util.HashSet;
9   import java.util.Map;
10  import java.util.Set;
11  
12  import javax.swing.BorderFactory;
13  import javax.swing.ButtonGroup;
14  import javax.swing.DefaultListModel;
15  import javax.swing.JCheckBox;
16  import javax.swing.JComponent;
17  import javax.swing.JList;
18  import javax.swing.JPanel;
19  import javax.swing.JRadioButton;
20  import javax.swing.JScrollPane;
21  import javax.swing.ListSelectionModel;
22  import javax.swing.event.ListSelectionEvent;
23  import javax.swing.event.ListSelectionListener;
24  
25  import org.netbeans.spi.wizard.Summary;
26  import org.netbeans.spi.wizard.WizardController;
27  import org.netbeans.spi.wizard.WizardException;
28  import org.netbeans.spi.wizard.WizardPanelProvider;
29  import org.ximtec.igesture.Recogniser;
30  import org.ximtec.igesture.algorithm.Algorithm;
31  import org.ximtec.igesture.algorithm.AlgorithmException;
32  import org.ximtec.igesture.configuration.Configuration;
33  import org.ximtec.igesture.core.GestureSet;
34  import org.ximtec.igesture.io.AbstractGestureDevice;
35  import org.ximtec.igesture.io.DeviceDiscoveryService;
36  import org.ximtec.igesture.tool.view.devicemanager.DeviceManagerController;
37  
38  /**
39   * GUI and logic implementation of the wizard. It extends {@link org.netbeans.spi.wizard.WizardPanelProvider}.
40   * @author Bjorn Puype, bpuype@gmail.com
41   *
42   */
43  public class AddDeviceWizardProvider extends WizardPanelProvider {
44  
45  	private int selectedIndexDevice = 0;
46  	private DeviceManagerController manager;
47  	private final Map<String, DeviceDiscoveryService> discoveryMapping;
48  
49  	protected AddDeviceWizardProvider(DeviceManagerController manager)
50  	{
51  		super("Add Device ...", new String[]{"connection", "device", "user"/*, "recogniser"*/}, new String[]{
52  				"Choose a connection type",
53  				"Choose a device to connect",
54  				"Associate the device with a user"/*,
55  				"Associate a recogniser"*/
56  		});
57  		this.manager = manager;
58  		discoveryMapping = manager.getDiscoveryMapping();
59  	}
60  	
61  	@Override
62  	protected JComponent createPanel(final WizardController controller, String id, final Map settings) {
63  		
64  		if("connection".equals(id))// STEP 1
65  		{
66  			String problem = "You must select a connection type";
67  			controller.setProblem(problem);	
68  			return createRadioButtonsPanel(discoveryMapping.keySet(), "connection", problem, controller, settings);
69  		}
70  		else if("device".equals(id)) // STEP 2
71  		{
72  			controller.setProblem("Choose a device to connect");
73  			
74  			// get the discovery service based on the user's choice in the previous step
75  			String key = (String) settings.get("connection");
76  			DeviceDiscoveryService service = discoveryMapping.get(key);
77  			// discover devices
78  			Set<AbstractGestureDevice<?,?>> devices = service.discover();
79  			
80  			final Object[] discoveredDevices = devices.toArray(); 
81  			                           
82  			//add the devices to a list
83  			JPanel panel = new JPanel();
84  			panel.setLayout(new BorderLayout());
85  			JList list = new JList(discoveredDevices);
86  			list.setName("device");
87  			list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
88  			JScrollPane scroll = new JScrollPane(list);
89  			panel.add(scroll, BorderLayout.CENTER);
90  			
91  			//cleanup the service
92  			service.dispose();
93  			
94  			//listen to the selection of the user
95  			list.addListSelectionListener(new ListSelectionListener(){
96  
97  				@Override
98  				public void valueChanged(ListSelectionEvent e) {
99  					
100 					if(!e.getValueIsAdjusting())
101 					{
102 						//change to a different index
103 						if(!(e.getFirstIndex() == selectedIndexDevice))
104 							selectedIndexDevice = e.getFirstIndex();
105 						else
106 							selectedIndexDevice = e.getLastIndex();
107 						controller.setProblem(null);
108 						
109 						settings.put("device", discoveredDevices[selectedIndexDevice]);
110 						
111 					}
112 				}
113 				
114 			});
115 			
116 			return panel;
117 		}
118 		else //if("user".equals(id)) // STEP 3
119 		{
120 			controller.setProblem("Associate the device with a user");
121 			
122 			// get the users and add them to the list
123 			JPanel panel = new JPanel();
124 			panel.setLayout(new BorderLayout());
125 			final JList list = new JList(manager.getUsers().toArray());
126 			list.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
127 			JScrollPane scroll = new JScrollPane(list);
128 			panel.add(scroll, BorderLayout.CENTER);
129 			
130 			// listen to the selection of the user
131 			list.addListSelectionListener(new ListSelectionListener(){
132 
133 				@Override
134 				public void valueChanged(ListSelectionEvent e) {
135 					
136 					if(!e.getValueIsAdjusting())
137 					{
138 						controller.setProblem(null);
139 						Object result = list.getSelectedValue();
140 						settings.put("user", result);
141 					}
142 				}
143 				
144 			});
145 			
146 			return panel;
147 
148 		}/*
149 		else //if("choice".equals("id")) // STEP 4 (BRANCH)
150 		{
151 			String problem = "You must select a recogniser type";
152 			controller.setProblem(problem);
153 			Set<String> names = new HashSet<String>();
154 			names.add("Existing Recogniser");
155 			names.add("New Recogniser");
156 			return createRadioButtonsPanel(names, "recogniser", problem, controller, settings);
157 		}*/
158 	}
159 	
160 	private JPanel createRadioButtonsPanel(Set<String> names, final String key, final String problem, final WizardController controller, final Map settings)
161 	{	
162 		JPanel panel = new JPanel();
163 		ButtonGroup group = new ButtonGroup();
164 		final ArrayList<JRadioButton> radioButtons = new ArrayList<JRadioButton>();
165 		
166 		for(String name : names)
167 		{
168 			JRadioButton rdb = new JRadioButton();
169 			rdb.setText(name);
170 			rdb.setName(name);
171 			rdb.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
172 			group.add(rdb);
173 			panel.add(rdb);
174 			radioButtons.add(rdb);
175 		}
176 		
177 		//actionlistener for the radiobuttons
178 		ActionListener l = new ActionListener(){
179 			
180 			@Override
181 			public void actionPerformed(ActionEvent e) {
182 				
183 				boolean isSelected = false;
184 				JRadioButton choice = null;
185 				for(JRadioButton btn : radioButtons)
186 				{
187 					if(btn.isSelected())
188 					{
189 						isSelected = true;
190 						choice = btn;
191 						break;
192 					}
193 				}
194 				if(!isSelected)//no next if nothing selected
195 				{
196 					controller.setProblem(problem);
197 				}
198 				else
199 				{
200 					controller.setProblem(null);
201 					//save the users choice
202 					settings.put(key, choice.getText());
203 				}
204 			}
205 			
206 		};
207 		
208 		for(JRadioButton rdb : radioButtons)
209 		{
210 			rdb.addActionListener(l);
211 		}
212 		
213 		return panel;
214 
215 	}
216 	
217 
218 	@Override
219 	public Object finish(Map wizardData) throws WizardException {
220 		
221 		//summary
222 		String[] items = new String[wizardData.size()];
223 		items[0] = "Connection:\t" + wizardData.get("connection");
224 		items[1] = "Device:\t" + wizardData.get("device");
225 		items[2] = "User:\t" + wizardData.get("user");
226 		
227 		//result
228 		ArrayList<Object> result = new ArrayList<Object>();
229 		result.add(wizardData.get("device"));
230 		result.add(wizardData.get("user"));
231 		
232 		return Summary.create(items,result);
233 	}
234 }