com.intellij.openapi.fileEditor.FileEditorManagerListener Java Examples

The following examples show how to use com.intellij.openapi.fileEditor.FileEditorManagerListener. 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: CurrentFileTodosPanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
CurrentFileTodosPanel(Project project, TodoPanelSettings settings, Content content) {
  super(project, settings, true, content);

  VirtualFile[] files = FileEditorManager.getInstance(project).getSelectedFiles();
  setFile(files.length == 0 ? null : PsiManager.getInstance(myProject).findFile(files[0]), true);
  // It's important to remove this listener. It prevents invocation of setFile method after the tree builder is disposed
  project.getMessageBus().connect(this).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@Nonnull FileEditorManagerEvent e) {
      VirtualFile file = e.getNewFile();
      final PsiFile psiFile = file != null && file.isValid() ? PsiManager.getInstance(myProject).findFile(file) : null;
      // This invokeLater is required. The problem is setFile does a commit to PSI, but setFile is
      // invoked inside PSI change event. It causes an Exception like "Changes to PSI are not allowed inside event processing"
      DumbService.getInstance(myProject).smartInvokeLater(() -> setFile(psiFile, false));
    }
  });
}
 
Example #2
Source File: BreadcrumbsInitializingActivity.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void runActivity(@Nonnull Project project) {
  if (project.isDefault() || ApplicationManager.getApplication().isUnitTestMode() || project.isDisposed()) {
    return;
  }

  MessageBusConnection connection = project.getMessageBus().connect();
  connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new MyFileEditorManagerListener());
  connection.subscribe(FileTypeManager.TOPIC, new FileTypeListener() {
    @Override
    public void fileTypesChanged(@Nonnull FileTypeEvent event) {
      reinitBreadcrumbsInAllEditors(project);
    }
  });

  VirtualFileManager.getInstance().addVirtualFileListener(new MyVirtualFileListener(project), project);
  connection.subscribe(UISettingsListener.TOPIC, uiSettings -> reinitBreadcrumbsInAllEditors(project));

  UIUtil.invokeLaterIfNeeded(() -> reinitBreadcrumbsInAllEditors(project));
}
 
Example #3
Source File: StatusBarUpdater.java    From consulo with Apache License 2.0 6 votes vote down vote up
public StatusBarUpdater(Project project) {
  myProject = project;

  project.getMessageBus().connect(this).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
    @Override
    public void selectionChanged(@Nonnull FileEditorManagerEvent event) {
      updateLater();
    }
  });

  project.getMessageBus().connect(this).subscribe(DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC, new DaemonCodeAnalyzer.DaemonListenerAdapter() {
    @Override
    public void daemonFinished() {
      updateLater();
    }
  });
}
 
Example #4
Source File: EditorTracker.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Inject
public EditorTracker(@Nonnull Project project, @Nonnull Provider<WindowManager> windowManagerProvider) {
  myProject = project;

  project.getMessageBus().connect(this).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@Nonnull FileEditorManagerEvent event) {
      JFrame frame = windowManagerProvider.get().getFrame(myProject);
      if (frame == null || frame.getFocusOwner() == null) {
        return;
      }

      setActiveWindow(TargetAWT.from(frame));
    }
  });
}
 
Example #5
Source File: OutdatedVersionNotifier.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Inject
public OutdatedVersionNotifier(Provider<FileEditorManager> fileEditorManager,
                               CommittedChangesCache cache,
                               Project project) {
  myFileEditorManager = fileEditorManager;
  myCache = cache;
  myProject = project;
  MessageBusConnection busConnection = project.getMessageBus().connect();
  busConnection.subscribe(CommittedChangesCache.COMMITTED_TOPIC, new CommittedChangesAdapter() {
    @Override
    public void incomingChangesUpdated(@Nullable final List<CommittedChangeList> receivedChanges) {
      if (myCache.getCachedIncomingChanges() == null) {
        requestLoadIncomingChanges();
      }
      else {
        updateAllEditorsLater();
      }
    }

    @Override
    public void changesCleared() {
      updateAllEditorsLater();
    }
  });
  busConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new MyFileEditorManagerListener());
}
 
