/** Update the child views in response to a document event. */ void updateChildren(DocumentEvent e, Shape a) { Element elem = getElement(); DocumentEvent.ElementChange ec = e.getChange(elem); // This occurs when syntax highlighting only changes on lines // (i.e. beginning a multiline comment). if (e.getType() == DocumentEvent.EventType.CHANGE) { // System.err.println("Updating the damage due to a CHANGE event..."); // FIXME: Make me repaint more intelligently. getContainer().repaint(); // damageLineRange(startLine,endLine, a, host); } else if (ec != null) { // the structure of this element changed. Element[] removedElems = ec.getChildrenRemoved(); Element[] addedElems = ec.getChildrenAdded(); View[] added = new View[addedElems.length]; for (int i = 0; i < addedElems.length; i++) added[i] = new WrappedLine(addedElems[i]); // System.err.println("Replacing " + removedElems.length + // " children with " + addedElems.length); replace(ec.getIndex(), removedElems.length, added); // should damge a little more intelligently. if (a != null) { preferenceChanged(null, true, true); getContainer().repaint(); } } // update font metrics which may be used by the child views updateMetrics(); }
@Override public void insertUpdate(DocumentEvent e) { try { // Check for change in lines only if a newline // character is inserted. String insertion = e.getDocument().getText(e.getOffset(), e.getLength()); if (insertion.contains("\n")) documentChanged(); } catch (BadLocationException ex) { /* nothing to do */ } }
/** * Alerts all listeners to this document of an insertion. This is overridden so we can update our * syntax highlighting stuff. * * <p>The syntax highlighting stuff has to be here instead of in <code>insertUpdate</code> because * <code>insertUpdate</code> is not called by the undo/redo actions, but this method is. * * @param e The change. */ protected void fireInsertUpdate(DocumentEvent e) { /* * Now that the text is actually inserted into the content and * element structure, we can update our token elements and "last * tokens on lines" structure. */ Element lineMap = getDefaultRootElement(); DocumentEvent.ElementChange change = e.getChange(lineMap); Element[] added = change == null ? null : change.getChildrenAdded(); int numLines = lineMap.getElementCount(); int line = lineMap.getElementIndex(e.getOffset()); int previousLine = line - 1; int previousTokenType = (previousLine > -1 ? lastTokensOnLines.get(previousLine) : Token.NULL); // If entire lines were added... if (added != null && added.length > 0) { Element[] removed = change.getChildrenRemoved(); int numRemoved = removed != null ? removed.length : 0; int endBefore = line + added.length - numRemoved; // System.err.println("... adding lines: " + line + " - " + (endBefore-1)); // System.err.println("... ... added: " + added.length + ", removed:" + numRemoved); for (int i = line; i < endBefore; i++) { setSharedSegment(i); // Loads line i's text into s. int tokenType = tokenMaker.getLastTokenTypeOnLine(s, previousTokenType); lastTokensOnLines.add(i, tokenType); // System.err.println("--------- lastTokensOnLines.size() == " + // lastTokensOnLines.getSize()); previousTokenType = tokenType; } // End of for (int i=line; i<endBefore; i++). // Update last tokens for lines below until they stop changing. updateLastTokensBelow(endBefore, numLines, previousTokenType); } // End of if (added!=null && added.length>0). // Otherwise, text was inserted on a single line... else { // Update last tokens for lines below until they stop changing. updateLastTokensBelow(line, numLines, previousTokenType); } // End of else. // Let all listeners know about the insertion. super.fireInsertUpdate(e); }
/** * This method is called AFTER the content has been inserted into the document and the element * structure has been updated. * * <p>The syntax-highlighting updates need to be done here (as opposed to an override of <code> * postRemoveUpdate</code>) as this method is called in response to undo/redo events, whereas * <code>postRemoveUpdate</code> is not. * * <p>Now that the text is actually inserted into the content and element structure, we can update * our token elements and "last tokens on lines" structure. * * @param chng The change that occurred. * @see #removeUpdate */ protected void fireRemoveUpdate(DocumentEvent chng) { Element lineMap = getDefaultRootElement(); int numLines = lineMap.getElementCount(); DocumentEvent.ElementChange change = chng.getChange(lineMap); Element[] removed = change == null ? null : change.getChildrenRemoved(); // If entire lines were removed... if (removed != null && removed.length > 0) { int line = change.getIndex(); // First line entirely removed. int previousLine = line - 1; // Line before that. int previousTokenType = (previousLine > -1 ? lastTokensOnLines.get(previousLine) : Token.NULL); Element[] added = change.getChildrenAdded(); int numAdded = added == null ? 0 : added.length; // Remove the cached last-token values for the removed lines. int endBefore = line + removed.length - numAdded; // System.err.println("... removing lines: " + line + " - " + (endBefore-1)); // System.err.println("... added: " + numAdded + ", removed: " + removed.length); lastTokensOnLines.removeRange( line, endBefore); // Removing values for lines [line-(endBefore-1)]. // System.err.println("--------- lastTokensOnLines.size() == " + lastTokensOnLines.getSize()); // Update last tokens for lines below until they've stopped changing. updateLastTokensBelow(line, numLines, previousTokenType); } // End of if (removed!=null && removed.size()>0). // Otherwise, text was removed from just one line... else { int line = lineMap.getElementIndex(chng.getOffset()); if (line >= lastTokensOnLines.getSize()) return; // If we're editing the last line in a document... int previousLine = line - 1; int previousTokenType = (previousLine > -1 ? lastTokensOnLines.get(previousLine) : Token.NULL); // System.err.println("previousTokenType for line : " + previousLine + " is " + // previousTokenType); // Update last tokens for lines below until they've stopped changing. updateLastTokensBelow(line, numLines, previousTokenType); } // Let all of our listeners know about the removal. super.fireRemoveUpdate(chng); }
/** * Gives notification that something was inserted into the document in a location that this view * is responsible for. This is implemented to simply update the children. * * @param changes 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 View#insertUpdate */ public void insertUpdate(DocumentEvent changes, Shape a, ViewFactory f) { updateChildren(changes, a); Rectangle alloc = ((a != null) && isAllocationValid()) ? getInsideAllocation(a) : null; int pos = changes.getOffset(); View v = getViewAtPosition(pos, alloc); if (v != null) v.insertUpdate(changes, alloc, f); }
/** * Repaint the region of change covered by the given document event. Damages the line that begins * the range to cover the case when the insert/remove is only on one line. If lines are added or * removed, damages the whole view. The longest line is checked to see if it has changed. */ protected void updateDamage(DocumentEvent changes, Shape a, ViewFactory f) { 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) { int addedAt = ec.getIndex(); // FIXME: Is this correct????? for (int i = 0; i < added.length; i++) possiblyUpdateLongLine(added[i], addedAt + i); } if (removed != null) { for (int i = 0; i < removed.length; i++) { if (removed[i] == longLine) { longLineWidth = -1; // Must do this!! calculateLongestLine(); break; } } } preferenceChanged(null, true, true); host.repaint(); } // This occurs when syntax highlighting only changes on lines // (i.e. beginning a multiline comment). else if (changes.getType() == DocumentEvent.EventType.CHANGE) { // System.err.println("Updating the damage due to a CHANGE event..."); int startLine = changes.getOffset(); int endLine = changes.getLength(); damageLineRange(startLine, endLine, a, host); } 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); if (e == longLine) { // We must recalculate longest line's width here // because it has gotten longer. longLineWidth = getLineWidth(line); preferenceChanged(null, true, false); } else { // If long line gets updated, update the status bars too. if (possiblyUpdateLongLine(e, line)) preferenceChanged(null, true, false); } } else if (changes.getType() == DocumentEvent.EventType.REMOVE) { if (map.getElement(line) == longLine) { // removed from longest line... recalc longLineWidth = -1; // Must do this! calculateLongestLine(); preferenceChanged(null, true, false); } } } }
public void insertUpdate(DocumentEvent evt) { documentChanged(evt); int offset = evt.getOffset(); int length = evt.getLength(); int newStart; int newEnd; if (selectionStart > offset || (selectionStart == selectionEnd && selectionStart == offset)) newStart = selectionStart + length; else newStart = selectionStart; if (selectionEnd >= offset) newEnd = selectionEnd + length; else newEnd = selectionEnd; select(newStart, newEnd); }
public void insertUpdate(DocumentEvent e) { Document doc = e.getDocument(); try { prop.set(doc.getText(0, doc.getLength())); } catch (BadLocationException b) { // Once again, no idea what this is supposed to be. // I don't think I like this interface much :-(. System.out.println(b); } }
public void removeUpdate(DocumentEvent evt) { documentChanged(evt); int offset = evt.getOffset(); int length = evt.getLength(); int newStart; int newEnd; if (selectionStart > offset) { if (selectionStart > offset + length) newStart = selectionStart - length; else newStart = offset; } else newStart = selectionStart; if (selectionEnd > offset) { if (selectionEnd > offset + length) newEnd = selectionEnd - length; else newEnd = offset; } else newEnd = selectionEnd; select(newStart, newEnd); }
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(); } }
void updateTheLabel(DocumentEvent e) { Document doc = (Document) e.getDocument(); String text = null; try { text = doc.getText(0, doc.getLength()); doc = null; } catch (BadLocationException ex) { text = null; } if (text != null) { try { double number = Double.parseDouble(text); if (number > 1) { label.setText(labelPair.getPlural()); } else { label.setText(labelPair.getSingular()); } } catch (NumberFormatException ex) { // Do nothing } finally { text = null; } } }