diff --git a/.vscode/settings.json b/.vscode/settings.json index 5a1027d..b056168 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,3 +1,11 @@ { - "cSpell.words": ["Cascadia", "Lucida"] + "cSpell.words": [ + "Autoscrolls", + "Cascadia", + "flatlaf", + "formdev", + "Lucida", + "mbahler" + ], + "include": "**/*.{ts,js,php,css,scss,html,pde}" } diff --git a/mainCode/.vscode/settings.json b/mainCode/.vscode/settings.json index e953a6b..f54cd95 100644 --- a/mainCode/.vscode/settings.json +++ b/mainCode/.vscode/settings.json @@ -1,3 +1,10 @@ { - "cSpell.words": ["lstop", "tstamp"] + "cSpell.words": [ + "Autoscrolls", + "baudedit", + "flatlaf", + "formdev", + "lstop", + "tstamp" + ] } diff --git a/mainCode/baudEditPopup.pde b/mainCode/baudEditPopup.pde new file mode 100644 index 0000000..c3366ac --- /dev/null +++ b/mainCode/baudEditPopup.pde @@ -0,0 +1,120 @@ +// TODO(mbahler): Add custom baud rate read/write handler to preferences.csv +public void initDialogBaudEdit() { + dialogBaudEdit = new JDialog(dialogSettingsMain, "Baud Rate list"); + dialogBaudEdit.setSize(new Dimension(300, 300)); + dialogBaudEdit.setResizable(false); + dialogBaudEdit.setAlwaysOnTop(true); + dialogBaudEdit.setLocationRelativeTo(dialogSettingsMain); + dialogBaudEdit.setLayout(layoutBaudEdit); + dialogBaudEdit.getContentPane().setBackground(Color.WHITE); + dialogBaudEdit.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); + dialogBaudEdit.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + buttonBaudEditCancel.doClick(); //simulate cancel button click + } + } + ); + + // initialize and add text area to dialogBaudEdit + textAreaBaudEdit = new JTextArea(java.util.Arrays.toString(baudRateList).replace(",", "\n").replace("[", "").replace("]", "").replace(" ", "").trim()); + scrollPaneBaudEdit = new JScrollPane(textAreaBaudEdit); + scrollPaneBaudEdit.setPreferredSize(new Dimension(274, 226)); + textAreaBaudEdit.setBackground(Color.WHITE); + textAreaBaudEdit.setLineWrap(true); + textAreaBaudEdit.setAutoscrolls(true); + + layoutBaudEdit.putConstraint(SpringLayout.WEST, scrollPaneBaudEdit, 5, SpringLayout.WEST, dialogBaudEdit); + layoutBaudEdit.putConstraint(SpringLayout.NORTH, scrollPaneBaudEdit, 5, SpringLayout.NORTH, dialogBaudEdit); + dialogBaudEdit.add(scrollPaneBaudEdit); + + + // initialize and add OK button to dialogBaudEdit + buttonBaudEditOk = new JButton("OK"); + buttonBaudEditOk.setPreferredSize(new Dimension(60, 20)); + buttonBaudEditOk.setFocusPainted(false); + layoutBaudEdit.putConstraint(SpringLayout.EAST, buttonBaudEditOk, 0, SpringLayout.EAST, scrollPaneBaudEdit); + layoutBaudEdit.putConstraint(SpringLayout.NORTH, buttonBaudEditOk, 5, SpringLayout.SOUTH, scrollPaneBaudEdit); + dialogBaudEdit.add(buttonBaudEditOk); + buttonBaudEditOk.addActionListener( new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + /*Process TextArea Get text from textArea and append it to string array*/ + int textAreaBaudEditLineCount = textAreaBaudEdit.getLineCount(); + String[] textAreaBaudEditArray = {}; + try {// Traverse the text in the JTextArea line by line + for (int i = 0; i < textAreaBaudEditLineCount; i ++) { + int start = textAreaBaudEdit.getLineStartOffset(i); + int end = textAreaBaudEdit.getLineEndOffset(i); + //processLine(textArea.getText(start, end-start)); + // Step 1: Create a new array with length = original length + 1 + textAreaBaudEditArray = Arrays.copyOf(textAreaBaudEditArray, textAreaBaudEditArray.length + 1); + + // Step 2: Add the new element to the last index of the new array + textAreaBaudEditArray[textAreaBaudEditArray.length - 1] = textAreaBaudEdit.getText(start, end-start).trim(); + systemPrintln(textAreaBaudEdit.getText(start, end-start).trim()); + } + } + catch(BadLocationException e) { + // Handle exception as you see fit + } + //end Process TextArea + baudRateList = textAreaBaudEditArray; + newBaudRateModel = new DefaultComboBoxModel(baudRateList); // populate newBaudRateModel with textAreaBaudEdit's text + comboBoxBaudRate.setModel(newBaudRateModel); // set comboBoxBaudRate's model to currBaudRateModel + dialogSettingsMain.setEnabled(true); // enable settings window + dialogBaudEdit.setVisible(false); // hide baud edit window + systemPrintln("buttonBaudEditOk clicked @ " + millis()); // print debug statement + } + } + ); + + // initialize and add CANCEL button to dialogBaudEdit + buttonBaudEditCancel = new JButton("CANCEL"); + buttonBaudEditCancel.setPreferredSize(new Dimension(60, 20)); + buttonBaudEditCancel.setMargin(new Insets(0, 0, 0, 0)); + buttonBaudEditCancel.setFocusPainted(false); + layoutBaudEdit.putConstraint(SpringLayout.EAST, buttonBaudEditCancel, -5, SpringLayout.WEST, buttonBaudEditOk); + layoutBaudEdit.putConstraint(SpringLayout.NORTH, buttonBaudEditCancel, 5, SpringLayout.SOUTH, scrollPaneBaudEdit); + dialogBaudEdit.add(buttonBaudEditCancel); + buttonBaudEditCancel.addActionListener(new ActionListener() { + public void actionPerformed (ActionEvent actionEvent) { + /*Process TextArea Get text from textArea and append it to string array*/ + int textAreaBaudEditLineCount = textAreaBaudEdit.getLineCount(); + String[] textAreaBaudEditArray = {}; + try {// Traverse the text in the JTextArea line by line + for (int i = 0; i < textAreaBaudEditLineCount; i ++) { + int start = textAreaBaudEdit.getLineStartOffset(i); + int end = textAreaBaudEdit.getLineEndOffset(i); + //processLine(textArea.getText(start, end-start)); + // Step 1: Create a new array with length = original length + 1 + textAreaBaudEditArray = Arrays.copyOf(textAreaBaudEditArray, textAreaBaudEditArray.length + 1); + + // Step 2: Add the new element to the last index of the new array + textAreaBaudEditArray[textAreaBaudEditArray.length - 1] = textAreaBaudEdit.getText(start, end-start).trim(); + systemPrintln(textAreaBaudEdit.getText(start, end-start).trim()); + } + } + catch(BadLocationException e) { + // Handle exception as you see fit + } + //end Process TextArea + if (!textAreaBaudEditArray.equals(baudRateList)) { + textAreaBaudEdit.setText(java.util.Arrays.toString(baudRateList).replace(",", "\n").replace("[", "\n").replace("]", "\n").replace(" ", "").trim()); + } + dialogSettingsMain.setEnabled(true); //enable settings window + dialogBaudEdit.setVisible(false); //hide baud edit window + } + } + ); + + //dialogBaudEdit.pack(); + + if (dialogBaudEdit != null && textAreaBaudEdit != null && buttonBaudEditOk != null && buttonBaudEditCancel != null) { + dialogBaudEditIsInit = true; + dialogBaudEdit.setVisible(true); // show baud rate edit window + dialogSettingsMain.setEnabled(false); // disable setting window + } else { + dialogBaudEditIsInit = false; + } +} + diff --git a/mainCode/code/flatlaf-3.7.1.jar b/mainCode/code/flatlaf-3.7.1.jar new file mode 100644 index 0000000..017a2e8 Binary files /dev/null and b/mainCode/code/flatlaf-3.7.1.jar differ diff --git a/mainCode/data/editBaud.png b/mainCode/data/editBaud.png new file mode 100644 index 0000000..17983cf Binary files /dev/null and b/mainCode/data/editBaud.png differ diff --git a/mainCode/data/preferences.csv b/mainCode/data/preferences.csv index 74bcfa1..0ce8770 100644 --- a/mainCode/data/preferences.csv +++ b/mainCode/data/preferences.csv @@ -1,2 +1,2 @@ -mode,theme,font,fontSize -0,1,jetbrains-mono.regular.ttf,14.0 +mode,theme,font,fontSize,baudRateList +1,1,jetbrains-mono.regular.ttf,14.0,"[4800, 9600, 38400, 57600, 115200]" diff --git a/mainCode/data/refresh.png b/mainCode/data/refresh.png new file mode 100644 index 0000000..6ae48b5 Binary files /dev/null and b/mainCode/data/refresh.png differ diff --git a/mainCode/logData.pde b/mainCode/logData.pde index 252aaf7..7e6ed08 100644 --- a/mainCode/logData.pde +++ b/mainCode/logData.pde @@ -50,4 +50,3 @@ public void writeToFile(String data) { } } } - diff --git a/mainCode/mainCode.pde b/mainCode/mainCode.pde index a5994b9..38b1184 100644 --- a/mainCode/mainCode.pde +++ b/mainCode/mainCode.pde @@ -1,23 +1,54 @@ -import processing.serial.*; //import serial library -import com.formdev.flatlaf.FlatLightLaf; //import flatlaf light theme -import com.formdev.flatlaf.FlatDarkLaf; //import flatlaf dark theme -import java.awt.*; //import awt library -import java.awt.Font; //import awt font library -import java.awt.event.*; //import awt event library -import java.awt.event.KeyAdapter; //import awt key adapter library -import java.awt.event.KeyEvent; //import awt key event library -import java.awt.Dimension.*; //import awt dimension library -import java.awt.image.BufferedImage; //import awt buffered image library -import javax.swing.*; //import swing library -import javax.swing.event.*; //import swing event library -import javax.swing.text.*; //import swing text library +import processing.serial.Serial; // import processing Serial +import com.formdev.flatlaf.FlatLightLaf; // import flatlaf light theme +import com.formdev.flatlaf.FlatDarkLaf; // import flatlaf dark theme +import java.awt.Font; // import java Font +import java.awt.FontFormatException; // import java FontFormatException +import java.awt.event.WindowAdapter; // import java WindowAdapter +import java.awt.event.WindowEvent; // import java WindowEvent +import java.awt.event.ActionListener; // import java ActionListener +import java.awt.event.ActionEvent; // import java ActionEvent +import java.awt.event.KeyAdapter; // import java KeyAdapter +import java.awt.event.KeyEvent; // import java KeyEvent +import java.awt.event.ComponentEvent; // import java ComponentEvent +import java.awt.event.FocusListener; // import java FocusListener +import java.awt.event.FocusEvent; // import java FocusEvent +import java.awt.KeyboardFocusManager; // import java KeyBoardFocusManger +import java.awt.Dimension; // import java dimension library +import java.awt.image.BufferedImage; // import java buffered image library +import java.awt.FlowLayout; // import java FlowLayout +import java.awt.Color; // import java Color +import java.awt.Insets; // import java insets + +import javax.swing.JFrame; // import javax JFrame +import javax.swing.JPanel; // import javax JPanel +import javax.swing.JButton; // import javax JButton +import javax.swing.JDialog; // import javax JDialog +import javax.swing.JTextArea; // import javax JTextArea +import javax.swing.JTextField; // import javax JTextField +import javax.swing.JScrollPane; // import javax JScrollPane +import javax.swing.JComboBox; // import javax JComboBox +import javax.swing.JCheckBox; // import javax JCheckBox +import javax.swing.JLabel; // import javax JLabel +import javax.swing.SpringLayout; // import javax SpringLayout +import javax.swing.DefaultComboBoxModel; // import javax DefaultComboBox +import javax.swing.UIManager; // import javax UIManager +import javax.swing.UnsupportedLookAndFeelException; // import javax UnsupportedLookAndFeelManager +import javax.swing.event.DocumentListener; // import javax DocumentListener +import javax.swing.event.DocumentEvent; // import javax DocumentEvent +import javax.swing.text.Highlighter; // import javax Highlighter +import javax.swing.text.DefaultHighlighter; // import javax DefaultHighlighter +import javax.swing.text.BadLocationException; // import javax BadLocationException +import javax.swing.ImageIcon; // import javax ImageIcon +import javax.swing.JFileChooser; // import javax JFileChooser + import java.io.File; //import file library import java.io.FileWriter; //import file writer library import java.util.Collections; //import collections library import java.util.Scanner; //import scanner library +import java.util.Arrays; //import arrays library -javax.swing.JFrame frame; //create instance of JFrame -java.awt.Canvas canvas; //create instance of Canvas +javax.swing.JFrame frameMainWindow; //create instance of JFrame +java.awt.Canvas canvasMainWindow; //create instance of Canvas //append text to textAreaMain public void textAreaMainMsg(String A, String MSG, String B) { @@ -185,23 +216,27 @@ public void setTheme(String theme) { // Processing setup function public void setup() { - setTheme("light"); //set software theme - icon = loadImage("icon.png"); //import software icon - bufferedIcon = convertToBufferedImage(icon); //convert PImage to BufferedImage for use as JFrame icon - frame = (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame(); - canvas = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative(); - frame.setLocation(displayWidth/2 - wndMinW/2, displayHeight/2 - wndMinH/2); - frame.setSize(wndMinW, wndMinH); - frame.remove(canvas); - frame.setMinimumSize(new Dimension(wndMinW, wndMinH)); - frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frame.setIconImage(bufferedIcon); //set custom icon - frame.setTitle(versionInfo); //set frame title - frame.setResizable(true); //allow frame resizing - frame.setVisible(true); //make frame visible + setTheme("light"); // set software theme + iconMain = loadImage("icon.png"); // import software icon + iconRefresh = loadImage("refresh.png"); // import refresh button icon + iconEditBaud = loadImage("editBaud.png"); // import edit baud rate button icon + bufferedIconMain = convertToBufferedImage(iconMain); // convert PImage to BufferedImage for use as JFrame icon + bufferedIconRefresh = convertToBufferedImage(iconRefresh); // convert PImage to BufferedImage for use as refresh button icon + bufferedIconEditBaud = convertToBufferedImage(iconEditBaud); // convert PImage to BufferedImage for use as edit baud rate button icon + frameMainWindow = (javax.swing.JFrame) ((processing.awt.PSurfaceAWT.SmoothCanvas) surface.getNative()).getFrame(); + canvasMainWindow = (processing.awt.PSurfaceAWT.SmoothCanvas) ((processing.awt.PSurfaceAWT)surface).getNative(); + frameMainWindow.setLocation(displayWidth/2 - wndMinW/2, displayHeight/2 - wndMinH/2); + frameMainWindow.setSize(wndMinW, wndMinH); + frameMainWindow.remove(canvasMainWindow); + frameMainWindow.setMinimumSize(new Dimension(wndMinW, wndMinH)); + frameMainWindow.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); + frameMainWindow.setIconImage(bufferedIconMain); // set custom icon + frameMainWindow.setTitle(versionInfo); // set frame title + frameMainWindow.setResizable(true); // allow frame resizing + frameMainWindow.setVisible(true); // make frame visible //add component listener for main frame - frame.addComponentListener(new java.awt.event.ComponentAdapter() { + frameMainWindow.addComponentListener(new java.awt.event.ComponentAdapter() { public void componentResized(ComponentEvent e) { if (mainUiInit == true) { //only run if main UI has been initialized panelMain.setBounds(0, 0, width, height); @@ -272,7 +307,7 @@ public void setup() { } textAreaMainMsg("\n", "Enter -h for help", ""); //print help message systemPrintln("Startup complete" + " @ " + millis()); -} // END setup +} // end of setup() // Processing loop function public void draw() { @@ -282,3 +317,4 @@ public void draw() { public void settings() { size(wndMinW, wndMinH); } + diff --git a/mainCode/mainWindowUI.pde b/mainCode/mainWindowUI.pde index 5098f5e..c3067ba 100644 --- a/mainCode/mainWindowUI.pde +++ b/mainCode/mainWindowUI.pde @@ -22,7 +22,7 @@ void drawPanelMain() { panelMain.setBounds(0, 0, width, height); panelMain.setBackground(Color.WHITE); panelMain.setLayout(new FlowLayout()); - frame.add(panelMain); //add panel to main frame + frameMainWindow.add(panelMain); //add panel to main frame systemPrintln("EDT panelMain = " + javax.swing.SwingUtilities.isEventDispatchThread() + " @ " + millis()); panelMain.repaint(); } @@ -288,14 +288,15 @@ void drawButtonSettings() { buttonSettings.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent actionEvent) { - if (frameSettings == null) { //if settings window has not been drawn + if (dialogSettingsMain == null) { //if settings window has not been drawn settingsUI(); //draw settings window availableCOMs = processing.serial.Serial.list(); //get available serial ports comboBoxPort.setModel(new DefaultComboBoxModel(availableCOMs)); } else { //otherwise if settings window has been drawn make it visible - frameSettings.setVisible(true); - availableCOMs = processing.serial.Serial.list();//get available serial ports - comboBoxPort.setModel(new DefaultComboBoxModel(availableCOMs)); + dialogSettingsMain.setLocationRelativeTo(frameMainWindow); // Center the settings window relative to the main frame + dialogSettingsMain.setVisible(true); // show settings window + frameMainWindow.setEnabled(false); // disable main window + availableCOMs = processing.serial.Serial.list(); // get available serial ports } systemPrintln("buttonSettings clicked" + " @ " + millis()); } diff --git a/mainCode/processCommands.pde b/mainCode/processCommands.pde index aef2dd8..0c41d30 100644 --- a/mainCode/processCommands.pde +++ b/mainCode/processCommands.pde @@ -22,14 +22,14 @@ public void processCommands() { if (enteredCommand.contains("=")) { String enteredCommandSplit = enteredCommand.split("=")[1]; if (enteredCommandSplit.equals("true")) { - frameSettings = null; //reset settings frame to force rebuild with new advanced options + dialogSettingsMain = null; //reset settings frame to force rebuild with new advanced options advancedOptions = true; //enable advanced options - setTableData(); //save advanced options to preferences table + setTableData("advanced"); //save advanced options to preferences table textAreaMainMsg("\n", "Advanced serial port options enabled.", ""); } else if (enteredCommandSplit.equals("false")) { - frameSettings = null; //reset settings frame to force rebuild with removed advanced options + dialogSettingsMain = null; //reset settings frame to force rebuild with removed advanced options advancedOptions = false;//disable advanced options - setTableData(); //save advanced options to preferences table + setTableData("advanced"); //save advanced options to preferences table textAreaMainMsg("\n", "Advanced serial port options disabled.", ""); } } else { @@ -51,12 +51,12 @@ public void processCommands() { textAreaMainMsg("\n", "Invalid command parameter. Use -tstamp=", ""); //invalid format message } } else if (enteredCommand.equals("-settings")) { //open settings window - if (frameSettings == null) { //if settings window has not been drawn + if (dialogSettingsMain == null) { //if settings window has not been drawn settingsUI(); //draw settings window availableCOMs = processing.serial.Serial.list(); //get available serial ports comboBoxPort.setModel(new DefaultComboBoxModel(availableCOMs)); } else { //otherwise if settings window has been drawn make it visible - frameSettings.setVisible(true); + dialogSettingsMain.setVisible(true); availableCOMs = processing.serial.Serial.list();//get available serial ports comboBoxPort.setModel(new DefaultComboBoxModel(availableCOMs)); } @@ -86,7 +86,7 @@ public void processCommands() { int fontIndex = int(enteredCommandSplit) - 1; if (fontIndex >= 0 && fontIndex < fontList.length) { selectedFont = fontList[fontIndex]; //set selected font - setTableData(); //save selected font to preferences table + setTableData("advanced"); //save selected font to preferences table setFont(selectedFont, selectedFontSize); //apply selected font textAreaMainMsg("\n", "Set font to " + selectedFont + ".", ""); } else { @@ -101,7 +101,7 @@ public void processCommands() { int fontSize = int(enteredCommandSplit); if (fontSize == 10 || fontSize == 12 || fontSize == 14 || fontSize == 16 || fontSize == 18) { selectedFontSize = fontSize; //set selected font size - setTableData(); //save selected font size to preferences table + setTableData("advanced"); //save selected font size to preferences table setFont(selectedFont, selectedFontSize); //apply selected font size textAreaMainMsg("\n", "Set font size to " + selectedFontSize + ".", ""); } else { @@ -111,4 +111,4 @@ public void processCommands() { textAreaMainMsg("\n", "Invalid command parameter. Use -fontsize=", ""); //invalid format message } } -} // END processCommands +} // end of processCommands() diff --git a/mainCode/processPreferences.pde b/mainCode/processPreferences.pde index 55ec2b4..cbbda82 100644 --- a/mainCode/processPreferences.pde +++ b/mainCode/processPreferences.pde @@ -9,14 +9,22 @@ public void getTableData() { advancedOptions = boolean(preferenceTable.getInt(0, "mode")); selectedFont = preferenceTable.getString(0, "font"); selectedFontSize = preferenceTable.getInt(0, "fontSize"); - setFont(selectedFont,selectedFontSize); + String tempBaudRates = preferenceTable.getString(0, "baudRateList").replace("]","").replace("[","").replace(" ","").trim(); + baudRateList = tempBaudRates.split(","); + currBaudRateModel = new DefaultComboBoxModel(baudRateList); + setFont(selectedFont, selectedFontSize); systemPrintln("getTableData complete @ " + millis()); } -public void setTableData() { - preferenceTable.setInt(0, "mode", int(advancedOptions)); //save advanced options mode to preferences table - preferenceTable.setString(0, "font", selectedFont); //save selected font to preferences table - preferenceTable.setFloat(0, "fontSize", selectedFontSize); //save selected font size to preferences table +public void setTableData(String mode) { + if (mode.equals("advanced")) { + preferenceTable.setInt(0, "mode", int(advancedOptions)); //save advanced options mode to preferences table + preferenceTable.setString(0, "font", selectedFont); //save selected font to preferences table + preferenceTable.setFloat(0, "fontSize", selectedFontSize); //save selected font size to preferences table + } else if (mode.equals("basic")) { + preferenceTable.setString(0, "baudRateList", java.util.Arrays.toString(baudRateList)); + } saveTable(preferenceTable, "data/preferences.csv"); systemPrintln("setTableData complete @ " + millis()); } + diff --git a/mainCode/processSerial.pde b/mainCode/processSerial.pde index baa6081..7bffc11 100644 --- a/mainCode/processSerial.pde +++ b/mainCode/processSerial.pde @@ -122,4 +122,3 @@ public void serialEvent(Serial p) { textAreaMainMsg("", serialInputData, ""); textAreaMain.setCaretPosition(textAreaMain.getDocument().getLength()); //set textAreaMain to autoscroll } - diff --git a/mainCode/settingsWindowUI.pde b/mainCode/settingsWindowUI.pde index 22f521e..3a2be95 100644 --- a/mainCode/settingsWindowUI.pde +++ b/mainCode/settingsWindowUI.pde @@ -1,25 +1,32 @@ //draw main ui for settings window void settingsUI() { - frameSettings = new JFrame("Settings"); - frameSettings.setSize(500, 300); - frameSettings.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); - frameSettings.setResizable(false); - frameSettings.setIconImage(bufferedIcon); - panelMainSettings = new JPanel(); - panelMainSettings.setBackground(Color.white); - panelMainSettings.setLayout(layoutSettings); + dialogSettingsMain = new JDialog(frameMainWindow, "Settings"); + dialogSettingsMain.setSize(500, 350); + dialogSettingsMain.setResizable(false); + dialogSettingsMain.setIconImage(bufferedIconMain); + dialogSettingsMain.setLocationRelativeTo(frameMainWindow); + dialogSettingsMain.getContentPane().setBackground(Color.WHITE); + dialogSettingsMain.setLayout(layoutSettings); + dialogSettingsMain.setDefaultCloseOperation(JDialog.DO_NOTHING_ON_CLOSE); //simulate cancel button click on close to reset settings values + dialogSettingsMain.addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(WindowEvent e) { + buttonCancel.doClick(); //simulate cancel button click to reset settings values and close window + } + } + ); //add components here drawPortConfig(); //draw port config section ui drawDataConfig(); //draw data config section ui drawLogConfig(); //draw data logging section ui - frameSettings.add(panelMainSettings); - frameSettings.setLocationRelativeTo(null); + drawOkCancelButtons(); //draw ok and cancel buttons //check if settings UI initialized successfully - if (frameSettings != null && panelMainSettings != null && drawPortConfigInit == true && drawDataConfigInit == true && drawLogConfigInit == true) { + if (dialogSettingsMain != null && drawPortConfigInit == true && drawDataConfigInit == true && drawLogConfigInit == true) { systemPrintln("EDT settingsUI = " + javax.swing.SwingUtilities.isEventDispatchThread() + " @ " + millis()); settingsUiInit = true; - frameSettings.setVisible(true); //make settings window visible + dialogSettingsMain.setVisible(true); // show settings window + frameMainWindow.setEnabled(false); // disable main window } else { settingsUiInit = false; } @@ -33,54 +40,54 @@ void drawPortConfig() { labelPortConfig = new JLabel("Port Configuration"); labelPortConfig.setPreferredSize(new Dimension(120, 20)); labelPortConfig.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelPortConfig, 10, SpringLayout.WEST, panelMainSettings); - layoutSettings.putConstraint(SpringLayout.NORTH, labelPortConfig, 10, SpringLayout.NORTH, panelMainSettings); - panelMainSettings.add(labelPortConfig); + layoutSettings.putConstraint(SpringLayout.WEST, labelPortConfig, 10, SpringLayout.WEST, dialogSettingsMain); + layoutSettings.putConstraint(SpringLayout.NORTH, labelPortConfig, 10, SpringLayout.NORTH, dialogSettingsMain); + dialogSettingsMain.add(labelPortConfig); //draw (Port) label labelPort = new JLabel("Port"); labelPort.setPreferredSize(new Dimension(80, 20)); labelPort.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelPort, 10, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, labelPort, 10, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, labelPort, 30, SpringLayout.NORTH, labelPortConfig); - panelMainSettings.add(labelPort); + dialogSettingsMain.add(labelPort); //draw (Baud Rate) label labelBaudRate = new JLabel("Baud Rate"); labelBaudRate.setPreferredSize(new Dimension(80, 20)); labelBaudRate.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelBaudRate, 10, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, labelBaudRate, 10, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, labelBaudRate, 40, SpringLayout.NORTH, labelPort); - panelMainSettings.add(labelBaudRate); + dialogSettingsMain.add(labelBaudRate); //draw (Parity) label labelPortParity = new JLabel("Parity"); labelPortParity.setPreferredSize(new Dimension(80, 20)); labelPortParity.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelPortParity, 10, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, labelPortParity, 10, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, labelPortParity, 40, SpringLayout.NORTH, labelBaudRate); if (advancedOptions == true) { - panelMainSettings.add(labelPortParity); + dialogSettingsMain.add(labelPortParity); } //draw (Data Bits) label labelPortDataBits = new JLabel("Data Bits"); labelPortDataBits.setPreferredSize(new Dimension(80, 20)); labelPortDataBits.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelPortDataBits, 10, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, labelPortDataBits, 10, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, labelPortDataBits, 40, SpringLayout.NORTH, labelPortParity); if (advancedOptions == true) { - panelMainSettings.add(labelPortDataBits); + dialogSettingsMain.add(labelPortDataBits); } //draw (Stop Bits) label labelPortStopBits = new JLabel("Stop Bits"); labelPortStopBits.setPreferredSize(new Dimension(80, 20)); labelPortStopBits.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelPortStopBits, 10, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, labelPortStopBits, 10, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, labelPortStopBits, 40, SpringLayout.NORTH, labelPortDataBits); if (advancedOptions == true) { - panelMainSettings.add(labelPortStopBits); + dialogSettingsMain.add(labelPortStopBits); } //draw COM port combo box @@ -89,7 +96,7 @@ void drawPortConfig() { //comboBoxPort.setSelectedIndex(0); //throws error on startup when there are no available COM ports layoutSettings.putConstraint(SpringLayout.WEST, comboBoxPort, 80, SpringLayout.WEST, labelPort); layoutSettings.putConstraint(SpringLayout.NORTH, comboBoxPort, 0, SpringLayout.NORTH, labelPort); - panelMainSettings.add(comboBoxPort); + dialogSettingsMain.add(comboBoxPort); comboBoxPort.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent actionEvent) { systemPrintln("User selected " + comboBoxPort.getSelectedItem().toString()); @@ -97,15 +104,37 @@ void drawPortConfig() { } ); + //draw COM refresh button + buttonRefreshCOMs = new JButton(); + buttonRefreshCOMs.setPreferredSize(new Dimension(20, 20)); + buttonRefreshCOMs.setIcon(new ImageIcon(bufferedIconRefresh)); + layoutSettings.putConstraint(SpringLayout.WEST, buttonRefreshCOMs, 5, SpringLayout.EAST, comboBoxPort); + layoutSettings.putConstraint(SpringLayout.NORTH, buttonRefreshCOMs, 0, SpringLayout.NORTH, comboBoxPort); + dialogSettingsMain.add(buttonRefreshCOMs); + buttonRefreshCOMs.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + if (connectedToCOM == false) { + availableCOMs = processing.serial.Serial.list(); //get available serial ports + comboBoxPort.setModel(new DefaultComboBoxModel(availableCOMs)); + systemPrintln("Available COMs:" + java.util.Arrays.toString(availableCOMs)); + systemPrintln("buttonRefreshCOMs clicked @ " + millis()); + } else { + textAreaMainMsg("\n", "Cannot refresh COM ports while connected to port.", ""); + } + } + } + ); + //draw Baud Rate combo box - comboBoxBaudRate = new JComboBox(baudRateList); + comboBoxBaudRate = new JComboBox(); comboBoxBaudRate.setPreferredSize(new Dimension(110, 20)); - comboBoxBaudRate.setSelectedIndex(2); - comboBoxBaudRate.setEditable(true); + comboBoxBaudRate.setEditable(false); layoutSettings.putConstraint(SpringLayout.WEST, comboBoxBaudRate, 80, SpringLayout.WEST, labelBaudRate); layoutSettings.putConstraint(SpringLayout.NORTH, comboBoxBaudRate, 0, SpringLayout.NORTH, labelBaudRate); - panelMainSettings.add(comboBoxBaudRate); - + newBaudRateModel = currBaudRateModel; // set newBaudRateModel to currBaudRateModel values + comboBoxBaudRate.setModel(currBaudRateModel); // set comboBoxBaudRate's model to currBaudRateModel + comboBoxBaudRate.setSelectedIndex(2); // select baudRate no.2 with default baud list it equals 9600 + dialogSettingsMain.add(comboBoxBaudRate); //add action listener to comboBoxBaudRate comboBoxBaudRate.addActionListener(new ActionListener() { @@ -117,6 +146,27 @@ void drawPortConfig() { } ); + //draw Baud Rate edit button + buttonEditBaud = new JButton(); + buttonEditBaud.setPreferredSize(new Dimension(20, 20)); + buttonEditBaud.setIcon(new ImageIcon(bufferedIconEditBaud)); + layoutSettings.putConstraint(SpringLayout.WEST, buttonEditBaud, 5, SpringLayout.EAST, comboBoxBaudRate); + layoutSettings.putConstraint(SpringLayout.NORTH, buttonEditBaud, 0, SpringLayout.NORTH, comboBoxBaudRate); + dialogSettingsMain.add(buttonEditBaud); + buttonEditBaud.addActionListener(new ActionListener() { + public void actionPerformed(ActionEvent actionEvent) { + if (dialogBaudEditIsInit == false) { + initDialogBaudEdit(); + } else { + dialogBaudEdit.setLocationRelativeTo(dialogSettingsMain); // Center the baud edit dialog relative to the settings dialog + dialogBaudEdit.setVisible(true); // show baud edit window + dialogSettingsMain.setEnabled(false); // disable settings window + } + systemPrintln("buttonEditBaud clicked @ " + millis()); + } + } + ); + //draw Parity combo box comboBoxPortParity = new JComboBox(parityList); comboBoxPortParity.setPreferredSize(new Dimension(110, 20)); @@ -124,7 +174,7 @@ void drawPortConfig() { layoutSettings.putConstraint(SpringLayout.WEST, comboBoxPortParity, 80, SpringLayout.WEST, labelPortParity); layoutSettings.putConstraint(SpringLayout.NORTH, comboBoxPortParity, 0, SpringLayout.NORTH, labelPortParity); if (advancedOptions == true) { - panelMainSettings.add(comboBoxPortParity); + dialogSettingsMain.add(comboBoxPortParity); } //add action listener to comboBoxPortParity @@ -145,7 +195,7 @@ void drawPortConfig() { layoutSettings.putConstraint(SpringLayout.WEST, comboBoxPortDataBits, 80, SpringLayout.WEST, labelPortDataBits); layoutSettings.putConstraint(SpringLayout.NORTH, comboBoxPortDataBits, 0, SpringLayout.NORTH, labelPortDataBits); if (advancedOptions == true) { - panelMainSettings.add(comboBoxPortDataBits); + dialogSettingsMain.add(comboBoxPortDataBits); } //add action listener to comboBoxPortDataBits @@ -165,7 +215,7 @@ void drawPortConfig() { layoutSettings.putConstraint(SpringLayout.WEST, comboBoxPortStopBits, 80, SpringLayout.WEST, labelPortStopBits); layoutSettings.putConstraint(SpringLayout.NORTH, comboBoxPortStopBits, 0, SpringLayout.NORTH, labelPortStopBits); if (advancedOptions == true) { - panelMainSettings.add(comboBoxPortStopBits); + dialogSettingsMain.add(comboBoxPortStopBits); } //add action listener to comboBoxPortStopBits @@ -179,84 +229,10 @@ void drawPortConfig() { } ); - //draw ok button - buttonOk = new JButton("OK"); - buttonOk.setPreferredSize(new Dimension(60, 20)); - buttonOk.setMargin(new Insets(0, 0, 0, 0)); - buttonOk.setFocusPainted(false); - layoutSettings.putConstraint(SpringLayout.EAST, buttonOk, -10, SpringLayout.EAST, panelMainSettings); - layoutSettings.putConstraint(SpringLayout.SOUTH, buttonOk, -10, SpringLayout.SOUTH, panelMainSettings); - panelMainSettings.add(buttonOk); - - //add action listener to buttonOk - buttonOk.addActionListener(new ActionListener() { - //action performed event handler - public void actionPerformed(ActionEvent actionEvent) { - if (connectedToCOM == false && portsFound == true) { - selectedPort = comboBoxPort.getSelectedItem().toString(); - selectedBaudRate = comboBoxBaudRate.getSelectedItem().toString(); - - switch (comboBoxPortParity.getSelectedItem().toString()) { - case "None": - selectedParity = 'N'; - break; - case "Even": - selectedParity = 'E'; - break; - case "Odd": - selectedParity = 'O'; - break; - case "Mark": - selectedParity = 'M'; - break; - case "Space": - selectedParity = 'S'; - break; - default: - selectedParity = 'N'; - break; - } - - selectedDataBits = int(comboBoxPortDataBits.getSelectedItem().toString()); - selectedStopBits = float(comboBoxPortStopBits.getSelectedItem().toString()); - comboBoxPortSelectedIndex = comboBoxPort.getSelectedIndex(); - if (advancedOptions == true) { - buttonConnect.setText("Disconnected-click to connect " + selectedPort + "@" + selectedBaudRate + "," + selectedParity + "," + selectedDataBits + "," + selectedStopBits); - } else { - buttonConnect.setText("Disconnected-click to connect " + selectedPort + "@" + selectedBaudRate); - } - frameSettings.setVisible(false); - } else { - frameSettings.setVisible(false); - } - systemPrintln("buttonOk clicked" + " @ " + millis()); - } - } - ); - - //draw ok button - buttonCancel = new JButton("CANCEL"); - buttonCancel.setPreferredSize(new Dimension(60, 20)); - buttonCancel.setMargin(new Insets(0, 0, 0, 0)); - buttonCancel.setFocusPainted(false); - layoutSettings.putConstraint(SpringLayout.EAST, buttonCancel, -65, SpringLayout.EAST, buttonOk); - layoutSettings.putConstraint(SpringLayout.SOUTH, buttonCancel, -10, SpringLayout.SOUTH, panelMainSettings); - panelMainSettings.add(buttonCancel); - - //add action listener to buttonCancel - buttonCancel.addActionListener(new ActionListener() { - - //action performed event handler - public void actionPerformed(ActionEvent actionEvent) { - frameSettings.setVisible(false); - systemPrintln("buttonCancel clicked" + " @ " + millis()); - } - } - ); //check if all components initialized successfully - if (labelPortConfig != null && labelPort != null && labelBaudRate != null && labelPortParity != null && labelPortDataBits != null && labelPortStopBits != null && comboBoxPort != null && comboBoxBaudRate != null && comboBoxPortParity != null && comboBoxPortDataBits != null && comboBoxPortStopBits != null && buttonOk != null && buttonCancel != null) { + if (labelPortConfig != null && labelPort != null && labelBaudRate != null && labelPortParity != null && labelPortDataBits != null && labelPortStopBits != null && comboBoxPort != null && comboBoxBaudRate != null && comboBoxPortParity != null && comboBoxPortDataBits != null && comboBoxPortStopBits != null) { drawPortConfigInit = true; systemPrintln("EDT drawPortConfig = " + javax.swing.SwingUtilities.isEventDispatchThread() + " @ " + millis()); } else { @@ -272,9 +248,9 @@ void drawDataConfig() { labelDataConfig = new JLabel("Data Configuration"); labelDataConfig.setPreferredSize(new Dimension(150, 20)); labelDataConfig.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelDataConfig, 255, SpringLayout.WEST, panelMainSettings); - layoutSettings.putConstraint(SpringLayout.NORTH, labelDataConfig, 10, SpringLayout.NORTH, panelMainSettings); - panelMainSettings.add(labelDataConfig); + layoutSettings.putConstraint(SpringLayout.WEST, labelDataConfig, 255, SpringLayout.WEST, dialogSettingsMain); + layoutSettings.putConstraint(SpringLayout.NORTH, labelDataConfig, 10, SpringLayout.NORTH, dialogSettingsMain); + dialogSettingsMain.add(labelDataConfig); //draw TimeStamp checkbox checkBoxTimeStamp = new JCheckBox("Time Stamp"); @@ -282,22 +258,16 @@ void drawDataConfig() { checkBoxTimeStamp.setFont(labelFont); checkBoxTimeStamp.setOpaque(false); checkBoxTimeStamp.setFocusPainted(false); - layoutSettings.putConstraint(SpringLayout.WEST, checkBoxTimeStamp, 255, SpringLayout.WEST, panelMainSettings); - layoutSettings.putConstraint(SpringLayout.NORTH, checkBoxTimeStamp, 40, SpringLayout.NORTH, panelMainSettings); - panelMainSettings.add(checkBoxTimeStamp); + checkBoxTimeStamp.setSelected(showTimeStamp); //set checkBoxTimeStamp to tStampIsChecked value + layoutSettings.putConstraint(SpringLayout.WEST, checkBoxTimeStamp, 255, SpringLayout.WEST, dialogSettingsMain); + layoutSettings.putConstraint(SpringLayout.NORTH, checkBoxTimeStamp, 40, SpringLayout.NORTH, dialogSettingsMain); + dialogSettingsMain.add(checkBoxTimeStamp); //add action listener to checkBoxTimeStamp checkBoxTimeStamp.addActionListener(new ActionListener() { //action performed event handler public void actionPerformed(ActionEvent actionEvent) { - if (checkBoxTimeStamp.isSelected() == true) { - showTimeStamp = true; - textAreaMainMsg("\n", "Enabled time stamp.", ""); - } else { - textAreaMainMsg("\n", "Disabled time stamp.", ""); - showTimeStamp = false; - } systemPrintln("checkBoxTimeStamp clicked" + " @ " + millis()); } } @@ -319,18 +289,18 @@ void drawLogConfig() { labelLogConfig = new JLabel("Log Configuration"); labelLogConfig.setPreferredSize(new Dimension(120, 20)); labelLogConfig.setFont(labelFont); - layoutSettings.putConstraint(SpringLayout.WEST, labelLogConfig, 255, SpringLayout.WEST, panelMainSettings); - layoutSettings.putConstraint(SpringLayout.NORTH, labelLogConfig, 100, SpringLayout.NORTH, panelMainSettings); - panelMainSettings.add(labelLogConfig); + layoutSettings.putConstraint(SpringLayout.WEST, labelLogConfig, 255, SpringLayout.WEST, dialogSettingsMain); + layoutSettings.putConstraint(SpringLayout.NORTH, labelLogConfig, 100, SpringLayout.NORTH, dialogSettingsMain); + dialogSettingsMain.add(labelLogConfig); // draw textFieldFileDir TextField textFieldFileDir = new JTextField(defaultLogDir); textFieldFileDir.setPreferredSize(new Dimension(155, 20)); textFieldFileDir.setFont(new Font("Monospaced", Font.PLAIN, 12)); textFieldFileDir.setForeground(Color.GRAY); - layoutSettings.putConstraint(SpringLayout.WEST, textFieldFileDir, 255, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, textFieldFileDir, 255, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, textFieldFileDir, 30, SpringLayout.NORTH, labelLogConfig); - panelMainSettings.add(textFieldFileDir); + dialogSettingsMain.add(textFieldFileDir); //add focus listener to textFieldFileDir textFieldFileDir.addFocusListener(new FocusListener() { @@ -362,9 +332,9 @@ void drawLogConfig() { textFieldFileName.setPreferredSize(new Dimension(155, 20)); textFieldFileName.setFont(new Font("Monospaced", Font.PLAIN, 12)); textFieldFileName.setForeground(Color.GRAY); - layoutSettings.putConstraint(SpringLayout.WEST, textFieldFileName, 255, SpringLayout.WEST, panelMainSettings); + layoutSettings.putConstraint(SpringLayout.WEST, textFieldFileName, 255, SpringLayout.WEST, dialogSettingsMain); layoutSettings.putConstraint(SpringLayout.NORTH, textFieldFileName, 30, SpringLayout.NORTH, textFieldFileDir); - panelMainSettings.add(textFieldFileName); + dialogSettingsMain.add(textFieldFileName); //add focus listener to textFieldFileName textFieldFileName.addFocusListener(new FocusListener() { @@ -399,7 +369,7 @@ void drawLogConfig() { buttonBrowse.setFocusPainted(false); layoutSettings.putConstraint(SpringLayout.WEST, buttonBrowse, 160, SpringLayout.WEST, textFieldFileName); layoutSettings.putConstraint(SpringLayout.NORTH, buttonBrowse, 0, SpringLayout.NORTH, textFieldFileDir); - panelMainSettings.add(buttonBrowse); + dialogSettingsMain.add(buttonBrowse); //add action listener to buttonBrowse buttonBrowse.addActionListener(new ActionListener() { @@ -433,7 +403,7 @@ void drawLogConfig() { buttonStartLog.setFocusPainted(false); layoutSettings.putConstraint(SpringLayout.WEST, buttonStartLog, 0, SpringLayout.WEST, buttonBrowse); layoutSettings.putConstraint(SpringLayout.NORTH, buttonStartLog, 30, SpringLayout.NORTH, buttonBrowse); - panelMainSettings.add(buttonStartLog); + dialogSettingsMain.add(buttonStartLog); //add action listener to buttonStartLog buttonStartLog.addActionListener(new ActionListener() { //action performed event handler @@ -454,7 +424,7 @@ void drawLogConfig() { buttonStopLog.setFocusPainted(false); layoutSettings.putConstraint(SpringLayout.WEST, buttonStopLog, 0, SpringLayout.WEST, buttonStartLog); layoutSettings.putConstraint(SpringLayout.NORTH, buttonStopLog, 30, SpringLayout.NORTH, buttonStartLog); - panelMainSettings.add(buttonStopLog); + dialogSettingsMain.add(buttonStopLog); //add action listener to buttonStopLog buttonStopLog.addActionListener(new ActionListener() { @@ -489,3 +459,112 @@ void drawLogConfig() { // end of drawLogConfig } + +//draw close open buttons +public void drawOkCancelButtons() { + //draw ok button + buttonOk = new JButton("OK"); + buttonOk.setPreferredSize(new Dimension(60, 20)); + buttonOk.setMargin(new Insets(0, 0, 0, 0)); + buttonOk.setFocusPainted(false); + layoutSettings.putConstraint(SpringLayout.WEST, buttonOk, 399, SpringLayout.EAST, dialogSettingsMain); + layoutSettings.putConstraint(SpringLayout.SOUTH, buttonOk, 262, SpringLayout.SOUTH, dialogSettingsMain); + dialogSettingsMain.add(buttonOk); + + //add action listener to buttonOk + buttonOk.addActionListener(new ActionListener() { + + //action performed event handler + public void actionPerformed(ActionEvent actionEvent) { + if (connectedToCOM == false && portsFound == true) { + selectedPort = comboBoxPort.getSelectedItem().toString(); + selectedBaudRate = comboBoxBaudRate.getSelectedItem().toString(); + currBaudRateModel = newBaudRateModel; // set currBaudRateModel to newBaudRateModel + comboBoxBaudRate.setModel(currBaudRateModel); // set comboBoxBaudRate model to currBaudRateModel + switch (comboBoxPortParity.getSelectedItem().toString()) { + case "None": + selectedParity = 'N'; + break; + case "Even": + selectedParity = 'E'; + break; + case "Odd": + selectedParity = 'O'; + break; + case "Mark": + selectedParity = 'M'; + break; + case "Space": + selectedParity = 'S'; + break; + default: + selectedParity = 'N'; + break; + } + + if (advancedOptions == true) { + selectedDataBits = int(comboBoxPortDataBits.getSelectedItem().toString()); + selectedStopBits = float(comboBoxPortStopBits.getSelectedItem().toString()); + comboBoxPortSelectedIndex = comboBoxPort.getSelectedIndex(); + selectedDataBitsString = comboBoxPortDataBits.getSelectedItem().toString(); + selectedStopBitsString = comboBoxPortStopBits.getSelectedItem().toString(); + selectedParityString = comboBoxPortParity.getSelectedItem().toString(); + buttonConnect.setText("Disconnected-click to connect " + selectedPort + "@" + selectedBaudRate + "," + selectedParity + "," + selectedDataBits + "," + selectedStopBits); + } else { + buttonConnect.setText("Disconnected-click to connect " + selectedPort + "@" + selectedBaudRate); + } + setTableData("basic"); + frameMainWindow.setEnabled(true); // disable main window when settings window is open + dialogSettingsMain.setVisible(false); // hide settings window + } else { + comboBoxPort.setSelectedItem(selectedPort); // reset comboBoxPort to selectedPort + comboBoxBaudRate.setSelectedItem(selectedBaudRate); // reset comboBoxBaudRate to selectedBaudRate + comboBoxBaudRate.setModel(currBaudRateModel); // set comboBoxBaudRate model to currBaudRateModel + if (advancedOptions == true) { + comboBoxPortParity.setSelectedItem(selectedParityString); // reset comboBoxPortParity to selectedParityString + comboBoxPortDataBits.setSelectedItem(selectedDataBitsString); // reset comboBoxPortDataBits to selectedDataBitsString + comboBoxPortStopBits.setSelectedItem(selectedStopBitsString); // reset comboBoxPortStopBits to selectedStopBitsString + } + frameMainWindow.setEnabled(true); // disable main window when settings window is open + dialogSettingsMain.setVisible(false); // hide settings window + } + + if (checkBoxTimeStamp.isSelected() == true) { + showTimeStamp = true; + } else { + showTimeStamp = false; + } + systemPrintln("buttonOk clicked" + " @ " + millis()); + } + } + ); + + //draw cancel button + buttonCancel = new JButton("CANCEL"); + buttonCancel.setPreferredSize(new Dimension(60, 20)); + buttonCancel.setMargin(new Insets(0, 0, 0, 0)); + buttonCancel.setFocusPainted(false); + layoutSettings.putConstraint(SpringLayout.EAST, buttonCancel, -10, SpringLayout.WEST, buttonOk); + layoutSettings.putConstraint(SpringLayout.SOUTH, buttonCancel, 262, SpringLayout.SOUTH, dialogSettingsMain); + dialogSettingsMain.add(buttonCancel); + + //add action listener to buttonCancel + buttonCancel.addActionListener(new ActionListener() { + + //action performed event handler + public void actionPerformed(ActionEvent actionEvent) { + newBaudRateModel = currBaudRateModel; // set newBaudRateModel to currBaudRateModel values + comboBoxPort.setSelectedItem(selectedPort); // reset comboBoxPort to selectedPort + comboBoxBaudRate.setModel(currBaudRateModel); // set comboBoxBaudRate model to currBaudRateModel + comboBoxBaudRate.setSelectedItem(selectedBaudRate); // reset comboBoxBaudRate to selectedBaudRate + comboBoxPortParity.setSelectedItem(selectedParityString); // reset comboBoxPortParity to selectedParityString + comboBoxPortDataBits.setSelectedItem(selectedDataBitsString); // reset comboBoxPortDataBits to selectedDataBitsString + comboBoxPortStopBits.setSelectedItem(selectedStopBitsString); // reset comboBoxPortStopBits to selectedStopBitsString + checkBoxTimeStamp.setSelected(showTimeStamp); // reset checkBoxTimeStamp to tStampIsChecked + frameMainWindow.setEnabled(true); // disable main window when settings window is open + dialogSettingsMain.setVisible(false); // hide settings window + systemPrintln("buttonCancel clicked" + " @ " + millis()); + } + } + ); +} diff --git a/mainCode/variables.pde b/mainCode/variables.pde index 55bdf0d..d77a095 100644 --- a/mainCode/variables.pde +++ b/mainCode/variables.pde @@ -1,149 +1,162 @@ -float selectedStopBits = 1.0; //serial port stop bits 1.0, 1.5, or 2.0 (1.0 is the default) -float fontSizeFloat[] = {10f, 12f, 14f, 16f, 18f}; //font size as float for textAreaMain and textFieldMain -float selectedFontSize = 14; //selected font size as float for textAreaMain and textFieldMain +float selectedStopBits = 1.0; // serial port stop bits 1.0, 1.5, or 2.0 (1.0 is the default) +float fontSizeFloat[] = {10f, 12f, 14f, 16f, 18f}; // font size as float for textAreaMain and textFieldMain +float selectedFontSize = 14; // selected font size as float for textAreaMain and textFieldMain -int lettersIndex = 0; //index for random file name letters - -int wndMinH = 500; //minimum height of main window -int wndMinW = 700; //minimum width of main window -int serialInputDataInt; -int selectedDataBits = 8; //serial port data bits 5-6-7-8 (8 is default) -int comboBoxPortSelectedIndex = 0; -int tmr1_lastMillis = 0; //tmr1 last millis reading -int prevCommandsLimit = 10; //limit of previous entered commands stored -int prevCommandsIndex = 0; //count of up key presses for previous command retrieval +int lettersIndex = 0; // index for random file name letters +int wndMinH = 500; // minimum height of main window +int wndMinW = 700; // minimum width of main window +int serialInputDataInt; // serial input data as integer for data logging +int selectedDataBits = 8; // serial port data bits 5-6-7-8 (8 is default) +int comboBoxPortSelectedIndex = 0; // index of selected port in comboBoxPort, used to check if selected port was removed while connected +int tmr1_lastMillis = 0; // tmr1 last millis reading +int prevCommandsLimit = 10; // limit of previous entered commands stored +int prevCommandsIndex = 0; // count of up key presses for previous command retrieval char selectedParity = 'N'; //serial port parity 'N' for none, 'E' for even, 'O' for odd, 'M' for mark, 'S' for space ('N' is the default) -boolean showDebugStatements = true; //if true show debug statements in console -boolean connectToCOM = false; //if connecting to com port -boolean connectedToCOM = false; //if connected to com port -boolean loggingData = false ; //if logging succeeded -boolean logDataButtonPressed = false; -boolean stopLoggingDataButtonPressed = false; -boolean dataLogPause = false; //toggles data logging pause/resume -boolean logData = false; -boolean initLogFileOk = false; //if log file successfully initialized -boolean showTimeStamp = false; -boolean portsFound = false; -boolean textAreaMainMsgIsRunning = false; -boolean textFieldSearchHasText = false; //if textFieldSearch has text other than prompt text -boolean serialPortRemoved = false; //if serial port was removed while connected -boolean logFileExists = false; //if log file already exists when creating log file - -boolean mainUiInit, settingsUiInit, drawPortConfigInit, drawDataConfigInit, drawLogConfigInit = false; //if UI has been initialized -boolean commandFound = false; //true if entered command is a valid command -boolean advancedOptions = false; //true if advanced serial port options are enabled +boolean showDebugStatements = true; // if true show debug statements in console +boolean connectToCOM = false; // if connecting to com port +boolean connectedToCOM = false; // if connected to com port +boolean loggingData = false ; // if logging succeeded +boolean logDataButtonPressed = false; +boolean stopLoggingDataButtonPressed = false; +boolean dataLogPause = false; // toggles data logging pause/resume +boolean logData = false; +boolean initLogFileOk = false; // if log file successfully initialized +boolean showTimeStamp = false; +boolean portsFound = false; +boolean textAreaMainMsgIsRunning = false; +boolean textFieldSearchHasText = false; // if textFieldSearch has text other than prompt text +boolean serialPortRemoved = false; // if serial port was removed while connected +boolean logFileExists = false; // if log file already exists when creating log file +boolean mainUiInit, settingsUiInit, dialogBaudEditIsInit, drawPortConfigInit, drawDataConfigInit, drawLogConfigInit = false; // if UI has been initialized +boolean commandFound = false; // true if entered command is a valid command +boolean advancedOptions = false; // true if advanced serial port options are enabled String versionInfo = "SerialTerminal 2.3.0"; -String selectedPort = null; // Name of selected COM port -String[] baudRateList = {"2400", "4800", "9600", "38400", "57600", "115200", "250000", "500000", "1000000", "2000000"}; +String selectedPort = null; // Name of selected COM port +String[] baudRateList = {"4800", "9600", "38400", "57600", "115200"}; String selectedBaudRate = baudRateList[2]; -String[] availableCOMs; // List of available COM ports -String oldFirstPort; +String[] availableCOMs; // List of available COM ports String serialInputData = "0"; -String textFieldFileDirInput; //Input from fileDirectoryTextField -String fileNameInput; //Input from fileNameTextField -String fileDirectory; //Directory for saving data table -String fileDirectoryReplaced; //Directory \ set to / +String textFieldFileDirInput; // Input from fileDirectoryTextField +String fileNameInput; // Input from fileNameTextField +String fileDirectory; // Directory for saving data table +String fileDirectoryReplaced; // Directory \ set to / String randomFileName; String logOutputData; String letters[] = {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}; String months[] = {"", "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"}; -String OS; //current operating system -String OsDirChar; //os specific directory character e.g.(windows dir is \)(linux dir is /) -String defaultLogDir; //os specific desktop directory +String OS; // current operating system +String OsDirChar; // os specific directory character e.g.(windows dir is \)(linux dir is /) +String defaultLogDir; // os specific desktop directory String parityList[] = {"None", "Even", "Odd", "Mark", "Space"}; String dataBitList[] = {"5", "6", "7", "8"}; String stopBitList[] = {"1.0", "1.5", "2.0"}; +String selectedParityString = parityList[0]; // String value of selected parity for display purposes +String selectedDataBitsString = dataBitList[3]; // String value of selected data bits for display purposes +String selectedStopBitsString = stopBitList[0]; // String value of selected stop bits for display purposes String serialPortList[]; -String enteredCommand = ""; //command entered in textFieldMain updates on enter press -String validCommands[] = {//list of valid commands - "-h", //help - "-v", //version - "-s", //connection status - "-a", //advanced options - "-a=true", //enable advanced options - "-a=false", //disable advanced options - "-connect", //connect - "-disconnect", //disconnect - "-clear", //clear - "-lpause", //log pause - "-lresume", //log resume - "-settings", //settings - "-tstamp", //timestamp - "-tstamp=true", //enable timestamp - "-tstamp=false", //disable timestamp - "-lstart", //log start - "-lstop", //log stop - "-font", //set font - "-font=1", //set font to Courier - "-font=2", //set font to Cascadia Code - "-font=3", //set font to JetBrains Mono (default) - "-font=4", //set font to Liberation Mono - "-fontsize", //set font size - "-fontsize=10", //set font size to 10 - "-fontsize=12", //set font size to 12 - "-fontsize=14", //set font size to 14 - "-fontsize=16", //set font size to 16 - "-fontsize=18" //set font size to 18 +String enteredCommand = ""; // command entered in textFieldMain updates on enter press +String validCommands[] = { // list of valid commands + "-h", // help + "-v", // version + "-s", // connection status + "-a", // advanced options + "-a=true", // enable advanced options + "-a=false", // disable advanced options + "-connect", // connect + "-disconnect", // disconnect + "-clear", // clear + "-lpause", // log pause + "-lresume", // log resume + "-settings", // settings + "-tstamp", // timestamp + "-tstamp=true", // enable timestamp + "-tstamp=false", // disable timestamp + "-lstart", // log start + "-lstop", // log stop + "-font", // set font + "-font=1", // set font to Courier + "-font=2", // set font to Cascadia Code + "-font=3", // set font to JetBrains Mono (default) + "-font=4", // set font to Liberation Mono + "-fontsize", // set font size + "-fontsize=10", // set font size to 10 + "-fontsize=12", // set font size to 12 + "-fontsize=14", // set font size to 14 + "-fontsize=16", // set font size to 16 + "-fontsize=18" // set font size to 18 }; //list of valid commands END -String fontList[] = {"courier-prime.regular.ttf", "cascadia.code.ttf", "jetbrains-mono.regular.ttf", "liberation-mono.regular.ttf"}; //list of available fonts for textAreaMain and textFieldMain -String selectedFont = fontList[0]; //selected font for textAreaMain and textFieldMain +String fontList[] = {"courier-prime.regular.ttf", "cascadia.code.ttf", "jetbrains-mono.regular.ttf", "liberation-mono.regular.ttf"}; // list of available fonts for textAreaMain and textFieldMain +String selectedFont = fontList[0]; // selected font for textAreaMain and textFieldMain +StringList previousEnteredCommands = new StringList(); // previous command entered in textFieldMain -StringList previousEnteredCommands = new StringList(); //previous command entered in textFieldMain - -Color buttonConnectRed = new Color(#EC4242); //red color for disconnected button +Color buttonConnectRed = new Color(#EC4242); //red color for disconnected button Color buttonConnectGreen = new Color(#3DC73D); //green color for connected button -PImage icon; //import software icon - -Font labelFont = new Font("Arial", Font.PLAIN, 12); //font for labels -Font terminalFont; -FileWriter Writer; //create object of FileWriter for data logging -int intBaudRate = int(selectedBaudRate); //integer value of selectedBaudRate for Serial constructor -processing.serial.Serial COMPort = null; //create object of Serial class - -Table preferenceTable; //preferences table +Font labelFont = new Font("Arial", Font.PLAIN, 12); // font for labels +Font terminalFont; // font for terminal +FileWriter Writer; // create object of FileWriter for data logging +int intBaudRate = int(selectedBaudRate); // integer value of selectedBaudRate for Serial constructor +processing.serial.Serial COMPort = null; // create object of Serial class +Table preferenceTable; // preferences table //Controls for main window -JPanel panelMain; //main window panel -JTextArea textAreaMain; //main window text area -JTextField textFieldMain; //main window text field -JTextField textFieldSearch; //main window search text field -JScrollPane textAreaMainScrollPane; //main window text area scroll pane -JButton buttonConnect; //main window connect button -JButton buttonClear; //main window clear button -JButton buttonSettings; //main window settings button -JButton buttonLogPauseResume; //main window log pause/resume button -Highlighter hilit; //highlighter for textAreaMain search function -Highlighter.HighlightPainter painter; //painter for textAreaMain search function +JPanel panelMain; // main window panel +JTextArea textAreaMain; // main window text area +JTextField textFieldMain; // main window text field +JTextField textFieldSearch; // main window search text field +JScrollPane textAreaMainScrollPane; // main window text area scroll pane +JButton buttonConnect; // main window connect button +JButton buttonClear; // main window clear button +JButton buttonSettings; // main window settings button +JButton buttonLogPauseResume; // main window log pause/resume button +Highlighter hilit; // highlighter for textAreaMain search function +Highlighter.HighlightPainter painter; // painter for textAreaMain search function +BufferedImage bufferedIconMain; // buffered image for software icon +PImage iconMain; //software icon + //Controls for settings window -JFrame frameSettings; //settings window frame -JLabel labelPortConfig; //settings window Port Configuration label -JLabel labelPort; //settings window Port label -JLabel labelBaudRate; //settings window Baud Rate label -JLabel labelDataConfig; //settings window Data Configuration label -JLabel labelLogConfig; //settings window Log Configuration label -JLabel labelPortParity; //settings window Port Parity label -JLabel labelPortDataBits; //settings window Port Data Bits label -JLabel labelPortStopBits; //settings window Port Stop Bits label -JPanel panelMainSettings; //settings window main panel -JComboBox comboBoxPort; //settings window Port combo box -JComboBox comboBoxBaudRate; //settings window Baud Rate combo box -JComboBox comboBoxPortParity; //settings window Port Parity combo box -JComboBox comboBoxPortDataBits; //settings window Port Data Bits combo box -JComboBox comboBoxPortStopBits; //settings window Port Stop Bits combo box -JCheckBox checkBoxTimeStamp = new JCheckBox(); //settings window Time Stamp check box Initialized here due to cli dependencies -JButton buttonOk; //settings window OK button -JButton buttonCancel; //settings window Cancel button -JButton buttonStartLog; //settings window Start Log button -JButton buttonStopLog; //settings window Stop Log button -JButton buttonBrowse; //settings window Browse button -JTextField textFieldFileName; //settings window File Name text field -JTextField textFieldFileDir; //settings window File Directory text field -SpringLayout layoutSettings = new SpringLayout(); //settings window layout manager +JLabel labelPortConfig; // settings window Port Configuration label +JLabel labelPort; // settings window Port label +JLabel labelBaudRate; // settings window Baud Rate label +JLabel labelDataConfig; // settings window Data Configuration label +JLabel labelLogConfig; // settings window Log Configuration label +JLabel labelPortParity; // settings window Port Parity label +JLabel labelPortDataBits; // settings window Port Data Bits label +JLabel labelPortStopBits; // settings window Port Stop Bits label +JDialog dialogSettingsMain; // settings window main panel +JComboBox comboBoxPort; // settings window Port combo box +JComboBox comboBoxBaudRate; // settings window Baud Rate combo box +JComboBox comboBoxPortParity; // settings window Port Parity combo box +JComboBox comboBoxPortDataBits; // settings window Port Data Bits combo box +JComboBox comboBoxPortStopBits; // settings window Port Stop Bits combo box +JCheckBox checkBoxTimeStamp = new JCheckBox(); // settings window Time Stamp check box Initialized here due to cli dependencies +JButton buttonOk; // settings window OK button +JButton buttonCancel; // settings window Cancel button +JButton buttonStartLog; // settings window Start Log button +JButton buttonStopLog; // settings window Stop Log button +JButton buttonBrowse; // settings window Browse button +JButton buttonRefreshCOMs; // settings window refresh COMs button Icon should be a refresh symbol +JButton buttonEditBaud; // settings window edit baud rate button Icon should be ... or a pencil +JTextField textFieldFileName; // settings window File Name text field +JTextField textFieldFileDir; // settings window File Directory text field +SpringLayout layoutSettings = new SpringLayout(); // settings window layout manager +BufferedImage bufferedIconRefresh; // buffered image for refresh button icon +BufferedImage bufferedIconEditBaud; // buffered image for edit baud rate button icon +DefaultComboBoxModel currBaudRateModel = new DefaultComboBoxModel(baudRateList); // current model for comboBoxBaudRate +DefaultComboBoxModel newBaudRateModel = new DefaultComboBoxModel(); // new model for comboBoxBaudRate is defined on baudEdit OK, set on settings OK +PImage iconRefresh; // serial port refresh button icon +PImage iconEditBaud; // serial port custom baud rate icon + +//Controls for baud edit window +JDialog dialogBaudEdit; // baud rate edit popup dialog +JTextArea textAreaBaudEdit; // dialogBaudEdit dialog text area +JScrollPane scrollPaneBaudEdit; // dialogBaudEdit textArea scroll pane +JButton buttonBaudEditOk; // dialogBaudEdit dialog confirm button +JButton buttonBaudEditCancel; // dialogBaudEdit dialog cancel button +SpringLayout layoutBaudEdit = new SpringLayout(); // dialogBaudEdit dialog layout manager -BufferedImage bufferedIcon; //buffered image for icon