Example #6
Source File: ToggleReadOnlyAttributePanel.java    From consulo with Apache License 2.0 6 votes vote down vote up
@Override
public void install(@Nonnull StatusBar statusBar) {
  myStatusBar = statusBar;
  Project project = statusBar.getProject();
  if (project == null) {
    return;
  }

  project.getMessageBus().connect(this).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@Nonnull FileEditorManagerEvent event) {
      if (myStatusBar != null) {
        myStatusBar.updateWidget(ID());
      }
    }
  });
}
 
Example #7
Source File: AutoScrollFromSourceHandler.java    From consulo with Apache License 2.0 6 votes vote down vote up
public void install() {
  final MessageBusConnection connection = myProject.getMessageBus().connect(myProject);
  connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerAdapter() {
    @Override
    public void selectionChanged(@Nonnull FileEditorManagerEvent event) {
      final FileEditor editor = event.getNewEditor();
      if (editor != null && myComponent.isShowing() && isAutoScrollEnabled()) {
        myAlarm.cancelAllRequests();
        myAlarm.addRequest(new Runnable() {
          @Override
          public void run() {
            selectElementFromEditor(editor);
          }
        }, getAlarmDelay(), getModalityState());
      }
    }
  });
}
 
Example #8
Source File: NavBarListener.java    From consulo with Apache License 2.0 5 votes vote down vote up
static void subscribeTo(NavBarPanel panel) {
  if (panel.getClientProperty(LISTENER) != null) {
    unsubscribeFrom(panel);
  }

  final NavBarListener listener = new NavBarListener(panel);
  final Project project = panel.getProject();
  panel.putClientProperty(LISTENER, listener);
  KeyboardFocusManager.getCurrentKeyboardFocusManager().addPropertyChangeListener(listener);
  FileStatusManager.getInstance(project).addFileStatusListener(listener);
  PsiManager.getInstance(project).addPsiTreeChangeListener(listener);

  final MessageBusConnection connection = project.getMessageBus().connect();
  connection.subscribe(AnActionListener.TOPIC, listener);
  connection.subscribe(ProblemListener.TOPIC, listener);
  connection.subscribe(ProjectTopics.PROJECT_ROOTS, listener);
  connection.subscribe(NavBarModelListener.NAV_BAR, listener);
  connection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, listener);
  panel.putClientProperty(BUS, connection);
  panel.addKeyListener(listener);

  if (panel.isInFloatingMode()) {
    final Window window = SwingUtilities.windowForComponent(panel);
    if (window != null) {
      window.addWindowFocusListener(listener);
    }
  }
}
 
Example #9
Source File: AnnotationsPreloader.java    From consulo with Apache License 2.0 5 votes vote down vote up
public AnnotationsPreloader(final Project project) {
  myProject = project;
  myUpdateQueue = new MergingUpdateQueue("Annotations preloader queue", 1000, true, null, project, null, false);

  project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@Nonnull FileEditorManagerEvent event) {
      if (!isEnabled()) return;
      VirtualFile file = event.getNewFile();
      if (file != null) {
        schedulePreloading(file);
      }
    }
  });
}
 
Example #10
Source File: DesktopEditorComposite.java    From consulo with Apache License 2.0 5 votes vote down vote up
private void fireSelectedEditorChanged(final FileEditor oldSelectedEditor, final FileEditor newSelectedEditor) {
  if ((!EventQueue.isDispatchThread() || !myFileEditorManager.isInsideChange()) && !Comparing.equal(oldSelectedEditor, newSelectedEditor)) {
    myFileEditorManager.notifyPublisher(() -> {
      final FileEditorManagerEvent event = new FileEditorManagerEvent(myFileEditorManager, myFile, oldSelectedEditor, myFile, newSelectedEditor);
      final FileEditorManagerListener publisher = myFileEditorManager.getProject().getMessageBus().syncPublisher(FileEditorManagerListener.FILE_EDITOR_MANAGER);
      publisher.selectionChanged(event);
    });
    final JComponent component = newSelectedEditor.getComponent();
    final EditorWindowHolder holder = UIUtil.getParentOfType(EditorWindowHolder.class, component);
    if (holder != null) {
      ((FileEditorManagerImpl)myFileEditorManager).addSelectionRecord(myFile, holder.getEditorWindow());
    }
  }
}
 
