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

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

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