1 /* 2 * Copyright (c) 1998, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package javax.swing.text.html; 26 27 import java.awt.*; 28 import javax.swing.SizeRequirements; 29 import javax.swing.event.DocumentEvent; 30 import javax.swing.text.Document; 31 import javax.swing.text.Element; 32 import javax.swing.text.AttributeSet; 33 import javax.swing.text.StyleConstants; 34 import javax.swing.text.View; 35 import javax.swing.text.ViewFactory; 36 import javax.swing.text.BadLocationException; 37 import javax.swing.text.JTextComponent; 38 39 /** 40 * Displays the a paragraph, and uses css attributes for its 41 * configuration. 42 * 43 * @author Timothy Prinzing 44 */ 45 46 public class ParagraphView extends javax.swing.text.ParagraphView { 47 48 /** 49 * Constructs a ParagraphView for the given element. 50 * 51 * @param elem the element that this view is responsible for 52 */ 53 public ParagraphView(Element elem) { 54 super(elem); 55 } 56 57 /** 58 * Establishes the parent view for this view. This is 59 * guaranteed to be called before any other methods if the 60 * parent view is functioning properly. 61 * <p> 62 * This is implemented 63 * to forward to the superclass as well as call the 64 * {@link #setPropertiesFromAttributes setPropertiesFromAttributes} 65 * method to set the paragraph properties from the css 66 * attributes. The call is made at this time to ensure 67 * the ability to resolve upward through the parents 68 * view attributes. 69 * 70 * @param parent the new parent, or null if the view is 71 * being removed from a parent it was previously added 72 * to 73 */ 74 public void setParent(View parent) { 75 super.setParent(parent); 76 if (parent != null) { 77 setPropertiesFromAttributes(); 78 } 79 } 80 81 /** 82 * Fetches the attributes to use when rendering. This is 83 * implemented to multiplex the attributes specified in the 84 * model with a StyleSheet. 85 */ 86 public AttributeSet getAttributes() { 87 if (attr == null) { 88 StyleSheet sheet = getStyleSheet(); 89 attr = sheet.getViewAttributes(this); 90 } 91 return attr; 92 } 93 94 /** 95 * Sets up the paragraph from css attributes instead of 96 * the values found in StyleConstants (i.e. which are used 97 * by the superclass). Since 98 */ 99 protected void setPropertiesFromAttributes() { 100 StyleSheet sheet = getStyleSheet(); 101 attr = sheet.getViewAttributes(this); 102 painter = sheet.getBoxPainter(attr); 103 if (attr != null) { 104 super.setPropertiesFromAttributes(); 105 setInsets((short) painter.getInset(TOP, this), 106 (short) painter.getInset(LEFT, this), 107 (short) painter.getInset(BOTTOM, this), 108 (short) painter.getInset(RIGHT, this)); 109 Object o = attr.getAttribute(CSS.Attribute.TEXT_ALIGN); 110 if (o != null) { 111 // set horizontal alignment 112 String ta = o.toString(); 113 if (ta.equals("left")) { 114 setJustification(StyleConstants.ALIGN_LEFT); 115 } else if (ta.equals("center")) { 116 setJustification(StyleConstants.ALIGN_CENTER); 117 } else if (ta.equals("right")) { 118 setJustification(StyleConstants.ALIGN_RIGHT); 119 } else if (ta.equals("justify")) { 120 setJustification(StyleConstants.ALIGN_JUSTIFIED); 121 } 122 } 123 // Get the width/height 124 cssWidth = (CSS.LengthValue)attr.getAttribute( 125 CSS.Attribute.WIDTH); 126 cssHeight = (CSS.LengthValue)attr.getAttribute( 127 CSS.Attribute.HEIGHT); 128 } 129 } 130 131 /** 132 * Convenient method to get the StyleSheet. 133 * 134 * @return the StyleSheet 135 */ 136 protected StyleSheet getStyleSheet() { 137 HTMLDocument doc = (HTMLDocument) getDocument(); 138 return doc.getStyleSheet(); 139 } 140 141 142 /** 143 * Calculate the needs for the paragraph along the minor axis. 144 * 145 * <p>If size requirements are explicitly specified for the paragraph, 146 * use that requirements. Otherwise, use the requirements of the 147 * superclass {@link javax.swing.text.ParagraphView}.</p> 148 * 149 * <p>If the {@code axis} parameter is neither {@code View.X_AXIS} nor 150 * {@code View.Y_AXIS}, {@link IllegalArgumentException} is thrown. If the 151 * {@code r} parameter is {@code null,} a new {@code SizeRequirements} 152 * object is created, otherwise the supplied {@code SizeRequirements} 153 * object is returned.</p> 154 * 155 * @param axis the minor axis 156 * @param r the input {@code SizeRequirements} object 157 * @return the new or adjusted {@code SizeRequirements} object 158 * @throws IllegalArgumentException if the {@code axis} parameter is invalid 159 */ 160 protected SizeRequirements calculateMinorAxisRequirements( 161 int axis, SizeRequirements r) { 162 r = super.calculateMinorAxisRequirements(axis, r); 163 164 if (BlockView.spanSetFromAttributes(axis, r, cssWidth, cssHeight)) { 165 // Offset by the margins so that pref/min/max return the 166 // right value. 167 int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() : 168 getTopInset() + getBottomInset(); 169 r.minimum -= margin; 170 r.preferred -= margin; 171 r.maximum -= margin; 172 } 173 return r; 174 } 175 176 177 /** 178 * Indicates whether or not this view should be 179 * displayed. If none of the children wish to be 180 * displayed and the only visible child is the 181 * break that ends the paragraph, the paragraph 182 * will not be considered visible. Otherwise, 183 * it will be considered visible and return true. 184 * 185 * @return true if the paragraph should be displayed 186 */ 187 public boolean isVisible() { 188 189 int n = getLayoutViewCount() - 1; 190 for (int i = 0; i < n; i++) { 191 View v = getLayoutView(i); 192 if (v.isVisible()) { 193 return true; 194 } 195 } 196 if (n > 0) { 197 View v = getLayoutView(n); 198 if ((v.getEndOffset() - v.getStartOffset()) == 1) { 199 return false; 200 } 201 } 202 // If it's the last paragraph and not editable, it shouldn't 203 // be visible. 204 if (getStartOffset() == getDocument().getLength()) { 205 boolean editable = false; 206 Component c = getContainer(); 207 if (c instanceof JTextComponent) { 208 editable = ((JTextComponent)c).isEditable(); 209 } 210 if (!editable) { 211 return false; 212 } 213 } 214 return true; 215 } 216 217 /** 218 * Renders using the given rendering surface and area on that 219 * surface. This is implemented to delegate to the superclass 220 * after stashing the base coordinate for tab calculations. 221 * 222 * @param g the rendering surface to use 223 * @param a the allocated region to render into 224 * @see View#paint 225 */ 226 public void paint(Graphics g, Shape a) { 227 if (a == null) { 228 return; 229 } 230 231 Rectangle r; 232 if (a instanceof Rectangle) { 233 r = (Rectangle) a; 234 } else { 235 r = a.getBounds(); 236 } 237 painter.paint(g, r.x, r.y, r.width, r.height, this); 238 super.paint(g, a); 239 } 240 241 /** 242 * Determines the preferred span for this view. Returns 243 * 0 if the view is not visible, otherwise it calls the 244 * superclass method to get the preferred span. 245 * axis. 246 * 247 * @param axis may be either View.X_AXIS or View.Y_AXIS 248 * @return the span the view would like to be rendered into; 249 * typically the view is told to render into the span 250 * that is returned, although there is no guarantee; 251 * the parent may choose to resize or break the view 252 * @see javax.swing.text.ParagraphView#getPreferredSpan 253 */ 254 public float getPreferredSpan(int axis) { 255 if (!isVisible()) { 256 return 0; 257 } 258 return super.getPreferredSpan(axis); 259 } 260 261 /** 262 * Determines the minimum span for this view along an 263 * axis. Returns 0 if the view is not visible, otherwise 264 * it calls the superclass method to get the minimum span. 265 * 266 * @param axis may be either {@code View.X_AXIS} or 267 * {@code View.Y_AXIS} 268 * @return the minimum span the view can be rendered into 269 * @see javax.swing.text.ParagraphView#getMinimumSpan 270 */ 271 public float getMinimumSpan(int axis) { 272 if (!isVisible()) { 273 return 0; 274 } 275 return super.getMinimumSpan(axis); 276 } 277 278 /** 279 * Determines the maximum span for this view along an 280 * axis. Returns 0 if the view is not visible, otherwise 281 * it calls the superclass method ot get the maximum span. 282 * 283 * @param axis may be either {@code View.X_AXIS} or 284 * {@code View.Y_AXIS} 285 * @return the maximum span the view can be rendered into 286 * @see javax.swing.text.ParagraphView#getMaximumSpan 287 */ 288 public float getMaximumSpan(int axis) { 289 if (!isVisible()) { 290 return 0; 291 } 292 return super.getMaximumSpan(axis); 293 } 294 295 private AttributeSet attr; 296 private StyleSheet.BoxPainter painter; 297 private CSS.LengthValue cssWidth; 298 private CSS.LengthValue cssHeight; 299 }