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