1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.Color;
|
---|
5 | import java.awt.FontMetrics;
|
---|
6 | import java.awt.Graphics;
|
---|
7 | import java.awt.Graphics2D;
|
---|
8 | import java.awt.Insets;
|
---|
9 | import java.awt.RenderingHints;
|
---|
10 | import java.awt.event.FocusEvent;
|
---|
11 | import java.awt.event.FocusListener;
|
---|
12 |
|
---|
13 | import javax.swing.JTextField;
|
---|
14 | import javax.swing.text.Document;
|
---|
15 |
|
---|
16 | import org.openstreetmap.josm.Main;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * Subclass of {@link JTextField} that:<ul>
|
---|
20 | * <li>adds a "native" context menu (cut/copy/paste/select all)</li>
|
---|
21 | * <li>adds an optional "hint" displayed when no text has been entered</li>
|
---|
22 | * <li>disables the global advanced key press detector when focused</li>
|
---|
23 | * <br>This class must be used everywhere in core and plugins instead of {@code JTextField}.
|
---|
24 | * @since 5886
|
---|
25 | */
|
---|
26 | public class JosmTextField extends JTextField implements FocusListener {
|
---|
27 |
|
---|
28 | private String hint;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Constructs a new <code>JosmTextField</code> that uses the given text
|
---|
32 | * storage model and the given number of columns.
|
---|
33 | * This is the constructor through which the other constructors feed.
|
---|
34 | * If the document is <code>null</code>, a default model is created.
|
---|
35 | *
|
---|
36 | * @param doc the text storage to use; if this is <code>null</code>,
|
---|
37 | * a default will be provided by calling the
|
---|
38 | * <code>createDefaultModel</code> method
|
---|
39 | * @param text the initial string to display, or <code>null</code>
|
---|
40 | * @param columns the number of columns to use to calculate
|
---|
41 | * the preferred width >= 0; if <code>columns</code>
|
---|
42 | * is set to zero, the preferred width will be whatever
|
---|
43 | * naturally results from the component implementation
|
---|
44 | * @exception IllegalArgumentException if <code>columns</code> < 0
|
---|
45 | */
|
---|
46 | public JosmTextField(Document doc, String text, int columns) {
|
---|
47 | super(doc, text, columns);
|
---|
48 | TextContextualPopupMenu.enableMenuFor(this);
|
---|
49 | // Fix minimum size when columns are specified
|
---|
50 | if (columns > 0) {
|
---|
51 | setMinimumSize(getPreferredSize());
|
---|
52 | }
|
---|
53 | addFocusListener(this);
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructs a new <code>JosmTextField</code> initialized with the
|
---|
58 | * specified text and columns. A default model is created.
|
---|
59 | *
|
---|
60 | * @param text the text to be displayed, or <code>null</code>
|
---|
61 | * @param columns the number of columns to use to calculate
|
---|
62 | * the preferred width; if columns is set to zero, the
|
---|
63 | * preferred width will be whatever naturally results from
|
---|
64 | * the component implementation
|
---|
65 | */
|
---|
66 | public JosmTextField(String text, int columns) {
|
---|
67 | this(null, text, columns);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * Constructs a new <code>JosmTextField</code> initialized with the
|
---|
72 | * specified text. A default model is created and the number of
|
---|
73 | * columns is 0.
|
---|
74 | *
|
---|
75 | * @param text the text to be displayed, or <code>null</code>
|
---|
76 | */
|
---|
77 | public JosmTextField(String text) {
|
---|
78 | this(null, text, 0);
|
---|
79 | }
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Constructs a new empty <code>JosmTextField</code> with the specified
|
---|
83 | * number of columns.
|
---|
84 | * A default model is created and the initial string is set to
|
---|
85 | * <code>null</code>.
|
---|
86 | *
|
---|
87 | * @param columns the number of columns to use to calculate
|
---|
88 | * the preferred width; if columns is set to zero, the
|
---|
89 | * preferred width will be whatever naturally results from
|
---|
90 | * the component implementation
|
---|
91 | */
|
---|
92 | public JosmTextField(int columns) {
|
---|
93 | this(null, null, columns);
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Constructs a new <code>JosmTextField</code>. A default model is created,
|
---|
98 | * the initial string is <code>null</code>,
|
---|
99 | * and the number of columns is set to 0.
|
---|
100 | */
|
---|
101 | public JosmTextField() {
|
---|
102 | this(null, null, 0);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Replies the hint displayed when no text has been entered.
|
---|
107 | * @return the hint
|
---|
108 | * @since 7505
|
---|
109 | */
|
---|
110 | public final String getHint() {
|
---|
111 | return hint;
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Sets the hint to display when no text has been entered.
|
---|
116 | * @param hint the hint to set
|
---|
117 | * @since 7505
|
---|
118 | */
|
---|
119 | public final void setHint(String hint) {
|
---|
120 | this.hint = hint;
|
---|
121 | }
|
---|
122 |
|
---|
123 | @Override
|
---|
124 | public void paint(Graphics g) {
|
---|
125 | super.paint(g);
|
---|
126 | if (hint != null && !hint.isEmpty() && getText().isEmpty() && !isFocusOwner()) {
|
---|
127 | // Taken from http://stackoverflow.com/a/24571681/2257172
|
---|
128 | int h = getHeight();
|
---|
129 | ((Graphics2D)g).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
|
---|
130 | Insets ins = getInsets();
|
---|
131 | FontMetrics fm = g.getFontMetrics();
|
---|
132 | int c0 = getBackground().getRGB();
|
---|
133 | int c1 = getForeground().getRGB();
|
---|
134 | int m = 0xfefefefe;
|
---|
135 | int c2 = ((c0 & m) >>> 1) + ((c1 & m) >>> 1);
|
---|
136 | g.setColor(new Color(c2, true));
|
---|
137 | g.drawString(hint, ins.left, h / 2 + fm.getAscent() / 2 - 2);
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | @Override
|
---|
142 | public void focusGained(FocusEvent e) {
|
---|
143 | if (Main.map != null) {
|
---|
144 | Main.map.keyDetector.setEnabled(false);
|
---|
145 | }
|
---|
146 | repaint();
|
---|
147 | }
|
---|
148 |
|
---|
149 | @Override
|
---|
150 | public void focusLost(FocusEvent e) {
|
---|
151 | if (Main.map != null) {
|
---|
152 | Main.map.keyDetector.setEnabled(true);
|
---|
153 | }
|
---|
154 | repaint();
|
---|
155 | }
|
---|
156 | }
|
---|