001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Evgeniya G. Maenkova
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.Color;
023: import java.beans.PropertyChangeEvent;
024: import java.lang.reflect.Constructor;
025: import java.security.AccessController;
026: import java.security.PrivilegedAction;
027:
028: import javax.swing.JComponent;
029: import javax.swing.UIManager;
030: import javax.swing.plaf.ComponentUI;
031: import javax.swing.text.Document;
032: import javax.swing.text.Element;
033: import javax.swing.text.FieldView;
034: import javax.swing.text.View;
035:
036: import org.apache.harmony.x.swing.StringConstants;
037:
038: public class BasicTextFieldUI extends BasicTextUI {
039: static final String FIELD_VIEW_I18N_CLASS = "javax.swing.text.FieldViewI18N";
040: static String propertyPrefix = "TextField";
041:
042: public BasicTextFieldUI() {
043: super ();
044: }
045:
046: public View create(final Element elem) {
047: if (elem == null) {
048: return null;
049: }
050: Document doc = elem.getDocument();
051: Boolean i18n = (Boolean) doc
052: .getProperty(StringConstants.BIDI_PROPERTY);
053: if (i18n.booleanValue()) {
054: return (View) AccessController
055: .doPrivileged(new PrivilegedAction() {
056: public Object run() {
057: try {
058: Class cls = Class
059: .forName(FIELD_VIEW_I18N_CLASS);
060: Constructor constructor = cls
061: .getConstructor(new Class[] { Element.class });
062: constructor.setAccessible(true);
063: return constructor
064: .newInstance(new Object[] { elem });
065: } catch (Exception e) {
066: return null;
067: }
068: }
069: });
070: }
071: return new FieldView(elem);
072: }
073:
074: public static ComponentUI createUI(final JComponent c) {
075: return new BasicTextFieldUI();
076: }
077:
078: protected String getPropertyPrefix() {
079: return propertyPrefix;
080: }
081:
082: public void installUI(final JComponent c) {
083: super .installUI(c);
084: }
085:
086: private void updateBackgroundColor() {
087: String property = getComponent().isEditable() ? ".background"
088: : ".inactiveBackground";
089: Color color = UIManager.getColor(addPrefix(property));
090: getComponent().setBackground(color);
091: }
092:
093: protected void propertyChange(final PropertyChangeEvent evt) {
094: String propertyName = evt.getPropertyName();
095: if ("horizontalAlignment".equals(propertyName)) {
096: getComponent().repaint();
097: } else if (org.apache.harmony.x.swing.StringConstants.EDITABLE_PROPERTY_CHANGED
098: .equals(evt.getPropertyName())) {
099: updateBackgroundColor();
100: }
101: super .propertyChange(evt);
102: }
103:
104: final void updateFocusTraversalKeys() {
105:
106: }
107: }
|