001// License: GPL. For details, see LICENSE file. 002package org.openstreetmap.josm.gui.widgets; 003 004import java.awt.event.FocusEvent; 005import java.util.ArrayList; 006import java.util.HashSet; 007import java.util.List; 008import java.util.Set; 009 010import javax.swing.Action; 011import javax.swing.text.Document; 012 013import org.openstreetmap.josm.actions.JosmAction; 014import org.openstreetmap.josm.tools.Pair; 015import org.openstreetmap.josm.tools.Shortcut; 016 017/** 018 * A JTextField that disabled all JOSM shortcuts composed of a single key without modifier (except F1 to F12), 019 * in order to avoid them to be triggered while typing. 020 * This allows to include text fields in toggle dialogs (needed for relation filter). 021 * @since 5696 022 */ 023public class DisableShortcutsOnFocusGainedTextField extends JosmTextField implements DisableShortcutsOnFocusGainedComponent { 024 025 /** 026 * Constructs a new <code>TextField</code>. A default model is created, 027 * the initial string is <code>null</code>, and the number of columns is set to 0. 028 */ 029 public DisableShortcutsOnFocusGainedTextField() { 030 // Contents can be set with parent methods 031 } 032 033 /** 034 * Constructs a new <code>TextField</code> initialized with the 035 * specified text. A default model is created and the number of columns is 0. 036 * 037 * @param text the text to be displayed, or <code>null</code> 038 */ 039 public DisableShortcutsOnFocusGainedTextField(String text) { 040 super(text); 041 } 042 043 /** 044 * Constructs a new empty <code>TextField</code> with the specified number of columns. 045 * A default model is created and the initial string is set to <code>null</code>. 046 * 047 * @param columns the number of columns to use to calculate 048 * the preferred width; if columns is set to zero, the 049 * preferred width will be whatever naturally results from the component implementation 050 */ 051 public DisableShortcutsOnFocusGainedTextField(int columns) { 052 super(columns); 053 } 054 055 /** 056 * Constructs a new <code>TextField</code> initialized with the 057 * specified text and columns. A default model is created. 058 * 059 * @param text the text to be displayed, or <code>null</code> 060 * @param columns the number of columns to use to calculate 061 * the preferred width; if columns is set to zero, the 062 * preferred width will be whatever naturally results from the component implementation 063 */ 064 public DisableShortcutsOnFocusGainedTextField(String text, int columns) { 065 super(text, columns); 066 } 067 068 /** 069 * Constructs a new <code>JTextField</code> that uses the given text 070 * storage model and the given number of columns. 071 * This is the constructor through which the other constructors feed. 072 * If the document is <code>null</code>, a default model is created. 073 * 074 * @param doc the text storage to use; if this is <code>null</code>, 075 * a default will be provided by calling the 076 * <code>createDefaultModel</code> method 077 * @param text the initial string to display, or <code>null</code> 078 * @param columns the number of columns to use to calculate 079 * the preferred width >= 0; if <code>columns</code> 080 * is set to zero, the preferred width will be whatever 081 * naturally results from the component implementation 082 * @throws IllegalArgumentException if <code>columns</code> < 0 083 */ 084 public DisableShortcutsOnFocusGainedTextField(Document doc, String text, int columns) { 085 super(doc, text, columns); 086 } 087 088 private final transient List<Pair<Action, Shortcut>> unregisteredActionShortcuts = new ArrayList<>(); 089 private final Set<JosmAction> disabledMenuActions = new HashSet<>(); 090 091 @Override 092 public void focusGained(FocusEvent e) { 093 super.focusGained(e); 094 DisableShortcutsOnFocusGainedComponent.super.focusGained(e); 095 } 096 097 @Override 098 public void focusLost(FocusEvent e) { 099 super.focusLost(e); 100 DisableShortcutsOnFocusGainedComponent.super.focusLost(e); 101 } 102 103 @Override 104 public List<Pair<Action, Shortcut>> getUnregisteredActionShortcuts() { 105 return this.unregisteredActionShortcuts; 106 } 107 108 @Override 109 public Set<JosmAction> getDisabledMenuActions() { 110 return this.disabledMenuActions; 111 } 112}