|
|
|
@@ -1,6 +1,7 @@ |
|
|
|
package ch.spherICIT.main; |
|
|
|
|
|
|
|
import ch.spherICIT.components.switchbtn.SwitchButton; |
|
|
|
import com.fazecast.jSerialComm.SerialPort; |
|
|
|
|
|
|
|
import javax.swing.DefaultComboBoxModel; |
|
|
|
import javax.swing.ImageIcon; |
|
|
|
@@ -20,15 +21,20 @@ import java.awt.GridBagLayout; |
|
|
|
import java.awt.Insets; |
|
|
|
import java.awt.event.ActionEvent; |
|
|
|
import java.awt.event.ActionListener; |
|
|
|
import java.awt.event.ItemEvent; |
|
|
|
import java.awt.event.ItemListener; |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.util.HashSet; |
|
|
|
import java.util.List; |
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Optional; |
|
|
|
import java.util.Set; |
|
|
|
|
|
|
|
public class MainPanel { |
|
|
|
|
|
|
|
private JPanel mainPanel; |
|
|
|
private JLabel serPortLbl; |
|
|
|
private JComboBox serPortCboBox; |
|
|
|
private JComboBox<CommPort> serPortCboBox; |
|
|
|
private JButton serPortOpenBtn; |
|
|
|
private JButton serPortCloseBtn; |
|
|
|
private JButton setZeroAzBtn; |
|
|
|
@@ -42,6 +48,7 @@ public class MainPanel { |
|
|
|
|
|
|
|
private Set<IConnectionState> connectionStateListeners; |
|
|
|
private ActionListener reconnectActionListener; |
|
|
|
private List<CommPort> commPorts; |
|
|
|
|
|
|
|
/** |
|
|
|
* Konstruktor. |
|
|
|
@@ -49,6 +56,9 @@ public class MainPanel { |
|
|
|
public MainPanel() { |
|
|
|
this.logWindow = new LogWindow(); |
|
|
|
this.connectionStateListeners = new HashSet<>(); |
|
|
|
$$$setupUI$$$(); |
|
|
|
initCommPorts(); |
|
|
|
|
|
|
|
this.reconnectActionListener = new ActionListener() { |
|
|
|
@Override |
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
@@ -58,22 +68,28 @@ public class MainPanel { |
|
|
|
this.serPortOpenBtn.addActionListener(new ActionListener() { |
|
|
|
@Override |
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTING, "", "")); |
|
|
|
// connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTING, "", "")); |
|
|
|
getSelectedCommPort().get().getSerialPort().get().openPort(); |
|
|
|
serPortOpenBtn.setEnabled(false); |
|
|
|
serPortCloseBtn.setEnabled(true); |
|
|
|
} |
|
|
|
}); |
|
|
|
serPortCloseBtn.addActionListener(new ActionListener() { |
|
|
|
this.serPortCloseBtn.addActionListener(new ActionListener() { |
|
|
|
@Override |
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTED, "COM4", "v1.2.0")); |
|
|
|
// connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTED, "COM4", "v1.2.0")); |
|
|
|
getSelectedCommPort().get().getSerialPort().get().closePort(); |
|
|
|
serPortOpenBtn.setEnabled(true); |
|
|
|
serPortCloseBtn.setEnabled(false); |
|
|
|
} |
|
|
|
}); |
|
|
|
setZeroAzBtn.addActionListener(new ActionListener() { |
|
|
|
this.setZeroAzBtn.addActionListener(new ActionListener() { |
|
|
|
@Override |
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.TIMEOUT, "", "")); |
|
|
|
} |
|
|
|
}); |
|
|
|
logWindowBtn.addActionListener(new ActionListener() { |
|
|
|
this.logWindowBtn.addActionListener(new ActionListener() { |
|
|
|
@Override |
|
|
|
public void actionPerformed(ActionEvent e) { |
|
|
|
logWindow.setVisible(true); |
|
|
|
@@ -81,6 +97,34 @@ public class MainPanel { |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
private void initCommPorts() { |
|
|
|
this.commPorts = new ArrayList<>(); |
|
|
|
this.commPorts.add(new CommPort(null)); |
|
|
|
for (SerialPort serialPort : SerialPort.getCommPorts()) { |
|
|
|
this.commPorts.add(new CommPort(serialPort)); |
|
|
|
} |
|
|
|
this.serPortCboBox.setModel(new DefaultComboBoxModel(this.commPorts.toArray())); |
|
|
|
serPortCboBox.addItemListener(new ItemListener() { |
|
|
|
@Override |
|
|
|
public void itemStateChanged(ItemEvent e) { |
|
|
|
Optional<CommPort> selCommPortOptional = getSelectedCommPort(); |
|
|
|
if (selCommPortOptional.isPresent() && selCommPortOptional.get().getSerialPort().isPresent()) { |
|
|
|
selCommPortOptional.get().getSerialPort().get().openPort(); |
|
|
|
serPortOpenBtn.setEnabled(false); |
|
|
|
serPortCloseBtn.setEnabled(true); |
|
|
|
} else { |
|
|
|
for (IConnectionState listener : connectionStateListeners) { |
|
|
|
listener.connectionStateChanged(new ConnectionStateInfo(ConnectionState.READY, null, null)); |
|
|
|
serPortOpenBtn.setEnabled(false); |
|
|
|
serPortCloseBtn.setEnabled(false); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
this.serPortCboBox.setSelectedIndex(-1); |
|
|
|
} |
|
|
|
|
|
|
|
public void registerConnectionStateListener(IConnectionState connectionStateListener) { |
|
|
|
this.connectionStateListeners.add(connectionStateListener); |
|
|
|
} |
|
|
|
@@ -93,12 +137,36 @@ public class MainPanel { |
|
|
|
return reconnectActionListener; |
|
|
|
} |
|
|
|
|
|
|
|
private Optional<CommPort> getSelectedCommPort() { |
|
|
|
return this.serPortCboBox.getSelectedIndex() != -1 ? Optional.of(this.serPortCboBox.getItemAt(this.serPortCboBox.getSelectedIndex())) : Optional.empty(); |
|
|
|
} |
|
|
|
|
|
|
|
{ |
|
|
|
// GUI initializer generated by IntelliJ IDEA GUI Designer |
|
|
|
// >>> IMPORTANT!! <<< |
|
|
|
// DO NOT EDIT OR ADD ANY CODE HERE! |
|
|
|
$$$setupUI$$$(); |
|
|
|
private void createUIComponents() { |
|
|
|
this.serPortCboBox = new JComboBox<>(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private class CommPort { |
|
|
|
|
|
|
|
private SerialPort serialPort; |
|
|
|
|
|
|
|
/** |
|
|
|
* Constructor |
|
|
|
* |
|
|
|
* @param serialPort serial port from JSerialComm |
|
|
|
*/ |
|
|
|
public CommPort(SerialPort serialPort) { |
|
|
|
this.serialPort = serialPort; |
|
|
|
} |
|
|
|
|
|
|
|
public Optional<SerialPort> getSerialPort() { |
|
|
|
return this.serialPort != null ? Optional.of(this.serialPort) : Optional.empty(); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
public String toString() { |
|
|
|
return this.serialPort != null ? this.serialPort.getSystemPortName() : ""; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
@@ -109,6 +177,7 @@ public class MainPanel { |
|
|
|
* @noinspection ALL |
|
|
|
*/ |
|
|
|
private void $$$setupUI$$$() { |
|
|
|
createUIComponents(); |
|
|
|
mainPanel = new JPanel(); |
|
|
|
mainPanel.setLayout(new GridBagLayout()); |
|
|
|
final JPanel spacer1 = new JPanel(); |
|
|
|
@@ -137,12 +206,10 @@ public class MainPanel { |
|
|
|
gbc.weighty = 1.0; |
|
|
|
gbc.fill = GridBagConstraints.VERTICAL; |
|
|
|
mainPanel.add(spacer2, gbc); |
|
|
|
serPortCboBox = new JComboBox(); |
|
|
|
Font serPortCboBoxFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, serPortCboBox.getFont()); |
|
|
|
if (serPortCboBoxFont != null) serPortCboBox.setFont(serPortCboBoxFont); |
|
|
|
serPortCboBox.setMinimumSize(new Dimension(120, 32)); |
|
|
|
final DefaultComboBoxModel defaultComboBoxModel1 = new DefaultComboBoxModel(); |
|
|
|
defaultComboBoxModel1.addElement("COM4"); |
|
|
|
serPortCboBox.setModel(defaultComboBoxModel1); |
|
|
|
serPortCboBox.setPreferredSize(new Dimension(120, 32)); |
|
|
|
gbc = new GridBagConstraints(); |
|
|
|
@@ -154,6 +221,7 @@ public class MainPanel { |
|
|
|
mainPanel.add(serPortCboBox, gbc); |
|
|
|
serPortOpenBtn = new JButton(); |
|
|
|
serPortOpenBtn.setContentAreaFilled(true); |
|
|
|
serPortOpenBtn.setEnabled(false); |
|
|
|
Font serPortOpenBtnFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, serPortOpenBtn.getFont()); |
|
|
|
if (serPortOpenBtnFont != null) serPortOpenBtn.setFont(serPortOpenBtnFont); |
|
|
|
serPortOpenBtn.setHideActionText(false); |
|
|
|
@@ -172,6 +240,7 @@ public class MainPanel { |
|
|
|
gbc.insets = new Insets(10, 10, 10, 10); |
|
|
|
mainPanel.add(serPortOpenBtn, gbc); |
|
|
|
serPortCloseBtn = new JButton(); |
|
|
|
serPortCloseBtn.setEnabled(false); |
|
|
|
Font serPortCloseBtnFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, serPortCloseBtn.getFont()); |
|
|
|
if (serPortCloseBtnFont != null) serPortCloseBtn.setFont(serPortCloseBtnFont); |
|
|
|
serPortCloseBtn.setLabel("Schliessen"); |