diff --git a/src/main/java/info/istlab/IoTP/MqttWindow.java b/src/main/java/info/istlab/IoTP/MqttWindow.java index c067002..02d0dac 100644 --- a/src/main/java/info/istlab/IoTP/MqttWindow.java +++ b/src/main/java/info/istlab/IoTP/MqttWindow.java @@ -16,6 +16,7 @@ import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JFrame; +import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import javax.swing.JTextArea; @@ -26,7 +27,7 @@ import org.eclipse.paho.client.mqttv3.MqttException; import org.eclipse.paho.client.mqttv3.MqttMessage; -public class MqttWindow extends JFrame implements ActionListener, WindowListener, MqttCallback { +public class MqttWindow extends JFrame implements ActionListener, WindowListener, MqttCallback, Runnable { static MqttWindow theMqttWindow; JTextField jtf; JTextArea jta; @@ -38,9 +39,11 @@ JButton connectB; MqttSubscriber subscriber; - public static MqttWindow create(){ - if (theMqttWindow != null) return theMqttWindow; - else theMqttWindow = new MqttWindow(); + public static MqttWindow create() { + if (theMqttWindow != null) + return theMqttWindow; + else + theMqttWindow = new MqttWindow(); return theMqttWindow; } @@ -61,7 +64,8 @@ JPanel settingP = new JPanel(); settingP.setLayout(new BoxLayout(settingP, BoxLayout.Y_AXIS)); JPanel v01p = new JPanel(new BorderLayout()); - v01p.setBorder(BorderFactory.createTitledBorder("Broker ( ex. 192.168.11.11 | mqtt.istlab.info ) and port ( ex. 1883 )")); + v01p.setBorder(BorderFactory + .createTitledBorder("Broker ( ex. 192.168.11.11 | mqtt.istlab.info ) and port ( ex. 1883 )")); v01p.add(brokerjtf, BorderLayout.CENTER); v01p.add(brokerportjtf, BorderLayout.EAST); settingP.add(v01p); @@ -120,19 +124,35 @@ // jta.requestFocus(); appendToJTA("まだ接続していません。Topicを設定してConnectをおしてください。\n"); } + @Override + public void run() { + subscriber = new MqttSubscriber(brokerjtf.getText(), brokerportjtf.getText(), topicjtf.getText(), this); + jta.setBackground(Color.YELLOW); + appendToJTA("Try Connecting to broker: tcp://" + brokerjtf.getText() + ":" + brokerportjtf.getText() + ".\n"); + try { + subscriber.subscribe(); + } catch (MqttException | InterruptedException e1) { + e1.printStackTrace(); + appendToJTA("接続できませんでした。\n"); + jta.setBackground(Color.magenta); + thread = null; + return; + } + appendToJTA("Connected.\n"); + jta.setBackground(Color.white); + topicjtf.setEditable(false); + connectB.setText("Disconnect"); + } + private void connect(ActionEvent e) { if (e.getActionCommand().equals("Connect")) { - subscriber = new MqttSubscriber(brokerjtf.getText(), brokerportjtf.getText(), topicjtf.getText(), this); - try { - subscriber.subscribe(); - } catch (MqttException | InterruptedException e1) { - e1.printStackTrace(); + if (thread != null) { + JOptionPane.showMessageDialog(this, "まだ接続試行中です..."); + return; } - appendToJTA("Connected.\n"); - jta.setBackground(Color.white); - topicjtf.setEditable(false); - connectB.setText("Disconnect"); + thread = new Thread(this); + thread.start(); } else if (e.getActionCommand().equals("Disconnect")) { try { subscriber.unsubscribe(); @@ -145,6 +165,7 @@ topicjtf.setEditable(true); } connectB.setText("Connect"); + thread = null; } } @@ -180,7 +201,7 @@ if (message.length() > 0) { // Topic name check String topic = topicjtf.getText(); - if ( topic.contains("#") || topic.contains("+") ){ + if (topic.contains("#") || topic.contains("+")) { appendToJTA("Publishするとき、Topic にワイルドカード文字 #+ を含めることはできません。"); return; } @@ -234,7 +255,7 @@ } connectB.setText("Connect"); MqttWindow.theMqttWindow = null; - if (Launcher.theapp == null) + if (Launcher.theapp == null || !Launcher.theapp.isVisible()) System.exit(0); else dispose(); @@ -294,4 +315,5 @@ public void deliveryComplete(IMqttDeliveryToken token) { throw new UnsupportedOperationException("Unimplemented method 'deliveryComplete'"); } + }