summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/diff/impl/incrementalMerge/ui/MergePanel2.java
blob: cbad541fe12934b67193de9ca93ecb8fa91f220a (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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.diff.impl.incrementalMerge.ui;

import com.intellij.icons.AllIcons;
import com.intellij.openapi.Disposable;
import com.intellij.openapi.actionSystem.ActionManager;
import com.intellij.openapi.actionSystem.DataContext;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.actionSystem.PlatformDataKeys;
import com.intellij.openapi.components.ServiceManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.diff.*;
import com.intellij.openapi.diff.actions.NextDiffAction;
import com.intellij.openapi.diff.actions.PreviousDiffAction;
import com.intellij.openapi.diff.actions.ToggleAutoScrollAction;
import com.intellij.openapi.diff.impl.*;
import com.intellij.openapi.diff.impl.highlighting.FragmentSide;
import com.intellij.openapi.diff.impl.incrementalMerge.ChangeCounter;
import com.intellij.openapi.diff.impl.incrementalMerge.ChangeList;
import com.intellij.openapi.diff.impl.incrementalMerge.MergeList;
import com.intellij.openapi.diff.impl.mergeTool.MergeRequestImpl;
import com.intellij.openapi.diff.impl.mergeTool.MergeTool;
import com.intellij.openapi.diff.impl.settings.DiffMergeEditorSetting;
import com.intellij.openapi.diff.impl.settings.DiffMergeSettings;
import com.intellij.openapi.diff.impl.settings.DiffMergeSettingsAction;
import com.intellij.openapi.diff.impl.settings.MergeToolSettings;
import com.intellij.openapi.diff.impl.splitter.DiffDividerPaint;
import com.intellij.openapi.diff.impl.splitter.LineBlocks;
import com.intellij.openapi.diff.impl.util.*;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.colors.EditorColorsManager;
import com.intellij.openapi.editor.colors.EditorColorsScheme;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.ex.EditorMarkupModel;
import com.intellij.openapi.editor.highlighter.EditorHighlighter;
import com.intellij.openapi.editor.highlighter.EditorHighlighterFactory;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.FileTypes;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.DialogBuilder;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.LabeledComponent;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.ui.EditorNotificationPanel;
import com.intellij.util.containers.Convertor;
import gnu.trove.TIntHashSet;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;

public class MergePanel2 implements DiffViewer {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.diff.impl.incrementalMerge.ui.MergePanel2");
  private final DiffPanelOuterComponent myPanel;
  private DiffRequest myData;
  private MergeList myMergeList;
  private boolean myDuringCreation = false;
  private final SyncScrollSupport myScrollSupport = new SyncScrollSupport();
  private final DiffDivider[] myDividers = {new DiffDivider(FragmentSide.SIDE2), new DiffDivider(FragmentSide.SIDE1)};
  private boolean myScrollToFirstDiff = true;

  private final LabeledComponent[] myEditorsPanels = new LabeledComponent[EDITORS_COUNT];
  public static final int EDITORS_COUNT = 3;
  private final DividersRepainter myDividersRepainter = new DividersRepainter();
  private StatusUpdater myStatusUpdater;
  private final DialogBuilder myBuilder;
  private final MyDataProvider myProvider;

  public MergePanel2(DialogBuilder builder, @NotNull Disposable parent) {
    ArrayList<EditorPlace> editorPlaces = new ArrayList<EditorPlace>();
    EditorPlace.EditorListener placeListener = new EditorPlace.EditorListener() {
      public void onEditorCreated(EditorPlace place) {
        if (myDuringCreation) return;
        disposeMergeList();
        myDuringCreation = true;
        try {
          tryInitView();
        }
        finally {
          myDuringCreation = false;
        }
      }

      public void onEditorReleased(Editor releasedEditor) {
        LOG.assertTrue(!myDuringCreation);
        disposeMergeList();
      }
    };
    for (int i = 0; i < EDITORS_COUNT; i++) {
      EditorPlace editorPlace = new EditorPlace(new DiffEditorState(i), indexToColumn(i), this);
      Disposer.register(parent, editorPlace);
      editorPlaces.add(editorPlace);
      editorPlace.addListener(placeListener);
      myEditorsPanels[i] = new LabeledComponent();
      myEditorsPanels[i].setLabelLocation(BorderLayout.NORTH);
      myEditorsPanels[i].setComponent(editorPlace);
    }
    FontSizeSynchronizer.attachTo(editorPlaces);
    myPanel = new DiffPanelOuterComponent(TextDiffType.MERGE_TYPES, createToolbar());
    myPanel.insertDiffComponent(new ThreePanels(myEditorsPanels, myDividers), new MyScrollingPanel());
    myProvider = new MyDataProvider();
    myPanel.setDataProvider(myProvider);
    myBuilder = builder;
  }

  /**
   * Convert legacy-style editor (or panel) number to the {@link MergePanelColumn}.
   * @param i 0, 1 or 2
   * @return  Left, base or right, respectively.
   */
  private static MergePanelColumn indexToColumn(int i) {
    switch (i) {
      case 0: return MergePanelColumn.LEFT;
      case 1: return MergePanelColumn.BASE;
      case 2: return MergePanelColumn.RIGHT;
      default: throw new IllegalStateException("Incorrect value for a merge column: " + i);
    }
  }

  @NotNull
  private DiffRequest.ToolbarAddons createToolbar() {
    return new DiffRequest.ToolbarAddons() {
      public void customize(DiffToolbar toolbar) {
        toolbar.addAction(PreviousDiffAction.find());
        toolbar.addAction(NextDiffAction.find());
        toolbar.addSeparator();
        toolbar.addAction(new OpenPartialDiffAction(0, 1, AllIcons.Diff.LeftDiff));
        toolbar.addAction(new OpenPartialDiffAction(1, 2, AllIcons.Diff.RightDiff));
        toolbar.addAction(new OpenPartialDiffAction(0, 2, AllIcons.Diff.BranchDiff));
        toolbar.addSeparator();
        toolbar.addAction(new ApplyNonConflicts(myPanel));
        toolbar.addSeparator();
        toolbar.addAction(new ToggleAutoScrollAction());
        Project project = myData.getProject();
        if (project != null) {
          toolbar.addSeparator();
          toolbar.addAction(new DiffMergeSettingsAction(getEditors(), ServiceManager.getService(project, MergeToolSettings.class)));
        }
      }
    };
  }

  @NotNull
  private Collection<Editor> getEditors() {
    Collection<Editor> editors = new ArrayList<Editor>(3);
    for (EditorPlace place : getEditorPlaces()) {
      editors.add(place.getEditor());
    }
    return editors;
  }

  @NotNull
  private Collection<EditorPlace> getEditorPlaces() {
    Collection<EditorPlace> editorPlaces = new ArrayList<EditorPlace>(3);
    for (LabeledComponent editorsPanel : myEditorsPanels) {
      editorPlaces.add((EditorPlace) editorsPanel.getComponent());
    }
    return editorPlaces;
  }

  public void setScrollToFirstDiff(final boolean scrollToFirstDiff) {
    myScrollToFirstDiff = scrollToFirstDiff;
  }

  /**
   * @deprecated Because it references by index.
   */
  @Nullable
  @Deprecated
  public Editor getEditor(int index) {
    return getEditorPlace(index).getEditor();
  }

  public FileType getContentType() {
    return myData == null ? FileTypes.PLAIN_TEXT : getContentType(myData);
  }

  /**
   * @deprecated Because it references by index.
   */
  @Deprecated
  public String getVersionTitle(int index) {
    return myEditorsPanels[index].getRawText();
  }

  /**
   * @deprecated Because it references by index.
   */
  @Deprecated
  public EditorPlace getEditorPlace(int index) {
    return (EditorPlace)myEditorsPanels[index].getComponent();
  }

  private void createMergeList() {
    if (myData == null) return;
    DiffContent[] contents = myData.getContents();
    for (int i = 0; i < EDITORS_COUNT; i++) {
      EditorPlace editorPlace = getEditorPlace(i);
      editorPlace.setDocument(contents[i].getDocument());
      setHighlighterSettings(null, editorPlace);
    }
    tryInitView();
  }

  private void tryInitView() {
    if (!hasAllEditors()) return;
    if (myMergeList != null) return;
    myMergeList = MergeList.create(myData);
    myMergeList.addListener(myDividersRepainter);
    myStatusUpdater = StatusUpdater.install(myMergeList, myPanel);
    Editor left = getEditor(0);
    Editor base = getEditor(1);
    Editor right = getEditor(2);

    setupHighlighterSettings(left, base, right);

    myMergeList.setMarkups(left, base, right);
    EditingSides[] sides = {getFirstEditingSide(), getSecondEditingSide()};
    myScrollSupport.install(sides);
    for (int i = 0; i < myDividers.length; i++) {
      myDividers[i].listenEditors(sides[i]);
    }
    if (myScrollToFirstDiff) {
      myPanel.requestScrollEditors();
    }
    if (myMergeList.getErrorMessage() != null) {
      myPanel.insertTopComponent(new EditorNotificationPanel() {
        {
          myLabel.setText(myMergeList.getErrorMessage());
        }
      });
    }
  }

  @NotNull
  EditingSides getFirstEditingSide() {
    return new MyEditingSides(FragmentSide.SIDE1);
  }

  @NotNull
  EditingSides getSecondEditingSide() {
    return new MyEditingSides(FragmentSide.SIDE2);
  }

  public void setAutoScrollEnabled(boolean enabled) {
    myScrollSupport.setEnabled(enabled);
  }

  public boolean isAutoScrollEnabled() {
    return myScrollSupport.isEnabled();
  }

  private void setupHighlighterSettings(Editor left, Editor base, Editor right) {
    Editor[] editors = new Editor[]{left, base, right};
    DiffContent[] contents = myData.getContents();
    FileType[] types = DiffUtil.chooseContentTypes(contents);

    VirtualFile fallbackFile = contents[1].getFile();
    FileType fallbackType = contents[1].getContentType();

    for (int i = 0; i < 3; i++) {
      Editor editor = editors[i];
      DiffContent content = contents[i];

      EditorHighlighter highlighter =
        createHighlighter(types[i], content.getFile(), fallbackFile, fallbackType, myData.getProject()).createHighlighter();
      if (highlighter != null) {
        ((EditorEx)editor).setHighlighter(highlighter);
      }
    }
  }

  private static DiffHighlighterFactory createHighlighter(FileType contentType,
                                                          VirtualFile file,
                                                          VirtualFile otherFile,
                                                          FileType otherType,
                                                          Project project) {
    if (file == null) file = otherFile;
    if (contentType == null) contentType = otherType;

    return new DiffHighlighterFactoryImpl(contentType, file, project);
  }

  public void setHighlighterSettings(@Nullable EditorColorsScheme settings) {
    for (EditorPlace place : getEditorPlaces()) {
      setHighlighterSettings(settings, place);
    }
  }

  private void setHighlighterSettings(@Nullable EditorColorsScheme settings, @NotNull EditorPlace place) {
    if (settings == null) {
      settings = EditorColorsManager.getInstance().getGlobalScheme();
    }
    Editor editor = place.getEditor();
    DiffEditorState editorState = place.getState();
    if (editor != null) {
      ((EditorEx)editor).setHighlighter(EditorHighlighterFactory.getInstance().
        createEditorHighlighter(editorState.getFileType(), settings, editorState.getProject()));
    }
  }

  private static void initEditorSettings(@NotNull Editor editor) {
    Project project = editor.getProject();
    DiffMergeSettings settings = project == null ? null : ServiceManager.getService(project, MergeToolSettings.class);
    for (DiffMergeEditorSetting property : DiffMergeEditorSetting.values()) {
      property.apply(editor, settings == null ? property.getDefault() : settings.getPreference(property));
    }
    editor.getSettings().setLineMarkerAreaShown(true);
  }

  private void disposeMergeList() {
    if (myMergeList == null) return;
    if (myStatusUpdater != null) {
      myStatusUpdater.dispose(myMergeList);
      myStatusUpdater = null;
    }
    myMergeList.removeListener(myDividersRepainter);

    myMergeList = null;
    for (DiffDivider myDivider : myDividers) {
      myDivider.stopListenEditors();
    }
  }

  @Override
  public boolean canShowRequest(DiffRequest request) {
    return MergeTool.canShowRequest(request);
  }

  public void setDiffRequest(DiffRequest data) {
    setTitle(data.getWindowTitle());
    disposeMergeList();
    for (int i = 0; i < EDITORS_COUNT; i++) {
      getEditorPlace(i).setDocument(null);
    }
    LOG.assertTrue(!myDuringCreation);
    myDuringCreation = true;
    myProvider.putData(data.getGenericData());
    try {
      myData = data;
      String[] titles = myData.getContentTitles();
      for (int i = 0; i < myEditorsPanels.length; i++) {
        LabeledComponent editorsPanel = myEditorsPanels[i];
        editorsPanel.getLabel().setText(titles[i].isEmpty() ? " " : titles[i]);
      }
      createMergeList();
      data.customizeToolbar(myPanel.resetToolbar());
      myPanel.registerToolbarActions();
      if ( data instanceof MergeRequestImpl && myBuilder != null){
        Convertor<DialogWrapper, Boolean> preOkHook = new Convertor<DialogWrapper, Boolean>() {
          @Override
          public Boolean convert(DialogWrapper dialog) {
            ChangeCounter counter = ChangeCounter.getOrCreate(myMergeList);
            int changes = counter.getChangeCounter();
            int conflicts = counter.getConflictCounter();
            if (changes == 0 && conflicts == 0) return true;
            return Messages.showYesNoDialog(dialog.getRootPane(),
                                            DiffBundle.message("merge.dialog.apply.partially.resolved.changes.confirmation.message", changes, conflicts),
                                            DiffBundle.message("apply.partially.resolved.merge.dialog.title"),
                                            Messages.getQuestionIcon()) == Messages.YES;
          }
        };
        ((MergeRequestImpl)data).setActions(myBuilder, this, preOkHook);
      }
    }
    finally {
      myDuringCreation = false;
    }
  }

  private void setTitle(String windowTitle) {
    JDialog parent = getDialogWrapperParent();
    if (parent == null) return;
    parent.setTitle(windowTitle);
  }

  @Nullable
  private JDialog getDialogWrapperParent() {
    Component panel = myPanel;
    while (panel != null){
      if (panel instanceof JDialog) return (JDialog)panel;
      panel = panel.getParent();
    }
    return null;
  }

  public JComponent getComponent() {
    return myPanel;
  }

  @Nullable
  public JComponent getPreferredFocusedComponent() {
    return getEditorPlace(1).getContentComponent();
  }

  public int getContentsNumber() {
    return 3;
  }

  @Override
  public boolean acceptsType(DiffViewerType type) {
    return DiffViewerType.merge.equals(type);
  }

  private boolean hasAllEditors() {
    for (int i = 0; i < EDITORS_COUNT; i++) {
      if (getEditor(i) == null) return false;
    }
    return true;
  }

  @Nullable
  public MergeRequestImpl getMergeRequest() {
    return (MergeRequestImpl)(myData instanceof MergeRequestImpl ? myData : null);
  }

  private class MyEditingSides implements EditingSides {
    private final FragmentSide mySide;

    private MyEditingSides(FragmentSide side) {
      mySide = side;
    }

    @Nullable
    public Editor getEditor(FragmentSide side) {
      return MergePanel2.this.getEditor(mySide.getIndex() + side.getIndex());
    }

    public LineBlocks getLineBlocks() {
      return myMergeList.getChanges(mySide).getLineBlocks();
    }
  }

  private class MyScrollingPanel implements DiffPanelOuterComponent.ScrollingPanel {
    public void scrollEditors() {
      Editor centerEditor = getEditor(1);
      JComponent centerComponent = centerEditor.getContentComponent();
      if (centerComponent.isShowing()) {
        centerComponent.requestFocus();
      }
      int[] toLeft = getPrimaryBeginnings(myDividers[0].getPaint());
      int[] toRight = getPrimaryBeginnings(myDividers[1].getPaint());
      int line;
      if (toLeft.length > 0 && toRight.length > 0) {
        line = Math.min(toLeft[0], toRight[0]);
      }
      else if (toLeft.length > 0) {
        line = toLeft[0];
      }
      else if (toRight.length > 0) {
        line = toRight[0];
      }
      else {
        return;
      }
      SyncScrollSupport.scrollEditor(centerEditor, line);
    }

    private int[] getPrimaryBeginnings(DiffDividerPaint paint) {
      FragmentSide primarySide = paint.getLeftSide();
      LOG.assertTrue(getEditor(1) == paint.getSides().getEditor(primarySide));
      return paint.getSides().getLineBlocks().getBeginnings(primarySide, true);
    }
  }

  class DiffEditorState {
    private final int myIndex;
    private Document myDocument;

    private DiffEditorState(int index) {
      myIndex = index;
    }

    public void setDocument(Document document) {
      myDocument = document;
    }

    public Document getDocument() {
      return myDocument;
    }

    @Nullable
    public EditorEx createEditor() {
      Document document = getDocument();
      if (document == null) return null;
      Project project = myData.getProject();
      EditorEx editor = DiffUtil.createEditor(document, project, myIndex != 1);

      if (editor == null) return editor;
      //FileType type = getFileType();
      //editor.setHighlighter(HighlighterFactory.createHighlighter(project, type));
      if (myIndex == 0) editor.setVerticalScrollbarOrientation(EditorEx.VERTICAL_SCROLLBAR_LEFT);
      if (myIndex != 1) ((EditorMarkupModel)editor.getMarkupModel()).setErrorStripeVisible(true);
      editor.getSettings().setFoldingOutlineShown(false);
      editor.getFoldingModel().setFoldingEnabled(false);
      editor.getSettings().setLineMarkerAreaShown(false);
      editor.getSettings().setFoldingOutlineShown(false);
      editor.getGutterComponentEx().setShowDefaultGutterPopup(false);
      initEditorSettings(editor);

      return editor;
    }

    public FileType getFileType() {
      return getContentType();
    }

    @Nullable
    public Project getProject() {
      return myData == null ? null : myData.getProject();
    }
  }

  private static FileType getContentType(DiffRequest diffData) {
    FileType contentType = diffData.getContents()[1].getContentType();
    if (contentType == null) contentType = FileTypes.PLAIN_TEXT;
    return contentType;
  }

  private class MyDataProvider extends GenericDataProvider {
    public Object getData(String dataId) {
      if (FocusDiffSide.DATA_KEY.is(dataId)) {
        int index = getFocusedEditorIndex();
        if (index < 0) return null;
        switch (index) {
          case 0:
            return new BranchFocusedSide(FragmentSide.SIDE1);
          case 1:
            return new MergeFocusedSide();
          case 2:
            return new BranchFocusedSide(FragmentSide.SIDE2);
        }
      }
      else if (PlatformDataKeys.DIFF_VIEWER.is(dataId)) return MergePanel2.this;
      return super.getData(dataId);
    }

    private int getFocusedEditorIndex() {
      for (int i = 0; i < EDITORS_COUNT; i++) {
        Editor editor = getEditor(i);
        if (editor == null) continue;
        if (editor.getContentComponent().isFocusOwner()) return i;
      }
      return -1;
    }
  }

  private class BranchFocusedSide implements FocusDiffSide {
    private final FragmentSide mySide;

    private BranchFocusedSide(FragmentSide side) {
      mySide = side;
    }

    @Nullable
    public Editor getEditor() {
      return MergePanel2.this.getEditor(mySide.getMergeIndex());
    }

    public int[] getFragmentStartingLines() {
      return myMergeList.getChanges(mySide).getLineBlocks().getBeginnings(MergeList.BRANCH_SIDE);
    }
  }

  private class MergeFocusedSide implements FocusDiffSide {
    public Editor getEditor() {
      return MergePanel2.this.getEditor(1);
    }

    public int[] getFragmentStartingLines() {
      TIntHashSet beginnings = new TIntHashSet();
      if (myMergeList != null) {
        for (int i = 0; i < 2; i++) {
          FragmentSide branchSide = FragmentSide.fromIndex(i);
          beginnings.addAll(myMergeList.getChanges(branchSide).getLineBlocks().getBeginnings(MergeList.BASE_SIDE));
        }
      }
      int[] result = beginnings.toArray();
      Arrays.sort(result);
      return result;
    }
  }

  @Nullable
  public static MergePanel2 fromDataContext(DataContext dataContext) {
    DiffViewer diffComponent = PlatformDataKeys.DIFF_VIEWER.getData(dataContext);
    return diffComponent instanceof MergePanel2 ? (MergePanel2)diffComponent : null;
  }

  public MergeList getMergeList() {
    return myMergeList;
  }

  public void setColorScheme(EditorColorsScheme scheme) {
    for (Editor editor : getEditors()) {
      if (editor != null) {
        ((EditorEx)editor).setColorsScheme(scheme);
      }
    }
    myPanel.setColorScheme(scheme);
  }

  private class DividersRepainter implements ChangeList.Listener {

    @Override
    public void onChangeApplied(ChangeList source) {
      FragmentSide side = myMergeList.getSideOf(source);
      myDividers[side.getIndex()].repaint();
    }

    public void onChangeRemoved(ChangeList source) {
      FragmentSide side = myMergeList.getSideOf(source);
      myDividers[side.getIndex()].repaint();
    }
  }

  private static class StatusUpdater implements ChangeCounter.Listener {
    private final DiffPanelOuterComponent myPanel;

    private StatusUpdater(DiffPanelOuterComponent panel) {
      myPanel = panel;
    }

    public void onCountersChanged(ChangeCounter counter) {
      int changes = counter.getChangeCounter();
      int conflicts = counter.getConflictCounter();
      String text;
      if (changes == 0 && conflicts == 0) {
        text = DiffBundle.message("merge.dialog.all.conflicts.resolved.message.text");
      }
      else {
        // The Bundle doesn't support such complex formats. Until that is fixed, constructing manually
        //text = DiffBundle.message("merge.statistics.message", changes, conflicts);
        text = makeCountersText(changes, conflicts);
      }
      myPanel.setStatusBarText(text);
    }

    @NotNull
    private static String makeCountersText(int changes, int conflicts) {
      return makeCounterWord(changes, "change") + ". " + makeCounterWord(conflicts, "conflict");
    }

    @NotNull
    private static String makeCounterWord(int number, @NotNull String word) {
      if (number == 0) {
        return "No " + StringUtil.pluralize(word);
      }
      return number + " " + StringUtil.pluralize(word, number);
    }

    public void dispose(@NotNull MergeList mergeList) {
      ChangeCounter.getOrCreate(mergeList).removeListener(this);
    }

    public static StatusUpdater install(MergeList mergeList, DiffPanelOuterComponent panel) {
      ChangeCounter counters = ChangeCounter.getOrCreate(mergeList);
      StatusUpdater updater = new StatusUpdater(panel);
      counters.addListener(updater);
      updater.onCountersChanged(counters);
      return updater;
    }
  }

  public static class AsComponent extends JPanel{
    private final MergePanel2 myMergePanel;

    public AsComponent(@NotNull Disposable parent) {
      super(new BorderLayout());
      myMergePanel = new MergePanel2(null, parent);
      add(myMergePanel.getComponent(), BorderLayout.CENTER);
    }

    public MergePanel2 getMergePanel() {
      return myMergePanel;
    }

    @SuppressWarnings({"UnusedDeclaration"})
    public boolean isToolbarEnabled() {
      return myMergePanel.myPanel.isToolbarEnabled();
    }

    public void setToolbarEnabled(boolean enabled) {
      myMergePanel.myPanel.disableToolbar(!enabled);
    }
  }
}