-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathController.java
140 lines (126 loc) · 4.31 KB
/
Controller.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
package sample;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
import javafx.scene.control.TextArea;
import javafx.scene.layout.VBox;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import java.io.*;
public class Controller {
@FXML
public TextArea textArea;
@FXML
public VBox root;
@FXML
private void handleOpen(ActionEvent event){
FileChooser fileChooser = new FileChooser();
Stage stage = (Stage) root.getScene().getWindow();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text files", "*.txt"));
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Java Files", "*.java"));
File file = fileChooser.showOpenDialog(stage);
if(file!=null){
BufferedReader bufferedReader = null;
String document = "";
String line = "";
try {
bufferedReader = new BufferedReader(new FileReader(file));
while ((line = bufferedReader.readLine()) != null){
document += line + "\n";
}
textArea.setText(document);
} catch (FileNotFoundException e) {
e.printStackTrace();
handleExceptionAlert();
} catch (IOException e) {
e.printStackTrace();
handleExceptionAlert();
} catch (Exception e){
e.printStackTrace();
handleExceptionAlert();
} finally {
if(bufferedReader!=null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
handleExceptionAlert();
}
}
}
}
}
@FXML
private void handleSave(ActionEvent event){
FileChooser fileChooser = new FileChooser();
Stage stage = (Stage) root.getScene().getWindow();
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Text files", "*.txt"));
File file = fileChooser.showSaveDialog(stage);
if (file!=null){
FileWriter writer = null;
try {
writer = new FileWriter(file);
writer.write(textArea.getText());
} catch (IOException e) {
e.printStackTrace();
handleExceptionAlert();
} catch (Exception e){
e.printStackTrace();
handleExceptionAlert();
} finally {
if(writer!=null){
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
handleExceptionAlert();
}
}
}
}
}
@FXML
public void handleExceptionAlert(){
Alert alert = new Alert(Alert.AlertType.ERROR);
alert.setTitle("Error");
alert.setContentText("Something went wrong");
alert.show();
}
@FXML
public void handleAboutAlert(){
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setTitle("About The Program");
alert.setContentText("The program was built and designed " +
"by a freshman at Computer Science Department in Ala-Too International University in 2021. " +
"The overall functionality of a program is simple and straightforward.");
alert.show();
}
@FXML
public void handleNewFile(ActionEvent event){
textArea.clear();
}
@FXML
private void handleClear(ActionEvent event){
textArea.clear();
}
@FXML
private void handlePaste(ActionEvent event){
textArea.paste();
}
@FXML
private void handleCopy(ActionEvent event){
textArea.copy();
}
@FXML
private void handleCut(ActionEvent event){
textArea.cut();
}
@FXML
private void handleUndo(ActionEvent event){
textArea.undo();
}
@FXML
private void handleRedo(ActionEvent event){
textArea.redo();
}
}