diff --git a/commander/pom.xml b/commander/pom.xml index 42bb88f3..437a64bf 100644 --- a/commander/pom.xml +++ b/commander/pom.xml @@ -43,6 +43,12 @@ 4.0.18 test + + org.testfx + openjfx-monocle + jdk-12.0.1+2 + test + diff --git a/commander/src/main/java/hse/java/commander/MainController.java b/commander/src/main/java/hse/java/commander/MainController.java index 5de3b66d..13d23f1d 100644 --- a/commander/src/main/java/hse/java/commander/MainController.java +++ b/commander/src/main/java/hse/java/commander/MainController.java @@ -3,8 +3,9 @@ import javafx.fxml.FXML; import javafx.scene.control.Button; import javafx.scene.control.ListView; - -import java.nio.file.Path; +import java.lang.Throwable; +import java.nio.file.*; +import java.util.stream.Stream; public class MainController { @@ -14,32 +15,149 @@ public class MainController { @FXML public ListView right; + @FXML + public Button copy; + @FXML public Button move; - private Path leftDir; - private Path rightDir; + @FXML + public Button delete; + + private Path leftPath; + private Path rightPath; + private ListView Panel; // for testing public void setInitialDirs(Path leftStart, Path rightStart) { - this.leftDir = leftStart; - this.rightDir = rightStart; + this.leftPath = leftStart; + this.rightPath = rightStart; + update(); } public void initialize() { - move.setOnMouseClicked(event -> { + if (leftPath == null) { + leftPath = Paths.get(System.getProperty("user.home")); + } + if (rightPath == null) { + rightPath = Paths.get(System.getProperty("user.home")); + } + Panel = left; + update(); + left.setOnMouseClicked(e -> { + Panel = left; + if (e.getClickCount() == 2) { + open(left); + } }); - System.out.println(System.getProperty("user.home")); - left.getItems().add("Kek"); - - left.setOnMouseClicked(event -> { - if (event.getClickCount() == 2) { - int index = left.getSelectionModel().getSelectedIndex(); - if (index >= 0) { - left.getItems().set(index, "clicked"); - } + + right.setOnMouseClicked(e -> { + Panel = right; + if (e.getClickCount() == 2) { + open(right); } }); + + copy.setOnAction(e -> copy()); + move.setOnAction(e -> move()); + delete.setOnAction(e -> delete()); + } + + private void dir(ListView panel, Path path) { + panel.getItems().clear(); + panel.getItems().add("..."); + try (Stream f = Files.list(path)) { + f.forEach(p -> panel.getItems().add(p.getFileName().toString())); + } catch (Throwable e) {} + } + + private void update() { + dir(left, leftPath); + dir(right, rightPath); + } + + private void open(ListView panel) { + String name = panel.getSelectionModel().getSelectedItem(); + if (name == null) return; + + Path curr = panel == left ? leftPath : rightPath; + + if (name.equals("...")) { + Path par = curr.getParent(); + if (par != null) { + if (panel == left) { + leftPath = par; + } else { + rightPath = par; + } + update(); + } + return; + } + Path next = curr.resolve(name); + if (Files.isDirectory(next)) { + if (panel == left) { + leftPath = next; + } else { + rightPath = next; + } + update(); + } + } + + @FXML + public void copy() { + if (Panel == null) return; + + String name = Panel.getSelectionModel().getSelectedItem(); + if (name == null || name.equals("...")) return; + + Path srcPath = (Panel == left) ? leftPath : rightPath; + Path dstPath = (Panel == left) ? rightPath : leftPath; + + Path src = srcPath.resolve(name); + Path dst = dstPath.resolve(name); + + try { + Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING); + } catch (Throwable e) {} + + update(); + } + + @FXML + public void move() { + if (Panel == null) return; + + String name = Panel.getSelectionModel().getSelectedItem(); + if (name == null || name.equals("...")) return; + + Path srcPath = (Panel == left) ? leftPath : rightPath; + Path dstPath = (Panel == left) ? rightPath : leftPath; + + Path src = srcPath.resolve(name); + Path dst = dstPath.resolve(name); + + try { + Files.move(src, dst, StandardCopyOption.REPLACE_EXISTING); + } catch (Throwable e) {} + update(); + } + + @FXML + public void delete() { + if (Panel == null) return; + + String name = Panel.getSelectionModel().getSelectedItem(); + if (name == null || name.equals("...")) return; + + Path dir = (Panel == left) ? leftPath : rightPath; + Path file = dir.resolve(name); + + try { + Files.deleteIfExists(file); + } catch (Throwable e) {} + update(); } -} +} \ No newline at end of file