@Override
 public void onDocumentChange(final DocumentChangeEvent event) {
   if (documentHandle == null || !documentHandle.isSameAs(event.getDocument())) {
     return;
   }
   createDirtyRegion(event);
   autoSaveTimer.cancel();
   autoSaveTimer.schedule(DELAY);
 }
  /**
   * Creates a dirty region for a document event and adds it to the queue.
   *
   * @param event the document event for which to create a dirty region
   */
  private void createDirtyRegion(final DocumentChangeEvent event) {
    if (event.getLength() == 0 && event.getText() != null) {
      // Insert
      dirtyRegionQueue.addDirtyRegion(
          new DirtyRegion(
              event.getOffset(), event.getText().length(), DirtyRegion.INSERT, event.getText()));

    } else if (event.getText() == null || event.getText().length() == 0) {
      // Remove
      dirtyRegionQueue.addDirtyRegion(
          new DirtyRegion(event.getOffset(), event.getLength(), DirtyRegion.REMOVE, null));

    } else {
      // Replace (Remove + Insert)
      dirtyRegionQueue.addDirtyRegion(
          new DirtyRegion(event.getOffset(), event.getLength(), DirtyRegion.REMOVE, null));
      dirtyRegionQueue.addDirtyRegion(
          new DirtyRegion(
              event.getOffset(), event.getText().length(), DirtyRegion.INSERT, event.getText()));
    }
  }