Java Code Examples for javax.swing.event.DocumentEvent#getLength()

The following examples show how to use javax.swing.event.DocumentEvent#getLength() . 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: JEditTextArea.java    From rapidminer-studio with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
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);
}
 
Example 2
Source File: InstantRefactoringPerformer.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
   public synchronized void insertUpdate(DocumentEvent e) {
if (inSync)
    return ;

       //check for modifications outside the first region:
       if (e.getOffset() < region.getFirstRegionStartOffset() || (e.getOffset() + e.getLength()) > region.getFirstRegionEndOffset()) {
           release(true);
           return;
       }
       
       inSync = true;
       region.sync();
       span = region.getFirstRegionLength();
       ui.updateInput(getRegionText(0));
       inSync = false;
       
       requestRepaint();
   }
 
Example 3
Source File: BlockView.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
    super.changedUpdate(changes, a, f);
    int pos = changes.getOffset();
    if (pos <= getStartOffset() && (pos + changes.getLength()) >=
        getEndOffset()) {
        setPropertiesFromAttributes();
    }
}
 
Example 4
Source File: TableView.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
    super.changedUpdate(e, a, f);
    int pos = e.getOffset();
    if (pos <= getStartOffset() && (pos + e.getLength()) >=
        getEndOffset()) {
        RowView.this.setPropertiesFromAttributes();
    }
}
 
Example 5
Source File: HRuleView.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
    super.changedUpdate(changes, a, f);
    int pos = changes.getOffset();
    if (pos <= getStartOffset() && (pos + changes.getLength()) >=
        getEndOffset()) {
        setPropertiesFromAttributes();
    }
}
 
Example 6
Source File: BlockView.java    From JDKSourceCode1.8 with MIT License 5 votes vote down vote up
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
    super.changedUpdate(changes, a, f);
    int pos = changes.getOffset();
    if (pos <= getStartOffset() && (pos + changes.getLength()) >=
        getEndOffset()) {
        setPropertiesFromAttributes();
    }
}
 
Example 7
Source File: AbstractOutputPane.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public final void insertUpdate(DocumentEvent e) {
    //Ensure it is consumed
    e.getLength();
    documentChanged();
    if (e.getOffset() + e.getLength() >= getCaretPos() && (locked || !(e instanceof OutputDocument.DO))) {
        //#119985 only move caret when not in editable section
        OutputDocument doc = (OutputDocument)e.getDocument();
        if (! (e instanceof OutputDocument.DO) && getCaretPos() >= doc.getOutputLength()) {
            return ;
        }
        
        getCaret().setDot(e.getOffset() + e.getLength());
    }
}
 
Example 8
Source File: BaseDocumentUnitTestCase.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void insertUpdate(DocumentEvent e) {
    int offset = e.getOffset();
    int length = e.getLength();
    int newDot = dot;
    short changed = 0;
    if (newDot >= offset) {
        newDot += length;
        changed |= 1;
    }
    int newMark = mark;
    if (newMark >= offset) {
        newMark += length;
        changed |= 2;
    }

    if (changed != 0) {
        if (newMark == newDot) {
            setDot(newDot);
            ensureValidPosition();
        } else {
            setDot(newMark);
            if (getDot() == newMark) {
                moveDot(newDot);
            }
            ensureValidPosition();
        }

    }
}
 
Example 9
Source File: GuardedBlocksHighlighting.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void insertUpdate(DocumentEvent e) {
    int changeStart = e.getOffset();
    int changeEnd = e.getOffset() + e.getLength();

    if (isAffectedByChange(changeStart, changeEnd)) {
        fireHighlightsChange(changeStart, changeEnd);
    }
}
 
Example 10
Source File: HRuleView.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
    super.changedUpdate(changes, a, f);
    int pos = changes.getOffset();
    if (pos <= getStartOffset() && (pos + changes.getLength()) >=
        getEndOffset()) {
        setPropertiesFromAttributes();
    }
}
 
Example 11
Source File: TableView.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
    super.changedUpdate(e, a, f);
    int pos = e.getOffset();
    if (pos <= getStartOffset() && (pos + e.getLength()) >=
        getEndOffset()) {
        RowView.this.setPropertiesFromAttributes();
    }
}
 