Example #11
Source File: BuildifierAutoFormatter.java    From buck with Apache License 2.0 5 votes vote down vote up
@Override
public void initComponent() {
  mMessageBusConnection = ApplicationManager.getApplication().getMessageBus().connect(this);
  mMessageBusConnection.subscribe(
      FileEditorManagerListener.FILE_EDITOR_MANAGER,
      new FileEditorManagerListener() {
        @Override
        public void selectionChanged(@NotNull FileEditorManagerEvent event) {
          Project project = event.getManager().getProject();
          if (!BuckProjectSettingsProvider.getInstance(project).isAutoFormatOnBlur()) {
            return;
          }
          FileEditor newFileEditor = event.getNewEditor();
          FileEditor oldFileEditor = event.getOldEditor();
          if (oldFileEditor == null || oldFileEditor.equals(newFileEditor)) {
            return; // still editing same file
          }
          VirtualFile virtualFile = oldFileEditor.getFile();
          Document document = FileDocumentManager.getInstance().getDocument(virtualFile);
          if (document == null) {
            return; // couldn't find document
          }
          PsiFile psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document);
          if (!BuckFileType.INSTANCE.equals(psiFile.getFileType())) {
            return; // file type isn't a Buck file
          }
          Runnable runnable = () -> BuildifierUtil.doReformat(project, virtualFile);
          LOGGER.info("Autoformatting " + virtualFile.getPath());
          CommandProcessor.getInstance()
              .executeCommand(
                  project,
                  runnable,
                  null,
                  null,
                  UndoConfirmationPolicy.DO_NOT_REQUEST_CONFIRMATION,
                  document);
        }
      });
}
 
Example #12
Source File: ConfigProjectComponent.java    From editorconfig-jetbrains with MIT License 5 votes vote down vote up
public ConfigProjectComponent(Project project) {
    this.project = project;

    // Register project-level config managers
    MessageBus bus = project.getMessageBus();
    codeStyleManager = new CodeStyleManager(project);
    EditorSettingsManager editorSettingsManager = new EditorSettingsManager();
    EncodingManager encodingManager = new EncodingManager(project);
    LineEndingsManager lineEndingsManager = new LineEndingsManager(project);
    bus.connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, codeStyleManager);
    bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, encodingManager);
    bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, editorSettingsManager);
    bus.connect().subscribe(AppTopics.FILE_DOCUMENT_SYNC, lineEndingsManager);
}
 
Example #13
Source File: AutoSyncHandler.java    From intellij with Apache License 2.0 5 votes vote down vote up
protected AutoSyncHandler(Project project) {
  this.project = project;
  if (!Blaze.isBlazeProject(project)) {
    return;
  }
  // listen for changes to the VFS
  VirtualFileManager.getInstance().addVirtualFileListener(new FileListener(), project);

  // trigger a check when navigating away from an unsaved file
  project
      .getMessageBus()
      .connect()
      .subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileFocusListener());
}
 
Example #14
Source File: WidgetPerfTipsPanel.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public WidgetPerfTipsPanel(Disposable parentDisposable, @NotNull FlutterApp app) {
  setLayout(new VerticalLayout(5));

  add(new JSeparator());

  perfManager = FlutterWidgetPerfManager.getInstance(app.getProject());
  perfTips = new JPanel();
  perfTips.setLayout(new VerticalLayout(0));

  linkListener = (source, tip) -> handleTipSelection(tip);
  final Project project = app.getProject();
  final MessageBusConnection bus = project.getMessageBus().connect(project);
  final FileEditorManagerListener listener = new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@NotNull FileEditorManagerEvent event) {
      selectedEditorChanged();
    }
  };
  selectedEditorChanged();
  bus.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, listener);

  // Computing performance tips is somewhat expensive so we don't want to
  // compute them too frequently. Performance tips are only computed when
  // new performance stats are available but performance stats are updated
  // at 60fps so to be conservative we delay computing perf tips.
  final Timer perfTipComputeDelayTimer = new Timer(PERF_TIP_COMPUTE_DELAY, this::onComputePerfTips);
  perfTipComputeDelayTimer.start();
  Disposer.register(parentDisposable, perfTipComputeDelayTimer::stop);
}
 
