-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileController.java
More file actions
165 lines (136 loc) · 3.97 KB
/
Copy pathFileController.java
File metadata and controls
165 lines (136 loc) · 3.97 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package controller.file;
import java.io.File;
import java.io.IOException;
import javax.swing.JFileChooser;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.filechooser.FileNameExtensionFilter;
import dao.file.LoadFile;
import dao.file.SaveFile;
import exception.file.FileException;
public class FileController {
private File file;
private LoadFile colLoadFile;
private SaveFile colSaveFile;
private int chooseType;
private boolean append;
public static final int CHOOSER_TYPE_LOAD = 1;
public static final int CHOOSER_TYPE_SAVE = 2;
//Constructors
public FileController(int chooseType, boolean append){
this.file = null;
this.colLoadFile = null;
this.colSaveFile = null;
this.chooseType = chooseType;
this.append = append;
}
//Getters and Setters
public File getFile() {
return file;
}
public LoadFile getColLoadFile() {
return colLoadFile;
}
public SaveFile getColSaveFile() {
return colSaveFile;
}
public int getChooseType() {
return this.chooseType;
}
public boolean isAppend() {
return this.append;
}
//Other functions
public boolean fileIsEmpty() {
if(getFile() == null) {
return true;
}
else {
return false;
}
}
public File chooseFile() throws ClassNotFoundException, InstantiationException, IllegalAccessException, UnsupportedLookAndFeelException, FileException, IOException{
File file = null;
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
JFileChooser fileChooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Text File", "txt");
fileChooser.setFileFilter(filter);
if(getChooseType() == CHOOSER_TYPE_LOAD) {
fileChooser.setDialogTitle("Selecione o arquivo de entrada do algoritmo");
if (fileChooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
this.file = file;
this.colLoadFile = new LoadFile(file);
return file;
}
else {
throw new FileException("No file selected.");
}
}
else if(getChooseType() == CHOOSER_TYPE_SAVE) {
fileChooser.setDialogTitle("Selecione o arquivo para salvar a saída do algoritmo");
if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
file = fileChooser.getSelectedFile();
this.file = file;
this.colSaveFile = new SaveFile(file, isAppend());
return file;
}
else {
throw new FileException("No file selected.");
}
}
else {
throw new FileException("Invalid chooser type.");
}
}
public int countLines() throws IOException, FileException {
if(!fileIsEmpty()) {
if(getColLoadFile() != null)
return getColLoadFile().countLines();
else
throw new FileException("The file choosed must be load type.");
}
else {
throw new FileException("There is no file selected.");
}
}
public String readLine() throws IOException, FileException {
if(!fileIsEmpty()) {
if(getColLoadFile() != null)
return getColLoadFile().readLine();
else
throw new FileException("The file choosed must be load type.");
}
else {
throw new FileException("There is no file selected.");
}
}
public void skipLines(int numberOfLines) throws IOException, FileException {
if(!fileIsEmpty()) {
if(getColLoadFile() != null)
getColLoadFile().skipLine(numberOfLines);
else
throw new FileException("The file choosed must be load type.");
}
else {
throw new FileException("There is no file selected.");
}
}
public void writeFile(String content) throws FileException, IOException {
if(!fileIsEmpty()) {
if(getColSaveFile() != null)
getColSaveFile().writeFile(content);
else
throw new FileException("The file choosed must be save type.");
}
else {
throw new FileException("There is no file selected.");
}
}
public void closeBuffer() throws IOException {
if(getColLoadFile() != null)
getColLoadFile().closeBuffer();
if(getColSaveFile() != null)
getColSaveFile().closeBuffer();
}
}