package ch.spherICIT.main; import ch.spherICIT.components.switchbtn.SwitchButton; import com.fazecast.jSerialComm.SerialPort; import javax.swing.DefaultComboBoxModel; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComboBox; import javax.swing.JComponent; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.plaf.FontUIResource; import javax.swing.text.StyleContext; import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridBagConstraints; 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 JButton serPortOpenBtn; private JButton serPortCloseBtn; private JButton setZeroAzBtn; private SwitchButton relaisPwrBtn; private SwitchButton camVelocityModeBtn; private SwitchButton camStepperPwrBtn; private JLabel relaisPwrLed; private JLabel camStepperPwrLed; private JButton logWindowBtn; private final LogWindow logWindow; private Set connectionStateListeners; private ActionListener reconnectActionListener; private List commPorts; /** * Konstruktor. */ public MainPanel() { this.logWindow = new LogWindow(); this.connectionStateListeners = new HashSet<>(); $$$setupUI$$$(); initCommPorts(); this.reconnectActionListener = new ActionListener() { @Override public void actionPerformed(ActionEvent e) { JOptionPane.showMessageDialog(mainPanel, "Test", "Title", JOptionPane.INFORMATION_MESSAGE); } }; this.serPortOpenBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTING, "", "")); getSelectedCommPort().get().getSerialPort().get().openPort(); serPortOpenBtn.setEnabled(false); serPortCloseBtn.setEnabled(true); } }); this.serPortCloseBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.CONNECTED, "COM4", "v1.2.0")); getSelectedCommPort().get().getSerialPort().get().closePort(); serPortOpenBtn.setEnabled(true); serPortCloseBtn.setEnabled(false); } }); this.setZeroAzBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { connectionStateListeners.iterator().next().connectionStateChanged(new ConnectionStateInfo(ConnectionState.TIMEOUT, "", "")); } }); this.logWindowBtn.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { logWindow.setVisible(true); } }); } 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 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); } public void removeConnectionStateListener(IConnectionState connectionStateListener) { this.connectionStateListeners.remove(connectionStateListener); } public ActionListener getReconnectActionListener() { return reconnectActionListener; } private Optional getSelectedCommPort() { return this.serPortCboBox.getSelectedIndex() != -1 ? Optional.of(this.serPortCboBox.getItemAt(this.serPortCboBox.getSelectedIndex())) : Optional.empty(); } 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 getSerialPort() { return this.serialPort != null ? Optional.of(this.serialPort) : Optional.empty(); } @Override public String toString() { return this.serialPort != null ? this.serialPort.getSystemPortName() : ""; } } /** * Method generated by IntelliJ IDEA GUI Designer * >>> IMPORTANT!! <<< * DO NOT edit this method OR call it in your code! * * @noinspection ALL */ private void $$$setupUI$$$() { createUIComponents(); mainPanel = new JPanel(); mainPanel.setLayout(new GridBagLayout()); final JPanel spacer1 = new JPanel(); GridBagConstraints gbc; gbc = new GridBagConstraints(); gbc.gridx = 7; gbc.gridy = 1; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(spacer1, gbc); serPortLbl = new JLabel(); Font serPortLblFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, serPortLbl.getFont()); if (serPortLblFont != null) serPortLbl.setFont(serPortLblFont); serPortLbl.setText("Serieller Port:"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = new Insets(18, 10, 10, 10); mainPanel.add(serPortLbl, gbc); final JPanel spacer2 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 6; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.VERTICAL; mainPanel.add(spacer2, gbc); 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(); serPortCboBox.setModel(defaultComboBoxModel1); serPortCboBox.setPreferredSize(new Dimension(120, 32)); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(10, 0, 10, 10); 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); serPortOpenBtn.setHorizontalTextPosition(0); serPortOpenBtn.setLabel("Öffnen"); serPortOpenBtn.setMinimumSize(new Dimension(130, 32)); serPortOpenBtn.setPreferredSize(new Dimension(130, 32)); serPortOpenBtn.setSelected(false); serPortOpenBtn.setText("Öffnen"); serPortOpenBtn.setVerticalAlignment(3); gbc = new GridBagConstraints(); gbc.gridx = 3; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; 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"); serPortCloseBtn.setMinimumSize(new Dimension(130, 32)); serPortCloseBtn.setPreferredSize(new Dimension(130, 32)); serPortCloseBtn.setText("Schliessen"); serPortCloseBtn.setVerticalAlignment(3); gbc = new GridBagConstraints(); gbc.gridx = 4; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(10, 10, 10, 10); mainPanel.add(serPortCloseBtn, gbc); final JLabel label1 = new JLabel(); Font label1Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label1.getFont()); if (label1Font != null) label1.setFont(label1Font); label1.setForeground(new Color(-15834670)); label1.setHorizontalAlignment(0); label1.setText("Relais Power Schalter"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.gridwidth = 3; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(10, 10, 10, 10); mainPanel.add(label1, gbc); final JLabel label2 = new JLabel(); Font label2Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label2.getFont()); if (label2Font != null) label2.setFont(label2Font); label2.setForeground(new Color(-15834670)); label2.setHorizontalAlignment(0); label2.setText("Drehwinkel WebCam"); gbc = new GridBagConstraints(); gbc.gridx = 3; gbc.gridy = 1; gbc.gridwidth = 2; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(10, 10, 10, 10); mainPanel.add(label2, gbc); final JLabel label3 = new JLabel(); Font label3Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label3.getFont()); if (label3Font != null) label3.setFont(label3Font); label3.setForeground(new Color(-15834670)); label3.setHorizontalAlignment(0); label3.setText("WebCam 0° Azimut"); gbc = new GridBagConstraints(); gbc.gridx = 5; gbc.gridy = 1; gbc.gridwidth = 2; gbc.weightx = 1.0; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(10, 0, 10, 10); mainPanel.add(label3, gbc); final JPanel panel1 = new JPanel(); panel1.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 3; gbc.fill = GridBagConstraints.BOTH; mainPanel.add(panel1, gbc); final JLabel label4 = new JLabel(); Font label4Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 20, label4.getFont()); if (label4Font != null) label4.setFont(label4Font); label4.setForeground(new Color(-65536)); label4.setText("AUS"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(0, 0, 0, 10); panel1.add(label4, gbc); final JLabel label5 = new JLabel(); Font label5Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 20, label5.getFont()); if (label5Font != null) label5.setFont(label5Font); label5.setForeground(new Color(-16738404)); label5.setText("EIN"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 0; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(0, 10, 0, 0); panel1.add(label5, gbc); relaisPwrBtn = new SwitchButton(); relaisPwrBtn.setBackground(new Color(-16738404)); relaisPwrBtn.setPreferredSize(new Dimension(80, 40)); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 0; panel1.add(relaisPwrBtn, gbc); final JLabel label6 = new JLabel(); Font label6Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label6.getFont()); if (label6Font != null) label6.setFont(label6Font); label6.setForeground(new Color(-15834670)); label6.setHorizontalAlignment(0); label6.setText("Relais Power Status"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 4; gbc.gridwidth = 3; gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(10, 10, 10, 10); mainPanel.add(label6, gbc); final JPanel spacer3 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 3; gbc.gridwidth = 2; gbc.fill = GridBagConstraints.VERTICAL; gbc.ipady = 80; mainPanel.add(spacer3, gbc); relaisPwrLed = new JLabel(); relaisPwrLed.setHorizontalAlignment(0); relaisPwrLed.setIcon(new ImageIcon(getClass().getResource("/blue_led_off.png"))); relaisPwrLed.setText(""); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 5; gbc.gridwidth = 3; gbc.fill = GridBagConstraints.HORIZONTAL; mainPanel.add(relaisPwrLed, gbc); setZeroAzBtn = new JButton(); Font setZeroAzBtnFont = this.$$$getFont$$$("Bahnschrift", Font.PLAIN, 14, setZeroAzBtn.getFont()); if (setZeroAzBtnFont != null) setZeroAzBtn.setFont(setZeroAzBtnFont); setZeroAzBtn.setMaximumSize(new Dimension(130, 32)); setZeroAzBtn.setMinimumSize(new Dimension(130, 32)); setZeroAzBtn.setPreferredSize(new Dimension(130, 32)); setZeroAzBtn.setText("0° setzen"); setZeroAzBtn.setVerticalAlignment(3); gbc = new GridBagConstraints(); gbc.gridx = 5; gbc.gridy = 2; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.NORTHWEST; mainPanel.add(setZeroAzBtn, gbc); final JPanel panel2 = new JPanel(); panel2.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 5; gbc.gridy = 3; gbc.gridwidth = 2; gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.VERTICAL; mainPanel.add(panel2, gbc); final JLabel label7 = new JLabel(); Font label7Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label7.getFont()); if (label7Font != null) label7.setFont(label7Font); label7.setForeground(new Color(-65536)); label7.setText("SLOW"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(0, 0, 0, 10); panel2.add(label7, gbc); final JLabel label8 = new JLabel(); Font label8Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label8.getFont()); if (label8Font != null) label8.setFont(label8Font); label8.setForeground(new Color(-15834670)); label8.setHorizontalAlignment(0); label8.setText("WebCam Geschwindigkeit"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 4; gbc.weightx = 1.0; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(10, 0, 10, 10); panel2.add(label8, gbc); camVelocityModeBtn = new SwitchButton(); camVelocityModeBtn.setBackground(new Color(-16738404)); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; panel2.add(camVelocityModeBtn, gbc); final JLabel label9 = new JLabel(); Font label9Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label9.getFont()); if (label9Font != null) label9.setFont(label9Font); label9.setForeground(new Color(-16738404)); label9.setText("FAST"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 1; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(0, 10, 0, 0); panel2.add(label9, gbc); final JPanel spacer4 = new JPanel(); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 2; gbc.gridwidth = 4; gbc.weighty = 1.0; gbc.fill = GridBagConstraints.BOTH; panel2.add(spacer4, gbc); final JPanel panel3 = new JPanel(); panel3.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 5; gbc.gridy = 4; gbc.gridwidth = 2; gbc.gridheight = 2; gbc.anchor = GridBagConstraints.WEST; gbc.fill = GridBagConstraints.VERTICAL; mainPanel.add(panel3, gbc); final JLabel label10 = new JLabel(); Font label10Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label10.getFont()); if (label10Font != null) label10.setFont(label10Font); label10.setForeground(new Color(-65536)); label10.setText("AUS"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.anchor = GridBagConstraints.SOUTHWEST; gbc.insets = new Insets(0, 0, 12, 10); panel3.add(label10, gbc); final JLabel label11 = new JLabel(); Font label11Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label11.getFont()); if (label11Font != null) label11.setFont(label11Font); label11.setForeground(new Color(-16738404)); label11.setText("Ein"); gbc = new GridBagConstraints(); gbc.gridx = 2; gbc.gridy = 1; gbc.anchor = GridBagConstraints.SOUTH; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(0, 10, 12, 0); panel3.add(label11, gbc); final JLabel label12 = new JLabel(); Font label12Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 16, label12.getFont()); if (label12Font != null) label12.setFont(label12Font); label12.setForeground(new Color(-15834670)); label12.setHorizontalAlignment(0); label12.setText("WebCam Stepper Power"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.gridwidth = 4; gbc.anchor = GridBagConstraints.WEST; gbc.insets = new Insets(10, 0, 10, 10); panel3.add(label12, gbc); final JPanel panel4 = new JPanel(); panel4.setLayout(new GridBagLayout()); gbc = new GridBagConstraints(); gbc.gridx = 3; gbc.gridy = 1; gbc.anchor = GridBagConstraints.EAST; gbc.fill = GridBagConstraints.VERTICAL; panel3.add(panel4, gbc); final JLabel label13 = new JLabel(); Font label13Font = this.$$$getFont$$$("Bahnschrift", Font.BOLD, 14, label13.getFont()); if (label13Font != null) label13.setFont(label13Font); label13.setForeground(new Color(-15834670)); label13.setHorizontalAlignment(0); label13.setText("Status"); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.insets = new Insets(0, 10, 5, 10); panel4.add(label13, gbc); camStepperPwrLed = new JLabel(); camStepperPwrLed.setHorizontalAlignment(0); camStepperPwrLed.setIcon(new ImageIcon(getClass().getResource("/blue_led_off.png"))); camStepperPwrLed.setText(""); gbc = new GridBagConstraints(); gbc.gridx = 0; gbc.gridy = 1; gbc.fill = GridBagConstraints.HORIZONTAL; panel4.add(camStepperPwrLed, gbc); camStepperPwrBtn = new SwitchButton(); camStepperPwrBtn.setBackground(new Color(-16738404)); gbc = new GridBagConstraints(); gbc.gridx = 1; gbc.gridy = 1; gbc.anchor = GridBagConstraints.SOUTH; gbc.insets = new Insets(0, 0, 10, 0); panel3.add(camStepperPwrBtn, gbc); final JLabel label14 = new JLabel(); label14.setIcon(new ImageIcon(getClass().getResource("/Arduino_Logo_small.png"))); label14.setText(""); gbc = new GridBagConstraints(); gbc.gridx = 5; gbc.gridy = 0; gbc.anchor = GridBagConstraints.EAST; gbc.insets = new Insets(10, 80, 20, 10); mainPanel.add(label14, gbc); logWindowBtn = new JButton(); logWindowBtn.setIcon(new ImageIcon(getClass().getResource("/log24.png"))); logWindowBtn.setMaximumSize(new Dimension(32, 32)); logWindowBtn.setMinimumSize(new Dimension(32, 32)); logWindowBtn.setPreferredSize(new Dimension(32, 32)); logWindowBtn.setText(""); gbc = new GridBagConstraints(); gbc.gridx = 5; gbc.gridy = 0; gbc.anchor = GridBagConstraints.NORTHWEST; gbc.insets = new Insets(10, 10, 10, 10); mainPanel.add(logWindowBtn, gbc); } /** * @noinspection ALL */ private Font $$$getFont$$$(String fontName, int style, int size, Font currentFont) { if (currentFont == null) return null; String resultName; if (fontName == null) { resultName = currentFont.getName(); } else { Font testFont = new Font(fontName, Font.PLAIN, 10); if (testFont.canDisplay('a') && testFont.canDisplay('1')) { resultName = fontName; } else { resultName = currentFont.getName(); } } Font font = new Font(resultName, style >= 0 ? style : currentFont.getStyle(), size >= 0 ? size : currentFont.getSize()); boolean isMac = System.getProperty("os.name", "").toLowerCase(Locale.ENGLISH).startsWith("mac"); Font fontWithFallback = isMac ? new Font(font.getFamily(), font.getStyle(), font.getSize()) : new StyleContext().getFont(font.getFamily(), font.getStyle(), font.getSize()); return fontWithFallback instanceof FontUIResource ? fontWithFallback : new FontUIResource(fontWithFallback); } /** * @noinspection ALL */ public JComponent $$$getRootComponent$$$() { return mainPanel; } }