Example #15
Source File: WidgetPerfTipsPanel.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public WidgetPerfTipsPanel(Disposable parentDisposable, @NotNull FlutterApp app) {
  setLayout(new VerticalLayout(5));

  add(new JSeparator());

  perfManager = FlutterWidgetPerfManager.getInstance(app.getProject());
  perfTips = new JPanel();
  perfTips.setLayout(new VerticalLayout(0));

  linkListener = (source, tip) -> handleTipSelection(tip);
  final Project project = app.getProject();
  final MessageBusConnection bus = project.getMessageBus().connect(project);
  final FileEditorManagerListener listener = new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@NotNull FileEditorManagerEvent event) {
      selectedEditorChanged();
    }
  };
  selectedEditorChanged();
  bus.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, listener);

  // Computing performance tips is somewhat expensive so we don't want to
  // compute them too frequently. Performance tips are only computed when
  // new performance stats are available but performance stats are updated
  // at 60fps so to be conservative we delay computing perf tips.
  final Timer perfTipComputeDelayTimer = new Timer(PERF_TIP_COMPUTE_DELAY, this::onComputePerfTips);
  perfTipComputeDelayTimer.start();
  Disposer.register(parentDisposable, perfTipComputeDelayTimer::stop);
}
 
Example #16
Source File: OnCodeAnalysisFinishedListener.java    From litho with Apache License 2.0 5 votes vote down vote up
OnCodeAnalysisFinishedListener(Project project) {
  this.project = project;
  this.bus = project.getMessageBus().connect();
  bus.subscribe(DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC, this);
  bus.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
  LOG.debug("Connected");
}
 
Example #17
Source File: HintManagerImpl.java    From consulo with Apache License 2.0 4 votes vote down vote up
@Override
public void projectOpened(@Nonnull Project project) {
  project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, myEditorManagerListener);
}
 
Example #18
Source File: DesktopEditorComposite.java    From consulo with Apache License 2.0 4 votes vote down vote up
/**
 * @param file    {@code file} for which composite is being constructed
 * @param editors {@code edittors} that should be placed into the composite
 * @throws IllegalArgumentException if {@code editors}
 *                                  is {@code null} or {@code providers} is {@code null} or {@code myEditor} arrays is empty
 */
DesktopEditorComposite(@Nonnull final VirtualFile file, @Nonnull final FileEditor[] editors, @Nonnull final FileEditorManagerEx fileEditorManager) {
  myFile = file;
  myEditors = editors;
  if (NullUtils.hasNull(editors)) throw new IllegalArgumentException("Must not pass null editors in " + Arrays.asList(editors));
  myFileEditorManager = fileEditorManager;
  myInitialFileTimeStamp = myFile.getTimeStamp();

  Disposer.register(fileEditorManager.getProject(), this);

  if (editors.length > 1) {
    myTabbedPaneWrapper = createTabbedPaneWrapper(editors);
    JComponent component = myTabbedPaneWrapper.getComponent();
    myComponent = new MyComponent(component, component);
  }
  else if (editors.length == 1) {
    myTabbedPaneWrapper = null;
    FileEditor editor = editors[0];
    myComponent = new MyComponent(createEditorComponent(editor), editor.getPreferredFocusedComponent());
  }
  else {
    throw new IllegalArgumentException("editors array cannot be empty");
  }

  mySelectedEditor = editors[0];
  myFocusWatcher = new FocusWatcher();
  myFocusWatcher.install(myComponent);

  myFileEditorManager.addFileEditorManagerListener(new FileEditorManagerListener() {
    @Override
    public void selectionChanged(@Nonnull final FileEditorManagerEvent event) {
      final VirtualFile oldFile = event.getOldFile();
      final VirtualFile newFile = event.getNewFile();
      if (Comparing.equal(oldFile, newFile) && Comparing.equal(getFile(), newFile)) {
        Runnable runnable = () -> {
          final FileEditor oldEditor = event.getOldEditor();
          if (oldEditor != null) oldEditor.deselectNotify();
          final FileEditor newEditor = event.getNewEditor();
          if (newEditor != null) newEditor.selectNotify();
          ((FileEditorProviderManagerImpl)FileEditorProviderManager.getInstance()).providerSelected(DesktopEditorComposite.this);
          ((IdeDocumentHistoryImpl)IdeDocumentHistory.getInstance(myFileEditorManager.getProject())).onSelectionChanged();
        };
        if (ApplicationManager.getApplication().isDispatchThread()) {
          CommandProcessor.getInstance().executeCommand(myFileEditorManager.getProject(), runnable, "Switch Active Editor", null);
        }
        else {
          runnable.run(); // not invoked by user
        }
      }
    }
  }, this);
}
 
