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