From 1cfba5fbb35d98b95f6462d301f7e7292a397d81 Mon Sep 17 00:00:00 2001 From: Alessandro Ferro <49845537+xfarrow@users.noreply.github.com> Date: Fri, 4 Aug 2023 17:30:39 +0200 Subject: [PATCH] bugfix Fixed a bug showing "UNSAVED" at the mere opening of a file --- Guify/src/views/Notepad.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/Guify/src/views/Notepad.java b/Guify/src/views/Notepad.java index 62ebf1a..fb4efef 100644 --- a/Guify/src/views/Notepad.java +++ b/Guify/src/views/Notepad.java @@ -28,12 +28,12 @@ public class Notepad extends JFrame implements INotepadFrame { private static final long serialVersionUID = 1L; private NotepadController controller; + private boolean jtextAreaShouldListenForChanges = true; + private JTextArea textArea; + /** * Create the application. */ - - JTextArea textArea; - public Notepad(Object controller) { this.controller = (NotepadController) controller; setTitle(this.controller.getFilePath()); @@ -70,7 +70,7 @@ public class Notepad extends JFrame implements INotepadFrame { } private void handleTextChange() { - if(!((NotepadController)controller).isUnsaved()) { + if(!((NotepadController)controller).isUnsaved() && jtextAreaShouldListenForChanges) { ((NotepadController)controller).setUnsaved(true); setTitle(((NotepadController)controller).getTitle()); } @@ -118,7 +118,7 @@ public class Notepad extends JFrame implements INotepadFrame { JButton searchBtn = new JButton(); searchBtn.setBorderPainted(false); - searchBtn.setBorder(new EmptyBorder(0, 0, 0, 0)); // Set empty border); + searchBtn.setBorder(new EmptyBorder(0, 0, 0, 0)); // Set empty border; searchBtn.setToolTipText("Serch/Replace"); try { searchBtn.setIcon(new ImageIcon(ImageIO.read(getClass().getClassLoader().getResource("search_icon.png")).getScaledInstance(25, 25, Image.SCALE_SMOOTH))); @@ -166,8 +166,14 @@ public class Notepad extends JFrame implements INotepadFrame { }); } + /** + * Sets text in the JTextArea without triggering + * a text changed action + */ public void displayContent(String content) { + jtextAreaShouldListenForChanges = false; textArea.setText(content); + jtextAreaShouldListenForChanges = true; } }