Example 12
Source File: DocumentModel.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void documentChanged(DocumentEvent documentEvent) {
    if(!documentDirty) {
        fireSourceChanged();
    }
    //indicate that the synchronization between document content and the element may be broken
    //not used by the model logic itself but by the "resortElements" method.
    documentDirty = true;
    
    try {
        //test whether a new text was inserted before or after the root element boundaries (positions)
        if(getRootElement().getStartOffset() > 0 || getRootElement().getEndOffset() < getDocument().getLength()) {
            getRootElement().setStartPosition(0);
            getRootElement().setEndPosition(getDocument().getLength());
        }
        
        //TODO: here we have to decide whether the document change affects
        //the model and how.
        int change_offset = documentEvent.getOffset();
        int change_length = documentEvent.getLength();
        
        int type = documentEvent.getType().equals(EventType.REMOVE) ? DocumentChange.REMOVE : DocumentChange.INSERT;
        DocumentChange dchi = new DocumentChange(getDocument().createPosition(change_offset), change_length, type);
        documentChanges.add(dchi);
        if(debug) System.out.println(dchi);
    }catch(BadLocationException e) {
        e.printStackTrace();
    }
    
    requestModelUpdate(false);
}
 
Example 13
Source File: TableView.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
    super.changedUpdate(e, a, f);
    int pos = e.getOffset();
    if (pos <= getStartOffset() && (pos + e.getLength()) >=
        getEndOffset()) {
        RowView.this.setPropertiesFromAttributes();
    }
}
 
Example 14
Source File: TableView.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
    super.changedUpdate(e, a, f);
    int pos = e.getOffset();
    if (pos <= getStartOffset() && (pos + e.getLength()) >=
        getEndOffset()) {
        RowView.this.setPropertiesFromAttributes();
    }
}
 
Example 15
Source File: HRuleView.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent changes, Shape a, ViewFactory f) {
    super.changedUpdate(changes, a, f);
    int pos = changes.getOffset();
    if (pos <= getStartOffset() && (pos + changes.getLength()) >=
        getEndOffset()) {
        setPropertiesFromAttributes();
    }
}
 
Example 16
Source File: TableView.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public void changedUpdate(DocumentEvent e, Shape a, ViewFactory f) {
    super.changedUpdate(e, a, f);
    int pos = e.getOffset();
    if (pos <= getStartOffset() && (pos + e.getLength()) >=
        getEndOffset()) {
        RowView.this.setPropertiesFromAttributes();
    }
}
 
Example 17
Source File: LineRootElement.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void insertUpdate(DocumentEvent evt, UndoableEdit edit, AttributeSet attr) {
        int insertOffset = evt.getOffset();
        int insertEndOffset = insertOffset + evt.getLength();
        CharSequence text = DocumentUtilities.getText(doc);
        if (insertOffset > 0) { // [Swing] marks (and elements) at offset zero do not move up
            insertOffset--;
        }
        try {
            int index = -1; // Index of the elements modification - computed lazily
            List<LineElement> addedLines = null; // Collected added lines
            LineElement removedLine = null; // Removed line element
            Position lastAddedLineEndPos = null;
            for (int offset = insertOffset; offset < insertEndOffset; offset++) {
                if (text.charAt(offset) == '\n') {
                    if (index == -1) { // Not computed yet
                        index = getElementIndex(offset);
                        removedLine = (LineElement)getElement(index);
                        lastAddedLineEndPos = removedLine.getStartPosition();
                        addedLines = new ArrayList<LineElement>(2);
                    }
                    Position lineEndPos = doc.createPosition(offset + 1);
                    addedLines.add(new LineElement(this, lastAddedLineEndPos, lineEndPos));
                    lastAddedLineEndPos = lineEndPos;
                }
            }
            if (index != -1) { // Some lines were added
                // If the text was inserted at the line boundary i.e. right after existing '\n'
                // and the ending char of the inserted text was not '\n' (otherwise
                // it would be a "clean" line insert) then there must be two line elements
                // removed.
                Position removedLineEndPos = removedLine.getEndPosition();
                int removedLineEndOffset = removedLineEndPos.getOffset();
                Element[] removed; // removed line elements
                int lastAddedLineEndOffset = lastAddedLineEndPos.getOffset();
                if (insertEndOffset == removedLineEndOffset
                        && lastAddedLineEndOffset != removedLineEndOffset
//                        && index + 1 < getElementCount()
                ) {
                    LineElement removedLine2 = (LineElement)getElement(index + 1);
                    removed = new Element[] { removedLine, removedLine2 };
                    removedLineEndPos = removedLine2.getEndPosition();
                    removedLineEndOffset = removedLineEndPos.getOffset();
                } else { // just one line removed
                    removed = new Element[] { removedLine };
                }
                if (lastAddedLineEndOffset < removedLineEndOffset) {
                    addedLines.add(new LineElement(this, lastAddedLineEndPos, removedLineEndPos));
                }

                Element[] added = new Element[addedLines.size()];
                addedLines.toArray(added);

                edit.addEdit(new Edit(index, removed, added));
                replace(index, removed.length, added);
            }
        } catch (BadLocationException e) {
            throw new IllegalStateException(e.toString());
        }
    }
 
