Java Code Examples for javax.swing.event.DocumentEvent#getChange()
The following examples show how to use
javax.swing.event.DocumentEvent#getChange() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GapBoxView.java From netbeans with Apache License 2.0 | 6 votes |
/** * Get the offset area in which the views should be rebuilt * in reaction to insert update in the underlying document. * * @param evt document event for the document modification. * @return two-item integer array containing starting and ending offset * of the area to be rebuilt or <code>null</code> in case * no views should be rebuilt. */ protected int[] getInsertUpdateRebuildOffsetRange(DocumentEvent evt) { DocumentEvent.ElementChange lineChange = evt.getChange(evt.getDocument().getDefaultRootElement()); if (lineChange == null) { return null; } int startOffset = evt.getOffset(); int endOffset = startOffset + evt.getLength(); int[] offsetRange = new int[] {startOffset, endOffset}; Element[] addedLines = lineChange.getChildrenAdded(); ElementUtilities.updateOffsetRange(addedLines, offsetRange); Element[] removedLines = lineChange.getChildrenRemoved(); ElementUtilities.updateOffsetRange(removedLines, offsetRange); return offsetRange; }
Example 2
Source File: GapBoxView.java From netbeans with Apache License 2.0 | 6 votes |
/** * Get the offset area in which the views should be rebuilt * in reaction to insert update in the underlying document. * * @param evt document event for the document modification. * @return two-item integer array containing starting and ending offset * of the area to be rebuilt or <code>null</code> in case * no views should be rebuilt. */ protected int[] getRemoveUpdateRebuildOffsetRange(DocumentEvent evt) { DocumentEvent.ElementChange lineChange = evt.getChange(evt.getDocument().getDefaultRootElement()); if (lineChange == null) { return null; } int startOffset = evt.getOffset(); int endOffset = startOffset; int[] offsetRange = new int[] {startOffset, endOffset}; Element[] addedLines = lineChange.getChildrenAdded(); ElementUtilities.updateOffsetRange(addedLines, offsetRange); Element[] removedLines = lineChange.getChildrenRemoved(); ElementUtilities.updateOffsetRange(removedLines, offsetRange); return offsetRange; }
Example 3
Source File: PlainDocumentCompatibilityRandomTest.java From netbeans with Apache License 2.0 | 6 votes |
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); }
Example 4
Source File: JEditTextArea.java From rapidminer-studio with GNU Affero General Public License v3.0 | 6 votes |
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(); } }
Example 5
Source File: OutputDocumentTest.java From netbeans with Apache License 2.0 | 5 votes |
public void testDocumentEvents() throws Exception { System.out.println("testDocumentEvents"); OutWriter ow = new OutWriter (); OutputDocument doc = new OutputDocument (ow); ODListener od = new ODListener(); doc.addDocumentListener(od); for (int i=0; i < 100; i++) { ow.println("This is string " + i); } // SwingUtilities.invokeAndWait (new Runnable() { // public void run(){ // System.currentTimeMillis(); // } // }); ow.flush(); //Thread.currentThread().sleep(1500); DocumentEvent de = od.getEvent(); assertTrue ("Event should have been fired", de != null); int elCount = doc.getElementCount(); assertTrue ("Element count should be 101 after printing 100 lines, " + "not " + elCount, elCount == 101); DocumentEvent.ElementChange ec = de.getChange(doc); Element[] added = ec.getChildrenAdded(); assertNotNull("Children added should not be null", added); // assertTrue("Number of children added should be 100, not " + added.length, added.length == 100); // assertTrue ("Index of change should be 0, not " + ec.getIndex(), ec.getIndex() == 0); }
Example 6
Source File: DocumentUtilities.java From netbeans with Apache License 2.0 | 5 votes |
/** * Sets a property for the document event, if the event supports the feature. * Returns true, if the property was actually set. * * @param evt the event to modify * @param key property key * @param value property value * @return true, if the property was set * @since 1.59 */ public static boolean putEventPropertyIfSupported(DocumentEvent evt, Object key, Object value) { EventPropertiesElementChange change = (EventPropertiesElementChange) evt.getChange(EventPropertiesElement.INSTANCE); if (change != null) { putEventProperty(evt, key, value); return true; } else { return false; } }
Example 7
Source File: DocumentUtilities.java From netbeans with Apache License 2.0 | 5 votes |
/** * Set a property of a given document event. * * @param evt non-null document event to which the property should be stored. * @param key non-null key of the property. * @param value for the given property. */ public static void putEventProperty(DocumentEvent evt, Object key, Object value) { EventPropertiesElementChange change = (EventPropertiesElementChange) evt.getChange(EventPropertiesElement.INSTANCE); if (change == null) { throw new IllegalStateException("addEventPropertyStorage() not called for evt=" + evt); // NOI18N } change.putProperty(key, value); }
Example 8
Source File: DocumentUtilities.java From netbeans with Apache License 2.0 | 5 votes |
/** * Set a property of a given document event by using the given map entry. * <br/> * The present implementation is able to directly store instances * of <code>CompactMap.MapEntry</code>. Other map entry implementations * will be delegated to {@link #putEventProperty(DocumentEvent, Object, Object)}. * * @param evt non-null document event to which the property should be stored. * @param mapEntry non-null map entry which should be stored. * Generally after this method finishes the {@link #getEventProperty(DocumentEvent, Object)} * will return <code>mapEntry.getValue()</code> for <code>mapEntry.getKey()</code> key. */ public static void putEventProperty(DocumentEvent evt, Map.Entry mapEntry) { if (mapEntry instanceof CompactMap.MapEntry) { EventPropertiesElementChange change = (EventPropertiesElementChange) evt.getChange(EventPropertiesElement.INSTANCE); if (change == null) { throw new IllegalStateException("addEventPropertyStorage() not called for evt=" + evt); // NOI18N } change.putEntry((CompactMap.MapEntry)mapEntry); } else { putEventProperty(evt, mapEntry.getKey(), mapEntry.getValue()); } }
Example 9
Source File: SyntaxDocument.java From rapidminer-studio with GNU Affero General Public License v3.0 | 5 votes |
/** * We overwrite this method to update the token marker state immediately so that any event * listeners get a consistent token marker. */ @Override protected void fireInsertUpdate(DocumentEvent evt) { if (tokenMarker != null) { DocumentEvent.ElementChange ch = evt.getChange(getDefaultRootElement()); if (ch != null) { tokenMarker.insertLines(ch.getIndex() + 1, ch.getChildrenAdded().length - ch.getChildrenRemoved().length); } } super.fireInsertUpdate(evt); }
Example 10
Source File: SyntaxDocument.java From rapidminer-studio with GNU Affero General Public License v3.0 | 5 votes |
/** * We overwrite this method to update the token marker state immediately so that any event * listeners get a consistent token marker. */ @Override protected void fireRemoveUpdate(DocumentEvent evt) { if (tokenMarker != null) { DocumentEvent.ElementChange ch = evt.getChange(getDefaultRootElement()); if (ch != null) { tokenMarker.deleteLines(ch.getIndex() + 1, ch.getChildrenRemoved().length - ch.getChildrenAdded().length); } } super.fireRemoveUpdate(evt); }
Example 11
Source File: ExtPlainView.java From netbeans with Apache License 2.0 | 4 votes |
@Override protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f) { Document doc = getDocument(); if (!(doc instanceof Document)) { super.updateDamage(changes, a, f); return; } if (longestLineLength == -1) { calcLongestLineLength(); } Component host = getContainer(); updateMetrics(); Element elem = getElement(); DocumentEvent.ElementChange ec = changes.getChange(elem); Element[] added = (ec != null) ? ec.getChildrenAdded() : null; Element[] removed = (ec != null) ? ec.getChildrenRemoved() : null; if (((added != null) && (added.length > 0)) || ((removed != null) && (removed.length > 0))) { // lines were added or removed... if (added != null) { for (int i = 0; i < added.length; i++) { int w = getLineWidth(added[i]); if (w > longestLineLength) { longestLineLength = w; longestLine = added[i]; } } } if (removed != null) { for (int i = 0; i < removed.length; i++) { if (removed[i] == longestLine) { calcLongestLineLength(); break; } } } preferenceChanged(null, true, true); host.repaint(); } else { Element map = getElement(); int line = map.getElementIndex(changes.getOffset()); damageLineRange(line, line, a, host); if (changes.getType() == DocumentEvent.EventType.INSERT) { // check to see if the line is longer than current longest line. Element e = map.getElement(line); int lineLen = getLineWidth(e); if (e == longestLine) { preferenceChanged(null, true, false); } else if (lineLen > longestLineLength) { longestLineLength = lineLen; longestLine = e; preferenceChanged(null, true, false); } } else if (changes.getType() == DocumentEvent.EventType.REMOVE) { if (map.getElement(line) == longestLine) { // removed from longest line... recalc calcLongestLineLength(); preferenceChanged(null, true, false); } } } }
Example 12
Source File: OutputDocumentTest.java From netbeans with Apache License 2.0 | 4 votes |
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); } }
Example 13
Source File: DocumentUtilities.java From netbeans with Apache License 2.0 | 2 votes |
/** * Get a property of a given document event. * * @param evt non-null document event from which the property should be retrieved. * @param key non-null key of the property. * @return value for the given property. */ public static Object getEventProperty(DocumentEvent evt, Object key) { EventPropertiesElementChange change = (EventPropertiesElementChange) evt.getChange(EventPropertiesElement.INSTANCE); return (change != null) ? change.getProperty(key) : null; }