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 Roman I. Chernyatchik
019: * @version $Revision$
020: */package javax.swing.plaf.basic;
021:
022: import java.awt.Font;
023: import java.beans.PropertyChangeEvent;
024: import java.util.EventObject;
025:
026: import javax.swing.JComponent;
027: import javax.swing.JTextPane;
028: import javax.swing.plaf.ComponentUI;
029: import javax.swing.text.Style;
030: import javax.swing.text.StyleConstants;
031: import javax.swing.text.StyleContext;
032: import javax.swing.text.ViewFactory;
033:
034: import org.apache.harmony.x.swing.StringConstants;
035:
036: public class BasicTextPaneUI extends BasicEditorPaneUI implements
037: ViewFactory {
038: private static final String propertyPrefix = "TextPane";
039:
040: public static ComponentUI createUI(final JComponent c) {
041: return new BasicTextPaneUI();
042: }
043:
044: protected void propertyChange(final PropertyChangeEvent e) {
045: super .propertyChange(e);
046:
047: final String propName = e.getPropertyName();
048:
049: if (e.getNewValue() == null) {
050: return;
051: }
052:
053: if (StringConstants.FONT_PROPERTY_CHANGED.equals(propName)) {
054: updateFontAttributes(getDefaultStyle(e), (Font) e
055: .getNewValue());
056: } else if (StringConstants.FOREGROUND_PROPERTY_CHANGED
057: .equals(propName)) {
058: getDefaultStyle(e).addAttribute(StyleConstants.Foreground,
059: e.getNewValue());
060: } else if (StringConstants.TEXT_COMPONENT_DOCUMENT_PROPERTY
061: .equals(propName)) {
062: setDefaultStyle(e);
063: }
064: }
065:
066: private Style getDefaultStyle(final EventObject e) {
067: return ((JTextPane) e.getSource()).getStyledDocument()
068: .getStyle(StyleContext.DEFAULT_STYLE);
069: }
070:
071: private void setDefaultStyle(final PropertyChangeEvent e) {
072: final JTextPane pane = (JTextPane) e.getSource();
073: if (pane.getStyledDocument() == null) {
074: return;
075: }
076:
077: final Font f = pane.getFont();
078: if (f == null) {
079: return;
080: }
081:
082: final Style style = getDefaultStyle(e);
083:
084: updateFontAttributes(style, f);
085: style.addAttribute(StyleConstants.Foreground, pane
086: .getForeground());
087: }
088:
089: protected String getPropertyPrefix() {
090: return propertyPrefix;
091: }
092:
093: private void updateFontAttributes(final Style style, final Font f) {
094: style.addAttribute(StyleConstants.Italic, Boolean.valueOf(f
095: .isItalic()));
096: style.addAttribute(StyleConstants.Bold, Boolean.valueOf(f
097: .isBold()));
098: style.addAttribute(StyleConstants.FontFamily, f.getName());
099: style.addAttribute(StyleConstants.FontSize, new Integer(f
100: .getSize()));
101: }
102: }
|