Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
* @run main PasswordFieldInputMapWordTest
*/

import java.util.Arrays;
import java.util.Collection;
import java.util.TreeSet;

import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JPasswordField;
Expand Down Expand Up @@ -65,32 +69,40 @@ private static boolean setLookAndFeel(UIManager.LookAndFeelInfo laf) {
}
}

static int[] inputMapConditions = new int[] {
JComponent.WHEN_IN_FOCUSED_WINDOW,
JComponent.WHEN_FOCUSED,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
};

/**
* These are all the actions with "word" in their field name.
*/
static Collection<String> wordActions = new TreeSet<>(Arrays.asList(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would Set.of do the job?

Perhaps, List.of would be as good.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, this is updated to Set.of

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Using Set ensures there are no duplicates in the list.

DefaultEditorKit.deleteNextWordAction,
DefaultEditorKit.deletePrevWordAction,
DefaultEditorKit.beginWordAction,
DefaultEditorKit.endWordAction,
DefaultEditorKit.selectionBeginWordAction,
DefaultEditorKit.selectionEndWordAction,
DefaultEditorKit.previousWordAction,
DefaultEditorKit.nextWordAction,
DefaultEditorKit.selectionPreviousWordAction,
DefaultEditorKit.selectionNextWordAction
));

private static void runTest() {
JPasswordField field = new JPasswordField();

boolean testPassed = true;
for (int condition : new int[] {
JComponent.WHEN_IN_FOCUSED_WINDOW,
JComponent.WHEN_FOCUSED,
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
}) {
for (int condition : inputMapConditions) {
InputMap inputMap = field.getInputMap(condition);
if (inputMap.allKeys() == null) {
continue;
}
for (KeyStroke keyStroke : inputMap.allKeys()) {
Object actionBinding = inputMap.get(keyStroke);
// these are all the actions with "word" in the name:
if (actionBinding == DefaultEditorKit.deleteNextWordAction ||
actionBinding == DefaultEditorKit.deletePrevWordAction ||
actionBinding == DefaultEditorKit.beginWordAction ||
actionBinding == DefaultEditorKit.endWordAction ||
actionBinding == DefaultEditorKit.selectionBeginWordAction ||
actionBinding == DefaultEditorKit.selectionEndWordAction ||
actionBinding == DefaultEditorKit.previousWordAction ||
actionBinding == DefaultEditorKit.nextWordAction ||
actionBinding == DefaultEditorKit.selectionPreviousWordAction ||
actionBinding == DefaultEditorKit.selectionNextWordAction ) {
if (wordActions.contains(actionBinding)) {
if (testPassed) {
System.err.println("The following inputs/actions should not be available in a JPasswordField:");
}
Expand Down