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.event.DocumentEvent;
028: import javax.swing.text.AttributeSet;
029: import javax.swing.text.BoxView;
030: import javax.swing.text.Element;
031: import javax.swing.text.View;
032: import javax.swing.text.ViewFactory;
033: import javax.swing.text.html.StyleSheet.BoxPainter;
034:
035: public class BlockView extends BoxView {
036: private AttributeSet attrs;
037: private BoxPainter boxPainter;
038:
039: private CSS.Length width;
040: private CSS.Length height;
041: private float effectiveWidth;
042: private float effectiveHeight;
043:
044: public BlockView(final Element elem, final int axis) {
045: super (elem, axis);
046: }
047:
048: public float getAlignment(final int axis) {
049: return 0;
050: }
051:
052: public AttributeSet getAttributes() {
053: if (attrs == null) {
054: attrs = getStyleSheet().getViewAttributes(this );
055: }
056: return attrs;
057: }
058:
059: public void setParent(final View parent) {
060: super .setParent(parent);
061: if (parent != null) {
062: setPropertiesFromAttributes();
063: }
064: }
065:
066: public void changedUpdate(final DocumentEvent event,
067: final Shape shape, final ViewFactory factory) {
068: attrs = getStyleSheet().getViewAttributes(this );
069: setPropertiesFromAttributes();
070: super .changedUpdate(event, shape, factory);
071: }
072:
073: public void paint(final Graphics g, final Shape allocation) {
074: Rectangle rc = allocation.getBounds();
075:
076: // Fix for HARMONY-4755, boxPainter is only initialized
077: // after setPropertiesFromAttributes() is called.
078: if (boxPainter != null) {
079: boxPainter.paint(g, rc.x, rc.y, rc.width, rc.height, this );
080: }
081: super .paint(g, allocation);
082: }
083:
084: public void setSize(final float width, final float height) {
085: boolean widthChanged = effectiveWidth != calculateEffectiveSize(X_AXIS);
086: boolean heightChanged = effectiveHeight != calculateEffectiveSize(Y_AXIS);
087: if (widthChanged || heightChanged) {
088: preferenceChanged(this , widthChanged, heightChanged);
089: return;
090: }
091: super .setSize(width, height);
092: }
093:
094: protected SizeRequirements calculateMajorAxisRequirements(
095: final int axis, final SizeRequirements r) {
096:
097: SizeRequirements result = super .calculateMajorAxisRequirements(
098: axis, r);
099: float size = calculateEffectiveSize(axis);
100: if (size - getFullInsets(axis) > result.minimum) {
101: result.minimum = (int) size - getFullInsets(axis);
102: result.preferred = result.minimum;
103: result.maximum = result.minimum;
104: }
105: return result;
106: }
107:
108: protected SizeRequirements calculateMinorAxisRequirements(
109: final int axis, final SizeRequirements r) {
110:
111: SizeRequirements result = super .calculateMinorAxisRequirements(
112: axis, r);
113: float size = calculateEffectiveSize(axis);
114: if (size - getFullInsets(axis) > result.minimum) {
115: result.minimum = (int) size - getFullInsets(axis);
116: result.preferred = result.minimum;
117: result.maximum = result.minimum;
118: }
119: return result;
120: }
121:
122: protected StyleSheet getStyleSheet() {
123: return ((HTMLDocument) getDocument()).getStyleSheet();
124: }
125:
126: protected void setPropertiesFromAttributes() {
127: boxPainter = getStyleSheet().getBoxPainter(getAttributes());
128: boxPainter.setView(this );
129: setInsets((short) boxPainter.getInset(TOP, this ),
130: (short) boxPainter.getInset(LEFT, this ),
131: (short) boxPainter.getInset(BOTTOM, this ),
132: (short) boxPainter.getInset(RIGHT, this ));
133: width = getSizeAttribute(X_AXIS);
134: height = getSizeAttribute(Y_AXIS);
135: }
136:
137: private float calculateEffectiveSize(final int axis) {
138: if (axis == X_AXIS) {
139: effectiveWidth = calculateEffectiveSize(width);
140: return effectiveWidth;
141: } else {
142: effectiveHeight = calculateEffectiveSize(height);
143: return effectiveHeight;
144: }
145: }
146:
147: private float calculateEffectiveSize(final CSS.Length size) {
148: return size != null ? size.floatValue(this ) : 0;
149: }
150:
151: private CSS.Length getSizeAttribute(final int axis) {
152: return (CSS.Length) getAttributes().getAttribute(
153: axis == X_AXIS ? CSS.Attribute.WIDTH
154: : CSS.Attribute.HEIGHT);
155: }
156:
157: private int getFullInsets(final int axis) {
158: return axis == X_AXIS ? getLeftInset() + getRightInset()
159: : getTopInset() + getBottomInset();
160: }
161: }
|