我遇到了上述方法的奇怪行为。有一个用于显示 xml 文件内容的遗留组件,它在 java6 中工作,但是当我切换到 1.7_10 时它会失败,因为结束偏移超出了视图的范围。
我对这个问题进行了一些研究,发现了以下方法 javax.swing.text.ParagraphView.findEdgeSpan(View, int, int, int, int)
这是方法的代码:
/**
* Binary search for the longest non-breakable fragment at the view edge.
*/
private float findEdgeSpan(View v, int axis, int fp, int p0, int p1) {
int len = p1 - p0;
if (len <= 1) {
// further fragmentation is not possible
return v.getMinimumSpan(axis);
} else {
int mid = p0 + len / 2;
boolean startEdge = mid > fp;
// initial view is breakable hence must support fragmentation
View f = startEdge ?
v.createFragment(fp, mid) : v.createFragment(mid, fp);
boolean breakable = f.getBreakWeight(
axis, 0, f.getMaximumSpan(axis)) > View.BadBreakWeight;
if (breakable == startEdge) {
p1 = mid;
} else {
p0 = mid;
}
return findEdgeSpan(f, axis, fp, p0, p1);
}
}
它创建视图的片段,然后在该片段上运行,但偏移边界取自外部视图。并且可能会发生传递给createFragment
的束缚超出视野f
边界的情况。
有人可以解释这是否是正确的行为吗?
在 javadoc 中javax.swing.text.View.createFragment
:
Parameters:
p0 the starting offset >= 0. This should be a value greater or equal to the element starting offset and less than the element ending offset.
p1 the ending offset > p0. This should be a value less than or equal to the elements end offset and greater than the elements starting offset.
据我了解,p1 不应大于视图的 endOffset,不是吗?