SelectPort.java
3.14 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package org.streamparty.remoterfid;
import gnu.io.CommPortIdentifier;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class SelectPort extends JDialog {
/**
*
*/
private static final long serialVersionUID = 433109152542158881L;
JList lista;
Set<PortListener> portListeners;
private JTextField readerName;
private Cashier cashier;
private static final Logger logger = LoggerFactory.getLogger(SelectPort.class);
public SelectPort(Cashier c) {
super();
cashier = c;
this.setSize(200, 400);
this.requestFocus(true);
setLocation(400, 300);
//setModalityType(Dialog.DEFAULT_MODALITY_TYPE);
portListeners = new HashSet<PortListener>();
try {
logger.debug("Getting serial port list");
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
Vector<String> portit = new Vector<String>();
while (portList.hasMoreElements()) {
portId = (CommPortIdentifier) portList.nextElement();
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL) {
portit.add(portId.getName());
}
}
lista = new JList(portit);
} catch (UnsatisfiedLinkError e) {
logger.debug(e.getMessage());
logger.debug("wtf: ", e);
throw e;
}
JPanel paneeli = new JPanel(new GridLayout(0, 1));
readerName = new javax.swing.JTextField();
this.getContentPane().add(paneeli);
paneeli.add(readerName);
paneeli.add(lista);
JButton nappi = new JButton("Select");
nappi.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
String selectedValue = lista.getSelectedValue().toString();
logger.debug("Select port button pushed");
Enumeration<?> portList = CommPortIdentifier.getPortIdentifiers();
CommPortIdentifier portId = null;
boolean done = false;
while (portList.hasMoreElements() && !done) {
portId = (CommPortIdentifier) portList.nextElement();
logger.info("Looping through elements {}", portId.getName());
if (portId.getPortType() == CommPortIdentifier.PORT_SERIAL && portId.getName().equals(selectedValue)) {
RfidAction rfidParser = RfidAction.init(portId);
logger.info("Setting reader name to {}", readerName.getText());
cashier.setReaderName(readerName.getText());
Iterator<PortListener> loop = portListeners.iterator();
while (loop.hasNext()) {
logger.debug("Setting port to listeners");
loop.next().portIdentifier(rfidParser);
}
portListeners.clear();
dispose();
done = true;
}
}
}
});
paneeli.add(nappi);
JButton close = new JButton("Close");
close.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
System.exit(0);
}
});
paneeli.add(close);
paneeli.setVisible(true);
this.setVisible(true);
}
}