Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions commander/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@
<version>4.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.testfx</groupId>
<artifactId>openjfx-monocle</artifactId>
<version>jdk-12.0.1+2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
152 changes: 135 additions & 17 deletions commander/src/main/java/hse/java/commander/MainController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -14,32 +15,149 @@ public class MainController {
@FXML
public ListView<String> 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<String> 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<String> panel, Path path) {
panel.getItems().clear();
panel.getItems().add("...");
try (Stream<Path> 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<String> 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();
}
}
}
Loading