1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
/* * @(#)ParagraphView.java 1.30 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.swing.text.html; import java.awt.*; import javax.swing.SizeRequirements; import javax.swing.event.DocumentEvent; import javax.swing.text.Document; import javax.swing.text.Element; import javax.swing.text.AttributeSet; import javax.swing.text.StyleConstants; import javax.swing.text.View; import javax.swing.text.ViewFactory; import javax.swing.text.BadLocationException; import javax.swing.text.JTextComponent; /** * Displays the a paragraph, and uses css attributes for its * configuration. * * @author Timothy Prinzing * @version 1.30 11/17/05 */ public class ParagraphView extends javax.swing.text.ParagraphView { /** * Constructs a ParagraphView for the given element. * * @param elem the element that this view is responsible for */ public ParagraphView(Element elem) { super(elem); } /** * Establishes the parent view for this view. This is * guaranteed to be called before any other methods if the * parent view is functioning properly. * <p> * This is implemented * to forward to the superclass as well as call the * <a href="#setPropertiesFromAttributes">setPropertiesFromAttributes</a> * method to set the paragraph properties from the css * attributes. The call is made at this time to ensure * the ability to resolve upward through the parents * view attributes. * * @param parent the new parent, or null if the view is * being removed from a parent it was previously added * to */ public void setParent(View parent) { super.setParent(parent); if (parent != null) { setPropertiesFromAttributes(); } } /** * Fetches the attributes to use when rendering. This is * implemented to multiplex the attributes specified in the * model with a StyleSheet. */ public AttributeSet getAttributes() { if (attr == null) { StyleSheet sheet = getStyleSheet(); attr = sheet.getViewAttributes(this); } return attr; } /** * Sets up the paragraph from css attributes instead of * the values found in StyleConstants (i.e. which are used * by the superclass). Since */ protected void setPropertiesFromAttributes() { StyleSheet sheet = getStyleSheet(); attr = sheet.getViewAttributes(this); painter = sheet.getBoxPainter(attr); if (attr != null) { super.setPropertiesFromAttributes(); setInsets((short) painter.getInset(TOP, this), (short) painter.getInset(LEFT, this), (short) painter.getInset(BOTTOM, this), (short) painter.getInset(RIGHT, this)); Object o = attr.getAttribute(CSS.Attribute.TEXT_ALIGN); if (o != null) { // set horizontal alignment String ta = o.toString(); if (ta.equals("left")) { setJustification(StyleConstants.ALIGN_LEFT); } else if (ta.equals("center")) { setJustification(StyleConstants.ALIGN_CENTER); } else if (ta.equals("right")) { setJustification(StyleConstants.ALIGN_RIGHT); } else if (ta.equals("justify")) { setJustification(StyleConstants.ALIGN_JUSTIFIED); } } // Get the width/height cssWidth = (CSS.LengthValue)attr.getAttribute( CSS.Attribute.WIDTH); cssHeight = (CSS.LengthValue)attr.getAttribute( CSS.Attribute.HEIGHT); } } protected StyleSheet getStyleSheet() { HTMLDocument doc = (HTMLDocument) getDocument(); return doc.getStyleSheet(); } /** * Calculate the needs for the paragraph along the minor axis. * This implemented to use the requirements of the superclass, * modified slightly to set a minimum span allowed. Typical * html rendering doesn't let the view size shrink smaller than * the length of the longest word. */ protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) { r = super.calculateMinorAxisRequirements(axis, r); if (!BlockView.spanSetFromAttributes(axis, r, cssWidth, cssHeight)) { // PENDING(prinz) Need to make this better so it doesn't require // InlineView and works with font changes within the word. // find the longest minimum span. float min = 0; int n = getLayoutViewCount(); for (int i = 0; i < n; i++) { View v = getLayoutView(i); if (v instanceof InlineView) { float wordSpan = ((InlineView) v).getLongestWordSpan(); min = Math.max(wordSpan, min); } else { min = Math.max(v.getMinimumSpan(axis), min); } } r.minimum = Math.max(r.minimum, (int) min); r.preferred = Math.max(r.minimum, r.preferred); r.maximum = Math.max(r.preferred, r.maximum); } else { // Offset by the margins so that pref/min/max return the // right value. int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() : getTopInset() + getBottomInset(); r.minimum -= margin; r.preferred -= margin; r.maximum -= margin; } return r; } /** * Indicates whether or not this view should be * displayed. If none of the children wish to be * displayed and the only visible child is the * break that ends the paragraph, the paragraph * will not be considered visible. Otherwise, * it will be considered visible and return true. * * @return true if the paragraph should be displayed */ public boolean isVisible() { int n = getLayoutViewCount() - 1; for (int i = 0; i < n; i++) { View v = getLayoutView(i); if (v.isVisible()) { return true; } } if (n > 0) { View v = getLayoutView(n); if ((v.getEndOffset() - v.getStartOffset()) == 1) { return false; } } // If it's the last paragraph and not editable, it shouldn't // be visible. if (getStartOffset() == getDocument().getLength()) { boolean editable = false; Component c = getContainer(); if (c instanceof JTextComponent) { editable = ((JTextComponent)c).isEditable(); } if (!editable) { return false; } } return true; } /** * Renders using the given rendering surface and area on that * surface. This is implemented to delgate to the superclass * after stashing the base coordinate for tab calculations. * * @param g the rendering surface to use * @param a the allocated region to render into * @see View#paint */ public void paint(Graphics g, Shape a) { if (a == null) { return; } Rectangle r; if (a instanceof Rectangle) { r = (Rectangle) a; } else { r = a.getBounds(); } painter.paint(g, r.x, r.y, r.width, r.height, this); super.paint(g, a); } /** * Determines the preferred span for this view. Returns * 0 if the view is not visible, otherwise it calls the * superclass method to get the preferred span. * axis. * * @param axis may be either View.X_AXIS or View.Y_AXIS * @return the span the view would like to be rendered into; * typically the view is told to render into the span * that is returned, although there is no guarantee; * the parent may choose to resize or break the view * @see javax.swing.text.ParagraphView#getPreferredSpan */ public float getPreferredSpan(int axis) { if (!isVisible()) { return 0; } return super.getPreferredSpan(axis); } /** * Determines the minimum span for this view along an * axis. Returns 0 if the view is not visible, otherwise * it calls the superclass method to get the minimum span. * * @param axis may be either <code>View.X_AXIS</code> or * <code>View.Y_AXIS</code> * @return the minimum span the view can be rendered into * @see javax.swing.text.ParagraphView#getMinimumSpan */ public float getMinimumSpan(int axis) { if (!isVisible()) { return 0; } return super.getMinimumSpan(axis); } /** * Determines the maximum span for this view along an * axis. Returns 0 if the view is not visible, otherwise * it calls the superclass method ot get the maximum span. * * @param axis may be either <code>View.X_AXIS</code> or * <code>View.Y_AXIS</code> * @return the maximum span the view can be rendered into * @see javax.swing.text.ParagraphView#getMaximumSpan */ public float getMaximumSpan(int axis) { if (!isVisible()) { return 0; } return super.getMaximumSpan(axis); } private AttributeSet attr; private StyleSheet.BoxPainter painter; private CSS.LengthValue cssWidth; private CSS.LengthValue cssHeight; }