1
2
3
4 package org.ximtec.igesture.tool.view;
5
6 import java.awt.BorderLayout;
7 import java.awt.Color;
8 import java.util.ArrayList;
9 import java.util.List;
10
11 import javax.swing.BorderFactory;
12 import javax.swing.DefaultListModel;
13 import javax.swing.DefaultListSelectionModel;
14 import javax.swing.JLabel;
15 import javax.swing.JList;
16 import javax.swing.JPanel;
17 import javax.swing.JScrollPane;
18 import javax.swing.ListSelectionModel;
19 import javax.swing.event.ListSelectionEvent;
20 import javax.swing.event.ListSelectionListener;
21
22 import org.sigtec.ink.Note;
23 import org.sigtec.ink.Point;
24 import org.ximtec.igesture.core.Gesture;
25 import org.ximtec.igesture.io.AbstractGestureDevice;
26
27
28
29
30
31 public class DeviceListPanel extends JPanel implements ListSelectionListener{
32
33 private List<DeviceListPanelListener> listeners;
34
35 private JList list = null;
36
37 public DeviceListPanel()
38 {
39
40 listeners = new ArrayList<DeviceListPanelListener>();
41
42
43 setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
44 setLayout(new BorderLayout());
45
46
47 add(new JLabel("Choose a capture device:"),BorderLayout.NORTH);
48
49
50 list = new JList();
51 DefaultListModel model = new DefaultListModel();
52 list.setModel(model);
53 list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
54
55 list.addListSelectionListener(this);
56
57 list.setBorder(BorderFactory.createLineBorder(Color.RED));
58
59 JScrollPane scrollpane = new JScrollPane(list);
60 add(scrollpane,BorderLayout.CENTER);
61 }
62
63 public void addDevice(AbstractGestureDevice<?,?> device)
64 {
65
66 int index = list.getSelectedIndex();
67 Object d = null;
68 if(index > 1)
69 d = getCastedModel().getElementAt(index);
70
71 getCastedModel().addElement(device);
72
73 if(index > -1)
74 {
75 index = getCastedModel().indexOf(d);
76 list.setSelectedIndex(index);
77 }
78 }
79
80 public void removeDevice(AbstractGestureDevice<?,?> device)
81 {
82
83 int index = list.getSelectedIndex();
84 Object d = null;
85 if(index > -1)
86 d = getCastedModel().getElementAt(index);
87
88 getCastedModel().removeElement(device);
89
90 if(d == device)
91 {
92 index = -1;
93 }
94 else
95 {
96 index = getCastedModel().indexOf(d);
97 }
98 list.setSelectedIndex(index);
99 }
100
101 private DefaultListModel getCastedModel()
102 {
103 return (DefaultListModel) list.getModel();
104 }
105
106 public void addDevicePanelListener(DeviceListPanelListener listener)
107 {
108 listeners.add(listener);
109 }
110
111 public void removeDevicePanelListener(DeviceListPanelListener listener)
112 {
113 listeners.remove(listener);
114 }
115
116 public void removeAllDevicePanelListener()
117 {
118 listeners.clear();
119 }
120
121 private void notifyDevicePanelListener(AbstractGestureDevice<?,?> device)
122 {
123 for(DeviceListPanelListener listener : listeners)
124 {
125 listener.updateDeviceListPanelListener(device);
126 }
127 }
128
129
130
131
132 @Override
133 public void valueChanged(ListSelectionEvent e) {
134 if(!e.getValueIsAdjusting() && list.getSelectedIndex() > -1)
135 {
136 list.setBorder(BorderFactory.createLineBorder(Color.WHITE));
137 DefaultListModel model = (DefaultListModel) list.getModel();
138 AbstractGestureDevice<?,?> device = (AbstractGestureDevice<?,?>)model.getElementAt(list.getSelectedIndex());
139 notifyDevicePanelListener(device);
140 }
141 }
142
143 }
144