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.html;
021:
022: import java.awt.Graphics;
023: import java.awt.Rectangle;
024: import java.awt.Shape;
025:
026: import javax.swing.SizeRequirements;
027: import javax.swing.text.AttributeSet;
028: import javax.swing.text.Element;
029: import javax.swing.text.StyleConstants;
030: import javax.swing.text.View;
031: import javax.swing.text.html.StyleSheet.BoxPainter;
032:
033: public class ParagraphView extends javax.swing.text.ParagraphView {
034: private AttributeSet attrs;
035: private BoxPainter boxPainter;
036: private float xAlign;
037: private CSS.Length width;
038: private int effectiveWidth;
039:
040: public ParagraphView(final Element elem) {
041: super (elem);
042: }
043:
044: public float getMinimumSpan(final int axis) {
045: if (!isVisible()) {
046: return 0;
047: }
048: return super .getMinimumSpan(axis);
049: }
050:
051: public float getPreferredSpan(final int axis) {
052: if (!isVisible()) {
053: return 0;
054: }
055: return super .getPreferredSpan(axis);
056: }
057:
058: public float getMaximumSpan(final int axis) {
059: if (!isVisible()) {
060: return 0;
061: }
062: return super .getMaximumSpan(axis);
063: }
064:
065: public AttributeSet getAttributes() {
066: return attrs;
067: }
068:
069: public float getAlignment(final int axis) {
070: if (axis == X_AXIS) {
071: return xAlign;
072: }
073: return super .getAlignment(axis);
074: }
075:
076: public void setParent(final View parent) {
077: super .setParent(parent);
078: if (parent != null) {
079: setPropertiesFromAttributes();
080: }
081: }
082:
083: public boolean isVisible() {
084: final int count = getLayoutViewCount();
085: boolean result = false;
086: for (int i = 0; i < count && !result; i++) {
087: View child = getLayoutView(i);
088: result = child.getElement().getAttributes().getAttribute(
089: HTML.Attribute.IMPLIED_NEW_LINE) == null
090: && child.isVisible();
091: }
092: return result;
093: }
094:
095: public void setSize(final float width, final float height) {
096: if (effectiveWidth != calculateEffectiveWidth()) {
097: preferenceChanged(this , true, true);
098: }
099: super .setSize(width, height);
100: }
101:
102: public void paint(final Graphics g, final Shape a) {
103: Rectangle rc = a.getBounds();
104: boxPainter.paint(g, rc.x, rc.y, rc.width, rc.height, this );
105:
106: super .paint(g, a);
107: }
108:
109: protected SizeRequirements calculateMinorAxisRequirements(
110: final int axis, final SizeRequirements r) {
111: SizeRequirements result = super .calculateMinorAxisRequirements(
112: axis, r);
113: result.minimum = getMinimumSpan();
114: effectiveWidth = calculateEffectiveWidth();
115: if (effectiveWidth > result.minimum) {
116: result.minimum = effectiveWidth;
117: result.preferred = result.minimum;
118: result.maximum = result.minimum;
119: }
120: return result;
121: }
122:
123: // protected SizeRequirements
124: // calculateMajorAxisRequirements(final int axis,
125: // final SizeRequirements sr) {
126: //
127: // SizeRequirements result = super.calculateMajorAxisRequirements(axis,
128: // sr);
129: // result.alignment = getAlign();
130: // return result;
131: // }
132:
133: protected StyleSheet getStyleSheet() {
134: return ((HTMLDocument) getDocument()).getStyleSheet();
135: }
136:
137: protected void setPropertiesFromAttributes() {
138: attrs = getStyleSheet().getViewAttributes(this );
139: super .setPropertiesFromAttributes();
140: boxPainter = getStyleSheet().getBoxPainter(getAttributes());
141: boxPainter.setView(this );
142: setInsets((short) boxPainter.getInset(TOP, this ),
143: (short) boxPainter.getInset(LEFT, this ),
144: (short) boxPainter.getInset(BOTTOM, this ),
145: (short) boxPainter.getInset(RIGHT, this ));
146:
147: xAlign = getAlign();
148: width = (CSS.Length) getAttributes().getAttribute(
149: CSS.Attribute.WIDTH);
150: }
151:
152: private int calculateEffectiveWidth() {
153: return width != null ? effectiveWidth = width.intValue(this )
154: : 0;
155: }
156:
157: private int getMinimumSpan() {
158: int result = 0;
159: final int count = getLayoutViewCount();
160: for (int i = 0; i < count; i++) {
161: View child = getLayoutView(i);
162: if (child instanceof InlineView) {
163: result = Math.max(result, ((InlineView) child)
164: .getLongestWordSpan());
165: } else {
166: result = Math.max(result, (int) child
167: .getMinimumSpan(X_AXIS));
168: }
169: }
170: return result;
171: }
172:
173: private float getAlign() {
174: final View parent = getParent();
175: if (parent == null) {
176: return 0.0f;
177: }
178:
179: CSS.TextAlign ta = (CSS.TextAlign) parent.getAttributes()
180: .getAttribute(CSS.Attribute.TEXT_ALIGN);
181: int textAlign = ta != null ? ((Integer) ta.fromCSS())
182: .intValue() : StyleConstants.ALIGN_LEFT;
183: switch (textAlign) {
184: case StyleConstants.ALIGN_LEFT:
185: return 0.0f;
186: case StyleConstants.ALIGN_CENTER:
187: return 0.5f;
188: case StyleConstants.ALIGN_RIGHT:
189: return 1.0f;
190: default:
191: return 0.0f;
192: }
193: }
194: }
|