Example #19
Source File: LightToolWindowManager.java    From consulo with Apache License 2.0 4 votes vote down vote up
private void initListeners() {
  myConnection = myProject.getMessageBus().connect(myProject);
  myConnection.subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, myListener);
}
 
Example #20
Source File: RoboVmPlugin.java    From robovm-idea with GNU General Public License v2.0 4 votes vote down vote up
public static void initializeProject(final Project project) {
    // setup a compile task if there isn't one yet
    boolean found = false;
    for (CompileTask task : CompilerManager.getInstance(project).getAfterTasks()) {
        if (task instanceof RoboVmCompileTask) {
            found = true;
            break;
        }
    }
    if (!found) {
        CompilerManager.getInstance(project).addAfterTask(new RoboVmCompileTask());
    }

    // hook ito the message bus so we get to know if a storyboard/xib
    // file is opened
    project.getMessageBus().connect().subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, new RoboVmFileEditorManagerListener(project));

    // initialize our tool window to which we
    // log all messages
    UIUtil.invokeLaterIfNeeded(new Runnable() {
        @Override
        public void run() {
            if (project.isDisposed()) {
                return;
            }
            ToolWindow toolWindow = ToolWindowManager.getInstance(project).registerToolWindow(ROBOVM_TOOLWINDOW_ID, false, ToolWindowAnchor.BOTTOM, project, true);
            ConsoleView consoleView = TextConsoleBuilderFactory.getInstance().createBuilder(project).getConsole();
            Content content = toolWindow.getContentManager().getFactory().createContent(consoleView.getComponent(), "Console", true);
            toolWindow.getContentManager().addContent(content);
            toolWindow.setIcon(RoboVmIcons.ROBOVM_SMALL);
            consoleViews.put(project, consoleView);
            toolWindows.put(project, toolWindow);
            logInfo(project, "RoboVM plugin initialized");
        }
    });

    // initialize virtual file change listener so we can
    // trigger recompiles on file saves
    VirtualFileListener listener = new VirtualFileAdapter() {
        @Override
        public void contentsChanged(@NotNull VirtualFileEvent event) {
            compileIfChanged(event, project);
        }
    };
    VirtualFileManager.getInstance().addVirtualFileListener(listener);
    fileListeners.put(project, listener);
}
 
Example #21
Source File: OREditorTracker.java    From reasonml-idea-plugin with MIT License 4 votes vote down vote up
public OREditorTracker(@NotNull Project project) {
    m_fileEditorListener = new ORFileEditorListener(project);
    project.getMessageBus().
            connect(this).
            subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, m_fileEditorListener);
}
 
Example #22
Source File: ConnectDocumentToLanguageServerSetupParticipant.java    From intellij-quarkus with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void projectOpened() {
    project.getMessageBus().connect(project).subscribe(FileEditorManagerListener.FILE_EDITOR_MANAGER, this);
}