1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.widgets;
|
---|
3 |
|
---|
4 | import java.awt.event.ActionEvent;
|
---|
5 | import java.awt.event.FocusEvent;
|
---|
6 | import java.awt.event.FocusListener;
|
---|
7 | import java.beans.PropertyChangeListener;
|
---|
8 |
|
---|
9 | import javax.swing.Action;
|
---|
10 | import javax.swing.JPasswordField;
|
---|
11 | import javax.swing.TransferHandler;
|
---|
12 | import javax.swing.text.Document;
|
---|
13 | import javax.swing.text.JTextComponent;
|
---|
14 |
|
---|
15 | import org.openstreetmap.josm.Main;
|
---|
16 | import org.openstreetmap.josm.tools.Logging;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * A subclass of {@link JPasswordField} to implement a workaround to
|
---|
20 | * <a href="https://bugs.openjdk.java.net/browse/JDK-6322854">JDK bug 6322854</a>.
|
---|
21 | *
|
---|
22 | * @see <a href="https://josm.openstreetmap.de/ticket/8404">https://josm.openstreetmap.de/ticket/8404</a>
|
---|
23 | * @see <a href="https://hg.netbeans.org/main/rev/33cb2e81b640">https://hg.netbeans.org/main/rev/33cb2e81b640</a>
|
---|
24 | * @since 5752
|
---|
25 | */
|
---|
26 | public class JosmPasswordField extends JPasswordField implements FocusListener {
|
---|
27 |
|
---|
28 | /**
|
---|
29 | * Constructs a new <code>JosmPasswordField</code>,
|
---|
30 | * with a default document, <code>null</code> starting
|
---|
31 | * text string, and 0 column width.
|
---|
32 | */
|
---|
33 | public JosmPasswordField() {
|
---|
34 | workaroundJdkBug6322854(this);
|
---|
35 | addFocusListener(this);
|
---|
36 | }
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Constructs a new <code>JosmPasswordField</code> that uses the
|
---|
40 | * given text storage model and the given number of columns.
|
---|
41 | * This is the constructor through which the other constructors feed.
|
---|
42 | * The echo character is set to '*', but may be changed by the current
|
---|
43 | * Look and Feel. If the document model is
|
---|
44 | * <code>null</code>, a default one will be created.
|
---|
45 | *
|
---|
46 | * @param doc the text storage to use
|
---|
47 | * @param txt the text to be displayed, <code>null</code> if none
|
---|
48 | * @param columns the number of columns to use to calculate
|
---|
49 | * the preferred width >= 0; if columns is set to zero, the
|
---|
50 | * preferred width will be whatever naturally results from
|
---|
51 | * the component implementation
|
---|
52 | */
|
---|
53 | public JosmPasswordField(Document doc, String txt, int columns) {
|
---|
54 | super(doc, txt, columns);
|
---|
55 | workaroundJdkBug6322854(this);
|
---|
56 | addFocusListener(this);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Constructs a new empty <code>JosmPasswordField</code> with the specified
|
---|
61 | * number of columns. A default model is created, and the initial string
|
---|
62 | * is set to <code>null</code>.
|
---|
63 | *
|
---|
64 | * @param columns the number of columns >= 0
|
---|
65 | */
|
---|
66 | public JosmPasswordField(int columns) {
|
---|
67 | super(columns);
|
---|
68 | workaroundJdkBug6322854(this);
|
---|
69 | addFocusListener(this);
|
---|
70 | }
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * Constructs a new <code>JPasswordField</code> initialized with
|
---|
74 | * the specified text and columns. The document model is set to
|
---|
75 | * the default.
|
---|
76 | *
|
---|
77 | * @param text the text to be displayed, <code>null</code> if none
|
---|
78 | * @param columns the number of columns >= 0
|
---|
79 | */
|
---|
80 | public JosmPasswordField(String text, int columns) {
|
---|
81 | super(text, columns);
|
---|
82 | workaroundJdkBug6322854(this);
|
---|
83 | addFocusListener(this);
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Constructs a new <code>JosmPasswordField</code> initialized
|
---|
88 | * with the specified text. The document model is set to the
|
---|
89 | * default, and the number of columns to 0.
|
---|
90 | *
|
---|
91 | * @param text the text to be displayed, <code>null</code> if none
|
---|
92 | */
|
---|
93 | public JosmPasswordField(String text) {
|
---|
94 | super(text);
|
---|
95 | workaroundJdkBug6322854(this);
|
---|
96 | addFocusListener(this);
|
---|
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 |
|
---|
113 | /**
|
---|
114 | * Implements a workaround to <a href="https://bugs.openjdk.java.net/browse/JDK-6322854">JDK bug 6322854</a>.
|
---|
115 | * This method can be deleted after Oracle decides to fix this bug...
|
---|
116 | * @param text The {@link JTextComponent} to protect.
|
---|
117 | */
|
---|
118 | public static final void workaroundJdkBug6322854(final JTextComponent text) {
|
---|
119 | if (text != null) {
|
---|
120 | text.getActionMap().put("paste", new Action() {
|
---|
121 |
|
---|
122 | private final Action pasteAction = TransferHandler.getPasteAction();
|
---|
123 |
|
---|
124 | @Override
|
---|
125 | public void actionPerformed(ActionEvent e) {
|
---|
126 | try {
|
---|
127 | pasteAction.actionPerformed(e);
|
---|
128 | } catch (NullPointerException npe) { // NOPMD
|
---|
129 | Logging.log(Logging.LEVEL_ERROR, "NullPointerException occured because of JDK bug 6322854. "
|
---|
130 | +"Copy/Paste operation has not been performed. Please complain to Oracle: "+
|
---|
131 | "https://bugs.openjdk.java.net/browse/JDK-6322854", npe);
|
---|
132 | }
|
---|
133 | }
|
---|
134 |
|
---|
135 | @Override
|
---|
136 | public void setEnabled(boolean b) {
|
---|
137 | pasteAction.setEnabled(b);
|
---|
138 | }
|
---|
139 |
|
---|
140 | @Override
|
---|
141 | public void removePropertyChangeListener(PropertyChangeListener listener) {
|
---|
142 | pasteAction.removePropertyChangeListener(listener);
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public void putValue(String key, Object value) {
|
---|
147 | pasteAction.putValue(key, value);
|
---|
148 | }
|
---|
149 |
|
---|
150 | @Override
|
---|
151 | public boolean isEnabled() {
|
---|
152 | return pasteAction.isEnabled();
|
---|
153 | }
|
---|
154 |
|
---|
155 | @Override
|
---|
156 | public Object getValue(String key) {
|
---|
157 | return pasteAction.getValue(key);
|
---|
158 | }
|
---|
159 |
|
---|
160 | @Override
|
---|
161 | public void addPropertyChangeListener(PropertyChangeListener listener) {
|
---|
162 | pasteAction.addPropertyChangeListener(listener);
|
---|
163 | }
|
---|
164 | });
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|