01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Evgeniya G. Maenkova
19: * @version $Revision$
20: */package javax.swing.plaf.basic;
21:
22: import java.beans.PropertyChangeEvent;
23: import java.lang.reflect.Constructor;
24: import java.security.AccessController;
25: import java.security.PrivilegedAction;
26:
27: import javax.swing.JComponent;
28: import javax.swing.JPasswordField;
29: import javax.swing.plaf.ComponentUI;
30: import javax.swing.text.Element;
31: import javax.swing.text.JTextComponent;
32: import javax.swing.text.PasswordView;
33: import javax.swing.text.View;
34:
35: import org.apache.harmony.x.swing.StringConstants;
36:
37: public class BasicPasswordFieldUI extends BasicTextFieldUI {
38: private static final String propertyPrefix = "PasswordField";
39:
40: public BasicPasswordFieldUI() {
41: }
42:
43: public View create(final Element element) {
44: if (echoCharIsSet() || !getI18nProperty()) {
45: return new PasswordView(element);
46: } else {
47: return (View) AccessController
48: .doPrivileged(new PrivilegedAction() {
49: public Object run() {
50: try {
51: Class cls = Class
52: .forName(FIELD_VIEW_I18N_CLASS);
53: Constructor constructor = cls
54: .getConstructor(new Class[] { Element.class });
55: constructor.setAccessible(true);
56: return constructor
57: .newInstance(new Object[] { element });
58: } catch (Exception e) {
59: return null;
60: }
61: }
62: });
63: }
64: }
65:
66: public static ComponentUI createUI(final JComponent c) {
67: return new BasicPasswordFieldUI();
68: }
69:
70: protected String getPropertyPrefix() {
71: return propertyPrefix;
72: }
73:
74: void propertyChangeImpl(final PropertyChangeEvent e) {
75: if (!StringConstants.PASSWORD_FIELD_ECHO_CHAR_PROPERTY.equals(e
76: .getPropertyName())) {
77: return;
78: }
79: if (getI18nProperty()
80: && (isZeroChar(e.getNewValue()) || isZeroChar(e
81: .getOldValue()))) {
82: modelChanged();
83: } else {
84: getComponent().repaint();
85: }
86: }
87:
88: private final boolean isZeroChar(final Object value) {
89: return (value instanceof Character)
90: && ((Character) value).charValue() == '\0';
91: }
92:
93: private final boolean echoCharIsSet() {
94: JTextComponent component = getComponent();
95: return (component instanceof JPasswordField)
96: && ((JPasswordField) component).echoCharIsSet();
97: }
98: }
|