Skip to content

Commit fbb71d3

Browse files
committed
Ask about saving of the file only if the file has changes
1 parent a0de474 commit fbb71d3

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/textEditor/TextEditorFrame.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class TextEditorFrame extends JFrame implements ActionListener {
99
private JTextArea textArea;
1010
private TextAreaController controller;
1111
private JMenuItem saveItem, removeAttributes, replace, capitalize;
12+
private String oldVersionOfFile;
1213

1314
private TextEditorFrame(String filename) {
1415
setSize(1000, 500);
@@ -53,6 +54,7 @@ private TextEditorFrame(String filename) {
5354
setTitle(filename);
5455
controller = new TextAreaController(textArea, filename);
5556
controller.readFile();
57+
oldVersionOfFile = textArea.getText();
5658

5759
setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
5860

@@ -64,6 +66,10 @@ public void windowOpened(WindowEvent windowEvent) {
6466

6567
@Override
6668
public void windowClosing(WindowEvent windowEvent) {
69+
if (oldVersionOfFile.equals(textArea.getText())) {
70+
windowEvent.getComponent().setVisible(false);
71+
return;
72+
}
6773
int closing = JOptionPane.showConfirmDialog(windowEvent.getComponent(),
6874
"Do you want to save the file?", "Exit", JOptionPane.YES_NO_CANCEL_OPTION);
6975
if (closing == 0) {

0 commit comments

Comments
 (0)