source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/JosmTextArea.java@ 11308

Last change on this file since 11308 was 11308, checked in by Don-vip, 7 years ago

fix #14031 - change focus with TAB in "add imagery" dialogs

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.KeyboardFocusManager;
5import java.awt.event.FocusEvent;
6import java.awt.event.FocusListener;
7
8import javax.swing.JTextArea;
9import javax.swing.text.Document;
10
11import org.openstreetmap.josm.Main;
12
13/**
14 * Subclass of {@link JTextArea} that adds a "native" context menu (cut/copy/paste/select all).
15 * @since 5886
16 */
17public class JosmTextArea extends JTextArea implements FocusListener {
18
19 /**
20 * Constructs a new {@code JosmTextArea}. A default model is set, the initial string
21 * is null, and rows/columns are set to 0.
22 */
23 public JosmTextArea() {
24 this(null, null, 0, 0);
25 }
26
27 /**
28 * Constructs a new {@code JosmTextArea} with the specified text displayed.
29 * A default model is created and rows/columns are set to 0.
30 *
31 * @param text the text to be displayed, or null
32 */
33 public JosmTextArea(String text) {
34 this(null, text, 0, 0);
35 }
36
37 /**
38 * Constructs a new {@code JosmTextArea} with the given document model, and defaults
39 * for all of the other arguments (null, 0, 0).
40 *
41 * @param doc the model to use
42 */
43 public JosmTextArea(Document doc) {
44 this(doc, null, 0, 0);
45 }
46
47 /**
48 * Constructs a new empty {@code JosmTextArea} with the specified number of
49 * rows and columns. A default model is created, and the initial
50 * string is null.
51 *
52 * @param rows the number of rows >= 0
53 * @param columns the number of columns >= 0
54 * @throws IllegalArgumentException if the rows or columns
55 * arguments are negative.
56 */
57 public JosmTextArea(int rows, int columns) {
58 this(null, null, rows, columns);
59 }
60
61 /**
62 * Constructs a new {@code JosmTextArea} with the specified text and number
63 * of rows and columns. A default model is created.
64 *
65 * @param text the text to be displayed, or null
66 * @param rows the number of rows >= 0
67 * @param columns the number of columns >= 0
68 * @throws IllegalArgumentException if the rows or columns
69 * arguments are negative.
70 */
71 public JosmTextArea(String text, int rows, int columns) {
72 this(null, text, rows, columns);
73 }
74
75 /**
76 * Constructs a new {@code JosmTextArea} with the specified number of rows
77 * and columns, and the given model. All of the constructors
78 * feed through this constructor.
79 *
80 * @param doc the model to use, or create a default one if null
81 * @param text the text to be displayed, null if none
82 * @param rows the number of rows >= 0
83 * @param columns the number of columns >= 0
84 * @throws IllegalArgumentException if the rows or columns
85 * arguments are negative.
86 */
87 public JosmTextArea(Document doc, String text, int rows, int columns) {
88 super(doc, text, rows, columns);
89 TextContextualPopupMenu.enableMenuFor(this, true);
90 addFocusListener(this);
91 }
92
93 /**
94 * Restore default behaviour of focus transfer with TAB, overriden by {@link JTextArea}.
95 * @return {@code this}
96 * @since 11308
97 */
98 public JosmTextArea transferFocusOnTab() {
99 // http://stackoverflow.com/a/525867/2257172
100 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
101 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
102 return this;
103 }
104
105 @Override
106 public void focusGained(FocusEvent e) {
107 if (Main.map != null) {
108 Main.map.keyDetector.setEnabled(false);
109 }
110 }
111
112 @Override
113 public void focusLost(FocusEvent e) {
114 if (Main.map != null) {
115 Main.map.keyDetector.setEnabled(true);
116 }
117 }
118}
Note: See TracBrowser for help on using the repository browser.