Blame view

VRGNYMusicLights/Sources/SerialPortChooserDialog.java 3.49 KB
742429d1   pfrison   VRGNYMusicLight b...
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
118
119
  import java.awt.BorderLayout;
  import java.awt.Dimension;
  import java.awt.FlowLayout;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.util.ArrayList;
  import java.util.Enumeration;
  
  import javax.swing.DefaultListModel;
  import javax.swing.JButton;
  import javax.swing.JDialog;
  import javax.swing.JLabel;
  import javax.swing.JList;
  import javax.swing.JPanel;
  import javax.swing.JScrollPane;
  import javax.swing.ListSelectionModel;
  import javax.swing.event.ListSelectionEvent;
  import javax.swing.event.ListSelectionListener;
  
  import gnu.io.CommPortIdentifier;
  
  public class SerialPortChooserDialog extends JDialog implements ActionListener, ListSelectionListener {
  	private static final long serialVersionUID = 1L;
  	
  	private boolean onlyOneChoice;
  	private boolean emptyChoice;
  	private ArrayList<String> portNames;
  	private String choosedPort = null;
  	
  	private JList<String> list;
  	private JButton ok;
  	private JButton cancel;
  
  	private SerialPortChooserDialog() {
  		Enumeration<?> portEnum = CommPortIdentifier.getPortIdentifiers();
  		portNames = new ArrayList<>();
  
  		while (portEnum.hasMoreElements()) {
  			CommPortIdentifier currPortId = (CommPortIdentifier) portEnum.nextElement();
  			portNames.add(currPortId.getName());
  		}
  
  		onlyOneChoice = portNames.size() <= 1;
  		emptyChoice = portNames.size() == 0;
  		if(onlyOneChoice || emptyChoice)
  			return;
  		
  		// JDialog creation
  		JPanel mainPanel = new JPanel(new BorderLayout());
  		setModal(true);
  		setTitle("Serial port chooser");
  		
  		// title
  		mainPanel.add(new JLabel("Choose a serial port : "), BorderLayout.NORTH);
  		
  		// list ports
  		DefaultListModel<String> listModel = new DefaultListModel<>();
  		for(String str : portNames)
  			listModel.addElement(str);
  		list = new JList<>(listModel);
  		list.addListSelectionListener(this);
  		list.setVisibleRowCount(3);
  		list.setLayoutOrientation(JList.VERTICAL);
  		list.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
  		
  		JScrollPane scrollPane = new JScrollPane(list, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
  		scrollPane.setPreferredSize(new Dimension(200, 100));
  		mainPanel.add(scrollPane, BorderLayout.CENTER);
  		
  		// buttons ok / cancel
  		JPanel borderLayoutPanel = new JPanel(new BorderLayout());
  		JPanel buttonsLayout = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 3));
  		ok = new JButton("Ok");
  		cancel = new JButton("Cancel");
  		ok.addActionListener(this);
  		cancel.addActionListener(this);
  		ok.setEnabled(false);
  		buttonsLayout.add(cancel);
  		buttonsLayout.add(ok);
  		borderLayoutPanel.add(buttonsLayout, BorderLayout.EAST);
  		mainPanel.add(borderLayoutPanel, BorderLayout.SOUTH);
  
  		// padding
  		JPanel padding = new JPanel(new FlowLayout(FlowLayout.LEADING, 5, 5));
  		padding.add(mainPanel);
  		add(padding);
  
  		setResizable(false);
  		pack();
  		setLocationRelativeTo(null);
  		setVisible(true);
  	}
  	
  	public static String showSerialPortChooserDialog() throws NoSerialPortException {
  		SerialPortChooserDialog dialog = new SerialPortChooserDialog();
  		if(dialog.emptyChoice)
  			throw new NoSerialPortException();
  		if(dialog.onlyOneChoice)
  			return dialog.portNames.get(0);
  		return dialog.choosedPort;
  	}
  
  	@Override
  	public void actionPerformed(ActionEvent e) {
  		if(e.getSource() == ok) {
  			choosedPort = list.getSelectedValue();
  			dispose();
  		} else if(e.getSource() == cancel) {
  			dispose();
  		}
  	}
  
  	@Override
  	public void valueChanged(ListSelectionEvent e) {
  		if(e.getSource() == list) {
  			ok.setEnabled(list.getSelectedIndex() != -1);
  		}
  	}
  }