-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathServerRemoveFileState.java
107 lines (90 loc) · 2.76 KB
/
ServerRemoveFileState.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
import java.io.*;
public class ServerRemoveFileState extends FileChooserState
{
/*
Amber Krause
November 28, 2016
CISC 230-02
Doctor Jarvis
This class models the ServerRemoveFileState file chooser.
The user may select a file from the shared directory
to remove.
Class variables:
instance
the ServerRemoveFileState object.
Enumeration types:
StateName
represents the buttons in the file chooser.
Constructors:
ServerRemoveFileState()
creates a new ServerRemoveFileState object.
Methods:
getInstance()
accessor for instance.
execute()
launches the file chooser and return 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 removed.
December 4, 2016
modified execute() so that the
ServerRemoveFileNotInSharedDirectoryState dialog is shown
when the selected file is not within the shared directory.
*/
private enum StateName
{
Delete,
Cancel;
} //enum StateName
private static ServerRemoveFileState instance = new ServerRemoveFileState();
private ServerRemoveFileState()
{
super(StateName.values());
} //constructor
public static ServerRemoveFileState getInstance()
{
return ServerRemoveFileState.instance;
} //getInstance
public NavigationState execute()
{
//launch the file chooser and return the next NavigationState
StateName buttonClicked;
NavigationState nextState;
String sharedDirectoryPath;
String targetPath;
buttonClicked = StateName.valueOf(super.showDialog(ServerSelectDirectoryState.getInstance().getSelectedFile(), null));
switch(buttonClicked)
{
case Delete:
{
//check if file is outside the shared directory
try
{
sharedDirectoryPath = ServerSelectDirectoryState.getInstance().getSelectedFile().getCanonicalPath();
targetPath = this.getSelectedFile().getCanonicalPath();
} //try
catch(IOException e) { throw new RuntimeException(e.getMessage()); }
if(targetPath.indexOf(sharedDirectoryPath) < 0)
{
nextState = ServerRemoveFileNotInSharedDirectoryState.getInstance();
} //if
else
{
//delete the file
this.getSelectedFile().delete();
ServerSelectDirectoryState.getInstance().updateTimeOfLastUpdate();
nextState = ServerStopServerState.getInstance();
} //else
break;
} //case Delete
case Cancel: { nextState = ServerStopServerState.getInstance(); break; }
default: { throw new IllegalArgumentException("ServerRemoveFileState.execute: no execution statement has been written for this case"); }
} //switch
return nextState;
} //execute
} //class ServerRemoveFileState