private static void reinitBreadcrumbsComponent(
      @NotNull final FileEditorManager fileEditorManager, @NotNull VirtualFile file) {
    if (isSuitable(fileEditorManager.getProject(), file)) {
      FileEditor[] fileEditors = fileEditorManager.getAllEditors(file);
      for (final FileEditor fileEditor : fileEditors) {
        if (fileEditor instanceof TextEditor) {
          Editor editor = ((TextEditor) fileEditor).getEditor();
          if (BreadcrumbsXmlWrapper.getBreadcrumbsComponent(editor) != null) {
            continue;
          }
          final BreadcrumbsXmlWrapper wrapper = new BreadcrumbsXmlWrapper(editor);
          final JComponent c = wrapper.getComponent();
          fileEditorManager.addTopComponent(fileEditor, c);

          Disposer.register(
              fileEditor,
              new Disposable() {
                @Override
                public void dispose() {
                  disposeWrapper(fileEditorManager, fileEditor, wrapper);
                }
              });
        }
      }
    } else {
      removeBreadcrumbs(fileEditorManager, file);
    }
  }
 private static void updateModifiedProperty(@NotNull VirtualFile file) {
   for (Project project : ProjectManager.getInstance().getOpenProjects()) {
     FileEditorManager fileEditorManager = FileEditorManager.getInstance(project);
     for (FileEditor editor : fileEditorManager.getAllEditors(file)) {
       if (editor instanceof TextEditorImpl) {
         ((TextEditorImpl) editor).updateModifiedProperty();
       }
     }
   }
 }
 @Override
 public void propertyChanged(@NotNull VirtualFilePropertyEvent event) {
   if (VirtualFile.PROP_NAME.equals(event.getPropertyName())) {
     FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);
     VirtualFile file = event.getFile();
     if (fileEditorManager.isFileOpen(file)) {
       reinitBreadcrumbsComponent(fileEditorManager, file);
     }
   }
 }
  public void dispose() {
    final EditorFactory editorFactory = EditorFactory.getInstance();
    editorFactory.releaseEditor(myConsoleEditor);
    editorFactory.releaseEditor(myHistoryViewer);

    final FileEditorManager editorManager = FileEditorManager.getInstance(getProject());
    final boolean isOpen = editorManager.isFileOpen(myVirtualFile);
    if (isOpen) {
      editorManager.closeFile(myVirtualFile);
    }
  }
  private void installEditorFactoryListener() {
    final FileEditorManagerAdapter fileEditorListener =
        new FileEditorManagerAdapter() {
          @Override
          public void fileOpened(FileEditorManager source, VirtualFile file) {
            if (!Comparing.equal(file, myVirtualFile) || myConsoleEditor == null) return;
            Editor selectedTextEditor = source.getSelectedTextEditor();
            for (FileEditor fileEditor : source.getAllEditors(file)) {
              if (!(fileEditor instanceof TextEditor)) continue;
              final EditorEx editor = (EditorEx) ((TextEditor) fileEditor).getEditor();
              editor.addFocusListener(myFocusListener);
              if (selectedTextEditor == editor) { // already focused
                myCurrentEditor = editor;
              }
              EmptyAction.registerActionShortcuts(
                  editor.getComponent(), myConsoleEditor.getComponent());
              editor
                  .getCaretModel()
                  .addCaretListener(
                      new CaretListener() {
                        public void caretPositionChanged(CaretEvent e) {
                          queueUiUpdate(false);
                        }
                      });
            }
            queueUiUpdate(false);
          }

          @Override
          public void fileClosed(FileEditorManager source, VirtualFile file) {
            if (!Comparing.equal(file, myVirtualFile)) return;
            if (myUiUpdateRunnable != null
                && !Boolean.TRUE.equals(
                    file.getUserData(FileEditorManagerImpl.CLOSING_TO_REOPEN))) {
              if (myCurrentEditor != null && myCurrentEditor.isDisposed()) myCurrentEditor = null;
              ApplicationManager.getApplication().runReadAction(myUiUpdateRunnable);
            }
          }
        };
    myProject
        .getMessageBus()
        .connect(this)
        .subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, fileEditorListener);
    FileEditorManager editorManager = FileEditorManager.getInstance(getProject());
    if (editorManager.isFileOpen(myVirtualFile)) {
      fileEditorListener.fileOpened(editorManager, myVirtualFile);
    }
  }
 private static void disposeWrapper(
     @NotNull FileEditorManager fileEditorManager,
     @NotNull FileEditor fileEditor,
     @NotNull BreadcrumbsXmlWrapper wrapper) {
   fileEditorManager.removeTopComponent(fileEditor, wrapper.getComponent());
   Disposer.dispose(wrapper);
 }
 private void fileChanged(VirtualFile newFile) {
   FileEditor fileEditor =
       newFile == null
           ? null
           : FileEditorManager.getInstance(getProject()).getSelectedEditor(newFile);
   Editor editor = fileEditor instanceof TextEditor ? ((TextEditor) fileEditor).getEditor() : null;
   myEditor = new WeakReference<Editor>(editor);
   update();
 }
 @Nullable
 private TextEditor getActiveLayoutXmlEditor() {
   FileEditor[] fileEditors = myFileEditorManager.getSelectedEditors();
   if (fileEditors.length > 0 && fileEditors[0] instanceof TextEditor) {
     final TextEditor textEditor = (TextEditor) fileEditors[0];
     if (isApplicableEditor(textEditor)) {
       return textEditor;
     }
   }
   return null;
 }
 public void calcData(DataKey key, DataSink sink) {
   if (OpenFileDescriptor.NAVIGATE_IN_EDITOR == key) {
     sink.put(OpenFileDescriptor.NAVIGATE_IN_EDITOR, myConsoleEditor);
     return;
   } else if (getProject().isInitialized()) {
     FileEditorManager editorManager = FileEditorManager.getInstance(getProject());
     final Object o =
         ((FileEditorManagerImpl) editorManager)
             .getData(key.getName(), myConsoleEditor, myVirtualFile);
     sink.put(key, o);
   }
 }
  @Override
  @Nullable
  public FileEditorLocation getLocation() {
    VirtualFile virtualFile = getFile();
    if (virtualFile == null) return null;
    FileEditor editor = FileEditorManager.getInstance(getProject()).getSelectedEditor(virtualFile);
    if (!(editor instanceof TextEditor)) return null;

    Segment segment = getUsageInfo().getSegment();
    if (segment == null) return null;
    return new TextEditorLocation(segment.getStartOffset(), (TextEditor) editor);
  }
 private static void removeBreadcrumbs(
     @NotNull FileEditorManager fileEditorManager, @NotNull VirtualFile file) {
   final FileEditor[] fileEditors = fileEditorManager.getAllEditors(file);
   for (FileEditor fileEditor : fileEditors) {
     if (fileEditor instanceof TextEditor) {
       Editor editor = ((TextEditor) fileEditor).getEditor();
       BreadcrumbsXmlWrapper wrapper = BreadcrumbsXmlWrapper.getBreadcrumbsComponent(editor);
       if (wrapper != null) {
         disposeWrapper(fileEditorManager, fileEditor, wrapper);
       }
     }
   }
 }
 private void reinitBreadcrumbsInAllEditors() {
   FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);
   for (VirtualFile virtualFile : fileEditorManager.getOpenFiles()) {
     reinitBreadcrumbsComponent(fileEditorManager, virtualFile);
   }
 }
 public Editor openTextEditor(boolean focus) {
   return FileEditorManager.getInstance(getProject()).openTextEditor(getDescriptor(), focus);
 }