Example 18
Source File: LuceneDataStoreSearchGUI.java    From gate-core with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void removeUpdate(DocumentEvent ev) {
  if(mode == PROGRAMMATIC || mode == INSERT) {
    return;
  }

  int pos = ev.getOffset();

  if(ev.getLength() != 1 || (pos < start || pos > end)) {
    // cancel any autocompletion if the user cut some text
    // or delete outside brackets when in POPUP mode
    cleanup();
    return;
  }
  if(mode == POPUP_TYPES) {
    end = pos;
    String type = getText().substring(start, end);
    if(!type.matches("[a-zA-Z0-9]+")) {
      return;
    }
    for(int i = 0; i < queryList.getModel().getSize(); i++) {
      if(startsWithIgnoreCase(
              queryList.getModel().getElementAt(i), type)) {
        queryPopupWindow.setVisible(true);
        queryList.setSelectedIndex((i));
        queryList.ensureIndexIsVisible(i);
        break;
      }
    }
  } else if(mode == POPUP_FEATURES) {
    end = pos;
    String feature = getText().substring(start, end);
    if(!feature.matches("[a-zA-Z0-9]+")) {
      return;
    }
    for(int i = 0; i < queryList.getModel().getSize(); i++) {
      if(startsWithIgnoreCase(
              queryList.getModel().getElementAt(i), feature)) {
        queryPopupWindow.setVisible(true);
        queryList.setSelectedIndex((i));
        queryList.ensureIndexIsVisible(i);
        break;
      }
    }
  }
}
 
Example 19
Source File: ConsoleTextEditor.java    From groovy with Apache License 2.0 4 votes vote down vote up
public void insertUpdate(DocumentEvent de) {
    lastUpdate = de.getOffset() + de.getLength();
}
 
Example 20
Source File: TextAreaDemo.java    From marathonv5 with Apache License 2.0 4 votes vote down vote up
public void insertUpdate(DocumentEvent ev) {
    if (ev.getLength() != 1) {
        return;
    }

    int pos = ev.getOffset();
    String content = null;
    try {
        content = textArea.getText(0, pos + 1);
    } catch (BadLocationException e) {
        e.printStackTrace();
    }

    // Find where the word starts
    int w;
    for (w = pos; w >= 0; w--) {
        if (!Character.isLetter(content.charAt(w))) {
            break;
        }
    }
    if (pos - w < 2) {
        // Too few chars
        return;
    }

    String prefix = content.substring(w + 1).toLowerCase();
    int n = Collections.binarySearch(words, prefix);
    if (n < 0 && -n <= words.size()) {
        String match = words.get(-n - 1);
        if (match.startsWith(prefix)) {
            // A completion is found
            String completion = match.substring(pos - w);
            // We cannot modify Document from within notification,
            // so we submit a task that does the change later
            SwingUtilities.invokeLater(new CompletionTask(completion, pos + 1));
        }
    } else {
        // Nothing found
        mode = Mode.INSERT;
    }
}