From abb0be66202c25e971e274b603f75783a68ca1de Mon Sep 17 00:00:00 2001 From: Wolf Posdorfer Date: Sun, 6 Oct 2019 20:49:41 +0200 Subject: [PATCH] #26 Fix sorting of sizeColumn change rowmodel.size to long instead of preformatting bytes in model add cellfactory to format strings --- .../nsusbloader/Controllers/NSLRowModel.java | 13 +++++------ .../Controllers/NSTableViewController.java | 22 ++++++++++++++++++- 2 files changed, 26 insertions(+), 9 deletions(-) diff --git a/src/main/java/nsusbloader/Controllers/NSLRowModel.java b/src/main/java/nsusbloader/Controllers/NSLRowModel.java index baa592e..0e7c9be 100644 --- a/src/main/java/nsusbloader/Controllers/NSLRowModel.java +++ b/src/main/java/nsusbloader/Controllers/NSLRowModel.java @@ -9,19 +9,14 @@ public class NSLRowModel { private String status; private File nspFile; private String nspFileName; - private String nspFileSize; + private long nspFileSize; private boolean markForUpload; NSLRowModel(File nspFile, boolean checkBoxValue){ this.nspFile = nspFile; this.markForUpload = checkBoxValue; this.nspFileName = nspFile.getName(); - if (nspFile.length()/1024.0/1024.0/1024.0 > 1) - this.nspFileSize = String.format("%.2f", nspFile.length()/1024.0/1024.0/1024.0)+" GB"; - else if (nspFile.length()/1024.0/1024.0 > 1) - this.nspFileSize = String.format("%.2f", nspFile.length()/1024.0/1024.0)+" MB"; - else - this.nspFileSize = String.format("%.2f", nspFile.length()/1024.0)+" kB"; + this.nspFileSize = nspFile.length(); this.status = ""; } // Model methods start @@ -31,7 +26,9 @@ public class NSLRowModel { public String getNspFileName(){ return nspFileName; } - public String getNspFileSize() { return nspFileSize; } + public long getNspFileSize() { + return nspFileSize; + } public boolean isMarkForUpload() { return markForUpload; } diff --git a/src/main/java/nsusbloader/Controllers/NSTableViewController.java b/src/main/java/nsusbloader/Controllers/NSTableViewController.java index a0dbec3..abf299e 100644 --- a/src/main/java/nsusbloader/Controllers/NSTableViewController.java +++ b/src/main/java/nsusbloader/Controllers/NSTableViewController.java @@ -53,7 +53,7 @@ public class NSTableViewController implements Initializable { TableColumn statusColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Status")); TableColumn fileNameColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_FileName")); - TableColumn fileSizeColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Size")); + TableColumn fileSizeColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Size")); TableColumn uploadColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Upload")); statusColumn.setEditable(false); @@ -93,6 +93,16 @@ public class NSTableViewController implements Initializable { }); uploadColumn.setCellFactory(paramFeatures -> new CheckBoxTableCell<>()); + fileSizeColumn.setCellFactory(col -> new TableCell() { + @Override + protected void updateItem(Long length, boolean empty) { + if (length == null || empty) { + setText(""); + } else { + setText(formatByteSize(length)); + } + } + }); table.setRowFactory( // this shit is made to implement context menu. It's such a pain.. nslRowModelTableView -> { final TableRow row = new TableRow<>(); @@ -208,5 +218,15 @@ public class NSTableViewController implements Initializable { rowsObsLst.removeIf(current -> ! current.getNspFileName().toLowerCase().endsWith("nsp")); table.refresh(); } + + + private String formatByteSize(long length) { + final String[] unitNames = { "bytes", "KiB", "MiB", "GiB", "TiB"}; + int i; + for (i = 0; length > 1024 && i < unitNames.length - 1; i++) { + length = length / 1024; + } + return String.format("%,d %s", length, unitNames[i]); + } } \ No newline at end of file