我会因为对此缺乏特异性而提前道歉,但我不确定这是如何引起的。
我们有一个扩展javax.swing.text.DefaultStyledDocument
的XMLDocument如果我们进行快速撤消/重做调用(例如,按住ctrl + z或ctrl + shift + z),AWT EventQueue线程会抛出异常:
javax.swing.text.BadLocationException: Length must be positive
getText():810, AbstractDocument {javax.swing.text}
getText():135, GlyphView {javax.swing.text}
getSpan():60, GlyphPainter1 {javax.swing.text}
getPreferredSpan():592, GlyphView {javax.swing.text}
getPreferredSpan():732, FlowView$LogicalView {javax.swing.text}
calculateMinorAxisRequirements():233, FlowView {javax.swing.text}
calculateMinorAxisRequirements():724, ParagraphView {javax.swing.text}
checkRequests():935, BoxView {javax.swing.text}
getMinimumSpan():568, BoxView {javax.swing.text}
calculateMinorAxisRequirements():903, BoxView {javax.swing.text}
checkRequests():935, BoxView {javax.swing.text}
setSpanOnAxis():343, BoxView {javax.swing.text}
layout():708, BoxView {javax.swing.text}
setSize():397, BoxView {javax.swing.text}
setSize():1714, BasicTextUI$RootView {javax.swing.plaf.basic}
modelToView():1046, BasicTextUI {javax.swing.plaf.basic}
repaintNewCaret():1311, DefaultCaret {javax.swing.text}
run():1290, DefaultCaret$1 {javax.swing.text}
dispatch():312, InvocationEvent {java.awt.event}
dispatchEventImpl():733, EventQueue {java.awt}
access$200():103, EventQueue {java.awt}
run():694, EventQueue$3 {java.awt}
run():692, EventQueue$3 {java.awt}
doPrivileged():-1, AccessController {java.security}
doIntersectionPrivilege():76, ProtectionDomain$1 {java.security}
dispatchEvent():703, EventQueue {java.awt}
pumpOneEventForFilters():242, EventDispatchThread {java.awt}
pumpEventsForFilter():161, EventDispatchThread {java.awt}
pumpEventsForHierarchy():150, EventDispatchThread {java.awt}
pumpEvents():146, EventDispatchThread {java.awt}
pumpEvents():138, EventDispatchThread {java.awt}
run():91, EventDispatchThread {java.awt}
现在,显然这是从AWT更新线程调用的。
它摒弃的实际元素是:
[4] = {javax.swing.text.AbstractDocument$LeafElement@6621}"LeafElement(content) 223,217\n"
p0 = {javax.swing.text.GapContent$StickyPosition@6652}"223"
p1 = {javax.swing.text.GapContent$StickyPosition@6655}"217"
this$0 = {com.timetra.nms.client.gui.script.editor.document.XmlApiDocument@6211}
parent = {javax.swing.text.AbstractDocument$BranchElement@6585}"BranchElement(paragraph) 191,238\n"
attributes = {javax.swing.text.StyleContext$SmallAttributeSet@6656}"{family=Courier,size=12,foreground=java.awt.Color[r=0,g=0,b=0],}"
this$0 = {com.timetra.nms.client.gui.script.editor.document.XmlApiDocument@6211}
(取自intelliJ调试器)。
你可以看到p0和p1不正确。而且,由于数组中的[3]和[5]元素与范围匹配,因此该元素不应该存在:
[3] = {javax.swing.text.AbstractDocument$LeafElement@6620}"LeafElement(content) 215,223\n"
[4] = {javax.swing.text.AbstractDocument$LeafElement@6621}"LeafElement(content) 223,217\n"
[5] = {javax.swing.text.AbstractDocument$LeafElement@6622}"LeafElement(content) 223,237\n"
我认为我错过了同步或SwingUtilities.runLater(...)调用,但我不知道在哪里,我已经将它们放在我想到的Undo /我们使用的重做类。
编辑:
这是如何将编辑添加到UndoManager:
public void undoableEditHappened(final UndoableEditEvent ev)
{
UndoableEdit edit = ev.getEdit();
// Include this method to ignore syntax changes
// To reduce memory consumption, do this check before adding runnable.
if (edit instanceof AbstractDocument.DefaultDocumentEvent &&
((AbstractDocument.DefaultDocumentEvent) edit).getType() ==
AbstractDocument.DefaultDocumentEvent.EventType.CHANGE)
{
return;
}
try {
UndoableEdit temp = ev.getEdit();
undoManager.addEdit(temp);
undoAction.updateState();
redoAction.updateState();
setChanged(true);
} catch (Exception e) {
e.printStackTrace();
}
}
编辑2:我发现我看到的大部分问题都是由invokeLater()runnable上的函数引起的,该函数尝试在撤消/重做事件后语法高亮显示文本。我担心即使它在EventQueue上也没有同步。是否有优先顺序或某些东西用于触发这些事件的顺序?
答案 0 :(得分:2)
推断出错误同步的可能性是正确的。由于另一个线程上的错误Document
更新,EDT上可能会发生异常。在最近的Java版本中,某些methods某些文本组件不再标记为线程安全。根据引用文章here中的建议,查找违反EDT的行为。对于初步检查,CheckThreadViolationRepaintManager
是更简单的方法。
答案 1 :(得分:0)
您必须将更改事件添加到UndoManager。在相反的情况下,文档结构已更改,但您尝试使用旧结构进行撤消。您可以合并多个UndoableEdits,请参阅java-sl.com/tip_merge_undo_edits.html
答案 2 :(得分:0)
我制定了一个解决方案,即在编辑后对文本框应用的语法突出显示的更新添加计时器。如果文本框在200ms计时器内更新,则更新将排队并在语法突出显示后应用。