Skip to content

Commit

Permalink
file filter, pio fixes and defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
tobozo committed Mar 16, 2024
1 parent bf734cd commit 97316d3
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 62 deletions.
13 changes: 8 additions & 5 deletions src/main/java/com/arduino/AppSettingsArduino.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ public AppSettingsArduino(Editor editor) {

public void init() {

// these don't need to be evaluated more than once
isWindows = PreferencesData.get("runtime.os").contentEquals("windows");
espotaCmd = "espota" + (isWindows ? ".exe" : ".py");
esptoolCmd = "esptool" + (isWindows ? ".exe" : ".py");
genEsp32PartCmd = "gen_esp32part" + (isWindows ? ".exe" : ".py");

load();

set("csvDir.path", get("sketchDir.path") );
Expand Down Expand Up @@ -70,14 +76,11 @@ public void init() {

@Override
public void load() {
// figure out what csv file is selected in the boards menu, and where it is
isWindows = PreferencesData.get("runtime.os").contentEquals("windows");
espotaCmd = "espota" + (isWindows ? ".exe" : ".py");
esptoolCmd = "esptool" + (isWindows ? ".exe" : ".py");
genEsp32PartCmd = "gen_esp32part" + (isWindows ? ".exe" : ".py");

defaultSketchbookFolder = BaseNoGui.getDefaultSketchbookFolder();
toolsPathBase = BaseNoGui.getToolsPath();
platform = BaseNoGui.getTargetPlatform();

set("sketch.name", editor.getSketch().getName() );
set("sketch.path", editor.getSketch().getMainFilePath() );
set("sketchDir.path", editor.getSketch().getFolder().getAbsolutePath() );
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/arduino/ESP32PartitionTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public String getMenuTitle() {
}

public void addUI(UI contentPane) {
contentPane.setFrame( frame );
frame.add(contentPane);
}

Expand All @@ -77,11 +78,18 @@ private void initGUI() {
}

if (fileManager == null) {

// try {
// UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
// } catch (Exception e) {
// // handle exception
// }

fileManager = new FileManager(contentPane, settings);
}

if (frame == null) {
frame = new JFrame("ESP32 Partition Tool");
frame = new JFrame("ESP32 Partition Tool (Arduino IDE)");

frame.setSize(1024, 640);
frame.setResizable(false);
Expand Down
39 changes: 33 additions & 6 deletions src/main/java/com/platformio/ESP32PartitionToolStandalone.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,33 @@ of this software and associated documentation files (the "Software"), to deal

package com.platformio;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.*;
import javax.swing.border.*;

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;

import com.serifpersia.esp32partitiontool.FileManager;
import com.serifpersia.esp32partitiontool.UI;
import com.serifpersia.esp32partitiontool.UIController;

import javax.swing.ImageIcon;

// local implementation of rounded borders to overwrite global styles
@SuppressWarnings("serial")
final class CustomBorder extends AbstractBorder {
@Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Graphics2D g2d = (Graphics2D)g;
g2d.setPaint( new Color(0xcc, 0xcc, 0xcc) );
Shape shape = new RoundRectangle2D.Float(1, 1, c.getWidth()-2, c.getHeight()-2, 5, 5);
g2d.draw(shape);
}
}


public class ESP32PartitionToolStandalone {

private UI contentPane = new UI();
Expand All @@ -64,13 +79,24 @@ public void addUI(UI contentPane) {

private void init(String[] args) {

// apply some style fixes globally

CompoundBorder borderTextField = BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(0, 0, 0, 0), new CustomBorder() );
CompoundBorder borderComboBox = BorderFactory.createCompoundBorder( BorderFactory.createEmptyBorder(2, 2, 2, 2), new CustomBorder() );

UIManager.put("TextField.background", Color.WHITE);
UIManager.put("TextField.border", borderTextField);

UIManager.put("ComboBox.background", Color.WHITE);
UIManager.put("ComboBox.border", borderComboBox);

settings = new AppSettingsStandalone(args);

fileManager = new FileManager(contentPane, settings);

// Create and show the JFrame
if (frame == null) {
frame = new JFrame("ESP32 Partition Tool");
frame = new JFrame("ESP32 Partition Tool (standalone)");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

// Size and display the frame
Expand Down Expand Up @@ -103,6 +129,7 @@ public void windowClosing(WindowEvent e) {
frame.toFront();
}

contentPane.setFrame( frame );
fileManager.loadDefaultCSV();
frame.setVisible(true);
}
Expand Down
18 changes: 15 additions & 3 deletions src/main/java/com/serifpersia/esp32partitiontool/AppSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,30 @@

public class AppSettings {

public boolean debug_settings = true;
public boolean hasFSPanel = false;
public Map<String, String> prefs = new HashMap<>();


public String get( String key ) {
return prefs.get( key );
String value = prefs.get( key );
if( debug_settings && value == null ) {
System.out.printf("settings.%s is null\n", key );
}
return value;
}

public String set( String key, String value ) {
return prefs.put( key, value );
String oldvalue = prefs.put( key, value );
if( debug_settings && oldvalue != null && value != null ) {
if( !oldvalue.equals(value) ) {
System.out.printf("Value change for settings.%s:\n [old] %s\n [new] %s\n", key, oldvalue, value );
}
}
return oldvalue;
}

// this method is meant to be overloaded by platformio or arduino AppSettings classes
// this method is meant to be overriden from platformio or arduino AppSettings classes
public void load() {

}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/serifpersia/esp32partitiontool/CSVRow.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public CSVRow(String values[]) {
sizeHex = new JTextField();
offset = new JTextField();

enabled.setOpaque(false);

// restrict the 'size' field to numbers, with the help of NumberFormat
NumberFormat format = NumberFormat.getInstance();
format.setGroupingUsed(false); // no comma separator
Expand Down
122 changes: 79 additions & 43 deletions src/main/java/com/serifpersia/esp32partitiontool/FileManager.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.serifpersia.esp32partitiontool;

import java.awt.FileDialog;
import java.awt.Frame;
import java.awt.*;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;

import javax.swing.JTextField;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.lang.Process;
import java.lang.Runtime;
import java.io.*;
Expand Down Expand Up @@ -80,32 +81,47 @@ public void loadDefaultCSV() {
public void importCSV(String file) {
File readerFile = null;

if (file == null) {
FileDialog dialog = new FileDialog((Frame) null, "Select CSV File", FileDialog.LOAD);
String directory;

if (file == null) { // show a dialog
FileDialog filedialog = new FileDialog(ui.getFrame(), "Select CSV File", FileDialog.LOAD);
FilenameFilter csvFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".csv");
}
};
filedialog.setFilenameFilter(csvFilter);

if (settings.get("csvDir.path") != null) {
System.out.println("dialog open at last CSV Dir: " + settings.get("csvDir.path") );
dialog.setDirectory( settings.get("csvDir.path") );
filedialog.setDirectory( settings.get("csvDir.path") );
}
dialog.setFile("*.csv");
dialog.setVisible(true);
String directory = dialog.getDirectory();
filedialog.setFile("*.csv");
filedialog.setAlwaysOnTop(true);
filedialog.setVisible(true);

directory = filedialog.getDirectory();
if (directory == null || directory.isEmpty())
return;
settings.set("csvDir.path", directory );
System.out.println("dialog returned CSV Dir: " + settings.get("csvDir.path") );
file = dialog.getFile();
//settings.set("csvDir.path", directory );
//System.out.println("dialog returned CSV Dir: " + settings.get("csvDir.path") );
file = filedialog.getFile();
filedialog.dispose();
if (file == null || file.isEmpty())
return;
readerFile = new File(directory, file);
} else {
readerFile = new File(file);
directory = readerFile.getParent();
// update last csvDir
// settings.set("csvDir.path", readerFile.getParent() );
}

try (BufferedReader reader = new BufferedReader(new FileReader(readerFile))) {
processCSV(reader);

settings.set("csvFile.path", basename(file) );
ui.updatePartitionLabel(settings.get("csvFile.path"));
settings.set("csvFile.path", directory + "/" + basename(file) );
ui.updatePartitionLabel( basename(file) );

} catch (IOException e) {
emitError("Error reading CSV file: " + e.getMessage());
Expand Down Expand Up @@ -230,50 +246,72 @@ private void calculateCSV() {
}
}

public boolean generateCSV() {

public boolean saveCSV( File file ) {
calculateCSV();

// Get the default directory path
try {
FileWriter writer = new FileWriter(file);
for (String partitionData : createdPartitionsData) {
writer.write(partitionData + "\n");
}
writer.close();
//System.out.println("partitions.csv written at: " + csvFile );
return true;
} catch (Exception ex) {
ex.printStackTrace();
}
return false;
}

// Export to CSV
FileDialog dialog = new FileDialog(new Frame(), "Create Partitions CSV", FileDialog.SAVE);

if (settings.get("csvFile.path") != null) {
dialog.setFile(settings.get("csvFile.path"));
// dialog.setDirectory(homeDir);
} else {
dialog.setFile("partitions.csv");

public boolean saveCSV() {
String sketchDir = settings.get("sketchDir.path");

if( sketchDir == null ) {
sketchDir = new File(FileManager.class.getProtectionDomain().getCodeSource().getLocation().getPath()).getParent();
System.out.println("Forced sketchDir to " + sketchDir );
}
dialog.setVisible(true);
String fileName = dialog.getFile();

if (fileName == null)
FilenameFilter csvFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return name.toLowerCase().endsWith(".csv");
}
};
FileDialog filedialog = new FileDialog(ui.getFrame(), "Save partitions.csv", FileDialog.SAVE);
filedialog.setDirectory(sketchDir);
filedialog.setFile("partitions.csv");
filedialog.setFilenameFilter(csvFilter);
filedialog.setVisible(true);

if (filedialog.getFile() == null) {
// aborted by user
return false;
}

String filePath = dialog.getDirectory() + fileName; // Construct the full file path
try (FileWriter writer = new FileWriter(filePath)) {
// Write the exported data to the CSV file
for (String partitionData : createdPartitionsData) {
writer.write(partitionData + "\n");
}
String csvPath = filedialog.getDirectory() + filedialog.getFile();

if (Files.notExists(Paths.get(filePath))) {
emitError("Failed to write " + filePath);
return false;
}
System.out.println("partitions.csv written at: " + filePath);
filedialog.dispose();

if( Files.notExists(Paths.get(csvPath))) {
// can't open
return false;
}

settings.set("csvFile.path", filePath );
File csvFile = new File( csvPath );

boolean success = saveCSV( csvFile );
if( success ) {
//settings.set("csvFile.path", csvFile.toString() );
//settings.set("csvDir.path", csvFile.getParent() );
return true;
} catch (IOException ex) {
emitError("Error creating CSV: " + ex.getMessage());
}
return false;
}

public boolean createSPIFFS() {

settings.load();

if( ! settings.hasFSPanel ) return false;
Expand Down Expand Up @@ -389,7 +427,6 @@ public boolean createSPIFFS() {
}

public boolean uploadSPIFFS() {

settings.load();

if( !settings.hasFSPanel ) return false;
Expand Down Expand Up @@ -464,7 +501,6 @@ public boolean uploadSPIFFS() {
}

public boolean createMergedBin() {

settings.load();

if( ! settings.hasFSPanel ) return false;
Expand Down
Loading

0 comments on commit 97316d3

Please sign in to comment.