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