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