Skip to content
This repository has been archived by the owner on Sep 15, 2023. It is now read-only.

Commit

Permalink
feat: limit user feedback text to 2000 characters
Browse files Browse the repository at this point in the history
  • Loading branch information
wglanzer committed Jul 17, 2023
1 parent dcdeaab commit 56973af
Showing 1 changed file with 45 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import javax.swing.*;
import javax.swing.border.*;
import javax.swing.event.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.*;
import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.*;
Expand Down Expand Up @@ -51,6 +51,7 @@ public class SendBugReportAction extends AbstractAction
.setNameFormat("tBugReport-%d")
.build());
private static final String DLG_SEND_DATA_OPTION = NbBundle.getMessage(SendBugReportAction.class, "NAME_SendBugReportDialog_SendData");
private static final int FEEDBACK_TEXT_LIMIT = 2000;
private final TemporaryFileProvider temporaryFileProvider = new TemporaryFileProvider();

public SendBugReportAction()
Expand Down Expand Up @@ -235,6 +236,7 @@ private void initComponents()

// Lower comment section for user input
commentArea = new JTextArea();
((AbstractDocument) commentArea.getDocument()).setDocumentFilter(new DocumentSizeFilter(FEEDBACK_TEXT_LIMIT));
tlu.add(0, 4, new JScrollPane(commentArea));

// Contact information
Expand Down Expand Up @@ -364,4 +366,46 @@ public void changedUpdate(DocumentEvent e)
}
}

/**
* Filter, that only allows a specific amount of text.
* Exceeding text will be truncated.
*/
@RequiredArgsConstructor
private static class DocumentSizeFilter extends DocumentFilter
{
/**
* Character limit
*/
private final int maxCharacters;

@Override
public void insertString(FilterBypass fb, int offset, String str, AttributeSet attr) throws BadLocationException
{
if ((fb.getDocument().getLength() + str.length()) <= maxCharacters)
super.insertString(fb, offset, str, attr);
else
{
// Handle the case where the inserted text would exceed the character limit.
int charactersToInsert = maxCharacters - fb.getDocument().getLength();
if (charactersToInsert > 0)
super.insertString(fb, offset, str.substring(0, charactersToInsert), attr);
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text, AttributeSet attrs) throws BadLocationException
{
if ((fb.getDocument().getLength() + text.length() - length) <= maxCharacters)
super.replace(fb, offset, length, text, attrs);
else
{
// Handle the case where the replacement text would exceed the character limit.
int charactersToInsert = maxCharacters - fb.getDocument().getLength() + length;
if (charactersToInsert > 0)
super.replace(fb, offset, length, text.substring(0, charactersToInsert), attrs);
}
}

}

}

0 comments on commit 56973af

Please sign in to comment.