Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-1428 - Updated the Comments Dialog to allow Shift-Enter to insert a newline at the cursor position.

dragonmacheradad94f28058
diff --git a/Ghidra/Features/Base/src/main/help/help/topics/CommentsPlugin/Comments.htm b/Ghidra/Features/Base/src/main/help/help/topics/CommentsPlugin/Comments.htm
index f3ae0b291d..677169e803 100644
--- a/Ghidra/Features/Base/src/main/help/help/topics/CommentsPlugin/Comments.htm
+++ b/Ghidra/Features/Base/src/main/help/help/topics/CommentsPlugin/Comments.htm
@@ -92,16 +92,7 @@
</OL>
</BLOCKQUOTE>
- <BLOCKQUOTE>
- <IMG src="../../shared/note.png" alt=""> <FONT size="4">If the <I><A name=
- "Comments_Option"></A>Enter accepts comment</I> checkbox is selected, you can simply press
- the <B>Enter</B> key to set the comment and close the dialog. When the checkbox is not
- selected, the <B>Enter</B> key simply adds a new line to the current comment for a multiple
- line comment.<BR>
- </FONT>
- </BLOCKQUOTE><BR>
-
-
+
<TABLE width="100%">
<TBODY>
<TR>
@@ -110,6 +101,30 @@
</TBODY>
</TABLE>
+
+ <BLOCKQUOTE>
+ <P>
+ This dialog has the following keyboard shortcut behavior:
+ </P>
+
+ <UL>
+ <LI>
+ When the <I>Enter accepts comment</I> checkbox is not selected, pressing <B>Enter</B>
+ simply adds newlines to the text; when the checkbox is selected, pressing <B>Enter</B>
+ will close the dialog and accept the current text without adding a newline.
+ </LI>
+ <LI>
+ Pressing <B>Shift-Enter</B> will always insert a newline into the text
+ </LI>
+ <LI>
+ Pressing <B>Control-Enter</B> will always close the dialog, accepting the current text
+ </LI>
+ </UL>
+ </BLOCKQUOTE>
+ <BR>
+
+
+
<H2><A name="Delete_Comments"></A><B>Deleting Comments</B></H2>
<BLOCKQUOTE>
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/comments/CommentsDialog.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/comments/CommentsDialog.java
index d71b0fc6d1..91ba90a4e2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/comments/CommentsDialog.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/comments/CommentsDialog.java
@@ -20,7 +20,8 @@ import java.awt.event.*;
import java.util.*;
import javax.swing.*;
-import javax.swing.event.*;
+import javax.swing.event.DocumentEvent;
+import javax.swing.event.DocumentListener;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
@@ -63,19 +64,13 @@ public class CommentsDialog extends DialogComponentProvider implements KeyListen
private boolean enterMode = false;
private JCheckBox enterBox = new GCheckBox("Enter accepts comment", enterMode);
{
- enterBox.addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent e) {
- enterMode = enterBox.isSelected();
- plugin.updateOptions();
- }
+ enterBox.addChangeListener(e -> {
+ enterMode = enterBox.isSelected();
+ plugin.updateOptions();
});
}
private JPopupMenu popup = new JPopupMenu();
- /**
- * Constructor
- */
CommentsDialog(CommentsPlugin plugin) {
super("Set Comments");
setHelpLocation(new HelpLocation(plugin.getName(), "Comments"));
@@ -269,16 +264,13 @@ public class CommentsDialog extends DialogComponentProvider implements KeyListen
Arrays.sort(annotations);
GComboBox<AnnotationAdapterWrapper> annotationsComboBox = new GComboBox<>(annotations);
JButton addAnnotationButton = new JButton("Add Annotation");
- addAnnotationButton.addActionListener(new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JTextArea currentTextArea = getSelectedTextArea();
- AnnotationAdapterWrapper aaw =
- (AnnotationAdapterWrapper) annotationsComboBox.getSelectedItem();
- currentTextArea.insert(aaw.getPrototypeString(),
- currentTextArea.getCaretPosition());
- currentTextArea.setCaretPosition(currentTextArea.getCaretPosition() - 1);
- }
+ addAnnotationButton.addActionListener(e -> {
+ JTextArea currentTextArea = getSelectedTextArea();
+ AnnotationAdapterWrapper aaw =
+ (AnnotationAdapterWrapper) annotationsComboBox.getSelectedItem();
+ currentTextArea.insert(aaw.getPrototypeString(),
+ currentTextArea.getCaretPosition());
+ currentTextArea.setCaretPosition(currentTextArea.getCaretPosition() - 1);
});
JPanel annoPanel = new JPanel();
annoPanel.add(addAnnotationButton);
@@ -360,30 +352,22 @@ public class CommentsDialog extends DialogComponentProvider implements KeyListen
tab.addTab(" Plate Comment ", new JScrollPane(plateField));
tab.addTab(" Repeatable Comment ", new JScrollPane(repeatableField));
- tab.addChangeListener(new ChangeListener() {
- @Override
- public void stateChanged(ChangeEvent ev) {
- chooseFocus();
- }
- });
+ tab.addChangeListener(ev -> chooseFocus());
- ActionListener addAnnotationAction = new ActionListener() {
- @Override
- public void actionPerformed(ActionEvent e) {
- JTextArea currentTextArea = getSelectedTextArea();
- for (int i = 0; i < annotations.length; i++) {
- if (annotations[i].toString().equals(e.getActionCommand())) {
- currentTextArea.insert(annotations[i].getPrototypeString(),
- currentTextArea.getCaretPosition());
- currentTextArea.setCaretPosition(currentTextArea.getCaretPosition() - 1);
- }
+ ActionListener addAnnotationAction = e -> {
+ JTextArea currentTextArea = getSelectedTextArea();
+ for (AnnotationAdapterWrapper annotation : annotations) {
+ if (annotation.toString().equals(e.getActionCommand())) {
+ currentTextArea.insert(annotation.getPrototypeString(),
+ currentTextArea.getCaretPosition());
+ currentTextArea.setCaretPosition(currentTextArea.getCaretPosition() - 1);
}
}
};
JMenu insertMenu = new JMenu("Insert");
- for (int i = 0; i < annotations.length; i++) {
- JMenuItem menuItem = new JMenuItem(annotations[i].toString());
+ for (AnnotationAdapterWrapper annotation : annotations) {
+ JMenuItem menuItem = new JMenuItem(annotation.toString());
menuItem.addActionListener(addAnnotationAction);
insertMenu.add(menuItem);
}
@@ -466,14 +450,26 @@ public class CommentsDialog extends DialogComponentProvider implements KeyListen
@Override
public void keyPressed(KeyEvent e) {
JTextArea textArea = (JTextArea) e.getSource();
+ if (e.getKeyCode() != KeyEvent.VK_ENTER) {
+ return;
+ }
- if (e.getKeyCode() == KeyEvent.VK_ENTER) {
- if (e.isShiftDown() && enterMode) {
- textArea.append("\n");
- }
- else if (e.isShiftDown() != enterMode) {
- okCallback();
- }
+ int modifiers = e.getModifiersEx();
+ if ((modifiers & InputEvent.SHIFT_DOWN_MASK) == InputEvent.SHIFT_DOWN_MASK) {
+ textArea.replaceSelection("\n");
+ e.consume();
+ return;
+ }
+
+ if ((modifiers & InputEvent.CTRL_DOWN_MASK) == InputEvent.CTRL_DOWN_MASK) {
+ okCallback(); // Control-Enter allows closes the dialog
+ e.consume();
+ return;
+ }
+
+ if (enterMode) {
+ e.consume();
+ okCallback();
}
}