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