protected void updateByOpTree(javax.swing.text.DocumentFilter.FilterBypass fb) { String oldStr = JMathTextField.this.getText(); String newStr = operatorTree.toString(); int commonStart = Utils.equalStartLen(oldStr, newStr); int commonEnd = Utils.equalEndLen(oldStr, newStr); if (commonEnd + commonStart >= Math.min(oldStr.length(), newStr.length())) commonEnd = Math.min(oldStr.length(), newStr.length()) - commonStart; try { fb.replace( commonStart, oldStr.length() - commonEnd - commonStart, newStr.substring(commonStart, newStr.length() - commonEnd), null); } catch (BadLocationException e) { // this should not happen. we should have checked this. this whole function should be safe throw new AssertionError(e); } }