1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package org.ximtec.igesture.tool.view.testbench.panel;
27
28 import java.awt.BorderLayout;
29 import java.awt.CardLayout;
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.FlowLayout;
33 import java.awt.event.ActionEvent;
34 import java.awt.event.ActionListener;
35 import java.util.HashMap;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Vector;
39 import java.util.concurrent.ConcurrentSkipListMap;
40
41 import javax.swing.JButton;
42 import javax.swing.JComboBox;
43 import javax.swing.JLabel;
44 import javax.swing.JList;
45 import javax.swing.JPanel;
46 import javax.swing.JScrollPane;
47 import javax.swing.JTextField;
48
49 import org.sigtec.graphix.widget.BasicButton;
50 import org.ximtec.igesture.algorithm.Algorithm;
51 import org.ximtec.igesture.algorithm.AlgorithmException;
52 import org.ximtec.igesture.algorithm.AlgorithmFactory;
53 import org.ximtec.igesture.configuration.Configuration;
54 import org.ximtec.igesture.core.GestureSet;
55 import org.ximtec.igesture.core.Result;
56 import org.ximtec.igesture.io.AbstractGestureDevice;
57 import org.ximtec.igesture.io.DeviceManagerListener;
58 import org.ximtec.igesture.io.GestureDevice;
59 import org.ximtec.igesture.io.GestureDevicePanel;
60 import org.ximtec.igesture.io.IDeviceManager;
61 import org.ximtec.igesture.tool.GestureConstants;
62 import org.ximtec.igesture.tool.binding.BindingFactory;
63 import org.ximtec.igesture.tool.binding.MapTextFieldBinding;
64 import org.ximtec.igesture.tool.core.Controller;
65 import org.ximtec.igesture.tool.gesturevisualisation.InputComponentPanel;
66 import org.ximtec.igesture.tool.gesturevisualisation.InputPanelFactory;
67 import org.ximtec.igesture.tool.service.DeviceManagerService;
68 import org.ximtec.igesture.tool.service.SwingMouseReaderService;
69 import org.ximtec.igesture.tool.util.ComponentFactory;
70 import org.ximtec.igesture.tool.util.TitleFactory;
71 import org.ximtec.igesture.tool.view.AbstractPanel;
72 import org.ximtec.igesture.tool.view.DeviceListPanel;
73 import org.ximtec.igesture.tool.view.DeviceListPanelListener;
74 import org.ximtec.igesture.tool.view.MainModel;
75 import org.ximtec.igesture.tool.view.admin.action.ClearGestureSampleAction;
76 import org.ximtec.igesture.tool.view.testbench.action.RecogniseAction;
77
78 import com.jgoodies.forms.builder.DefaultFormBuilder;
79 import com.jgoodies.forms.layout.FormLayout;
80
81
82
83
84
85
86
87 public class ConfigurationPanel extends AbstractPanel implements DeviceListPanelListener, DeviceManagerListener {
88
89 private static final int INPUTAREA_SIZE = 200;
90
91 public static final String IDENTIFIER = "testbench";
92
93 private Configuration configuration;
94 private Algorithm algorithm = null;
95
96 private GestureDevice<?, ?> gestureDevice;
97 private GestureDevice<?, ?> currentDevice;
98
99 private JScrollPane resultList;
100 private RecogniseAction recogniseAction;
101
102 private DeviceListPanel devicePanel;
103 private JPanel cardPanel;
104
105 private Map<String, InputComponentPanel> panelMapping;
106
107 private Controller controller;
108
109 public ConfigurationPanel(Controller controller, Configuration configuration) {
110 super(controller);
111 this.configuration = configuration;
112
113 try {
114 algorithm = AlgorithmFactory.createAlgorithm(configuration);
115 } catch (AlgorithmException e) {
116 e.printStackTrace();
117 }
118
119 this.panelMapping = new HashMap<String, InputComponentPanel>();
120
121 DeviceManagerService manager = controller.getLocator().getService(DeviceManagerService.IDENTIFIER, DeviceManagerService.class);
122 manager.addDeviceManagerListener(this);
123
124 this.controller = controller;
125
126 init(manager);
127
128 }
129
130 private void init(IDeviceManager manager) {
131 setTitle(TitleFactory.createDynamicTitle(configuration, Configuration.PROPERTY_NAME));
132 setContent(createParameterPanel());
133 setBottom(createWorkspace(manager));
134 }
135
136
137
138
139 private void initGestureDevice() {
140 gestureDevice = getController().getLocator().getService(SwingMouseReaderService.IDENTIFIER, GestureDevice.class);
141 }
142
143 private JPanel createWorkspace(IDeviceManager manager) {
144 JPanel basePanel = new JPanel();
145
146
147 basePanel.setLayout(new BorderLayout());
148
149 initGestureDevice();
150
151 recogniseAction = new RecogniseAction(getController(), configuration);
152 recogniseAction.setEnabled(false);
153
154 cardPanel = new JPanel();
155 cardPanel.setLayout(new CardLayout());
156 cardPanel.setSize(new Dimension(INPUTAREA_SIZE, INPUTAREA_SIZE));
157
158
159 currentDevice = null;
160
161 basePanel.add(cardPanel, BorderLayout.CENTER);
162
163 devicePanel = new DeviceListPanel();
164 for(AbstractGestureDevice<?,?> device : manager.getDevices())
165 addDevice(device);
166 devicePanel.addDevicePanelListener(this);
167 basePanel.add(devicePanel,BorderLayout.WEST);
168
169
170 resultList = new JScrollPane(new JList());
171 resultList.setPreferredSize(new Dimension(200, 50));
172
173 basePanel.add(resultList, BorderLayout.SOUTH);
174
175 basePanel.setOpaque(true);
176 basePanel.setBackground(Color.WHITE);
177
178 return basePanel;
179 }
180
181 @Override
182 public void refreshUILogic() {
183 super.refreshUILogic();
184 gestureDevice.clear();
185 }
186
187 private JPanel createParameterPanel() {
188
189 FormLayout layout = new FormLayout(
190 "100dlu, 4dlu, 100dlu",
191 "pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref, 4dlu, pref");
192
193 DefaultFormBuilder builder = new DefaultFormBuilder(layout);
194 builder.setDefaultDialogBorder();
195
196 String algorithmName = configuration.getAlgorithms().get(0);
197
198 builder.append(getComponentFactory().createLabel(GestureConstants.CONFIGURATION_PANEL_PARAMETERS));
199 builder.nextLine(2);
200
201 builder.append(getComponentFactory().createLabel(GestureConstants.CONFIGURATION_PANEL_NAME));
202 JTextField textField = new JTextField();
203 BindingFactory.createInstance(textField, configuration, Configuration.PROPERTY_NAME);
204 builder.append(textField);
205 builder.nextLine(2);
206
207 Map<String, String> parameter = configuration.getParameters(algorithmName);
208 ConcurrentSkipListMap<String, String> map = new ConcurrentSkipListMap<String,String>();
209 map.putAll(parameter);
210 for (String parameterName : map.keySet()) {
211 builder.append(new JLabel(parameterName));
212 JTextField paramTextField = new JTextField();
213 new MapTextFieldBinding(paramTextField, configuration, parameterName, algorithmName);
214 builder.append(paramTextField);
215 builder.nextLine(2);
216 }
217
218 JPanel panel = builder.getPanel();
219 panel.setOpaque(true);
220 panel.setBackground(Color.WHITE);
221 return panel;
222 }
223
224 public void setResultList(List<Result> classes) {
225 resultList.setViewportView(new JList(new Vector<Result>(classes)));
226 }
227
228 private InputComponentPanel createInputPanel(GestureDevice<?,?> device)
229 {
230 InputComponentPanel inputComponentPanel = new InputComponentPanel();
231 inputComponentPanel.setLayout(new FlowLayout());
232 GestureDevicePanel inputPanelInstance = InputPanelFactory.createPanel(device);
233 inputComponentPanel.setGestureDevicePanel(inputPanelInstance);
234 inputComponentPanel.add(inputPanelInstance);
235
236
237 JPanel buttonPanel = ComponentFactory.createBorderLayoutPanel();
238
239 final JComboBox comboBox = new JComboBox(getController().getLocator().getService(MainModel.IDENTIFIER, MainModel.class)
240 .getGestureSets().toArray());
241
242 comboBox.addActionListener(new ActionListener() {
243
244 @Override
245 public void actionPerformed(ActionEvent arg0) {
246 configuration.removeAllGestureSets();
247 GestureSet gestureSet = (GestureSet) comboBox.getSelectedItem();
248 configuration.addGestureSet(gestureSet);
249 recogniseAction.setEnabled(true);
250 }
251 });
252
253 comboBox.setSelectedIndex(0);
254
255 JButton clearButton = new BasicButton(new ClearGestureSampleAction(getController(), gestureDevice));
256
257 JButton recogniseButton = new BasicButton(recogniseAction);
258
259
260 buttonPanel.add(clearButton, BorderLayout.NORTH);
261 buttonPanel.add(recogniseButton, BorderLayout.CENTER);
262 buttonPanel.add(comboBox, BorderLayout.SOUTH);
263
264 inputComponentPanel.add(buttonPanel);
265
266 return inputComponentPanel;
267 }
268
269 private void addDevice(AbstractGestureDevice<?,?> device)
270 {
271
272 if(algorithm != null && algorithm.getType() == device.getDeviceType())
273 {
274
275 InputComponentPanel panel = createInputPanel(device);
276 panelMapping.put(device.toString(), panel);
277 cardPanel.add(panel,device.toString());
278
279 devicePanel.addDevice(device);
280 }
281 }
282
283 private void removeDevice(AbstractGestureDevice<?,?> device)
284 {
285 if(algorithm != null && algorithm.getType() == device.getDeviceType() )
286 {
287 devicePanel.removeDevice(device);
288
289 InputComponentPanel panel = panelMapping.get(device.toString());
290 cardPanel.remove(panel);
291 }
292 }
293
294
295
296
297 @Override
298 public void updateDeviceListPanelListener(AbstractGestureDevice<?, ?> device) {
299
300
301 if(currentDevice != null)
302 currentDevice.removeGestureHandler(panelMapping.get(currentDevice.toString()).getGestureDevicePanel());
303
304 ((CardLayout)cardPanel.getLayout()).show(cardPanel, device.toString());
305 currentDevice = device;
306
307 controller.getLocator().setSharedDevice(IDENTIFIER, currentDevice);
308
309
310 device.addGestureHandler(panelMapping.get(device.toString()).getGestureDevicePanel());
311
312 repaint();
313 }
314
315
316
317
318 @Override
319 public void updateDeviceManagerListener(int operation, AbstractGestureDevice<?, ?> device) {
320 if(operation == DeviceManagerListener.ADD)
321 {
322 addDevice(device);
323 }
324 else if(operation == DeviceManagerListener.REMOVE)
325 {
326 removeDevice(device);
327 }
328
329 }
330 }