Skip to content

Commit

Permalink
File sharing implemented.
Browse files Browse the repository at this point in the history
  • Loading branch information
rohan23chhabra committed Oct 3, 2018
1 parent 719f7bd commit fe8b89c
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 28 deletions.
4 changes: 2 additions & 2 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion p2p.iml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_5">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
13 changes: 13 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,19 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>

<groupId>com.rohan.net</groupId>
<artifactId>p2p</artifactId>
<version>1.0-SNAPSHOT</version>
Expand Down
39 changes: 35 additions & 4 deletions src/main/java/ConnectController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
import com.jfoenix.controls.JFXTextField;
import file.FileUtils;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import net.Session;
import ui.UIUtils;

Expand All @@ -28,6 +31,8 @@ public class ConnectController {
public static final String CONNECTION_STRING =
"Send shared file data";

public static final String FILE_DOWNLOAD_PATH =
"/home/rohan/Desktop";

public void connectOnAction(ActionEvent actionEvent)
throws IOException {
Expand Down Expand Up @@ -55,9 +60,35 @@ public void setNetworkLabels() {
}

private void displayOtherSharedDirectories(File directory) {
TreeView<File> treeView = new TreeView<File>();
treeView.setRoot(FileUtils.getNodesForDirectory(directory));
UIUtils.displayTreeView(treeView, "Files shared by remote " +
"user.");
TreeView<File> fileTreeView = new TreeView<File>();
fileTreeView
.setRoot(FileUtils.getNodesForDirectory(directory));
setTreeViewOnClickListener(fileTreeView);
UIUtils.displayTreeView(fileTreeView,
"Files shared by remote " +
"user.");
}

private void setTreeViewOnClickListener(
final TreeView<File> fileTreeView) {
fileTreeView.setOnMouseClicked(
new EventHandler<MouseEvent>() {
public void handle(MouseEvent event) {
if (event.getClickCount() == 2) {
TreeItem<File> selectedItem =
fileTreeView.getSelectionModel
().getSelectedItem();
File fileToDownload = selectedItem
.getValue();
try {
FileUtils.storeFileOnDisk
(fileToDownload,
FILE_DOWNLOAD_PATH);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
}
20 changes: 3 additions & 17 deletions src/main/java/Controller.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.input.MouseEvent;
import javafx.stage.Stage;
import ui.UIUtils;

Expand Down Expand Up @@ -85,27 +87,11 @@ public void chooseActionPerformed(ActionEvent actionEvent) {
} else {
this.sharedDirectory = dir;
fileTreeView.setRoot(FileUtils.getNodesForDirectory(dir));
setChangeListener(fileTreeView);
//treeViewOnClickListener(fileTreeView);
setFileDisplayLayout();
}
}

private void setChangeListener(TreeView<File> fileTreeView) {
fileTreeView.getSelectionModel().selectedItemProperty()
.addListener(
new ChangeListener<TreeItem<File>>() {
public void changed(
ObservableValue<? extends
TreeItem<File>>
observable,
TreeItem<File> oldValue,
TreeItem<File> newValue) {

}
}
);
}

private void setFileDisplayLayout() {
UIUtils.displayTreeView(fileTreeView, "Shared files.");
}
Expand Down
33 changes: 29 additions & 4 deletions src/main/java/file/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;

import java.io.File;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FileUtils {

public static final Logger LOGGER = Logger.getLogger(FileUtils.class.getName());
public static final Logger LOGGER =
Logger.getLogger(FileUtils.class.getName());

public static File chooseDirectory(Stage stage) {
DirectoryChooser directoryChooser = new DirectoryChooser();
directoryChooser.setTitle("Select folder to share with other peers");
directoryChooser.setInitialDirectory(new File(System.getProperty("user.home")));
directoryChooser
.setTitle("Select folder to share with other peers");
directoryChooser.setInitialDirectory(
new File(System.getProperty("user.home")));

File dir = directoryChooser.showDialog(stage);
return dir;
Expand All @@ -41,4 +44,26 @@ public static void actionOnNullDirectory() {
alert.setContentText("The file is invalid.");
alert.showAndWait();
}

public static void storeFileOnDisk(File remoteFile, String
path) throws IOException {
String name = remoteFile.getName();
path = path + "/" + name;
File file = new File(path);
InputStream inputStream = null;
OutputStream outputStream = null;
try {
inputStream = new FileInputStream(remoteFile);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int length;

while ((length = inputStream.read()) > 0) {
outputStream.write(buffer, 0, length);
}
} finally {
inputStream.close();
outputStream.close();
}
}
}
Binary file modified target/classes/ConnectController.class
Binary file not shown.
Binary file removed target/classes/Controller$1.class
Binary file not shown.
Binary file modified target/classes/Controller.class
Binary file not shown.
Binary file modified target/classes/Main.class
Binary file not shown.
Binary file modified target/classes/background/ListenThread.class
Binary file not shown.
Binary file modified target/classes/core/Peer.class
Binary file not shown.
Binary file modified target/classes/core/SceneStack.class
Binary file not shown.
Binary file modified target/classes/file/FileUtils.class
Binary file not shown.
Binary file modified target/classes/ui/TreeRow.class
Binary file not shown.

0 comments on commit fe8b89c

Please sign in to comment.