-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileOperations.java
72 lines (60 loc) · 1.97 KB
/
FileOperations.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
import java.awt.*;
import java.awt.event.*;
import java.awt.datatransfer.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.event.*;
import javax.swing.text.*;
import java.io.*;
public class FileOperations {
public static void newFile(TextEditorFrame parent) {
parent.setCurrentFile(null);
parent.clearTextArea();
}
public static void openFile(TextEditorFrame parent) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(parent);
File fileToOpen = new File(chooser.getSelectedFile().getAbsolutePath());
parent.setCurrentFile(fileToOpen);
}
public static void saveFile(TextEditorFrame parent) {
if (parent.getCurrentFile() == null) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save File");
if (chooser.showDialog(parent, "Save") != JFileChooser.APPROVE_OPTION) {
return;
}
parent.setCurrentFile(chooser.getSelectedFile());
}
try {
BufferedWriter b = new BufferedWriter(new FileWriter(parent.getCurrentFile()));
b.write(parent.getTextArea().getText());
b.close();
}
catch (IOException ex) {
}
}
public static void saveFileAs(TextEditorFrame parent) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogTitle("Save File");
if (chooser.showDialog(parent, "Save") != JFileChooser.APPROVE_OPTION) {
return;
}
parent.setCurrentFile(chooser.getSelectedFile());
try {
BufferedWriter b = new BufferedWriter(new FileWriter(parent.getCurrentFile()));
b.write(parent.getTextArea().getText());
b.close();
}
catch (IOException ex) {
}
}
public static void newWindow() {
TextEditorFrame newFrame = new TextEditorFrame("JPad");
newFrame.setVisible(true);
Driver.incrementFrameCount();
}
public static void closeWindow(TextEditorFrame parent) {
parent.dispatchEvent(new WindowEvent(parent, WindowEvent.WINDOW_CLOSING));
}
}