package nsusbloader.Controllers; import javafx.beans.property.SimpleBooleanProperty; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.fxml.FXML; import javafx.fxml.Initializable; import javafx.scene.control.Label; import javafx.scene.control.TableCell; import javafx.scene.control.TableColumn; import javafx.scene.control.TableView; import javafx.scene.control.cell.CheckBoxTableCell; import javafx.scene.control.cell.PropertyValueFactory; import javafx.util.Callback; import nsusbloader.NSLDataTypes.EFileStatus; import java.io.File; import java.net.URL; import java.util.ArrayList; import java.util.List; import java.util.ResourceBundle; public class NSTableViewController implements Initializable { @FXML private TableView table; private ObservableList rowsObsLst; private String protocol; @Override public void initialize(URL url, ResourceBundle resourceBundle) { rowsObsLst = FXCollections.observableArrayList(); table.setPlaceholder(new Label()); TableColumn statusColumn = new TableColumn<>(resourceBundle.getString("tableStatusLbl")); TableColumn fileNameColumn = new TableColumn<>(resourceBundle.getString("tableFileNameLbl")); TableColumn fileSizeColumn = new TableColumn<>(resourceBundle.getString("tableSizeLbl")); TableColumn uploadColumn = new TableColumn<>(resourceBundle.getString("tableUploadLbl")); statusColumn.setMinWidth(70.0); fileNameColumn.setMinWidth(250.0); fileSizeColumn.setMinWidth(70.0); uploadColumn.setMinWidth(70.0); statusColumn.setCellValueFactory(new PropertyValueFactory<>("status")); fileNameColumn.setCellValueFactory(new PropertyValueFactory<>("nspFileName")); fileSizeColumn.setCellValueFactory(new PropertyValueFactory<>("nspFileSize")); // >< uploadColumn.setCellValueFactory(new Callback, ObservableValue>() { @Override public ObservableValue call(TableColumn.CellDataFeatures paramFeatures) { NSLRowModel model = paramFeatures.getValue(); SimpleBooleanProperty booleanProperty = new SimpleBooleanProperty(model.isMarkForUpload()); booleanProperty.addListener(new ChangeListener() { @Override public void changed(ObservableValue observableValue, Boolean oldValue, Boolean newValue) { model.setMarkForUpload(newValue); restrictSelection(model); } }); return booleanProperty; } }); uploadColumn.setCellFactory(new Callback, TableCell>() { @Override public TableCell call(TableColumn paramFeatures) { CheckBoxTableCell cell = new CheckBoxTableCell<>(); return cell; } }); table.setItems(rowsObsLst); table.getColumns().addAll(statusColumn, fileNameColumn, fileSizeColumn, uploadColumn); /* debug content rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), true)); rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), false)); rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), false)); rowsObsLst.add(new NSLRowModel(new File("/home/loper/стихи_2"), true)); */ } /** * See uploadColumn callback. In case of GoldLeaf we have to restrict selection * */ private void restrictSelection(NSLRowModel modelChecked){ if (!protocol.equals("TinFoil") && rowsObsLst.size() > 1) { // Tinfoil doesn't need any restrictions. If only one file in list, also useless for (NSLRowModel model: rowsObsLst){ if (model != modelChecked) model.setMarkForUpload(false); } table.refresh(); } } /** * Add files when user selected them * */ public void setFiles(List files){ rowsObsLst.clear(); // TODO: consider table refresh if (files == null) { return; } if (protocol.equals("TinFoil")){ for (File nspFile: files){ rowsObsLst.add(new NSLRowModel(nspFile, true)); } } else { rowsObsLst.clear(); for (File nspFile: files){ rowsObsLst.add(new NSLRowModel(nspFile, false)); } rowsObsLst.get(0).setMarkForUpload(true); } } /** * Return files ready for upload. Requested from NSLMainController only * @return null if no files marked for upload * List if there are files * */ public List getFiles(){ List files = new ArrayList<>(); if (rowsObsLst.isEmpty()) return null; else { for (NSLRowModel model: rowsObsLst){ if (model.isMarkForUpload()) files.add(model.getNspFile()); } if (!files.isEmpty()) return files; else return null; } } /** * Update files in case something is wrong. Requested from UsbCommunications * */ public void setFileStatus(String fileName, EFileStatus status){ for (NSLRowModel model: rowsObsLst){ if (model.getNspFileName().equals(fileName)){ model.setStatus(status); } } } /** * Called if selected different USB protocol * */ public void setNewProtocol(String newProtocol){ protocol = newProtocol; if (rowsObsLst.isEmpty()) return; if (newProtocol.equals("TinFoil")){ for (NSLRowModel model: rowsObsLst) model.setMarkForUpload(true); } else { for (NSLRowModel model: rowsObsLst) model.setMarkForUpload(false); rowsObsLst.get(0).setMarkForUpload(true); } table.refresh(); } }