1 7 package javax.swing.text.html; 8 9 import java.awt.*; 10 import javax.swing.SizeRequirements ; 11 import javax.swing.event.DocumentEvent ; 12 import javax.swing.text.Document ; 13 import javax.swing.text.Element ; 14 import javax.swing.text.AttributeSet ; 15 import javax.swing.text.StyleConstants ; 16 import javax.swing.text.View ; 17 import javax.swing.text.ViewFactory ; 18 import javax.swing.text.BadLocationException ; 19 import javax.swing.text.JTextComponent ; 20 21 28 29 public class ParagraphView extends javax.swing.text.ParagraphView { 30 31 36 public ParagraphView(Element elem) { 37 super(elem); 38 } 39 40 57 public void setParent(View parent) { 58 super.setParent(parent); 59 if (parent != null) { 60 setPropertiesFromAttributes(); 61 } 62 } 63 64 69 public AttributeSet getAttributes() { 70 if (attr == null) { 71 StyleSheet sheet = getStyleSheet(); 72 attr = sheet.getViewAttributes(this); 73 } 74 return attr; 75 } 76 77 82 protected void setPropertiesFromAttributes() { 83 StyleSheet sheet = getStyleSheet(); 84 attr = sheet.getViewAttributes(this); 85 painter = sheet.getBoxPainter(attr); 86 if (attr != null) { 87 super.setPropertiesFromAttributes(); 88 setInsets((short) painter.getInset(TOP, this), 89 (short) painter.getInset(LEFT, this), 90 (short) painter.getInset(BOTTOM, this), 91 (short) painter.getInset(RIGHT, this)); 92 Object o = attr.getAttribute(CSS.Attribute.TEXT_ALIGN); 93 if (o != null) { 94 String ta = o.toString(); 96 if (ta.equals("left")) { 97 setJustification(StyleConstants.ALIGN_LEFT); 98 } else if (ta.equals("center")) { 99 setJustification(StyleConstants.ALIGN_CENTER); 100 } else if (ta.equals("right")) { 101 setJustification(StyleConstants.ALIGN_RIGHT); 102 } else if (ta.equals("justify")) { 103 setJustification(StyleConstants.ALIGN_JUSTIFIED); 104 } 105 } 106 cssWidth = (CSS.LengthValue )attr.getAttribute( 108 CSS.Attribute.WIDTH); 109 cssHeight = (CSS.LengthValue )attr.getAttribute( 110 CSS.Attribute.HEIGHT); 111 } 112 } 113 114 protected StyleSheet getStyleSheet() { 115 HTMLDocument doc = (HTMLDocument ) getDocument(); 116 return doc.getStyleSheet(); 117 } 118 119 120 127 protected SizeRequirements calculateMinorAxisRequirements(int axis, SizeRequirements r) { 128 r = super.calculateMinorAxisRequirements(axis, r); 129 130 if (!BlockView.spanSetFromAttributes(axis, r, cssWidth, cssHeight)) { 131 134 float min = 0; 136 int n = getLayoutViewCount(); 137 for (int i = 0; i < n; i++) { 138 View v = getLayoutView(i); 139 if (v instanceof InlineView ) { 140 float wordSpan = ((InlineView ) v).getLongestWordSpan(); 141 min = Math.max(wordSpan, min); 142 } else { 143 min = Math.max(v.getMinimumSpan(axis), min); 144 } 145 } 146 r.minimum = Math.max(r.minimum, (int) min); 147 r.preferred = Math.max(r.minimum, r.preferred); 148 r.maximum = Math.max(r.preferred, r.maximum); 149 } 150 else { 151 int margin = (axis == X_AXIS) ? getLeftInset() + getRightInset() : 154 getTopInset() + getBottomInset(); 155 r.minimum -= margin; 156 r.preferred -= margin; 157 r.maximum -= margin; 158 } 159 return r; 160 } 161 162 163 173 public boolean isVisible() { 174 175 int n = getLayoutViewCount() - 1; 176 for (int i = 0; i < n; i++) { 177 View v = getLayoutView(i); 178 if (v.isVisible()) { 179 return true; 180 } 181 } 182 if (n > 0) { 183 View v = getLayoutView(n); 184 if ((v.getEndOffset() - v.getStartOffset()) == 1) { 185 return false; 186 } 187 } 188 if (getStartOffset() == getDocument().getLength()) { 191 boolean editable = false; 192 Component c = getContainer(); 193 if (c instanceof JTextComponent ) { 194 editable = ((JTextComponent )c).isEditable(); 195 } 196 if (!editable) { 197 return false; 198 } 199 } 200 return true; 201 } 202 203 212 public void paint(Graphics g, Shape a) { 213 if (a == null) { 214 return; 215 } 216 217 Rectangle r; 218 if (a instanceof Rectangle) { 219 r = (Rectangle) a; 220 } else { 221 r = a.getBounds(); 222 } 223 painter.paint(g, r.x, r.y, r.width, r.height, this); 224 super.paint(g, a); 225 } 226 227 240 public float getPreferredSpan(int axis) { 241 if (!isVisible()) { 242 return 0; 243 } 244 return super.getPreferredSpan(axis); 245 } 246 247 257 public float getMinimumSpan(int axis) { 258 if (!isVisible()) { 259 return 0; 260 } 261 return super.getMinimumSpan(axis); 262 } 263 264 274 public float getMaximumSpan(int axis) { 275 if (!isVisible()) { 276 return 0; 277 } 278 return super.getMaximumSpan(axis); 279 } 280 281 private AttributeSet attr; 282 private StyleSheet.BoxPainter painter; 283 private CSS.LengthValue cssWidth; 284 private CSS.LengthValue cssHeight; 285 } 286 287 | Popular Tags |