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