-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerSaveFileState.java
142 lines (125 loc) · 3.86 KB
/
ServerSaveFileState.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.io.*;
public class ServerSaveFileState extends FileChooserState
{
/*
Amber Krause
November 28, 2016
CISC 230-02
Doctor Jarvis
This class models the ServerSaveFileState file chooser.
The user may select a file to save to the shared
directory.
Class variables:
instance
the ServerSaveFileState object.
Enumeration types:
StateName
represents the buttons in the file chooser.
Constructors:
ServerRemoveFileState()
creates a new ServerSaveFileState object.
Methods:
getInstance()
accessor for instance.
execute()
launches the file chooser and returns the next NavigationState.
Modification History:
November 28, 2016
original version.
November 29, 2016
implemented the functionality of execute().
November 30, 2016
modified execute() so that the timeOfLastUpdate is updated
after a file is added.
December 4, 2016
modified execute() so that the ServerTargetNotInSharedDirectoryState
dialog is shown when the save location is not within
the shared directory.
*/
private enum StateName
{
Save,
Cancel;
} //enum StateName
private static ServerSaveFileState instance = new ServerSaveFileState();
private ServerSaveFileState()
{
super(StateName.values());
} //constructor
public static ServerSaveFileState getInstance()
{
return ServerSaveFileState.instance;
} //getInstance
public NavigationState execute()
{
//launch the file chooser and return the next NavigationState
StateName buttonClicked;
BinaryFileCopier fileCopier;
InputStream inputStream;
NavigationState nextState;
OutputStream outputStream;
String sharedDirectoryPath;
File sourceFile;
File targetFile;
String targetPath;
buttonClicked = StateName.valueOf(super.showDialog(ServerSelectDirectoryState.getInstance().getSelectedFile(), ServerSelectFileState.getInstance().getSelectedFile()));
switch(buttonClicked)
{
case Save:
{
sourceFile = ServerSelectFileState.getInstance().getSelectedFile();
targetFile = this.getSelectedFile();
//check if sourceFile and targetFile are the same
if(sourceFile.equals(targetFile))
{
nextState = ServerSourceAndTargetAreSameState.getInstance();
} //if
else
{
//check if targetFile is outside the shared directory
try
{
sharedDirectoryPath = ServerSelectDirectoryState.getInstance().getSelectedFile().getCanonicalPath();
targetPath = targetFile.getCanonicalPath();
} //try
catch(IOException e) { throw new RuntimeException(e.getMessage()); }
if(targetPath.indexOf(sharedDirectoryPath) < 0)
{
nextState = ServerSaveOutsideSharedDirectoryState.getInstance();
} //if
else
{
//check if targetFile already exists
if(targetFile.exists())
{
nextState = ServerOverwriteExistingFileState.getInstance();
} //if
else
{
//copy the source file to the target file
try
{
fileCopier = new BinaryFileCopier();
inputStream = new FileInputStream(sourceFile);
outputStream = new FileOutputStream(targetFile);
fileCopier.copyFile(inputStream, outputStream, 10000);
inputStream.close();
outputStream.close();
ServerSelectDirectoryState.getInstance().updateTimeOfLastUpdate();
} //try
catch(Exception e)
{
throw new RuntimeException("ServerSaveFileState.execute: File could not be copied: " + e);
} //catch
nextState = ServerStopServerState.getInstance();
} //else
} //else
} //else
break;
} //case Save
case Cancel: { nextState = ServerStopServerState.getInstance(); break; }
default: { throw new IllegalArgumentException("ServerSaveFileState.execute: no execution statement has been written for this case"); }
} //switch
return nextState;
} //execute
} //class ServerSaveFileState