From ff56a353178ef435fd361a8a73c2d6f51a44d8f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20Th=C3=A0nh=20=C4=90=E1=BA=A1t?= Date: Mon, 6 Apr 2026 01:16:28 -0400 Subject: [PATCH] Implement Undo/Redo functionality in CheckUpPage's notes field using UndoManager. Add keyboard shortcuts for undo and redo actions, and ensure undo history is cleared when loading new content or templates. This enhancement improves user experience by allowing easy text editing and management. --- .../ui/component/CheckUpPage/CheckUpPage.java | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/main/java/BsK/client/ui/component/CheckUpPage/CheckUpPage.java b/src/main/java/BsK/client/ui/component/CheckUpPage/CheckUpPage.java index bce317f..a3ea807 100644 --- a/src/main/java/BsK/client/ui/component/CheckUpPage/CheckUpPage.java +++ b/src/main/java/BsK/client/ui/component/CheckUpPage/CheckUpPage.java @@ -132,6 +132,7 @@ import BsK.common.packet.res.UploadCheckupImageResponse; import BsK.common.packet.res.UploadCheckupPdfResponse; import BsK.common.util.date.DateUtils; import javax.swing.text.JTextComponent; +import javax.swing.undo.UndoManager; import java.nio.file.DirectoryStream; import java.util.Collections; @@ -194,6 +195,7 @@ public class CheckUpPage extends JPanel { private JTextField checkupIdField, customerLastNameField, customerFirstNameField,customerAddressField, customerPhoneField, customerIdField, customerCccdDdcnField; private JTextArea suggestionField, diagnosisField, conclusionField; // Changed symptomsField to suggestionField private JTextPane notesField; + private UndoManager notesUndoManager; private JComboBox doctorComboBox, ultrasoundDoctorComboBox; private JComboBox statusComboBox, genderComboBox, provinceComboBox, wardComboBox, checkupTypeComboBox, templateComboBox, orientationComboBox; // Added orientationComboBox private JCheckBox needRecheckupCheckbox; // Checkbox to indicate if re-checkup is needed @@ -1180,6 +1182,30 @@ public class CheckUpPage extends JPanel { // Set preferred size for notes field notesField.setPreferredSize(new Dimension(0, 400)); // Make it tall + // Add UndoManager + notesUndoManager = new UndoManager(); + notesField.getDocument().addUndoableEditListener(notesUndoManager); + + notesField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Z, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Undo"); + notesField.getActionMap().put("Undo", new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + if (notesUndoManager.canUndo()) { + notesUndoManager.undo(); + } + } + }); + + notesField.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_Y, Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()), "Redo"); + notesField.getActionMap().put("Redo", new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + if (notesUndoManager.canRedo()) { + notesUndoManager.redo(); + } + } + }); + // Enhanced Toolbar for notes with more formatting options JToolBar notesToolbar = new JToolBar(JToolBar.HORIZONTAL); @@ -4863,6 +4889,11 @@ public class CheckUpPage extends JPanel { // Load RTF content directly without any conversion notesField.getEditorKit().read(new ByteArrayInputStream(rtfContent.getBytes("ISO-8859-1")), notesField.getDocument(), 0); + + // Clear undo history after loading new content + if (notesUndoManager != null) { + notesUndoManager.discardAllEdits(); + } } catch (Exception e) { log.error("Error setting RTF content from string", e); } @@ -5028,6 +5059,11 @@ public class CheckUpPage extends JPanel { if (template.getContent() != null && !template.getContent().isEmpty()) { rtfEditorKit.read(new StringReader(template.getContent()), notesField.getDocument(), 0); } + + // Clear undo history after loading template + if (notesUndoManager != null) { + notesUndoManager.discardAllEdits(); + } } catch (Exception e) { log.error("Failed to apply RTF content from template '{}'", template.getTemplateName(), e); // Fallback to plain text or show an error