From 3b5728de6f2d8d2852fbb86fa8cac21e6dccc19b Mon Sep 17 00:00:00 2001 From: Stefan Suhren Date: Mon, 4 May 2015 22:11:28 +0200 Subject: Build a file browser looking like Finder --- .../java2/aufgabe05/view/DirectoryTableView.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryTableView.java (limited to 'src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryTableView.java') diff --git a/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryTableView.java b/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryTableView.java new file mode 100644 index 0000000..0b76632 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryTableView.java @@ -0,0 +1,88 @@ +/** + * + */ + +package de.fhswf.in.inf.java2.aufgabe05.view; + +import java.io.File; +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +import javafx.beans.property.SimpleObjectProperty; +import javafx.collections.FXCollections; +import javafx.scene.control.TableColumn; +import javafx.scene.control.TableView; +import javafx.scene.control.cell.PropertyValueFactory; +import de.fhswf.fbin.java2fx.entities.FXFile; +import de.fhswf.fbin.java2fx.tables.CheckBoxTableCellFactory; +import de.fhswf.fbin.java2fx.tables.LocalDateTimeTableCellFactory; +import de.fhswf.fbin.java2fx.tables.NumberTableCellFactory; + +/** + * TODO Add comment here + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class DirectoryTableView extends TableView +{ + private SimpleObjectProperty parentDirectoryProperty; + + public SimpleObjectProperty getParentDirectoryProperty() + { + return parentDirectoryProperty; + } + + public DirectoryTableView(FXFile parentDirectory) + { + this.parentDirectoryProperty = new SimpleObjectProperty(parentDirectory); + + // name column with default cell factory + TableColumn nameCol = new TableColumn<>("Name"); + nameCol.setCellValueFactory(new PropertyValueFactory( + "name")); + + // length column with custom number cell factory + TableColumn lengthCol = new TableColumn<>("Length"); + lengthCol.setCellValueFactory(new PropertyValueFactory( + "length")); + lengthCol.setCellFactory(new NumberTableCellFactory()); + + // last modified column with custom date cell factory + TableColumn lastModifiedCol = new TableColumn<>( + "Last Modified"); + lastModifiedCol + .setCellValueFactory(new PropertyValueFactory( + "lastModified")); + lastModifiedCol + .setCellFactory(new LocalDateTimeTableCellFactory()); + + // hidden attribute column with custom checkbox cell factory + TableColumn hiddenCol = new TableColumn<>("Hidden"); + hiddenCol + .setCellValueFactory(new PropertyValueFactory( + "hidden")); + hiddenCol.setCellFactory(new CheckBoxTableCellFactory()); + + this.getColumns() + .addAll(nameCol, lengthCol, lastModifiedCol, hiddenCol); + + this.parentDirectoryProperty + .addListener((ov, oldParent, newParent) -> { + updateContent(newParent); + }); + } + + private void updateContent(FXFile newParent) + { + File[] files = newParent.getFile().listFiles(f -> (f.isFile())); + List fxFiles = new ArrayList<>(); + for (File file : files) + { + fxFiles.add(new FXFile(file)); + } + this.getItems().setAll(FXCollections.observableArrayList(fxFiles)); + + } +} -- cgit v1.2.3-70-g09d2