List of usage examples for com.intellij.openapi.fileEditor FileEditorManager getSelectedEditors
public abstract FileEditor @NotNull [] getSelectedEditors();
From source file:com.android.tools.adtui.workbench.WorkBenchTest.java
License:Apache License
@Before public void before() { initMocks(this); myContent = new JPanel(); myContent.setPreferredSize(new Dimension(500, 400)); mySplitter = new ThreeComponentsSplitter(); Project project = ProjectManager.getInstance().getDefaultProject(); myPropertiesComponent = PropertiesComponent.getInstance(); myWorkBenchManager = WorkBenchManager.getInstance(); myFloatingToolWindowManager = FloatingToolWindowManager.getInstance(project); FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); myModel = new SideModel<>(project); myLeftMinimizePanel = spy(new MinimizedPanel<>(Side.RIGHT, myModel)); myLeftMinimizePanel.setLayout(new BoxLayout(myLeftMinimizePanel, BoxLayout.Y_AXIS)); myRightMinimizePanel = spy(new MinimizedPanel<>(Side.RIGHT, myModel)); myRightMinimizePanel.setLayout(new BoxLayout(myRightMinimizePanel, BoxLayout.Y_AXIS)); WorkBench.InitParams<String> initParams = new WorkBench.InitParams<>(myModel, mySplitter, myLeftMinimizePanel, myRightMinimizePanel); myWorkBench = new WorkBench<>(project, "BENCH", myFileEditor, initParams); JRootPane rootPane = new JRootPane(); rootPane.add(myWorkBench);//w ww. java 2 s. c o m List<ToolWindowDefinition<String>> definitions = ImmutableList.of(PalettePanelToolContent.getDefinition(), PalettePanelToolContent.getOtherDefinition(), PalettePanelToolContent.getThirdDefinition()); myWorkBench.init(myContent, "CONTEXT", definitions); myToolWindow1 = myModel.getAllTools().get(0); myToolWindow2 = myModel.getAllTools().get(1); myToolWindow3 = myModel.getAllTools().get(2); when(fileEditorManager.getSelectedEditors()).thenReturn(new FileEditor[] { myFileEditor, myFileEditor2 }); verify(myWorkBenchManager).register(eq(myWorkBench)); verify(myFloatingToolWindowManager).register(eq(myFileEditor), eq(myWorkBench)); reset(myWorkBenchManager, myFloatingToolWindowManager); }
From source file:com.android.tools.idea.uibuilder.actions.GenerateLayoutTestSkeletonAction.java
License:Apache License
@Nullable private static DesignSurface getSurface(@NotNull Project project) { FileEditorManager fileEditorManager = FileEditorManager.getInstance(project); FileEditor[] editors = fileEditorManager.getSelectedEditors(); for (FileEditor fileEditor : editors) { if (fileEditor instanceof NlEditor) { return ((NlEditor) fileEditor).getComponent().getSurface(); }/*from w w w. j a v a2s. com*/ } Editor editor = fileEditorManager.getSelectedTextEditor(); if (editor == null) { return null; } NlPreviewManager previewManager = NlPreviewManager.getInstance(project); if (previewManager.isWindowVisible()) { return previewManager.getPreviewForm().getSurface(); } PsiFile file = PsiUtilBase.getPsiFileInEditor(editor, project); if (file == null) { return null; } for (FileEditor fileEditor : fileEditorManager.getEditors(file.getVirtualFile())) { if (fileEditor instanceof NlEditor) { return ((NlEditor) fileEditor).getComponent().getSurface(); } } return null; }
From source file:com.intellij.codeInsight.daemon.impl.DaemonListeners.java
License:Apache License
public DaemonListeners(@NotNull final Project project, @NotNull DaemonCodeAnalyzerImpl daemonCodeAnalyzer, @NotNull final EditorTracker editorTracker, @NotNull EditorFactory editorFactory, @NotNull PsiDocumentManager psiDocumentManager, @NotNull CommandProcessor commandProcessor, @NotNull EditorColorsManager editorColorsManager, @NotNull final Application application, @NotNull InspectionProfileManager inspectionProfileManager, @NotNull InspectionProjectProfileManager inspectionProjectProfileManager, @NotNull TodoConfiguration todoConfiguration, @NotNull ActionManagerEx actionManagerEx, @NotNull VirtualFileManager virtualFileManager, @NotNull final NamedScopeManager namedScopeManager, @NotNull final DependencyValidationManager dependencyValidationManager, @NotNull final FileDocumentManager fileDocumentManager, @NotNull final PsiManager psiManager, @NotNull final FileEditorManager fileEditorManager, @NotNull TooltipController tooltipController, @NotNull UndoManager undoManager, @NotNull ProjectLevelVcsManager projectLevelVcsManager, @NotNull VcsDirtyScopeManager vcsDirtyScopeManager, @NotNull FileStatusManager fileStatusManager, @NotNull EditorColorsManager colorsManager) { Disposer.register(project, this); myProject = project;/*from w w w. j av a2s . c om*/ myDaemonCodeAnalyzer = daemonCodeAnalyzer; myPsiDocumentManager = psiDocumentManager; myFileEditorManager = fileEditorManager; myUndoManager = undoManager; myProjectLevelVcsManager = projectLevelVcsManager; myVcsDirtyScopeManager = vcsDirtyScopeManager; myFileStatusManager = fileStatusManager; myActionManager = actionManagerEx; myTooltipController = tooltipController; boolean replaced = ((UserDataHolderEx) myProject).replace(DAEMON_INITIALIZED, null, Boolean.TRUE); LOG.assertTrue(replaced, "Daemon listeners already initialized for the project " + myProject); MessageBus messageBus = myProject.getMessageBus(); myDaemonEventPublisher = messageBus.syncPublisher(DaemonCodeAnalyzer.DAEMON_EVENT_TOPIC); final MessageBusConnection connection = messageBus.connect(); if (project.isDefault()) return; EditorEventMulticaster eventMulticaster = editorFactory.getEventMulticaster(); eventMulticaster.addDocumentListener(new DocumentAdapter() { // clearing highlighters before changing document because change can damage editor highlighters drastically, so we'll clear more than necessary @Override public void beforeDocumentChange(final DocumentEvent e) { if (isUnderIgnoredAction(null)) return; Document document = e.getDocument(); VirtualFile virtualFile = fileDocumentManager.getFile(document); Project project = virtualFile == null ? null : ProjectUtil.guessProjectForFile(virtualFile); if (!worthBothering(document, project)) { return; //no need to stop daemon if something happened in the console } stopDaemon(true, "Document change"); UpdateHighlightersUtil.updateHighlightersByTyping(myProject, e); } }, this); eventMulticaster.addCaretListener(new CaretAdapter() { @Override public void caretPositionChanged(CaretEvent e) { final Editor editor = e.getEditor(); if (!editor.getComponent().isShowing() && !application.isUnitTestMode() || !worthBothering(editor.getDocument(), editor.getProject())) { return; //no need to stop daemon if something happened in the console } if (!application.isUnitTestMode()) { ApplicationManager.getApplication().invokeLater(new Runnable() { @Override public void run() { if (!editor.getComponent().isShowing() || myProject.isDisposed()) { return; } myDaemonCodeAnalyzer.hideLastIntentionHint(); } }, ModalityState.current()); } } }, this); eventMulticaster.addEditorMouseMotionListener(new MyEditorMouseMotionListener(), this); eventMulticaster.addEditorMouseListener(new MyEditorMouseListener(myTooltipController), this); EditorTrackerListener editorTrackerListener = new EditorTrackerListener() { private List<Editor> myActiveEditors = Collections.emptyList(); @Override public void activeEditorsChanged(@NotNull List<Editor> editors) { List<Editor> activeEditors = editorTracker.getActiveEditors(); if (myActiveEditors.equals(activeEditors)) { return; } myActiveEditors = activeEditors; stopDaemon(true, "Active editor change"); // do not stop daemon if idea loses/gains focus if (ApplicationManager.getApplication().isDispatchThread() && LaterInvocator.isInModalContext()) { // editor appear in modal context, re-enable the daemon myDaemonCodeAnalyzer.setUpdateByTimerEnabled(true); } for (Editor editor : activeEditors) { repaintErrorStripeRenderer(editor, myProject); } } }; editorTracker.addEditorTrackerListener(editorTrackerListener, this); EditorFactoryListener editorFactoryListener = new EditorFactoryListener() { @Override public void editorCreated(@NotNull EditorFactoryEvent event) { Editor editor = event.getEditor(); Document document = editor.getDocument(); Project editorProject = editor.getProject(); // worthBothering() checks for getCachedPsiFile, so call getPsiFile here PsiFile file = editorProject == null ? null : PsiDocumentManager.getInstance(editorProject).getPsiFile(document); if (!editor.getComponent().isShowing() || !worthBothering(document, editorProject)) { LOG.debug("Not worth: " + file); return; } repaintErrorStripeRenderer(editor, myProject); } @Override public void editorReleased(@NotNull EditorFactoryEvent event) { // mem leak after closing last editor otherwise UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { myDaemonCodeAnalyzer.hideLastIntentionHint(); } }); } }; editorFactory.addEditorFactoryListener(editorFactoryListener, this); PsiDocumentManagerImpl documentManager = (PsiDocumentManagerImpl) psiDocumentManager; PsiChangeHandler changeHandler = new PsiChangeHandler(myProject, documentManager, editorFactory, connection, daemonCodeAnalyzer.getFileStatusMap()); Disposer.register(this, changeHandler); psiManager.addPsiTreeChangeListener(changeHandler, changeHandler); connection.subscribe(ProjectTopics.PROJECT_ROOTS, new ModuleRootAdapter() { @Override public void rootsChanged(ModuleRootEvent event) { final FileEditor[] editors = fileEditorManager.getSelectedEditors(); if (editors.length == 0) return; application.invokeLater(new Runnable() { @Override public void run() { if (myProject.isDisposed()) return; for (FileEditor fileEditor : editors) { if (fileEditor instanceof TextEditor) { repaintErrorStripeRenderer(((TextEditor) fileEditor).getEditor(), myProject); } } } }, ModalityState.stateForComponent(editors[0].getComponent())); } }); connection.subscribe(DumbService.DUMB_MODE, new DumbService.DumbModeListener() { @Override public void enteredDumbMode() { stopDaemonAndRestartAllFiles(); } @Override public void exitDumbMode() { stopDaemonAndRestartAllFiles(); } }); connection.subscribe(PowerSaveMode.TOPIC, new PowerSaveMode.Listener() { @Override public void powerSaveStateChanged() { stopDaemon(true, "Power save mode change"); } }); colorsManager.addEditorColorsListener(new EditorColorsListener() { @Override public void globalSchemeChange(EditorColorsScheme scheme) { stopDaemonAndRestartAllFiles(); } }, this); commandProcessor.addCommandListener(new MyCommandListener(), this); application.addApplicationListener(new MyApplicationListener(), this); editorColorsManager.addEditorColorsListener(new MyEditorColorsListener(), this); inspectionProfileManager.addProfileChangeListener(new MyProfileChangeListener(), this); inspectionProjectProfileManager.addProfilesListener(new MyProfileChangeListener(), this); todoConfiguration.addPropertyChangeListener(new MyTodoListener(), this); todoConfiguration.colorSettingsChanged(); actionManagerEx.addAnActionListener(new MyAnActionListener(), this); virtualFileManager.addVirtualFileListener(new VirtualFileAdapter() { @Override public void propertyChanged(@NotNull VirtualFilePropertyEvent event) { String propertyName = event.getPropertyName(); if (VirtualFile.PROP_NAME.equals(propertyName)) { stopDaemonAndRestartAllFiles(); VirtualFile virtualFile = event.getFile(); PsiFile psiFile = !virtualFile.isValid() ? null : ((PsiManagerEx) psiManager).getFileManager().getCachedPsiFile(virtualFile); if (psiFile != null && !myDaemonCodeAnalyzer.isHighlightingAvailable(psiFile)) { Document document = fileDocumentManager.getCachedDocument(virtualFile); if (document != null) { // highlight markers no more //todo clear all highlights regardless the pass id // Here color scheme required for TextEditorFields, as far as I understand this // code related to standard file editors, which always use Global color scheme, // thus we can pass null here. final EditorColorsScheme editorColorScheme = null; UpdateHighlightersUtil.setHighlightersToEditor(myProject, document, 0, document.getTextLength(), Collections.<HighlightInfo>emptyList(), editorColorScheme, Pass.UPDATE_ALL); } } } if (!propertyName.equals(PsiTreeChangeEvent.PROP_WRITABLE)) { stopDaemon(true, "Virtual file property change"); } } }, this); ((EditorEventMulticasterEx) eventMulticaster).addErrorStripeListener(new ErrorStripeHandler(myProject), this); ModalityStateListener modalityStateListener = new ModalityStateListener() { @Override public void beforeModalityStateChanged(boolean entering) { // before showing dialog we are in non-modal context yet, and before closing dialog we are still in modal context boolean inModalContext = LaterInvocator.isInModalContext(); stopDaemon(inModalContext, "Modality change"); myDaemonCodeAnalyzer.setUpdateByTimerEnabled(inModalContext); } }; LaterInvocator.addModalityStateListener(modalityStateListener, this); messageBus.connect().subscribe(SeverityRegistrar.SEVERITIES_CHANGED_TOPIC, new Runnable() { @Override public void run() { UIUtil.invokeLaterIfNeeded(new Runnable() { @Override public void run() { if (!project.isDisposed()) { for (Editor editor : editorTracker.getActiveEditors()) { repaintErrorStripeRenderer(editor, project); } } } }); } }); if (RefResolveService.ENABLED) { RefResolveService resolveService = RefResolveService.getInstance(project); resolveService.addListener(this, new RefResolveService.Listener() { @Override public void allFilesResolved() { stopDaemon(true, "RefResolveService is up to date"); } }); } }
From source file:com.vladsch.idea.multimarkdown.editor.MultiMarkdownFxPreviewEditor.java
License:Apache License
protected boolean isMyTabSelected() { FileEditorManager manager = FileEditorManager.getInstance(project); FileEditor[] editors = manager.getSelectedEditors(); for (FileEditor editor : editors) { if (editor == this) return true; }/*from ww w . j av a 2s. c o m*/ return false; }