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 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
| package fr.issmi.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
import fr.issmi.controller.SerialController;
import fr.issmi.utils.Util;
public class Fenetre extends JFrame implements FenetreActionListener, ActionListener {
private static final long serialVersionUID = 9132616018963449482L;
private JComboBox<String> comboBox;
private JTextField textField;
private JButton[] buttons;
private SerialController serialController;
public Fenetre() {
super("Communication Série");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.getContentPane().setLayout(new BorderLayout());
this.serialController = new SerialController();
this.initialize();
this.getRootPane().setDefaultButton(this.buttons[2]);
this.pack();
Util.centerFrame(this);
this.setVisible(true);
}
protected void initialize() {
this.createComboBox();
this.createTextField();
this.createButtons();
final JPanel northPanel = new JPanel(new FlowLayout());
northPanel.add(this.comboBox);
northPanel.add(this.buttons[0]);
northPanel.add(this.buttons[1]);
final JPanel centerPanel = new JPanel(new FlowLayout());
centerPanel.add(this.textField);
centerPanel.add(this.buttons[2]);
this.getContentPane().add(northPanel, BorderLayout.NORTH);
this.getContentPane().add(centerPanel, BorderLayout.CENTER);
}
protected void createComboBox() {
final String[] ports = new String[this.serialController.getPortIdentifiers().size()];
for (int i = 0; i < ports.length; i++) ports[i] = this.serialController.getPortIdentifiers().get(i).getName();
this.comboBox = new JComboBox<String>(ports);
this.comboBox.setPreferredSize(new Dimension(300, 30));
}
protected void createTextField() {
this.textField = new JTextField();
this.textField.setEditable(false);
this.textField.setBackground(Color.WHITE);
this.textField.setPreferredSize(new Dimension(330, 30));
//this.textField.addKey
}
protected void createButtons() {
this.buttons = new JButton[3];
final String[] buttonsText = {"Connexion", "Déconnexion", "Envoyer"};
final Cursor cursor = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR);
for (int i = 0; i < this.buttons.length; i++) {
this.buttons[i] = new JButton(buttonsText[i]);
this.buttons[i].setActionCommand(String.valueOf(i+1));
this.buttons[i].addActionListener(this);
this.buttons[i].setCursor(cursor);
if (i != 0) this.buttons[i].setEnabled(false);
}
}
@Override
public void actionPerformed(ActionEvent evt) {
switch(Integer.parseInt(evt.getActionCommand())) {
case 1:
try {
this.connexion();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
case 2:
this.deconnexion();
break;
case 3:
this.envoyerMsg();
break;
}
}
@Override
public void connexion() throws Exception {
try {
this.serialController.setSerialPort(this.serialController.selectSerialPort(this.comboBox.getSelectedIndex()));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (this.serialController.getSerialPort() == null) return;
try { Thread.sleep(2000); } catch(InterruptedException e) {}
System.out.println("\nConnexion réussie au port: " + this.serialController.getSerialPort().getName());
this.buttons[0].setEnabled(false);
this.buttons[1].setEnabled(true);
this.buttons[2].setEnabled(true);
this.textField.setEditable(true);
}
@Override
public void deconnexion() {
try {
new Thread() {
@Override
public void run() {
try {
//serialController.getSerialPort().removeEventListener();
//serialController.getSerialPort().close();
//serialController.getOutputStream().close();
}catch(Exception e) {
Util.showError(e.getMessage());
}
}
}.start();
}catch(Exception e) {
Util.showError(e.getMessage());
}
this.buttons[0].setEnabled(true);
this.buttons[1].setEnabled(false);
this.buttons[2].setEnabled(false);
this.textField.setEditable(false);
}
@Override
public void envoyerMsg() {
if (this.textField.getText().length() > 0) {
try {
this.serialController.getOutputStream().write(this.textField.getText().getBytes());
System.out.println("Message envoyé: " + this.textField.getText());
this.textField.setText("");
}catch(IOException e) {
Util.showError(e.getMessage());
}
}
}
public synchronized void close() {
if (this.serialController.getSerialPort() != null) {
this.serialController.getSerialPort().notifyOnDataAvailable(false);
this.serialController.getSerialPort().notifyOnOverrunError(false);
this.serialController.getSerialPort().notifyOnOverrunError(false);
//this.serialController.getSerialPort().removeEventListener();
//this.serialController.getSerialPort().close();
this.serialController.setSerialPort(null);
this.buttons[0].setEnabled(true);
this.buttons[1].setEnabled(false);
this.buttons[2].setEnabled(false);
this.textField.setEditable(false);
}
}
} |
Partager