Skip to content

Commit

Permalink
Users can now retrieve resource to a specific directory
Browse files Browse the repository at this point in the history
  • Loading branch information
itsMatoosh committed May 9, 2018
1 parent 218aba7 commit af5346c
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import me.matoosh.undernet.event.EventHandler;
import me.matoosh.undernet.event.EventManager;
import me.matoosh.undernet.event.client.ClientExceptionEvent;
import me.matoosh.undernet.event.resource.retrieve.ResourceRetrieveFinalStopEvent;
import me.matoosh.undernet.event.router.RouterStatusEvent;
import me.matoosh.undernet.event.server.ServerExceptionEvent;
import me.matoosh.undernet.standalone.UnderNetStandalone;
Expand Down Expand Up @@ -245,16 +244,6 @@ public void onEventCalled(Event e) {
}
}, ServerExceptionEvent.class);

//Resource retrieve dialog.
EventManager.registerHandler(new EventHandler() {
@Override
public void onEventCalled(Event e) {
ResourceRetrieveFinalStopEvent resourceRetrieveFinalStopEvent = (ResourceRetrieveFinalStopEvent)e;
//Showing the published network id.
JOptionPane.showMessageDialog(AppFrame.this, String.format("Retrieved resource %s, \nNetwork id: %s", resourceRetrieveFinalStopEvent.resource, resourceRetrieveFinalStopEvent.resource.networkID));
}
}, ResourceRetrieveFinalStopEvent.class);

add(connectButton, new GridBagConstraints(0, 1, 3, 1, 1, 0.05, GridBagConstraints.FIRST_LINE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import me.matoosh.undernet.event.resource.retrieve.ResourceRetrieveFinalStopEvent;
import me.matoosh.undernet.p2p.router.data.NetworkID;
import me.matoosh.undernet.p2p.router.data.resource.FileResource;
import me.matoosh.undernet.standalone.ui.AppFrame;

import javax.swing.*;
import java.awt.*;
Expand All @@ -23,12 +24,6 @@
*/

public class PullResourceDialog extends JDialog {

/**
* Directory in which the resource will be saved.
*/
public File resourceSaveDir;

public PullResourceDialog(JFrame parent) {
//Setting the title of the dialog.
super(parent, "Pull Resource", true);
Expand Down Expand Up @@ -62,6 +57,7 @@ private void addContent() {
constraints2.gridx = 0;
constraints2.gridy = 1;
JButton fileChooser = new JButton("Save to...");
final File[] saveFile = {null};
fileChooser.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent actionEvent) {
Expand All @@ -72,7 +68,7 @@ public void actionPerformed(ActionEvent actionEvent) {
fileChooser.setDialogTitle("Choose the save directory");
int result = fileChooser.showOpenDialog(PullResourceDialog.this);
if(result == JFileChooser.APPROVE_OPTION) {
resourceSaveDir = fileChooser.getSelectedFile();
saveFile[0] = fileChooser.getSelectedFile();
}
}
});
Expand All @@ -99,16 +95,24 @@ public void onEventCalled(Event e) {
if(finalStopEvent.resource.networkID.equals(netId) && finalStopEvent.resource.getResourceType() == 0) {
FileResource fileResource = (FileResource)finalStopEvent.resource;

//Copying the pulled file to the save dir.
if(resourceSaveDir.isFile()) {
resourceSaveDir = resourceSaveDir.getParentFile();
}

try {
Files.copy(Paths.get(fileResource.fileInfo.toString()), Paths.get(resourceSaveDir.toString(), fileResource.fileInfo.fileName));
} catch (IOException e1) {
e1.printStackTrace();
//Copying the received file to dest.
if(saveFile[0] != null) {
try {
Files.copy(Paths.get(UnderNet.fileManager.getContentFolder().toString() + "/" + fileResource.fileInfo.fileName), Paths.get(saveFile[0].getAbsolutePath() + "/" + fileResource.fileInfo.fileName));
} catch (IOException e1) {
e1.printStackTrace();
}

//File dialog.
JOptionPane.showMessageDialog(null, String.format("Retrieved file %s! \nNetwork id: %s \nSaved to: %s", fileResource.fileInfo.fileName, finalStopEvent.resource.networkID.getStringValue(), saveFile[0]));
} else {
//File dialog.
JOptionPane.showMessageDialog(null, String.format("Retrieved file %s! \nNetwork id: %s \nSaved to: %s", fileResource.fileInfo.fileName, finalStopEvent.resource.networkID.getStringValue(), UnderNet.fileManager.getContentFolder()));
}
} else {
//Dialog
ResourceRetrieveFinalStopEvent resourceRetrieveFinalStopEvent = (ResourceRetrieveFinalStopEvent)e;
JOptionPane.showMessageDialog(AppFrame.contentPanel, String.format("Retrieved resource! \nNetwork id: %s", resourceRetrieveFinalStopEvent.resource.networkID));
}

//Unregistering the handler.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public void actionPerformed(ActionEvent actionEvent) {
clipboard.setContents(stringSelection, null);

//Showing the published network id.
JOptionPane.showMessageDialog(UploadResourceDialog.this.frame, String.format("Publishing %s, \nNetwork id: %s \nThe network id has been copied to your clipboard.", fileChooseResult, fileResource.networkID));
JOptionPane.showMessageDialog(UploadResourceDialog.this.frame, String.format("Publishing %s, \nNetwork id: %s \nThe network id has been copied to your clipboard.", fileChooseResult, fileResource.networkID.getStringValue()));
}
}
});
Expand Down

0 comments on commit af5346c

Please sign in to comment.