-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileController.java
More file actions
68 lines (62 loc) · 2.16 KB
/
Copy pathFileController.java
File metadata and controls
68 lines (62 loc) · 2.16 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
/*
* File: proj6BayyurtWenZhang.FileController.java
* Names: Izge Bayyurt, Muqing Wen, Chloe Zhang
* Class: CS361
* Project 6
* Date: 3/17/2022
*/
package proj6BayyurtWenZhang;
import org.fxmisc.richtext.CodeArea;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Contains helper methods for saving and opening files.
*/
public class FileController {
private final TabHelper tabHelper;
private final AlertHandler alertHandler;
public FileController(TabHelper tabHelper){
this.tabHelper = tabHelper;
this.alertHandler = new AlertHandler();
}
/**
* Writes the content of a file to the new CodeArea.
*
* @param openedFile the file to write into the CodeArea.
*/
public void writeFileToCodeArea(File openedFile){
CodeArea currentArea = this.tabHelper.getCurrentCodeArea();
currentArea.replaceText(""); // clear default "Sample text" message
try {
Scanner scan = new Scanner(new File(String
.valueOf(openedFile))).useDelimiter("\\s+");
while (scan.hasNextLine()) {
currentArea.appendText(scan.nextLine() + "\n");
}
}catch (FileNotFoundException ex) {
this.alertHandler.showAlert("The requested file was not found. Check the file" +
"exists and try again.", "File Not Found");
}
}
/**
* Writes the content of a Tab to a given file path.
* @param currentFile the File currently referenced by the Tab.
*
*/
public boolean saveCurrentFile(File currentFile){
PrintWriter outFile;
try {
outFile = new PrintWriter(String.valueOf(currentFile));
} catch (FileNotFoundException e) {
this.alertHandler.showAlert("File failed to save. Ensure a valid path is specified" +
"and try again.", "Failed to Save");
return false;
}
outFile.println(this.tabHelper.getCurrentCodeArea().getText());
outFile.close();
this.tabHelper.getCurrentTab().setText(currentFile.getName());
return true;
}
}