summaryrefslogtreecommitdiff
path: root/platform/platform-impl/src/com/intellij/openapi/diff/ex/DiffPanelOptions.java
blob: ec7a316357e230529e5ebd6d36df77ab5c647013 (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
/*
 * Copyright 2000-2009 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.ex;

import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.diff.impl.DiffPanelImpl;
import com.intellij.openapi.diff.impl.DiffSideView;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.fileEditor.OpenFileDescriptor;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.DialogWrapperDialog;
import com.intellij.openapi.util.Disposer;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.awt.*;

/**
 * @author dyoma
 */
public class DiffPanelOptions {
  private static final Logger LOG = Logger.getInstance("#com.intellij.openapi.diff.ex.DiffPanelOptions");
  private final DiffPanelImpl myDiffPanel;
  private boolean myRequestFocusOnNewContent = true;
  private ShowSourcePolicy myShowSourcePolicy = ShowSourcePolicy.DEFAULT;

  public DiffPanelOptions(DiffPanelImpl diffPanel) {
    myDiffPanel = diffPanel;
  }

  public void onNewContent(DiffSideView currentSide) {
    if (myRequestFocusOnNewContent)
      currentSide.getFocusableComponent().requestFocus();
  }

  public void setRequestFocusOnNewContent(boolean requestFocusOnNewContent) {
    myRequestFocusOnNewContent = requestFocusOnNewContent;
  }

  public void setShowSourcePolicy(ShowSourcePolicy showSourcePolicy) {
    if (showSourcePolicy == null) {
      LOG.error("");
      return;
    }
    myShowSourcePolicy = showSourcePolicy;
  }

  public void showSource(@Nullable OpenFileDescriptor descriptor) {
    if (descriptor == null || myDiffPanel.getProject() == null) return;
    myShowSourcePolicy.showSource(descriptor, myDiffPanel);
  }

  public interface ShowSourcePolicy {
    void showSource(@NotNull OpenFileDescriptor descriptor, @NotNull DiffPanelImpl diffPanel);

    ShowSourcePolicy DONT_SHOW = new ShowSourcePolicy() {
      public void showSource(@NotNull OpenFileDescriptor descriptor, @NotNull DiffPanelImpl diffPanel) {}
    };

    ShowSourcePolicy OPEN_EDITOR = new ShowSourcePolicy() {
      public void showSource(@NotNull OpenFileDescriptor descriptor, @NotNull DiffPanelImpl diffPanel) {
        FileEditorManager.getInstance(diffPanel.getProject()).openTextEditor(descriptor, true);
      }
    };

    ShowSourcePolicy OPEN_EDITOR_AND_CLOSE_DIFF = new ShowSourcePolicy() {
      public void showSource(@NotNull OpenFileDescriptor descriptor, @NotNull DiffPanelImpl diffPanel) {
        OPEN_EDITOR.showSource(descriptor, diffPanel);
        if (diffPanel.getOwnerWindow() == null) return;
        Disposer.dispose(diffPanel);

        if (!dialogWrapperClose(diffPanel.getOwnerWindow())) {
          diffPanel.getOwnerWindow().setVisible(false);
          diffPanel.getOwnerWindow().dispose();
        }
      }

      private boolean dialogWrapperClose(Container window) {
        if (!(window instanceof DialogWrapperDialog)) return false;
        while (window instanceof DialogWrapperDialog) {
          DialogWrapperDialog dlg = (DialogWrapperDialog)window;
          window = window.getParent();
          dlg.getDialogWrapper().close(DialogWrapper.CLOSE_EXIT_CODE);
        }
        return true;
      }
    };

    ShowSourcePolicy DEFAULT = new ShowSourcePolicy() {
      public void showSource(@NotNull OpenFileDescriptor descriptor, @NotNull DiffPanelImpl diffPanel) {
        Window window = diffPanel.getOwnerWindow();
        if (window == null) return;
        else if (window instanceof Frame) OPEN_EDITOR.showSource(descriptor, diffPanel);
        else OPEN_EDITOR_AND_CLOSE_DIFF.showSource(descriptor, diffPanel);
      }
    };
  }
}