Merge pull request #35 from wolfposd/master

#26 Fix sorting of sizeColumn
This commit is contained in:
Dmitry Isaenko 2019-10-13 05:10:24 +03:00 committed by GitHub
commit 6f5d02a864
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 9 deletions

View file

@ -9,19 +9,14 @@ public class NSLRowModel {
private String status; private String status;
private File nspFile; private File nspFile;
private String nspFileName; private String nspFileName;
private String nspFileSize; private long nspFileSize;
private boolean markForUpload; private boolean markForUpload;
NSLRowModel(File nspFile, boolean checkBoxValue){ NSLRowModel(File nspFile, boolean checkBoxValue){
this.nspFile = nspFile; this.nspFile = nspFile;
this.markForUpload = checkBoxValue; this.markForUpload = checkBoxValue;
this.nspFileName = nspFile.getName(); this.nspFileName = nspFile.getName();
if (nspFile.length()/1024.0/1024.0/1024.0 > 1) this.nspFileSize = nspFile.length();
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.status = ""; this.status = "";
} }
// Model methods start // Model methods start
@ -31,7 +26,9 @@ public class NSLRowModel {
public String getNspFileName(){ public String getNspFileName(){
return nspFileName; return nspFileName;
} }
public String getNspFileSize() { return nspFileSize; } public long getNspFileSize() {
return nspFileSize;
}
public boolean isMarkForUpload() { public boolean isMarkForUpload() {
return markForUpload; return markForUpload;
} }

View file

@ -53,7 +53,7 @@ public class NSTableViewController implements Initializable {
TableColumn<NSLRowModel, String> statusColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Status")); TableColumn<NSLRowModel, String> statusColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Status"));
TableColumn<NSLRowModel, String> fileNameColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_FileName")); TableColumn<NSLRowModel, String> fileNameColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_FileName"));
TableColumn<NSLRowModel, String> fileSizeColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Size")); TableColumn<NSLRowModel, Long> fileSizeColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Size"));
TableColumn<NSLRowModel, Boolean> uploadColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Upload")); TableColumn<NSLRowModel, Boolean> uploadColumn = new TableColumn<>(resourceBundle.getString("tab1_table_Lbl_Upload"));
statusColumn.setEditable(false); statusColumn.setEditable(false);
@ -93,6 +93,16 @@ public class NSTableViewController implements Initializable {
}); });
uploadColumn.setCellFactory(paramFeatures -> new CheckBoxTableCell<>()); uploadColumn.setCellFactory(paramFeatures -> new CheckBoxTableCell<>());
fileSizeColumn.setCellFactory(col -> new TableCell<NSLRowModel, Long>() {
@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.. table.setRowFactory( // this shit is made to implement context menu. It's such a pain..
nslRowModelTableView -> { nslRowModelTableView -> {
final TableRow<NSLRowModel> row = new TableRow<>(); final TableRow<NSLRowModel> row = new TableRow<>();
@ -208,5 +218,15 @@ public class NSTableViewController implements Initializable {
rowsObsLst.removeIf(current -> ! current.getNspFileName().toLowerCase().endsWith("nsp")); rowsObsLst.removeIf(current -> ! current.getNspFileName().toLowerCase().endsWith("nsp"));
table.refresh(); table.refresh();
} }
private String formatByteSize(double 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("%,.2f %s", length, unitNames[i]);
}
} }