1 7 package javax.swing.text.html; 8 9 import java.awt.*; 10 import java.text.BreakIterator ; 11 import javax.swing.event.DocumentEvent ; 12 import javax.swing.text.*; 13 14 21 public class InlineView extends LabelView { 22 23 28 public InlineView(Element elem) { 29 super(elem); 30 StyleSheet sheet = getStyleSheet(); 31 attr = sheet.getViewAttributes(this); 32 } 33 34 46 public void insertUpdate(DocumentEvent e, Shape a, ViewFactory f) { 47 super.insertUpdate(e, a, f); 48 longestWordSpan = -1.0f; 49 } 50 51 63 public void removeUpdate(DocumentEvent e, Shape a, ViewFactory f) { 64 super.removeUpdate(e, a, f); 65 longestWordSpan = -1.0f; 66 } 67 68 77 public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) { 78 super.changedUpdate(e, a, f); 79 StyleSheet sheet = getStyleSheet(); 80 attr = sheet.getViewAttributes(this); 81 longestWordSpan = -1.0f; 82 preferenceChanged(null, true, true); 83 } 84 85 90 public AttributeSet getAttributes() { 91 return attr; 92 } 93 94 132 public int getBreakWeight(int axis, float pos, float len) { 133 if (nowrap) { 134 return BadBreakWeight; 135 } 136 return super.getBreakWeight(axis, pos, len); 137 } 138 139 164 public View breakView(int axis, int offset, float pos, float len) { 165 InlineView view = (InlineView )super.breakView(axis, offset, pos, len); 166 if (view != this) { 167 view.longestWordSpan = -1; 168 } 169 return view; 170 } 171 172 175 float getLongestWordSpan() { 176 if (longestWordSpan < 0.0f) { 177 longestWordSpan = calculateLongestWordSpan(); 178 } 179 return longestWordSpan; 180 } 181 182 float calculateLongestWordSpan() { 183 float rv = 0f; 184 Document doc = getDocument(); 185 final Object MultiByteProperty = "multiByte"; 187 if (doc != null && 188 Boolean.TRUE.equals(doc.getProperty(MultiByteProperty))) { 189 rv = calculateLongestWordSpanUseBreakIterator(); 190 } else { 191 rv = calculateLongestWordSpanUseWhitespace(); 192 } 193 return rv; 194 } 195 196 float calculateLongestWordSpanUseBreakIterator() { 197 float span = 0; 198 Document doc = getDocument(); 199 int p0 = getStartOffset(); 200 int p1 = getEndOffset(); 201 if (p1 > p0) { 202 try { 203 FontMetrics metrics = getFontMetrics(); 204 Segment segment = new Segment(); 205 doc.getText(p0, p1 - p0, segment); 206 Container c = getContainer(); 207 BreakIterator line; 208 if (c != null) { 209 line = BreakIterator.getLineInstance(c.getLocale()); 210 } else { 211 line = BreakIterator.getLineInstance(); 212 } 213 line.setText(segment); 214 int start = line.first(); 215 for (int end = line.next(); 216 end != BreakIterator.DONE; 217 start = end, end = line.next()) { 218 if (end > start) { 219 span = Math.max(span, 220 metrics.charsWidth(segment.array, start, 221 end - start)); 222 } 223 } 224 } catch (BadLocationException ble) { 225 } 227 } 228 return span; 229 } 230 231 float calculateLongestWordSpanUseWhitespace() { 232 float span = 0; 233 Document doc = getDocument(); 234 int p0 = getStartOffset(); 235 int p1 = getEndOffset(); 236 if (p1 > p0) { 237 try { 238 Segment segment = new Segment(); 239 doc.getText(p0, p1 - p0, segment); 240 final int CONTENT = 0; 241 final int SPACES = 1; 242 int state = CONTENT; 243 int start = segment.offset; 244 int end = start; 245 FontMetrics metrics = getFontMetrics(); 246 final int lastIndex = segment.offset + segment.count - 1; 247 for (int i = segment.offset; i <= lastIndex; i++) { 248 boolean updateSpan = false; 249 if (Character.isWhitespace(segment.array[i])) { 250 if (state == CONTENT) { 251 updateSpan = true; 253 state = SPACES; 254 } 255 } else { 256 if (state == SPACES) { 257 start = i; 259 end = start; 260 state = CONTENT; 261 } else { 262 end = i; 263 } 264 if (i == lastIndex) { 266 updateSpan = true; 267 } 268 } 269 if (updateSpan) { 270 if (end > start) { 271 span = Math.max(span, 272 metrics.charsWidth(segment.array, start, 273 end - start + 1)); 274 } 275 } 276 277 } 278 } catch (BadLocationException ble) { 279 } 281 } 282 return span; 283 } 284 285 288 protected void setPropertiesFromAttributes() { 289 super.setPropertiesFromAttributes(); 290 AttributeSet a = getAttributes(); 291 Object decor = a.getAttribute(CSS.Attribute.TEXT_DECORATION); 292 boolean u = (decor != null) ? 293 (decor.toString().indexOf("underline") >= 0) : false; 294 setUnderline(u); 295 boolean s = (decor != null) ? 296 (decor.toString().indexOf("line-through") >= 0) : false; 297 setStrikeThrough(s); 298 Object vAlign = a.getAttribute(CSS.Attribute.VERTICAL_ALIGN); 299 s = (vAlign != null) ? (vAlign.toString().indexOf("sup") >= 0) : false; 300 setSuperscript(s); 301 s = (vAlign != null) ? (vAlign.toString().indexOf("sub") >= 0) : false; 302 setSubscript(s); 303 304 Object whitespace = a.getAttribute(CSS.Attribute.WHITE_SPACE); 305 if ((whitespace != null) && whitespace.equals("nowrap")) { 306 nowrap = true; 307 } else { 308 nowrap = false; 309 } 310 311 HTMLDocument doc = (HTMLDocument )getDocument(); 312 Color bg = doc.getBackground(a); 314 if (bg != null) { 315 setBackground(bg); 316 } 317 } 318 319 320 protected StyleSheet getStyleSheet() { 321 HTMLDocument doc = (HTMLDocument ) getDocument(); 322 return doc.getStyleSheet(); 323 } 324 325 private boolean nowrap; 326 private AttributeSet attr; 327 private float longestWordSpan = -1.0f; 328 } 329 | Popular Tags |