summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/fileEditor/impl/EditorComposite.java
blob: 5b15a35b127dc8025a67f513ffc4781a44ab0f64 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.openapi.fileEditor.impl;

import com.intellij.featureStatistics.fusCollectors.FileEditorCollector;
import com.intellij.ide.IdeBundle;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.CommonDataKeys;
import com.intellij.openapi.actionSystem.DataProvider;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.actionSystem.PlatformCoreDataKeys;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.command.CommandProcessor;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.editor.colors.EditorColors;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.fileEditor.*;
import com.intellij.openapi.fileEditor.ex.FileEditorManagerEx;
import com.intellij.openapi.fileEditor.ex.FileEditorProviderManager;
import com.intellij.openapi.fileEditor.ex.FileEditorWithProvider;
import com.intellij.openapi.fileEditor.ex.IdeDocumentHistory;
import com.intellij.openapi.project.DumbService;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.*;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.wm.FocusWatcher;
import com.intellij.openapi.wm.IdeFocusManager;
import com.intellij.ui.*;
import com.intellij.ui.components.JBPanelWithEmptyText;
import com.intellij.ui.components.panels.NonOpaquePanel;
import com.intellij.ui.components.panels.Wrapper;
import com.intellij.ui.tabs.JBTabs;
import com.intellij.ui.tabs.impl.JBTabsImpl;
import com.intellij.util.ArrayUtil;
import com.intellij.util.EventDispatcher;
import com.intellij.util.ObjectUtils;
import com.intellij.util.SmartList;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.Supplier;

/**
 * An abstraction over one or several file editors opened in the same tab (e.g. designer and code-behind).
 * It's a composite what can be pinned in the tabs list or opened as a preview, not concrete file editors.
 * It also manages the internal UI structure: bottom and top components, panels, labels, actions for navigating between editors it owns.
 */
public class EditorComposite extends UserDataHolderBase implements Disposable {
  private static final Logger LOG = Logger.getInstance(EditorComposite.class);

  /**
   * File for which composite is created
   */
  @NotNull private final VirtualFile myFile;

  @NotNull private final Project myProject;
  /**
   * Whether the composite is pinned or not
   */
  private boolean myPinned;
  /**
   * Whether the composite is opened as preview tab or not
   */
  private boolean myPreview;
  /**
   * This is initial timestamp of the file. It uses to implement
   * "close non modified editors first" feature.
   */
  private final long myInitialFileTimeStamp;
  private TabbedPaneWrapper myTabbedPaneWrapper;
  @NotNull
  private final MyComponent myComponent;
  private final FocusWatcher myFocusWatcher;
  /**
   * Currently selected myEditor
   */
  private FileEditorWithProvider mySelectedEditorWithProvider;
  private final FileEditorManagerEx myFileEditorManager;
  private final Map<FileEditor, JComponent> myTopComponents = new HashMap<>();
  private final Map<FileEditor, JComponent> myBottomComponents = new HashMap<>();
  private final Map<FileEditor, @NlsContexts.TabTitle String> myDisplayNames = new HashMap<>();

  /**
   * Editors opened in the composite
   */
  private final List<FileEditorWithProvider> myEditorsWithProviders = new CopyOnWriteArrayList<>();

  private final EventDispatcher<EditorCompositeListener> myDispatcher = EventDispatcher.create(EditorCompositeListener.class);

  EditorComposite(@NotNull final VirtualFile file,
                  @NotNull List<@NotNull FileEditorWithProvider> editorsWithProviders,
                  @NotNull final FileEditorManagerEx fileEditorManager) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    myFile = file;
    myEditorsWithProviders.addAll(editorsWithProviders);
    for (FileEditorWithProvider editorWithProvider : myEditorsWithProviders) {
      FileEditor.FILE_KEY.set(editorWithProvider.getFileEditor(), myFile);
    }
    myFileEditorManager = fileEditorManager;
    myInitialFileTimeStamp = myFile.getTimeStamp();

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

