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 --- .../in/inf/java2/aufgabe05/MainApplication.java | 82 ++++++++++++++++++++ .../java2/aufgabe05/view/DirectoryOverview.fxml | 17 +++++ .../view/DirectoryOverviewController.java | 55 ++++++++++++++ .../java2/aufgabe05/view/DirectoryTableView.java | 88 ++++++++++++++++++++++ .../in/inf/java2/aufgabe05/view/RootLayout.fxml | 31 ++++++++ .../java2/aufgabe05/view/RootLayoutController.java | 48 ++++++++++++ 6 files changed, 321 insertions(+) create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/MainApplication.java create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverview.fxml create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverviewController.java create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryTableView.java create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayout.fxml create mode 100644 src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayoutController.java (limited to 'src/de/fhswf/in/inf/java2/aufgabe05') diff --git a/src/de/fhswf/in/inf/java2/aufgabe05/MainApplication.java b/src/de/fhswf/in/inf/java2/aufgabe05/MainApplication.java new file mode 100644 index 0000000..7978cb1 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe05/MainApplication.java @@ -0,0 +1,82 @@ +package de.fhswf.in.inf.java2.aufgabe05; + +import java.io.IOException; + +import javafx.application.Application; +import javafx.fxml.FXMLLoader; +import javafx.scene.Scene; +import javafx.scene.control.SplitPane; +import javafx.scene.layout.AnchorPane; +import javafx.scene.layout.BorderPane; +import javafx.stage.Stage; + +public class MainApplication extends Application +{ + + private Stage primaryStage; + private BorderPane rootLayout; + + @Override + public void start(Stage primaryStage) + { + this.primaryStage = primaryStage; + this.primaryStage.setTitle("ThemeChooser"); + + initRootLayout(); + + showDirectoryOverview(); + } + + private void initRootLayout() + { + try + { + // Load root layout from fxml file. + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(MainApplication.class.getResource("view/RootLayout.fxml")); + rootLayout = (BorderPane) loader.load(); + + // Show the scene containing the root layout. + Scene scene = new Scene(rootLayout); + primaryStage.setScene(scene); + + // Give the controller access to the main app. + // RootLayoutController controller = loader.getController(); + // controller.setMainApp(this); + + primaryStage.show(); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + private void showDirectoryOverview() + { + try + { + // Load person overview. + FXMLLoader loader = new FXMLLoader(); + loader.setLocation(MainApplication.class + .getResource("view/DirectoryOverview.fxml")); + AnchorPane personOverview = (AnchorPane) loader.load(); + + // Set person overview into the center of root layout. + rootLayout.setCenter(personOverview); + + // Give the controller access to the main app. + // DirectoryOverviewController controller = loader.getController(); + // controller.setMainApp(this); + } + catch (IOException e) + { + e.printStackTrace(); + } + } + + public static void main(String[] args) + { + launch(args); + } +} diff --git a/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverview.fxml b/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverview.fxml new file mode 100644 index 0000000..5c1c3ed --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverview.fxml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + + + diff --git a/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverviewController.java b/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverviewController.java new file mode 100644 index 0000000..d12efee --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe05/view/DirectoryOverviewController.java @@ -0,0 +1,55 @@ +/** + * + */ +package de.fhswf.in.inf.java2.aufgabe05.view; + +import java.io.File; + +import javafx.fxml.FXML; +import javafx.scene.control.TableView; +import javafx.scene.layout.AnchorPane; +import de.fhswf.fbin.java2fx.trees.DirectoryTreeView; + +/** + * TODO Add comment here + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class DirectoryOverviewController +{ + @FXML + private AnchorPane leftPane; + + @FXML + private AnchorPane rightPane; + + private DirectoryTreeView dirTree; + + private DirectoryTableView dirTab; + + @FXML + private void initialize() + { + dirTree = new DirectoryTreeView(); + leftPane.getChildren().add(dirTree); + AnchorPane.setTopAnchor(dirTree, 0.0); + AnchorPane.setBottomAnchor(dirTree, 0.0); + AnchorPane.setLeftAnchor(dirTree, 0.0); + AnchorPane.setRightAnchor(dirTree, 0.0); + + dirTab = new DirectoryTableView(dirTree.getRoot().getValue()); + rightPane.getChildren().add(dirTab); + AnchorPane.setTopAnchor(dirTab, 0.0); + AnchorPane.setBottomAnchor(dirTab, 0.0); + AnchorPane.setLeftAnchor(dirTab, 0.0); + AnchorPane.setRightAnchor(dirTab, 0.0); + + dirTab.setColumnResizePolicy(TableView.CONSTRAINED_RESIZE_POLICY); + + dirTree.getSelectionModel().selectedItemProperty() + .addListener((ov, oldParent, newParent) -> { + dirTab.getParentDirectoryProperty().set(newParent.getValue()); + }); + } +} 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)); + + } +} diff --git a/src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayout.fxml b/src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayout.fxml new file mode 100644 index 0000000..c060ace --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayout.fxml @@ -0,0 +1,31 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayoutController.java b/src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayoutController.java new file mode 100644 index 0000000..efc1a10 --- /dev/null +++ b/src/de/fhswf/in/inf/java2/aufgabe05/view/RootLayoutController.java @@ -0,0 +1,48 @@ +/** + * + */ +package de.fhswf.in.inf.java2.aufgabe05.view; + +import javafx.application.Application; +import javafx.application.Platform; +import javafx.fxml.FXML; +import javafx.scene.control.Alert; +import javafx.scene.control.Alert.AlertType; + + +/** + * TODO Add comment here + * + * @author $Author: $ + * @version $Revision: $, $Date: $ UTC + */ +public class RootLayoutController +{ + @FXML + private void handelBeenden() + { + Platform.exit(); + } + + @FXML + private void handelModena() + { + Application.setUserAgentStylesheet(Application.STYLESHEET_MODENA); + } + + @FXML + private void handelCaspian() + { + Application.setUserAgentStylesheet(Application.STYLESHEET_CASPIAN); + } + + @FXML + private void handelInfo() + { + Alert info = new Alert(AlertType.INFORMATION); + info.setTitle("Info über das Programm"); + info.setHeaderText("Programmierer: Stefan Suhren"); + info.setContentText("Dieses Programm wurde programmiert."); + info.show(); + } +} -- cgit v1.2.3-70-g09d2