com.intellij.openapi.fileEditor.FileDocumentManagerAdapter Java Examples

The following examples show how to use com.intellij.openapi.fileEditor.FileDocumentManagerAdapter. 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: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void testSaveDocument_DoNotSaveIfModStampEqualsToFile() throws Exception {
  final VirtualFile file = createFile();
  final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);
  assertNotNull(file.toString(), document);
  WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
    @Override
    public void run() {
      document.insertString(0, "zzz");
      document.setModificationStamp(file.getModificationStamp());
    }
  });

  getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
    @Override
    public void beforeDocumentSaving(@Nonnull Document documentToSave) {
      assertNotSame(document, documentToSave);
    }
  });

  myDocumentManager.saveDocument(document);
}
 
Example #2
Source File: DelayedDocumentWatcher.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void activate() {
  if (myConnection == null) {
    myListenerDisposable = Disposable.newDisposable();
    Disposer.register(myProject, myListenerDisposable);
    EditorFactory.getInstance().getEventMulticaster().addDocumentListener(myListener, myListenerDisposable);
    myConnection = ApplicationManager.getApplication().getMessageBus().connect(myProject);
    myConnection.subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
      @Override
      public void beforeAllDocumentsSaving() {
        myDocumentSavingInProgress = true;
        ApplicationManager.getApplication().invokeLater(() -> myDocumentSavingInProgress = false, ModalityState.any());
      }
    });
  }
}
 
Example #3
Source File: FileDocumentManagerImplTest.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void testReplaceDocumentTextWithTheSameText() throws Exception {
  final VirtualFile file = createFile();
  final DocumentEx document = (DocumentEx)myDocumentManager.getDocument(file);

  final String newText = "test text";
  assertNotNull(file.toString(), document);
  WriteCommandAction.runWriteCommandAction(myProject, new Runnable() {
    @Override
    public void run() {
      document.replaceString(0, document.getTextLength(), newText);
      assertTrue(myDocumentManager.isDocumentUnsaved(document));
      myDocumentManager.saveDocument(document);

      getProject().getMessageBus().connect(getTestRootDisposable()).subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
        @Override
        public void beforeDocumentSaving(@Nonnull Document documentToSave) {
          assertNotSame(document, documentToSave);
        }
      });

      final long modificationStamp = document.getModificationStamp();

      document.replaceString(0, document.getTextLength(), newText);
      if (myDocumentManager.isDocumentUnsaved(document)) {
        assertTrue(document.getModificationStamp() > modificationStamp);
      }
      else {
        assertEquals(modificationStamp, document.getModificationStamp());
      }
    }
  });
}
 
Example #4
Source File: VetoSavingCommittingDocumentsAdapter.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Inject
public VetoSavingCommittingDocumentsAdapter(final FileDocumentManager fileDocumentManager) {
  myFileDocumentManager = fileDocumentManager;

  ApplicationManager.getApplication().getMessageBus().connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, new FileDocumentManagerAdapter() {
    @Override
    public void beforeAllDocumentsSaving() {
      Map<Document, Project> documentsToWarn = getDocumentsBeingCommitted();
      if (!documentsToWarn.isEmpty()) {
        boolean allowSave = showAllowSaveDialog(documentsToWarn);
        updateSaveability(documentsToWarn, allowSave);
      }
    }
  });
}
 
Example #5
Source File: ConsoleHistoryController.java    From consulo with Apache License 2.0 5 votes vote down vote up
public void install() {
  class Listener extends FileDocumentManagerAdapter implements ProjectEx.ProjectSaved {
    @Override
    public void beforeDocumentSaving(@Nonnull Document document) {
      if (document == myConsole.getEditorDocument()) {
        saveHistory();
      }
    }

    @Override
    public void saved(@Nonnull Project project) {
      saveHistory();
    }
  }
  Listener listener = new Listener();
  ApplicationManager.getApplication().getMessageBus().connect(myConsole).subscribe(ProjectEx.ProjectSaved.TOPIC, listener);
  myConsole.getProject().getMessageBus().connect(myConsole).subscribe(AppTopics.FILE_DOCUMENT_SYNC, listener);

  myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, this);
  Disposer.register(myConsole, new Disposable() {
    @Override
    public void dispose() {
      myConsole.getVirtualFile().putUserData(CONTROLLER_KEY, null);
      saveHistory();
    }
  });
  if (myHelper.getModel().getHistorySize() == 0) {
    loadHistory(myHelper.getId());
  }
  configureActions();
  myLastSaveStamp = getCurrentTimeStamp();
}