    if (myEditorsWithProviders.size() > 1) {
      myTabbedPaneWrapper = createTabbedPaneWrapper(null);
      JComponent component = myTabbedPaneWrapper.getComponent();
      myComponent = new MyComponent(component, () -> component);
    }
    else if (myEditorsWithProviders.size() == 1) {
      myTabbedPaneWrapper = null;
      FileEditor editor = myEditorsWithProviders.get(0).getFileEditor();
      myComponent = new MyComponent(createEditorComponent(editor), editor::getPreferredFocusedComponent);
    }
    else {
      throw new IllegalArgumentException("editors array cannot be empty");
    }

    mySelectedEditorWithProvider = myEditorsWithProviders.get(0);
    myFocusWatcher = new FocusWatcher();
    myFocusWatcher.install(myComponent);

    myProject.getMessageBus().connect(this).subscribe(
          FileEditorManagerListener.FILE_EDITOR_MANAGER, new FileEditorManagerListener() {
      @Override
      public void selectionChanged(@NotNull 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();

              FileEditorCollector.logAlternativeFileEditorSelected(myProject, newFile, newEditor);
            }
            ((FileEditorProviderManagerImpl)FileEditorProviderManager.getInstance()).providerSelected(EditorComposite.this);
            ((IdeDocumentHistoryImpl)IdeDocumentHistory.getInstance(myProject)).onSelectionChanged();
          };
          if (ApplicationManager.getApplication().isDispatchThread()) {
            CommandProcessor.getInstance().executeCommand(myProject, runnable,
                                                          IdeBundle.message("command.switch.active.editor"), null);
          }
          else {
            runnable.run(); // not invoked by user
          }
        }
      }
    });
  }

  public @NotNull Project getProject() {
    return myProject;
  }

  /**
   * @deprecated use {@link #getAllEditorsWithProviders()}
   */
  @Deprecated
  public FileEditorProvider @NotNull [] getProviders() {
    return getAllProviders().toArray(FileEditorProvider.EMPTY_ARRAY);
  }

  public @NotNull List<@NotNull FileEditorProvider> getAllProviders() {
    return ContainerUtil.map(getAllEditorsWithProviders(), it -> it.getProvider());
  }

  @NotNull
  private TabbedPaneWrapper.AsJBTabs createTabbedPaneWrapper(MyComponent myComponent) {
    PrevNextActionsDescriptor descriptor = new PrevNextActionsDescriptor(IdeActions.ACTION_NEXT_EDITOR_TAB, IdeActions.ACTION_PREVIOUS_EDITOR_TAB);
    final TabbedPaneWrapper.AsJBTabs wrapper = new TabbedPaneWrapper.AsJBTabs(myProject, SwingConstants.BOTTOM, descriptor, this);

    boolean firstEditor = true;
    for (FileEditorWithProvider editorWithProvider : myEditorsWithProviders) {
      FileEditor editor = editorWithProvider.getFileEditor();
      JComponent component = firstEditor && myComponent != null ? (JComponent)myComponent.getComponent(0) : createEditorComponent(editor);
      wrapper.addTab(getDisplayName(editor), component);
      firstEditor = false;
    }
    wrapper.addChangeListener(new MyChangeListener());

    return wrapper;
  }

  @NotNull
  private JComponent createEditorComponent(@NotNull FileEditor editor) {
    JPanel component = new JPanel(new BorderLayout());
    JComponent comp = editor.getComponent();
    if (!FileEditorManagerImpl.isDumbAware(editor)) {
      comp = DumbService.getInstance(myProject).wrapGently(comp, editor);
    }

    component.add(comp, BorderLayout.CENTER);

    JPanel topPanel = new TopBottomPanel();
    myTopComponents.put(editor, topPanel);
    component.add(topPanel, BorderLayout.NORTH);

    final JPanel bottomPanel = new TopBottomPanel();
    myBottomComponents.put(editor, bottomPanel);
    component.add(bottomPanel, BorderLayout.SOUTH);

    return component;
  }

  /**
   * @return whether myEditor composite is pinned
   */
  public boolean isPinned(){
    return myPinned;
  }

  /**
   * Sets new "pinned" state
   */
  void setPinned(final boolean pinned) {
    boolean oldPinned = myPinned;
    myPinned = pinned;
    ObjectUtils.consumeIfCast(myComponent.getParent(), JComponent.class,
                              c -> ClientProperty.put(c, JBTabsImpl.PINNED, myPinned ? Boolean.TRUE : null));
    if (pinned != oldPinned) {
      myDispatcher.getMulticaster().isPinnedChanged(pinned);
    }
  }

  public boolean isPreview() {
    return myPreview;
  }

  void setPreview(final boolean preview) {
    if (preview == myPreview) return;
    myPreview = preview;
    myDispatcher.getMulticaster().isPreviewChanged(preview);
  }

  protected void fireSelectedEditorChanged(@NotNull FileEditorWithProvider oldEditorWithProvider, @NotNull FileEditorWithProvider newEditorWithProvider) {
    if (EventQueue.isDispatchThread() && myFileEditorManager.isInsideChange() ||
        Comparing.equal(oldEditorWithProvider, newEditorWithProvider)) {
      return;
    }

    myFileEditorManager.notifyPublisher(() -> {
      final FileEditorManagerEvent event = new FileEditorManagerEvent(myFileEditorManager, oldEditorWithProvider, newEditorWithProvider);
      final FileEditorManagerListener publisher =
        myProject.getMessageBus().syncPublisher(FileEditorManagerListener.FILE_EDITOR_MANAGER);
      publisher.selectionChanged(event);
    });
    final JComponent component = newEditorWithProvider.getFileEditor().getComponent();
    final EditorWindowHolder holder =
      ComponentUtil.getParentOfType((Class<? extends EditorWindowHolder>)EditorWindowHolder.class, (Component)component);
    if (holder != null) {
      ((FileEditorManagerImpl)myFileEditorManager).addSelectionRecord(myFile, holder.getEditorWindow());
    }
  }

  public void addListener(EditorCompositeListener listener, Disposable disposable) {
    myDispatcher.addListener(listener, disposable);
  }

  /**
   * @return preferred focused component inside myEditor composite. Composite uses FocusWatcher to
   * track focus movement inside the myEditor.
   */
  @Nullable
  public JComponent getPreferredFocusedComponent() {
    if (mySelectedEditorWithProvider == null) return null;

    final Component component = myFocusWatcher.getFocusedComponent();
    if (!(component instanceof JComponent) || !component.isShowing() || !component.isEnabled() || !component.isFocusable()) {
      return getSelectedEditor().getPreferredFocusedComponent();
    }
    return (JComponent)component;
  }

  /**
   * @return file for which composite was created.
   */
  @NotNull
  public VirtualFile getFile() {
    return myFile;
  }

  @NotNull
  public FileEditorManager getFileEditorManager() {
    return myFileEditorManager;
  }

  /**
   * @return initial time stamp of the file (on moment of creation of
   * the composite)
   */
  public long getInitialFileTimeStamp() {
    return myInitialFileTimeStamp;
  }

  /**
   * @return editors which are opened in the composite. <b>Do not modify
   * this array</b>.
   * @deprecated use {@link #getAllEditors()}
   */
  @Deprecated
  public FileEditor @NotNull [] getEditors() {
    return getAllEditors().toArray(FileEditor.EMPTY_ARRAY);
  }

  public @NotNull List<@NotNull FileEditor> getAllEditors() {
    return ContainerUtil.map(getAllEditorsWithProviders(), it -> it.getFileEditor());
  }

  public @NotNull List<@NotNull FileEditorWithProvider> getAllEditorsWithProviders() {
    return Collections.unmodifiableList(new SmartList<>(myEditorsWithProviders));
  }

  @NotNull
  List<JComponent> getTopComponents(@NotNull FileEditor editor) {
    List<JComponent> result = new SmartList<>();
    JComponent container = myTopComponents.get(editor);
    for (Component each : container.getComponents()) {
      if (each instanceof NonOpaquePanel) {
        result.add(((NonOpaquePanel)each).getTargetComponent());
      }
    }
    return Collections.unmodifiableList(result);
  }

  @Nullable
  public JBTabs getTabs() {
    return myTabbedPaneWrapper == null ? null : ((TabbedPaneWrapper.AsJBTabs)myTabbedPaneWrapper).getTabs();
  }

  public void addTopComponent(@NotNull FileEditor editor, @NotNull JComponent component) {
    manageTopOrBottomComponent(editor, component, true, false);
  }

  public void removeTopComponent(@NotNull FileEditor editor, @NotNull JComponent component) {
    manageTopOrBottomComponent(editor, component, true, true);
  }

  public void addBottomComponent(@NotNull FileEditor editor, @NotNull JComponent component) {
    manageTopOrBottomComponent(editor, component, false, false);
  }

  public void removeBottomComponent(@NotNull FileEditor editor, @NotNull JComponent component) {
    manageTopOrBottomComponent(editor, component, false, true);
  }

  private void manageTopOrBottomComponent(@NotNull FileEditor editor, @NotNull JComponent component, boolean top, boolean remove) {
    final JComponent container = top ? myTopComponents.get(editor) : myBottomComponents.get(editor);
    assert container != null;

    if (remove) {
      container.remove(component.getParent());
      EditorCompositeListener multicaster = myDispatcher.getMulticaster();
      if (top) {
        multicaster.topComponentRemoved(editor, component);
      }
      else {
        multicaster.bottomComponentRemoved(editor,component);
      }
    }
    else {
      NonOpaquePanel wrapper = new NonOpaquePanel(component);
      if (!Boolean.TRUE.equals(component.getClientProperty(FileEditorManager.SEPARATOR_DISABLED))) {
        wrapper.setBorder(createTopBottomSideBorder(top));
      }
      int index = calcComponentInsertionIndex(component, container);
      container.add(wrapper, index);
      EditorCompositeListener multicaster = myDispatcher.getMulticaster();
      if (top) {
        multicaster.topComponentAdded(editor, index, component);
      }
      else {
        multicaster.bottomComponentAdded(editor, index, component);
      }
    }
    container.revalidate();
  }

  private static int calcComponentInsertionIndex(@NotNull JComponent newComponent, @NotNull JComponent container) {
    for (int i = 0, max = container.getComponentCount(); i < max; i++) {
      Component childWrapper = container.getComponent(i);
      Component childComponent = childWrapper instanceof Wrapper ? ((Wrapper)childWrapper).getTargetComponent() : childWrapper;
      boolean weighted1 = newComponent instanceof Weighted;
      boolean weighted2 = childComponent instanceof Weighted;
      if (!weighted2) continue;
      if (!weighted1) return i;

      double w1 = ((Weighted)newComponent).getWeight();
      double w2 = ((Weighted)childComponent).getWeight();
      if (w1 < w2) return i;
    }
    return -1;
  }

  public void setDisplayName(@NotNull FileEditor editor, @NlsContexts.TabTitle @NotNull String name) {
    int index = ContainerUtil.indexOf(myEditorsWithProviders, it -> it.getFileEditor().equals(editor));
    assert index != -1;

    myDisplayNames.put(editor, name);
    if (myTabbedPaneWrapper != null) {
      myTabbedPaneWrapper.setTitleAt(index, name);
    }
    myDispatcher.getMulticaster().displayNameChanged(editor, name);
  }

  @NotNull
  protected @NlsContexts.TabTitle String getDisplayName(@NotNull FileEditor editor) {
    return ObjectUtils.notNull(myDisplayNames.get(editor), editor.getName());
  }

  /**
   * @return currently selected myEditor.
   */
  @NotNull
  public FileEditor getSelectedEditor() {
    return getSelectedWithProvider().getFileEditor();
  }

  /**
   * @return currently selected myEditor with its provider.
   */
  @NotNull
  public FileEditorWithProvider getSelectedWithProvider() {
    if (myEditorsWithProviders.size() == 1) {
      LOG.assertTrue(myTabbedPaneWrapper == null);
      return myEditorsWithProviders.get(0);
    }
    else {
      // we have to get myEditor from tabbed pane
      LOG.assertTrue(myTabbedPaneWrapper != null);
      int index = myTabbedPaneWrapper.getSelectedIndex();
      if (index == -1) {
        index = 0;
      }
      LOG.assertTrue(index >= 0, index);
      LOG.assertTrue(index < myEditorsWithProviders.size(), index);
      return myEditorsWithProviders.get(index);
    }
  }

  public void setSelectedEditor(@NotNull String providerId) {
    FileEditorWithProvider newSelection = ContainerUtil.find(myEditorsWithProviders, it -> it.getProvider().getEditorTypeId().equals(providerId));
    if (newSelection != null) {
      setSelectedEditor(newSelection);
    }
  }

  public void setSelectedEditor(@NotNull FileEditor editor) {
    FileEditorWithProvider newSelection = ContainerUtil.find(myEditorsWithProviders, it -> it.getFileEditor().equals(editor));
    LOG.assertTrue(newSelection != null, "Unable to find editor=" + editor);
    setSelectedEditor(newSelection);
  }

  public void setSelectedEditor(@NotNull FileEditorWithProvider editorWithProvider) {
    if (myEditorsWithProviders.size() == 1) {
      LOG.assertTrue(myTabbedPaneWrapper == null);
      LOG.assertTrue(editorWithProvider.equals(myEditorsWithProviders.get(0)));
      return;
    }

    int index = myEditorsWithProviders.indexOf(editorWithProvider);
    LOG.assertTrue(index != -1);
    LOG.assertTrue(myTabbedPaneWrapper != null);
    myTabbedPaneWrapper.setSelectedIndex(index);
  }

  /**
   * @deprecated use {@link #getSelectedWithProvider()}
   */
  @Deprecated(forRemoval = true)
  @NotNull
  public Pair<FileEditor, FileEditorProvider> getSelectedEditorWithProvider() {
    FileEditorWithProvider info = getSelectedWithProvider();
    return Pair.create(info.getFileEditor(), info.getProvider());
  }

  /**
   * @return component which represents set of file editors in the UI
   */
  @NotNull
  public JComponent getComponent() {
    return myComponent;
  }

  /**
   * @return component which represents the component that is supposed to be focusable
   */
  @Nullable
  public JComponent getFocusComponent() {
    return myComponent.myFocusComponent.get();
  }

  /**
   * @return {@code true} if the composite contains at least one modified myEditor
   */
  public boolean isModified() {
    return ContainerUtil.exists(getAllEditors(), editor -> editor.isModified());
  }

  /**
   * Handles changes of selected myEditor
   */
  private final class MyChangeListener implements ChangeListener{
    @Override
    public void stateChanged(ChangeEvent e) {
      FileEditorWithProvider oldSelectedEditorWithProvider = mySelectedEditorWithProvider;
      int selectedIndex = myTabbedPaneWrapper.getSelectedIndex();
      LOG.assertTrue(selectedIndex != -1);
      mySelectedEditorWithProvider = myEditorsWithProviders.get(selectedIndex);
      fireSelectedEditorChanged(oldSelectedEditorWithProvider, mySelectedEditorWithProvider);
    }
  }

  public static boolean isEditorComposite(@NotNull Component component) {
    return component instanceof MyComponent;
  }

  private class MyComponent extends JPanel implements DataProvider{
    private @NotNull Supplier<? extends JComponent> myFocusComponent;

    MyComponent(@NotNull JComponent realComponent, @NotNull Supplier<? extends JComponent> focusComponent) {
      super(new BorderLayout());
      setFocusable(false);
      myFocusComponent = focusComponent;
      add(realComponent, BorderLayout.CENTER);
    }

    void setComponent(JComponent newComponent) {
      add(newComponent, BorderLayout.CENTER);
      myFocusComponent = () -> newComponent;
    }

    @Override
    public boolean requestFocusInWindow() {
      JComponent focusComponent = myFocusComponent.get();
      return focusComponent != null && focusComponent.requestFocusInWindow();
    }

    @Override
    public void requestFocus() {
      JComponent focusComponent = myFocusComponent.get();
      if (focusComponent != null) {
        IdeFocusManager.getGlobalInstance()
          .doWhenFocusSettlesDown(() -> IdeFocusManager.getGlobalInstance().requestFocus(focusComponent, true));
      }
    }

    @Override
    public boolean requestDefaultFocus() {
      JComponent focusComponent = myFocusComponent.get();
      return focusComponent != null && focusComponent.requestDefaultFocus();
    }

    @Override
    public final Object getData(@NotNull String dataId) {
      if (PlatformCoreDataKeys.FILE_EDITOR.is(dataId)) {
        return getSelectedEditor();
      }
      if (CommonDataKeys.VIRTUAL_FILE.is(dataId)) {
        return myFile.isValid() ? myFile : null;
      }
      if (CommonDataKeys.VIRTUAL_FILE_ARRAY.is(dataId)) {
        return myFile.isValid() ? new VirtualFile[]{myFile} : null;
      }
      if (CommonDataKeys.PROJECT.is(dataId)) {
        return getProject();
      }
      JComponent component = getPreferredFocusedComponent();
      if (component instanceof DataProvider && component != this) {
        return ((DataProvider)component).getData(dataId);
      }
      return null;
    }
  }

  @Override
  public void dispose() {
    for (FileEditorWithProvider editor : myEditorsWithProviders) {
      if (!Disposer.isDisposed(editor.getFileEditor())) {
        Disposer.dispose(editor.getFileEditor());
      }
    }
    myFocusWatcher.deinstall(myFocusWatcher.getTopComponent());
  }

  public void addEditor(@NotNull FileEditor editor, @NotNull FileEditorProvider provider) {
    ApplicationManager.getApplication().assertIsDispatchThread();
    FileEditorWithProvider editorWithProvider = new FileEditorWithProvider(editor, provider);
    myEditorsWithProviders.add(editorWithProvider);

    FileEditor.FILE_KEY.set(editor, myFile);
    if (myTabbedPaneWrapper == null) {
      myTabbedPaneWrapper = createTabbedPaneWrapper(myComponent);
      myComponent.setComponent(myTabbedPaneWrapper.getComponent());
    }
    else {
      JComponent component = createEditorComponent(editor);
      myTabbedPaneWrapper.addTab(getDisplayName(editor), component);
    }
    myFocusWatcher.deinstall(myFocusWatcher.getTopComponent());
    myFocusWatcher.install(myComponent);
    myDispatcher.getMulticaster().editorAdded(editorWithProvider);
  }

  private static final class TopBottomPanel extends JBPanelWithEmptyText {
    private TopBottomPanel() {
      setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));
    }

    @Override
    public Color getBackground() {
      EditorColorsScheme globalScheme = EditorColorsManager.getInstance().getGlobalScheme();
      if (ExperimentalUI.isNewEditorTabs()) {
        return globalScheme.getDefaultBackground();
      }
      Color color = globalScheme.getColor(EditorColors.GUTTER_BACKGROUND);
      return color == null ? EditorColors.GUTTER_BACKGROUND.getDefaultColor() : color;
    }
  }

  @NotNull
  private static SideBorder createTopBottomSideBorder(boolean top) {
    return new SideBorder(null, top ? SideBorder.BOTTOM : SideBorder.TOP) {
      @Override
      public Color getLineColor() {
        EditorColorsScheme scheme = EditorColorsManager.getInstance().getGlobalScheme();
        if (ExperimentalUI.isNewEditorTabs()) {
          return scheme.getDefaultBackground();
        }
        Color result = scheme.getColor(top ? EditorColors.SEPARATOR_ABOVE_COLOR : EditorColors.SEPARATOR_BELOW_COLOR);
        if (result == null) result = scheme.getColor(EditorColors.TEARLINE_COLOR);
        return result == null ? JBColor.BLACK : result;
      }
    };
  }

  @NotNull HistoryEntry currentStateAsHistoryEntry() {
    final FileEditor[] editors = getEditors();
    final FileEditorState[] states = new FileEditorState[editors.length];
    for (int j = 0; j < states.length; j++) {
      states[j] = editors[j].getState(FileEditorStateLevel.FULL);
      LOG.assertTrue(states[j] != null);
    }
    final int selectedProviderIndex = ArrayUtil.find(editors, getSelectedEditor());
    LOG.assertTrue(selectedProviderIndex != -1);
    final FileEditorProvider[] providers = getProviders();
    return HistoryEntry.createLight(getFile(), providers, states, providers[selectedProviderIndex]);
  }

  /**
   * A mapper for old API with arrays and pairs
   */
  @NotNull
  public static Pair<FileEditor @NotNull [], FileEditorProvider @NotNull []> retrofit(@Nullable EditorComposite composite) {
    if (composite == null) return new Pair<>(FileEditor.EMPTY_ARRAY, FileEditorProvider.EMPTY_ARRAY);

    FileEditor[] editors = composite.getAllEditors().toArray(FileEditor.EMPTY_ARRAY);
    FileEditorProvider[] providers = composite.getAllProviders().toArray(FileEditorProvider.EMPTY_ARRAY);
    return new Pair<>(editors, providers);
  }
}