下面列出了javax.swing.event.DocumentEvent#ElementChange() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public @Override DocumentEvent.ElementChange getChange(Element elem) {
// Super of getChange()
if (changeLookup2 != null) {
return (DocumentEvent.ElementChange) changeLookup2.get(elem);
}
int n = edits.size();
for (int i = 0; i < n; i++) {
Object o = edits.elementAt(i);
if (o instanceof DocumentEvent.ElementChange) {
DocumentEvent.ElementChange c = (DocumentEvent.ElementChange) o;
if (c.getElement() == elem) {
return c;
}
}
}
return null;
// End super of getChange()
}
private void checkEventsEqual(DocumentEvent testEvent) {
if (masterEvent.getOffset() != testEvent.getOffset()) {
fail("masterEvent.getOffset()=" + masterEvent.getOffset()
+ " != testEvent.getOffset()=" + testEvent.getOffset());
}
if (masterEvent.getLength() != testEvent.getLength()) {
fail("masterEvent.getLength()=" + masterEvent.getLength()
+ " != testEvent.getLength()=" + testEvent.getLength());
}
if (masterEvent.getType() != testEvent.getType()) {
fail("masterEvent.getType()=" + masterEvent.getType()
+ " != testEvent.getType()=" + testEvent.getType());
}
DocumentEvent.ElementChange masterChange = masterEvent.getChange(masterDoc.getDefaultRootElement());
DocumentEvent.ElementChange testChange = testEvent.getChange(testDoc.getDefaultRootElement());
checkElementChangesEqual(masterChange, testChange);
}
protected void documentChanged(DocumentEvent evt) {
DocumentEvent.ElementChange ch = evt.getChange(document.getDefaultRootElement());
int count;
if (ch == null) {
count = 0;
} else {
count = ch.getChildrenAdded().length - ch.getChildrenRemoved().length;
}
int line = getLineOfOffset(evt.getOffset());
if (count == 0) {
painter.invalidateLine(line);
}
// do magic stuff
else if (line < firstLine) {
setFirstLine(firstLine + count);
}
// end of magic stuff
else {
painter.invalidateLineRange(line, firstLine + visibleLines);
updateScrollBars();
}
}
/**
* Forwards the given <code>DocumentEvent</code> to the child views
* that need to be notified of the change to the model.
* If a child changed its requirements and the allocation
* was valid prior to forwarding the portion of the box
* from the starting child to the end of the box will
* be repainted.
*
* @param ec changes to the element this view is responsible
* for (may be <code>null</code> if there were no changes)
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
* @since 1.3
*/
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
boolean wasValid = isLayoutValid(majorAxis);
super.forwardUpdate(ec, e, a, f);
// determine if a repaint is needed
if (wasValid && (! isLayoutValid(majorAxis))) {
// Repaint is needed because one of the tiled children
// have changed their span along the major axis. If there
// is a hosting component and an allocated shape we repaint.
Component c = getContainer();
if ((a != null) && (c != null)) {
int pos = e.getOffset();
int index = getViewIndexAtPosition(pos);
Rectangle alloc = getInsideAllocation(a);
if (majorAxis == X_AXIS) {
alloc.x += majorOffsets[index];
alloc.width -= majorOffsets[index];
} else {
alloc.y += minorOffsets[index];
alloc.height -= minorOffsets[index];
}
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
super.forwardUpdate(ec, e, a, f);
// A change in any of the table cells usually effects the whole table,
// so redraw it all!
if (a != null) {
Component c = getContainer();
if (c != null) {
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
a.getBounds();
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
/**
* Forwards the given <code>DocumentEvent</code> to the child views
* that need to be notified of the change to the model.
* If a child changed its requirements and the allocation
* was valid prior to forwarding the portion of the box
* from the starting child to the end of the box will
* be repainted.
*
* @param ec changes to the element this view is responsible
* for (may be <code>null</code> if there were no changes)
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
* @since 1.3
*/
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
boolean wasValid = isLayoutValid(majorAxis);
super.forwardUpdate(ec, e, a, f);
// determine if a repaint is needed
if (wasValid && (! isLayoutValid(majorAxis))) {
// Repaint is needed because one of the tiled children
// have changed their span along the major axis. If there
// is a hosting component and an allocated shape we repaint.
Component c = getContainer();
if ((a != null) && (c != null)) {
int pos = e.getOffset();
int index = getViewIndexAtPosition(pos);
Rectangle alloc = getInsideAllocation(a);
if (majorAxis == X_AXIS) {
alloc.x += majorOffsets[index];
alloc.width -= majorOffsets[index];
} else {
alloc.y += minorOffsets[index];
alloc.height -= minorOffsets[index];
}
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
/**
* Forwards the given <code>DocumentEvent</code> to the child views
* that need to be notified of the change to the model.
* If a child changed its requirements and the allocation
* was valid prior to forwarding the portion of the box
* from the starting child to the end of the box will
* be repainted.
*
* @param ec changes to the element this view is responsible
* for (may be <code>null</code> if there were no changes)
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
* @since 1.3
*/
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
boolean wasValid = isLayoutValid(majorAxis);
super.forwardUpdate(ec, e, a, f);
// determine if a repaint is needed
if (wasValid && (! isLayoutValid(majorAxis))) {
// Repaint is needed because one of the tiled children
// have changed their span along the major axis. If there
// is a hosting component and an allocated shape we repaint.
Component c = getContainer();
if ((a != null) && (c != null)) {
int pos = e.getOffset();
int index = getViewIndexAtPosition(pos);
Rectangle alloc = getInsideAllocation(a);
if (majorAxis == X_AXIS) {
alloc.x += majorOffsets[index];
alloc.width -= majorOffsets[index];
} else {
alloc.y += minorOffsets[index];
alloc.height -= minorOffsets[index];
}
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
/**
* Forwards the given <code>DocumentEvent</code> to the child views
* that need to be notified of the change to the model.
* If a child changed its requirements and the allocation
* was valid prior to forwarding the portion of the box
* from the starting child to the end of the box will
* be repainted.
*
* @param ec changes to the element this view is responsible
* for (may be <code>null</code> if there were no changes)
* @param e the change information from the associated document
* @param a the current allocation of the view
* @param f the factory to use to rebuild if the view has children
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
* @since 1.3
*/
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
boolean wasValid = isLayoutValid(majorAxis);
super.forwardUpdate(ec, e, a, f);
// determine if a repaint is needed
if (wasValid && (! isLayoutValid(majorAxis))) {
// Repaint is needed because one of the tiled children
// have changed their span along the major axis. If there
// is a hosting component and an allocated shape we repaint.
Component c = getContainer();
if ((a != null) && (c != null)) {
int pos = e.getOffset();
int index = getViewIndexAtPosition(pos);
Rectangle alloc = getInsideAllocation(a);
if (majorAxis == X_AXIS) {
alloc.x += majorOffsets[index];
alloc.width -= majorOffsets[index];
} else {
alloc.y += minorOffsets[index];
alloc.height -= minorOffsets[index];
}
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
super.forwardUpdate(ec, e, a, f);
// A change in any of the table cells usually effects the whole table,
// so redraw it all!
if (a != null) {
Component c = getContainer();
if (c != null) {
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
a.getBounds();
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
protected void forwardUpdate(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a, ViewFactory f) {
super.forwardUpdate(ec, e, a, f);
// A change in any of the table cells usually effects the whole table,
// so redraw it all!
if (a != null) {
Component c = getContainer();
if (c != null) {
Rectangle alloc = (a instanceof Rectangle) ? (Rectangle)a :
a.getBounds();
c.repaint(alloc.x, alloc.y, alloc.width, alloc.height);
}
}
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory f) {
return false;
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory f) {
return false;
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory f) {
return false;
}
private void assertEventsIdentical (Document styled, OutputDocument doc, DocumentEvent styEvent, DocumentEvent docEvent) throws Exception {
int docOffset = docEvent.getOffset();
int styOffset = styEvent.getOffset();
int docLength = docEvent.getLength();
int styLength = styEvent.getLength();
assertTrue ("OutputDocument event offset is " + docOffset + " but " +
"offset of identical change to a StyledDocument is " + styOffset,
docOffset == styOffset);
assertTrue ("OutputDocument event length is " + docLength + " but " +
"length from identical change to a StyledDocument is " +
styLength, styLength == docLength);
DocumentEvent.ElementChange docEc = docEvent.getChange(doc);
DocumentEvent.ElementChange styEc = styEvent.getChange(styled.getDefaultRootElement());
int docIndex = docEc.getIndex();
int styIndex = styEc.getIndex();
Element[] docAdded = docEc.getChildrenAdded();
Element[] styAdded = styEc.getChildrenAdded();
assertTrue ("Index of change in OutputDocument was " + docIndex + " but" +
" an identical change on a StyledDocument returns " + styIndex,
styIndex == docIndex);
/*assertTrue ("OutputDocument returned an array of " + docAdded.length +
" affected elements, but an identical change on a StyledDocument " +
"produces an array of " + styAdded.length, styAdded.length ==
docAdded.length);*/
for (int i=0; i < styAdded.length; i++) {
int docStartOffset = docAdded[i].getStartOffset();
int styStartOffset = styAdded[i].getStartOffset();
assertTrue ("Start offset of element " + i + " from " +
"OutputDocument.ODDEvent.EC is " + docStartOffset + " but " +
"offset from identical change in a StyledDocument is " +
styStartOffset, styStartOffset == docStartOffset);
int docEndOffset = docAdded[i].getEndOffset();
int styEndOffset = styAdded[i].getEndOffset();
assertTrue ("End offset of element " + i + " from " +
"OutputDocument.ODDEvent.EC is " + docStartOffset + " but " +
"offset from identical change in a StyledDocument is " +
styEndOffset, styEndOffset == docEndOffset);
String styTxt = styled.getText(styAdded[i].getStartOffset(), styAdded[i].getEndOffset() - styAdded[i].getStartOffset());
String docTxt = styled.getText(styAdded[i].getStartOffset(), styAdded[i].getEndOffset() - styAdded[i].getStartOffset());
assertEquals("Element " + i + " text from styled document is " +
styTxt + " but OutputDocument return " + docTxt + " for the " +
"same indices", styTxt, docTxt);
}
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory f) {
return false;
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory f) {
return false;
}
protected boolean updateChildren(DocumentEvent.ElementChange ec,
DocumentEvent e, ViewFactory f) {
return false;
}
/**
* Update the layout in response to receiving notification of
* change from the model. This is implemented to note the
* change on the ChildLocator so that offsets of the children
* will be correctly computed.
*
* @param ec changes to the element this view is responsible
* for (may be null if there were no changes).
* @param e the change information from the associated document
* @param a the current allocation of the view
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
*/
protected void updateLayout(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a) {
if (ec != null) {
// the newly inserted children don't have a valid
// offset so the child locator needs to be messaged
// that the child prior to the new children has
// changed size.
int index = Math.max(ec.getIndex() - 1, 0);
ChildState cs = getChildState(index);
locator.childChanged(cs);
}
}
/**
* Update the layout in response to receiving notification of
* change from the model. This is implemented to note the
* change on the ChildLocator so that offsets of the children
* will be correctly computed.
*
* @param ec changes to the element this view is responsible
* for (may be null if there were no changes).
* @param e the change information from the associated document
* @param a the current allocation of the view
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
*/
protected void updateLayout(DocumentEvent.ElementChange ec,
DocumentEvent e, Shape a) {
if (ec != null) {
// the newly inserted children don't have a valid
// offset so the child locator needs to be messaged
// that the child prior to the new children has
// changed size.
int index = Math.max(ec.getIndex() - 1, 0);
ChildState cs = getChildState(index);
locator.childChanged(cs);
}
}
/**
* Update the layout in response to receiving notification of
* change from the model.
*
* @param ec changes to the element this view is responsible
* for (may be null if there were no changes).
* @param e the change information from the associated document
* @param a the current allocation of the view
* @see #insertUpdate
* @see #removeUpdate
* @see #changedUpdate
*/
protected @Override void updateLayout(DocumentEvent.ElementChange ec, DocumentEvent e, Shape a) {
//super.updateLayout(ec, e, a);
}