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.text;
021:
022: import java.awt.Component;
023: import java.awt.FontMetrics;
024: import java.awt.Graphics;
025: import java.awt.Rectangle;
026: import java.awt.Shape;
027: import javax.swing.event.DocumentEvent;
028:
029: import org.apache.harmony.awt.text.TextUtils;
030:
031: public class FieldView extends PlainView {
032:
033: public FieldView(final Element elem) {
034: super (elem);
035: }
036:
037: protected Shape adjustAllocation(final Shape shape) {
038: Component comp = getComponent();
039: allocation = TextUtils.getFieldViewAllocation(this , TextUtils
040: .getTextFieldKit(comp), shape, comp
041: .getComponentOrientation());
042: return allocation;
043: }
044:
045: private Shape getAllocation() {
046: return allocation;
047: }
048:
049: protected FontMetrics getFontMetrics() {
050: conditionalUpdateMetrics();
051: return metrics;
052: }
053:
054: public float getPreferredSpan(final int axis) {
055: FontMetrics fm = getFontMetrics();
056: if (fm == null) {
057: return 0;
058: }
059: int start = getElement().getStartOffset();
060: int end = getElement().getEndOffset() - 1;
061: Segment text = new Segment();
062: try {
063: getDocument().getText(start, end - start, text);
064: } catch (final BadLocationException e) {
065: }
066: return (axis == View.X_AXIS) ? TextUtils.getTabbedTextWidth(
067: text, getFontMetrics(), 0, this , start) : fm
068: .getHeight();
069: }
070:
071: public int getResizeWeight(final int axis) {
072: return (axis == View.X_AXIS) ? 1 : 0;
073: }
074:
075: private Shape allocation;
076:
077: public void insertUpdate(final DocumentEvent changes,
078: final Shape a, final ViewFactory f) {
079: super .insertUpdate(changes, adjustAllocation(a), f);
080: }
081:
082: final Rectangle getOldAllocation(final Shape a) {
083: if (getAllocation() == null) {
084: return adjustAllocation(a).getBounds();
085: }
086: Rectangle shape = getAllocation().getBounds();
087: Shape sh = TextUtils.getFieldViewAllocation(this , TextUtils
088: .getTextFieldKit(getComponent()), a, getComponent()
089: .getComponentOrientation());
090: shape.setLocation(sh.getBounds().getLocation());
091: return shape;
092:
093: }
094:
095: public Shape modelToView(final int pos, final Shape a,
096: final Position.Bias b) throws BadLocationException {
097: return super .modelToView(pos, getOldAllocation(a), b);
098: }
099:
100: public void paint(final Graphics g, final Shape a) {
101: super .paint(g, adjustAllocation(a));
102: }
103:
104: public void removeUpdate(final DocumentEvent changes,
105: final Shape a, final ViewFactory f) {
106: Shape oldAllocation = getAllocation();
107: adjustAllocation(a);
108: super .removeUpdate(changes, oldAllocation, f);
109: }
110:
111: public int viewToModel(final float fx, final float fy,
112: final Shape a, final Position.Bias[] bias) {
113: return super.viewToModel(fx, fy, adjustAllocation(a), bias);
114: }
115: }
|