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.Shape;
023: import java.text.BreakIterator;
024: import java.text.CharacterIterator;
025:
026: import javax.swing.event.DocumentEvent;
027: import javax.swing.text.AttributeSet;
028: import javax.swing.text.Element;
029: import javax.swing.text.LabelView;
030: import javax.swing.text.Segment;
031: import javax.swing.text.View;
032: import javax.swing.text.ViewFactory;
033:
034: public class InlineView extends LabelView {
035: private AttributeSet attrs;
036: private boolean canBreak;
037:
038: public InlineView(final Element elem) {
039: super (elem);
040: canBreak = canBreak();
041: }
042:
043: public int getBreakWeight(final int axis, final float x,
044: final float len) {
045: if (!canBreak) {
046: return BadBreakWeight;
047: }
048: return super .getBreakWeight(axis, x, len);
049: }
050:
051: public View breakView(final int axis, final int startOffset,
052: final float x, final float len) {
053: if (!canBreak) {
054: return this ;
055: }
056: return super .breakView(axis, startOffset, x, len);
057: }
058:
059: public AttributeSet getAttributes() {
060: if (attrs == null) {
061: attrs = getStyleSheet().getViewAttributes(this );
062: }
063: return attrs;
064: }
065:
066: public void changedUpdate(final DocumentEvent event,
067: final Shape alloc, final ViewFactory factory) {
068: attrs = getStyleSheet().getViewAttributes(this );
069: canBreak = canBreak();
070: super .changedUpdate(event, alloc, factory);
071: }
072:
073: protected StyleSheet getStyleSheet() {
074: return ((HTMLDocument) getDocument()).getStyleSheet();
075: }
076:
077: protected void setPropertiesFromAttributes() {
078: // TODO setPropertiesFromAttrs: 'text-decoration' for overline, blink
079: // TODO setPropertiesFromAttrs: 'vertical-align' for alignment???
080: super .setPropertiesFromAttributes();
081: canBreak = canBreak();
082: }
083:
084: final int getLongestWordSpan() {
085: int result = 0;
086:
087: final int startOffset = getStartOffset();
088: final int endOffset = getEndOffset();
089:
090: final BreakIterator bi = BreakIterator.getWordInstance();
091: final Segment text = getText(startOffset, endOffset);
092: bi.setText(text);
093:
094: int prev;
095: int curr = bi.first() - text.offset;
096: int next;
097: do {
098: prev = curr;
099: next = bi.next();
100: curr = next != BreakIterator.DONE ? next - text.offset
101: : endOffset - startOffset;
102: if (!isWhitespace(getText(startOffset + prev, startOffset
103: + curr))) {
104: result = Math.max(result, (int) getPartialSpan(
105: startOffset + prev, startOffset + curr));
106: }
107: } while (next != BreakIterator.DONE);
108:
109: return result;
110: }
111:
112: private boolean canBreak() {
113: Object ws = getAttributes().getAttribute(
114: CSS.Attribute.WHITE_SPACE);
115: return ws == null
116: || ((CSS.WhiteSpace) ws).getIndex() != CSS.WhiteSpace.NOWRAP;
117: }
118:
119: // TODO refactor isWhitespace: text.GlyphView and text.html.InlineView
120: private static boolean isWhitespace(final CharacterIterator text) {
121: boolean result;
122: char c = text.first();
123: do {
124: result = Character.isWhitespace(c);
125: c = text.next();
126: } while (result && c != CharacterIterator.DONE);
127: return result;
128: }
129: }
|