source: josm/trunk/src/org/openstreetmap/josm/gui/widgets/JosmPasswordField.java@ 17318

Last change on this file since 17318 was 14273, checked in by stoecker, 6 years ago

fix typos - patch by naoliv - fix #16781 - Thanks a lot

  • Property svn:eol-style set to native
File size: 6.0 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.widgets;
3
4import java.awt.event.ActionEvent;
5import java.awt.event.FocusEvent;
6import java.awt.event.FocusListener;
7import java.beans.PropertyChangeListener;
8
9import javax.swing.Action;
10import javax.swing.JPasswordField;
11import javax.swing.TransferHandler;
12import javax.swing.text.Document;
13import javax.swing.text.JTextComponent;
14
15import org.openstreetmap.josm.gui.MainApplication;
16import org.openstreetmap.josm.gui.MapFrame;
17import 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 */
27public 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 &gt;= 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 &gt;= 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 &gt;= 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 occurred 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}
Note: See TracBrowser for help on using the repository browser.