summaryrefslogtreecommitdiff
path: root/plugins/github/src/org/jetbrains/plugins/github/ui/GithubCreatePullRequestDialog.java
blob: f062742d191c59c2c320420c69b2eb61d11df5e8 (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
/*
 * Copyright 2000-2013 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 org.jetbrains.plugins.github.ui;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.Consumer;
import com.intellij.util.ui.UIUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.annotations.TestOnly;
import org.jetbrains.plugins.github.GithubCreatePullRequestWorker;
import org.jetbrains.plugins.github.api.GithubFullPath;
import org.jetbrains.plugins.github.util.GithubProjectSettings;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collection;
import java.util.Collections;

/**
 * @author Aleksey Pivovarov
 */
public class GithubCreatePullRequestDialog extends DialogWrapper {
  @NotNull private final GithubCreatePullRequestPanel myGithubCreatePullRequestPanel;
  @NotNull private final GithubCreatePullRequestWorker myWorker;
  @NotNull private final GithubProjectSettings myProjectSettings;

  public GithubCreatePullRequestDialog(@NotNull GithubCreatePullRequestWorker worker) {
    super(worker.getProject(), true);
    myWorker = worker;

    myProjectSettings = GithubProjectSettings.getInstance(myWorker.getProject());

    myGithubCreatePullRequestPanel = new GithubCreatePullRequestPanel();

    myGithubCreatePullRequestPanel.getShowDiffButton().addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        myWorker.showDiffDialog(myGithubCreatePullRequestPanel.getBranch());
      }
    });
    myGithubCreatePullRequestPanel.getSelectForkButton().addActionListener(new ActionListener() {
      @Override
      public void actionPerformed(ActionEvent e) {
        showTargetDialog();
      }
    });
    myGithubCreatePullRequestPanel.getBranchComboBox().addItemListener(new ItemListener() {
      @Override
      public void itemStateChanged(ItemEvent e) {
        if (e.getStateChange() == ItemEvent.SELECTED) {
          if (myWorker.canShowDiff()) {
            myGithubCreatePullRequestPanel.setBusy(true);
            myWorker.getDiffDescriptionInPooledThread(getTargetBranch(), new Consumer<GithubCreatePullRequestWorker.DiffDescription>() {
              @Override
              public void consume(final GithubCreatePullRequestWorker.DiffDescription info) {
                UIUtil.invokeLaterIfNeeded(new Runnable() {
                  @Override
                  public void run() {
                    if (info == null) {
                      myGithubCreatePullRequestPanel.setBusy(false);
                      return;
                    }
                    if (getTargetBranch().equals(info.getBranch())) {
                      myGithubCreatePullRequestPanel.setBusy(false);
                      if (myGithubCreatePullRequestPanel.isTitleDescriptionEmptyOrNotModified()) {
                        myGithubCreatePullRequestPanel.setTitle(info.getTitle());
                        myGithubCreatePullRequestPanel.setDescription(info.getDescription());
                      }
                    }
                  }
                });
              }
            });
          }
        }
      }
    });

    setTitle("Create Pull Request - " + myWorker.getCurrentBranch());
    init();
  }

  @Override
  public void show() {
    GithubFullPath defaultForkPath = myProjectSettings.getCreatePullRequestDefaultRepo();
    if (defaultForkPath != null) {
      setTarget(defaultForkPath);
    }
    else {
      if (!showTargetDialog(true)) {
        close(CANCEL_EXIT_CODE);
        return;
      }
    }
    super.show();
  }

  private boolean showTargetDialog() {
    return showTargetDialog(false);
  }

  private boolean showTargetDialog(boolean firstTime) {
    GithubFullPath forkPath = myWorker.showTargetDialog(firstTime);
    if (forkPath == null) {
      return false;
    }
    return setTarget(forkPath);
  }

  private boolean setTarget(@NotNull GithubFullPath forkPath) {
    GithubCreatePullRequestWorker.GithubTargetInfo forkInfo = myWorker.setTarget(forkPath);
    if (forkInfo == null) {
      return false;
    }
    myProjectSettings.setCreatePullRequestDefaultRepo(forkPath);
    myGithubCreatePullRequestPanel.setDiffEnabled(myWorker.canShowDiff());
    updateBranches(forkInfo.getBranches(), forkPath);
    return true;
  }

  private void updateBranches(@NotNull Collection<String> branches, @NotNull GithubFullPath forkPath) {
    myGithubCreatePullRequestPanel.setBranches(branches);

    String configBranch = myProjectSettings.getCreatePullRequestDefaultBranch();
    if (configBranch != null) myGithubCreatePullRequestPanel.setSelectedBranch(configBranch);

    myGithubCreatePullRequestPanel.setForkName(forkPath.getFullName());
  }

  @Override
  protected void doOKAction() {
    if (myWorker.checkAction(getTargetBranch())) {
      myProjectSettings.setCreatePullRequestDefaultBranch(getTargetBranch());
      myWorker.performAction(getRequestTitle(), getDescription(), getTargetBranch());
      super.doOKAction();
    }
  }

  @Nullable
  @Override
  protected JComponent createCenterPanel() {
    return myGithubCreatePullRequestPanel.getPanel();
  }

  @Nullable
  @Override
  public JComponent getPreferredFocusedComponent() {
    return myGithubCreatePullRequestPanel.getPreferredComponent();
  }

  @Override
  protected String getHelpId() {
    return "github.create.pull.request.dialog";
  }

  @Override
  protected String getDimensionServiceKey() {
    return "Github.CreatePullRequestDialog";
  }

  @NotNull
  private String getRequestTitle() {
    return myGithubCreatePullRequestPanel.getTitle();
  }

  @NotNull
  private String getDescription() {
    return myGithubCreatePullRequestPanel.getDescription();
  }

  @NotNull
  private String getTargetBranch() {
    return myGithubCreatePullRequestPanel.getBranch();
  }

  @Nullable
  @Override
  protected ValidationInfo doValidate() {
    if (StringUtil.isEmptyOrSpaces(getRequestTitle())) {
      return new ValidationInfo("Title can't be empty'", myGithubCreatePullRequestPanel.getTitleTextField());
    }
    return null;
  }

  @TestOnly
  public void testSetRequestTitle(String title) {
    myGithubCreatePullRequestPanel.setTitle(title);
  }

  @TestOnly
  public void testSetBranch(String branch) {
    myGithubCreatePullRequestPanel.setBranches(Collections.singleton(branch));
  }

  @TestOnly
  public void testCreatePullRequest() {
    myWorker.performAction(getRequestTitle(), getDescription(), getTargetBranch());
  }

  @TestOnly
  public void testSetTarget(@NotNull GithubFullPath forkPath) {
    GithubCreatePullRequestWorker.GithubTargetInfo forkInfo = myWorker.setTarget(forkPath);
    if (forkInfo == null) {
      doCancelAction();
      return;
    }
    myGithubCreatePullRequestPanel.setDiffEnabled(myWorker.canShowDiff());
  }
}