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: package javax.swing.plaf.basic;
018:
019: import java.awt.Dimension;
020: import java.beans.PropertyChangeEvent;
021: import java.lang.reflect.Constructor;
022: import java.security.AccessController;
023: import java.security.PrivilegedAction;
024:
025: import javax.swing.JComponent;
026: import javax.swing.JTextArea;
027: import javax.swing.plaf.ComponentUI;
028: import javax.swing.text.Caret;
029: import javax.swing.text.DefaultCaret;
030: import javax.swing.text.Document;
031: import javax.swing.text.Element;
032: import javax.swing.text.JTextComponent;
033: import javax.swing.text.PlainView;
034: import javax.swing.text.View;
035: import javax.swing.text.WrappedPlainView;
036:
037: import org.apache.harmony.x.swing.StringConstants;
038:
039: import org.apache.harmony.x.swing.internal.nls.Messages;
040:
041: public class BasicTextAreaUI extends BasicTextUI {
042: private static final String PLAIN_VIEW_I18N_CLASS = "javax.swing.text.PlainViewI18N";
043: private static final String propertyPrefix = "TextArea";
044:
045: public BasicTextAreaUI() {
046: super ();
047: }
048:
049: public static ComponentUI createUI(final JComponent c) {
050: return new BasicTextAreaUI();
051: }
052:
053: @Override
054: public View create(final Element element) {
055: Document doc = element.getDocument();
056: Boolean i18n = (Boolean) doc
057: .getProperty(StringConstants.BIDI_PROPERTY);
058: if (i18n.booleanValue()) {
059: return AccessController
060: .doPrivileged(new PrivilegedAction<View>() {
061: public View run() {
062: try {
063: Class cls = Class
064: .forName(PLAIN_VIEW_I18N_CLASS);
065: Constructor constructor = cls
066: .getConstructor(new Class[] { Element.class });
067: constructor.setAccessible(true);
068: return (View) constructor
069: .newInstance(new Object[] { element });
070: } catch (Exception e) {
071: return null;
072: }
073: }
074: });
075: }
076:
077: JTextComponent comp = getComponent();
078: boolean lineWrap = false;
079: boolean wordWrap = false;
080: if (comp instanceof JTextArea) {
081: JTextArea c = (JTextArea) getComponent();
082: lineWrap = c.getLineWrap();
083: wordWrap = c.getWrapStyleWord();
084: }
085: if (lineWrap) {
086: return new WrappedPlainView(element, wordWrap);
087: }
088:
089: return new PlainView(element);
090:
091: }
092:
093: @Override
094: public Dimension getMinimumSize(final JComponent c) {
095: if (!(c instanceof JTextComponent)) {
096: throw new IllegalArgumentException(Messages
097: .getString("swing.77")); //$NON-NLS-1$
098: }
099: Dimension dim = super .getMinimumSize(c);
100: Caret caret = ((JTextComponent) c).getCaret();
101: if (caret != null && caret instanceof DefaultCaret) {
102: dim.width += ((DefaultCaret) caret).width;
103: }
104: return dim;
105: }
106:
107: @Override
108: public Dimension getPreferredSize(final JComponent c) {
109: if (!(c instanceof JTextComponent)) {
110: throw new IllegalArgumentException(Messages
111: .getString("swing.77")); //$NON-NLS-1$
112: }
113: Dimension dim = super .getPreferredSize(c);
114:
115: Caret caret = ((JTextComponent) c).getCaret();
116: if (caret != null && caret instanceof DefaultCaret) {
117: dim.width += ((DefaultCaret) caret).width;
118: }
119: return dim;
120: }
121:
122: @Override
123: protected String getPropertyPrefix() {
124: return propertyPrefix;
125: }
126:
127: @Override
128: protected void propertyChange(final PropertyChangeEvent e) {
129: String name = e.getPropertyName();
130: if (StringConstants.TEXT_COMPONENT_LINE_WRAP_PROPERTY
131: .equals(name)
132: || StringConstants.TEXT_COMPONENT_WRAP_STYLE_WORD_PROPERTY
133: .equals(name)) {
134: modelChanged();
135: }
136: }
137: }
|