/**
   * Whenever an UndoableEdit happens the edit will either be absorbed by the current compound edit
   * or a new compound edit will be started
   */
  @Override
  public void undoableEditHappened(UndoableEditEvent e) {
    //  Start a new compound edit

    AbstractDocument.DefaultDocumentEvent docEvt = (DefaultDocumentEvent) e.getEdit();

    if (compoundEdit == null) {
      compoundEdit = startCompoundEdit(e.getEdit());
      startCombine = false;
      updateDirty();
      return;
    }

    int editLine = ((SyntaxDocument) docEvt.getDocument()).getLineNumberAt(docEvt.getOffset());

    //  Check for an incremental edit or backspace.
    //  The Change in Caret position and Document length should both be
    //  either 1 or -1.
    if ((startCombine || Math.abs(docEvt.getLength()) == 1) && editLine == lastLine) {
      compoundEdit.addEdit(e.getEdit());
      startCombine = false;
      updateDirty();
      return;
    }

    //  Not incremental edit, end previous edit and start a new one
    lastLine = editLine;

    compoundEdit.end();
    compoundEdit = startCompoundEdit(e.getEdit());

    updateDirty();
  }
Esempio n. 2
0
  public void undoableEditHappened(UndoableEditEvent e) {
    UndoableEdit edit = e.getEdit();
    if (edit instanceof AbstractDocument.DefaultDocumentEvent) {
      AbstractDocument.DefaultDocumentEvent event = (AbstractDocument.DefaultDocumentEvent) edit;
      int start = event.getOffset();
      int len = event.getLength();
      Debug.log(9, "undoableEditHappened " + start + "," + len);
      boolean isNeedStart = false;
      if (event.getType().equals(DocumentEvent.EventType.CHANGE)
          || event.getType().equals(DocumentEvent.EventType.INSERT)) {
        try {
          String text = event.getDocument().getText(start, len);
          if (text.contains("\n")) isNeedStart = true;
        } catch (BadLocationException e1) {
          e1.printStackTrace();
        }
      }

      if (current == null) {
        isNeedStart = true;
      } else if (lastEditName == null || !lastEditName.equals(edit.getPresentationName())) {
        isNeedStart = true;
      }

      while (pointer < edits.size() - 1) {
        edits.remove(edits.size() - 1);
        isNeedStart = true;
      }
      if (isNeedStart) {
        createCompoundEdit();
      }

      current.addEdit(edit);
      lastEditName = edit.getPresentationName();

      refreshControls();
    }
  }