Beruflich Dokumente
Kultur Dokumente
html
Then we correct minimum paragraph’s span calculation to provide correct feedback to container and let
container get real span. This fixes some issues when JEditorPane is added into JScrollPane.
The result is a paragraph with no wrap. If we need to switch warp on/off we can define a flag and call
super.layout() or custom layout depending on what we need – wrap/no wrap.
LabelView class represents separate fragment of text with the same attributes. ParagraphView layout
LabelViews in rows breaking labels if necessary. First step of layout is creation of single very long line (pool
of LabelViews) which will be continuously broken to fit available paragraph space.
Paragraph creates a row and adds to the row the first LabelView from the pool and measure current row. If
the width of row after adding the label is less than available paragraph width the next label is added till the
row width exceeds the available width. As soon as the width is exceeded paragraph starts to search where the
row can be broken. It means paragraph need an offset where we can break view. All the labels in the row are
asked for break weight. A quote from javadoc: “<break weight > - Determines how attractive a break
opportunity in this view is. This can be used for determining which view is the most attractive to call breakView
method on in the process of formatting. The higher the weight, the more attractive the break. A value equal to
or lower than View.BadBreakWeight should not be considered for a break. A value greater than or equal to
View.ForcedBreakWeight should be broken.”
There are four base break weights the LabelView can return:
View.BadBreakWeight – means no break possible.
View.GoodBreakWeight – can break if there is no better opportunity.
View.ExcellentBreakWeight – it’s preferred to break the view.
View.ForcedBreakWeight – the view must be broken.
In most cases LabelViews returns View.ExcellentBreakWeight if there is a space chars ‘ ‘, ‘\t’ etc. (Word
wrap supposes to break views by spaces) and View.GoodBreakWeight if there is no space but only letters. To
explain this imagine following. You type a very long string of characters and ask for break weight. If there is a
space we would prefer to break by space and start a new word on a new row. If there is no space we can break
in any possible place to fit our row in available paragraph width.
1 of 3 3/9/2011 12:04 AM
"Forced line wrap" and "No wrap" in the JEditorPane http://java-sl.com/wrap.html
We also associate line break insertion with SHIFT+ENTER key combination. The code looks like this:
and change code to break the label by the ‘\r’ char. Like this:
public View breakView(int axis, int p0, float pos, float len) {
if (axis == View.X_AXIS) {
checkPainter();
int p1 = getGlyphPainter().getBoundedPosition(this, p0, pos, len);
try {
//if the view contains line break char break the view
int index = getDocument().getText(p0, p1 - p0).indexOf("\r");
if (index >= 0) {
GlyphView v = (GlyphView) createFragment(p0, p0 + index + 1);
return v;
}
}
catch (BadLocationException ex) {
//should never happen
}
}
return super.breakView(axis, p0, pos, len);
}
The final step is extending kit. All we need it to replace SUN’s views with own implementation and remove the
2 of 3 3/9/2011 12:04 AM
"Forced line wrap" and "No wrap" in the JEditorPane http://java-sl.com/wrap.html
Appendix
3 of 3 3/9/2011 12:04 AM