package jturbojet;
import jturbojet.serial.SerialPorts;
import jturbojet.serial.SerialDAO;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.AdjustmentEvent;
import java.awt.event.AdjustmentListener;
import java.io.File;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import static javax.swing.JFileChooser.SAVE_DIALOG;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import jssc.SerialPort;
import jturbojet.File.FileWrite;
import jturbojet.File.XLSWrite;
import jturbojet.serial.interfaces.SerialUpdate;
public class TurboJetGUI extends JPanel implements ActionListener, AdjustmentListener, SerialUpdate {
private JButton con;
private JButton ref;
private JComboBox portb;
private JTextArea output;
private JTextField info;
private JScrollPane scrollPane;
private JMenuBar menuBar;
private JMenu file;
private JMenuItem saveTxt;
private JMenuItem saveXml;
private JFileChooser fc;
private XLSWrite xmlWriter;
private FileWrite txtWriter;
private SerialDAO serial;
public static void main(String[] args) {
javax.swing.SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
createAndShowGUI();
}
});
}
private void initFileChooser() {
fc = new JFileChooser() {
@Override
public void approveSelection() {
File f = getSelectedFile();
if (f.exists() && getDialogType() == SAVE_DIALOG) {
int result = JOptionPane.showConfirmDialog(this, "The file exists, overwrite?", "Existing file", JOptionPane.YES_NO_CANCEL_OPTION);
switch (result) {
case JOptionPane.YES_OPTION:
super.approveSelection();
return;
case JOptionPane.NO_OPTION:
return;
case JOptionPane.CLOSED_OPTION:
return;
case JOptionPane.CANCEL_OPTION:
cancelSelection();
return;
}
}
super.approveSelection();
}
};
fc.setCurrentDirectory(new File(System.getProperty("user.dir")));
}
private static void createAndShowGUI() {
JFrame frame = new JFrame("TurboChargerGUI");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(500, 500));
frame.setContentPane(new TurboJetGUI());
frame.pack();
frame.setVisible(true);
}
public TurboJetGUI() {
initFileChooser();
portb = new JComboBox(SerialPorts.getAvailablePorts());
// Menu bar
menuBar = new JMenuBar();
file = new JMenu("File");
saveTxt = new JMenuItem("Save as txt");
saveTxt.setActionCommand("saveTxt");
saveTxt.addActionListener(this);
saveXml = new JMenuItem("Save as xml");
saveXml.setActionCommand("saveXml");
saveXml.addActionListener(this);
file.add(saveTxt);
file.add(saveXml);
menuBar.add(file);
// Connect panel with port selection
JPanel connect = new JPanel(new GridLayout(1, 3));
con = new JButton("Connect");
con.setActionCommand("connect");
con.addActionListener(this);
ref = new JButton("Refrest");
ref.setActionCommand("refresh");
ref.addActionListener(this);
connect.add(portb);
connect.add(ref);
connect.add(con);
output = new JTextArea();
output.setEditable(false);
scrollPane = new JScrollPane(output);
scrollPane.getVerticalScrollBar().addAdjustmentListener(this);
info = new JTextField();
info.setEditable(false);
// add middle components
JPanel center = new JPanel(new BorderLayout());
center.add(connect, BorderLayout.NORTH);
center.add(scrollPane, BorderLayout.CENTER);
// add all ohter components
setLayout(new BorderLayout());
add(menuBar, BorderLayout.NORTH);
add(center, BorderLayout.CENTER);
add(info, BorderLayout.SOUTH);
}
private void stopReading() {
if (serial != null) {
serial.stopReading();
setInfoText("Stopped reading " + serial.getPortName());
con.setText("Connect");
serial = null;
}
}
@Override
public void actionPerformed(ActionEvent e) {
switch (e.getActionCommand()) {
case "connect": {
if (serial == null) {
con.setText("Disconnect");
output.setText("");
serial = new SerialDAO(portb.getSelectedItem().toString(),
SerialPort.BAUDRATE_9600, SerialPort.DATABITS_8,
SerialPort.STOPBITS_1, SerialPort.PARITY_NONE, this);
serial.startReading();
setInfoText("Started reading " + serial.getPortName());
} else if (serial != null) {
stopReading();
}
break;
}
case "refresh": {
this.portb.setModel(new DefaultComboBoxModel(SerialPorts.getAvailablePorts()));
setInfoText("Serial ports refreshed");
break;
}
case "saveTxt": {
stopReading();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path = fc.getSelectedFile().toString();
File f;
if (!path.endsWith(".txt")) {
f = new File(path + ".txt");
} else {
f = fc.getSelectedFile();
}
txtWriter = new FileWrite(f.getAbsolutePath(), output.getText());
txtWriter.storeToFile();
break;
}
}
case "saveXml": {
stopReading();
int returnVal = fc.showSaveDialog(this);
if (returnVal == JFileChooser.APPROVE_OPTION) {
String path = fc.getSelectedFile().toString();
File f;
if (!path.endsWith(".xls")) {
f = new File(path + ".xls");
} else {
f = fc.getSelectedFile();
}
xmlWriter = new XLSWrite(f.getAbsolutePath(), output.getText());
xmlWriter.saveToFile();
break;
}
break;
}
}
}
private synchronized void setInfoText(String text) {
this.info.setText(text);
}
@Override
public void adjustmentValueChanged(AdjustmentEvent e) {
// Automaticly scrolls bottom of the page
output.select(output.getHeight() + 1000, 0);
}
@Override
public void readLinePerformed(String line) {
this.output.setText(this.output.getText() + line);
}
@Override
public void infoUpdatePerformed(String info) {
setInfoText(info);
}
}