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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Color;
023: import java.awt.Component;
024: import java.awt.Font;
025: import java.awt.FontMetrics;
026:
027: import org.apache.harmony.awt.text.ComposedTextParams;
028: import org.apache.harmony.awt.text.PropertyNames;
029: import org.apache.harmony.awt.text.TextKit;
030:
031: /**
032: * Stores parameters to paint text in plain text views.
033: *
034: */
035: final class TextPaintParams {
036: private static final int DEFAULT_TAB_SIZE = 8;
037:
038: private static final float MIN_TAB_SIZE = 1;
039:
040: final Segment buffer = new Segment();
041:
042: Color color;
043:
044: int composedEnd;
045: int composedStart;
046: ComposedTextParams composedText;
047:
048: FontMetrics metrics;
049:
050: Color selColor;
051: int selEnd;
052: int selStart;
053:
054: float tabSize;
055:
056: final View view;
057:
058: TextPaintParams(final View view) {
059: this .view = view;
060: }
061:
062: void conditionalUpdateMetrics() {
063: if (areMetricsValid()) {
064: updateMetrics();
065: view.preferenceChanged(null, true, true);
066: }
067: }
068:
069: int getTabSize() {
070: final Document doc = view.getDocument();
071: final Object value = doc
072: .getProperty(PlainDocument.tabSizeAttribute);
073: return value != null ? ((Integer) value).intValue()
074: : DEFAULT_TAB_SIZE;
075: }
076:
077: boolean areMetricsValid() {
078: return metrics == null
079: || !view.getComponent().getFont().equals(
080: metrics.getFont());
081: }
082:
083: float nextTabStop(final float x) {
084: conditionalUpdateMetrics();
085: if (Math.abs(tabSize) <= MIN_TAB_SIZE) {
086: return x;
087: }
088: final int count = (int) x / (int) tabSize;
089: return (count + 1) * tabSize;
090: }
091:
092: void updateFields() {
093: final Component component = view.getComponent();
094: final TextKit textKit = view.getTextKit();
095: selStart = textKit.getSelectionStart();
096: selEnd = textKit.getSelectionEnd();
097:
098: composedText = (ComposedTextParams) view.getDocument()
099: .getProperty(PropertyNames.COMPOSED_TEXT_PROPERTY);
100:
101: if (composedText != null) {
102: composedStart = composedText.getComposedTextStart();
103: composedEnd = composedStart
104: + composedText.getComposedTextLength();
105: }
106:
107: if (component.isEnabled()) {
108: color = component.getForeground();
109: selColor = textKit.getSelectedTextColor();
110: } else {
111: color = textKit.getDisabledTextColor();
112: selColor = color;
113: }
114: }
115:
116: void updateMetrics() {
117: final Component component = view.getComponent();
118: final Font font = component.getFont();
119: metrics = component.getFontMetrics(font);
120: tabSize = metrics.charWidth('m') * getTabSize();
121: }
122: }
|