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

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

see #15182 - deprecate Main.map and Main.isDisplayingMapView(). Replacements: gui.MainApplication.getMap() / gui.MainApplication.isDisplayingMapView()

  • Property svn:eol-style set to native
File size: 3.8 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.gui.MainApplication;
12import org.openstreetmap.josm.gui.MapFrame;
13
14/**
15 * Subclass of {@link JTextArea} that adds a "native" context menu (cut/copy/paste/select all).
16 * @since 5886
17 */
18public 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 }
93
94 /**
95 * Restore default behaviour of focus transfer with TAB, overriden by {@link JTextArea}.
96 * @return {@code this}
97 * @since 11308
98 */
99 public JosmTextArea transferFocusOnTab() {
100 // http://stackoverflow.com/a/525867/2257172
101 setFocusTraversalKeys(KeyboardFocusManager.FORWARD_TRAVERSAL_KEYS, null);
102 setFocusTraversalKeys(KeyboardFocusManager.BACKWARD_TRAVERSAL_KEYS, null);
103 return this;
104 }
105
106 @Override
107 public void focusGained(FocusEvent e) {
108 MapFrame map = MainApplication.getMap();
109 if (map != null) {
110 map.keyDetector.setEnabled(false);
111 }
112 }
113
114 @Override
115 public void focusLost(FocusEvent e) {
116 MapFrame map = MainApplication.getMap();
117 if (map != null) {
118 map.keyDetector.setEnabled(true);
119 }
120 }
121}
Note: See TracBrowser for help on using the repository browser.