Merged gcj-eclipse branch to trunk.

From-SVN: r120621
This commit is contained in:
Tom Tromey 2007-01-09 19:58:05 +00:00
parent c648dedbde
commit 97b8365caf
17478 changed files with 606493 additions and 100744 deletions

View file

@ -38,13 +38,17 @@ exception statement from your version. */
package javax.swing.text.html;
import java.awt.FontMetrics;
import java.awt.Shape;
import java.text.BreakIterator;
import javax.swing.event.DocumentEvent;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.Element;
import javax.swing.text.LabelView;
import javax.swing.text.Segment;
import javax.swing.text.View;
import javax.swing.text.ViewFactory;
@ -59,6 +63,23 @@ public class InlineView
extends LabelView
{
/**
* The attributes used by this view.
*/
private AttributeSet attributes;
/**
* The span of the longest word in this view.
*
* @see #getLongestWord()
*/
private float longestWord;
/**
* Indicates if we may wrap or not.
*/
private boolean nowrap;
/**
* Creates a new <code>InlineView</code> that renders the specified element.
*
@ -115,6 +136,9 @@ public class InlineView
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f)
{
super.changedUpdate(e, a, f);
StyleSheet ss = getStyleSheet();
attributes = ss.getViewAttributes(this);
preferenceChanged(null, true, true);
setPropertiesFromAttributes();
}
@ -126,15 +150,23 @@ public class InlineView
*/
public AttributeSet getAttributes()
{
// FIXME: Implement this.
return super.getAttributes();
if (attributes == null)
{
StyleSheet ss = getStyleSheet();
attributes = ss.getViewAttributes(this);
}
return attributes;
}
public int getBreakWeight(int axis, float pos, float len)
{
// FIXME: Implement this.
return super.getBreakWeight(axis, pos, len);
int weight;
if (nowrap)
weight = BadBreakWeight;
else
weight = super.getBreakWeight(axis, pos, len);
return weight;
}
public View breakView(int axis, int offset, float pos, float len)
@ -143,10 +175,48 @@ public class InlineView
return super.breakView(axis, offset, pos, len);
}
/**
* Loads the character style properties from the stylesheet.
*/
protected void setPropertiesFromAttributes()
{
// FIXME: Implement this.
super.setPropertiesFromAttributes();
AttributeSet atts = getAttributes();
Object o = atts.getAttribute(CSS.Attribute.TEXT_DECORATION);
// Check for underline.
boolean b = false;
if (o != null && o.toString().contains("underline"))
b = true;
setUnderline(b);
// Check for line-through.
b = false;
if (o != null && o.toString().contains("line-through"))
b = true;
setStrikeThrough(b);
// Check for vertical alignment (subscript/superscript).
o = atts.getAttribute(CSS.Attribute.VERTICAL_ALIGN);
// Subscript.
b = false;
if (o != null && o.toString().contains("sub"))
b = true;
setSubscript(b);
// Superscript.
b = false;
if (o != null && o.toString().contains("sup"))
b = true;
setSuperscript(b);
// Fetch nowrap setting.
o = atts.getAttribute(CSS.Attribute.WHITE_SPACE);
if (o != null && o.equals("nowrap"))
nowrap = true;
else
nowrap = false;
}
/**
@ -163,4 +233,75 @@ public class InlineView
styleSheet = ((HTMLDocument) doc).getStyleSheet();
return styleSheet;
}
/**
* Returns the minimum span for the specified axis. This returns the
* width of the longest word for the X axis and the super behaviour for
* the Y axis. This is a slight deviation from the reference implementation.
* IMO this should improve rendering behaviour so that an InlineView never
* gets smaller than the longest word in it.
*/
public float getMinimumSpan(int axis)
{
float min = super.getMinimumSpan(axis);
if (axis == X_AXIS)
min = Math.max(getLongestWord(), min);
return min;
}
/**
* Returns the span of the longest word in this view.
*
* @return the span of the longest word in this view
*/
private float getLongestWord()
{
if (longestWord == -1)
longestWord = calculateLongestWord();
return longestWord;
}
/**
* Calculates the span of the longest word in this view.
*
* @return the span of the longest word in this view
*/
private float calculateLongestWord()
{
float span = 0;
try
{
Document doc = getDocument();
int p0 = getStartOffset();
int p1 = getEndOffset();
Segment s = new Segment();
doc.getText(p0, p1 - p0, s);
BreakIterator iter = BreakIterator.getWordInstance();
iter.setText(s);
int wordStart = p0;
int wordEnd = p0;
int start = iter.first();
for (int end = iter.next(); end != BreakIterator.DONE;
start = end, end = iter.next())
{
if ((end - start) > (wordEnd - wordStart))
{
wordStart = start;
wordEnd = end;
}
}
if (wordEnd - wordStart > 0)
{
FontMetrics fm = getFontMetrics();
int offset = s.offset + wordStart - s.getBeginIndex();
span = fm.charsWidth(s.array, offset, wordEnd - wordStart);
}
}
catch (BadLocationException ex)
{
// Return 0.
}
return span;
}
}