summaryrefslogtreecommitdiff
path: root/plugins/tasks/tasks-core/src/com/intellij/tasks/actions/OpenTaskDialog.java
blob: 6fe1467b1b47492ade784d57f141423481e874f2 (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
/*
 * 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.tasks.actions;

import com.intellij.ide.util.PropertiesComponent;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.options.binding.BindControl;
import com.intellij.openapi.options.binding.ControlBinder;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.intellij.openapi.ui.ValidationInfo;
import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vcs.AbstractVcs;
import com.intellij.openapi.vcs.VcsTaskHandler;
import com.intellij.openapi.vcs.VcsType;
import com.intellij.tasks.*;
import com.intellij.tasks.impl.TaskManagerImpl;
import com.intellij.tasks.impl.TaskUtil;
import com.intellij.ui.ColoredListCellRenderer;
import com.intellij.ui.components.JBCheckBox;
import com.intellij.ui.components.JBLabel;
import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * @author Dmitry Avdeev
 */
public class OpenTaskDialog extends DialogWrapper {
  private final static Logger LOG = Logger.getInstance("#com.intellij.tasks.actions.SimpleOpenTaskDialog");
  public static final String START_FROM_BRANCH = "start.from.branch";

  private JPanel myPanel;
  @BindControl(value = "clearContext", instant = true)
  private JCheckBox myClearContext;
  private JCheckBox myMarkAsInProgressBox;
  private JLabel myTaskNameLabel;
  private JPanel myVcsPanel;
  private JTextField myBranchName;
  private JTextField myChangelistName;
  private JBCheckBox myCreateBranch;
  private JBCheckBox myCreateChangelist;
  private JBLabel myFromLabel;
  private ComboBox myBranchFrom;

  private final Project myProject;
  private final Task myTask;
  private VcsTaskHandler myVcsTaskHandler;

  public OpenTaskDialog(@NotNull final Project project, @NotNull final Task task) {
    super(project, false);
    myProject = project;
    myTask = task;
    TaskManagerImpl taskManager = (TaskManagerImpl)TaskManager.getManager(myProject);
    setTitle("Open Task");
    myTaskNameLabel.setText(TaskUtil.getTrimmedSummary(task));
    myTaskNameLabel.setIcon(task.getIcon());

    TaskManagerImpl manager = (TaskManagerImpl)TaskManager.getManager(project);
    ControlBinder binder = new ControlBinder(manager.getState());
    binder.bindAnnotations(this);
    binder.reset();

    TaskRepository repository = task.getRepository();
    myMarkAsInProgressBox.setSelected(manager.getState().markAsInProgress);
    if (!TaskUtil.isStateSupported(repository, TaskState.IN_PROGRESS)) {
      myMarkAsInProgressBox.setVisible(false);
    }

    TaskManagerImpl.Config state = taskManager.getState();
    myClearContext.setSelected(state.clearContext);

    AbstractVcs vcs = manager.getActiveVcs();
    if (vcs == null) {
      myVcsPanel.setVisible(false);
    }
    else {
      ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
          updateFields(false);
        }
      };
      myCreateChangelist.addActionListener(listener);
      myCreateBranch.addActionListener(listener);
      myCreateChangelist.setSelected(manager.getState().createChangelist);

      if (vcs.getType() != VcsType.distributed) {
        myCreateBranch.setSelected(false);
        myCreateBranch.setVisible(false);
        myBranchName.setVisible(false);
        myFromLabel.setVisible(false);
        myBranchFrom.setVisible(false);
      }
      else {
        VcsTaskHandler[] handlers = VcsTaskHandler.getAllHandlers(project);
        for (VcsTaskHandler handler : handlers) {
          VcsTaskHandler.TaskInfo[] tasks = handler.getCurrentTasks();
          if (tasks.length > 0) {
            myVcsTaskHandler = handler;
            //noinspection unchecked
            myBranchFrom.setModel(new DefaultComboBoxModel(tasks));
            myBranchFrom.setEnabled(true);
            final String startFrom = PropertiesComponent.getInstance(project).getValue(START_FROM_BRANCH);
            VcsTaskHandler.TaskInfo info = null;
            if (startFrom != null) {
              info = ContainerUtil.find(tasks, new Condition<VcsTaskHandler.TaskInfo>() {
                @Override
                public boolean value(VcsTaskHandler.TaskInfo taskInfo) {
                  return startFrom.equals(taskInfo.getName());
                }
              });
            }
            if (info == null) {
              info = handler.getActiveTask();
            }
            myBranchFrom.setSelectedItem(info);
            myBranchFrom.addActionListener(new ActionListener() {
              @Override
              public void actionPerformed(ActionEvent e) {
                VcsTaskHandler.TaskInfo item = (VcsTaskHandler.TaskInfo)myBranchFrom.getSelectedItem();
                if (item != null) {
                  PropertiesComponent.getInstance(project).setValue(START_FROM_BRANCH, item.getName());
                }
              }
            });
            break;
          }
        }
        myCreateBranch.setSelected(manager.getState().createBranch && myBranchFrom.getItemCount() > 0);
        myBranchFrom.setRenderer(new ColoredListCellRenderer<VcsTaskHandler.TaskInfo>() {
          @Override
          protected void customizeCellRenderer(JList list, VcsTaskHandler.TaskInfo value, int index, boolean selected, boolean hasFocus) {
            if (value != null) {
              append(value.getName());
            }
          }
        });
      }

      myBranchName.setText(taskManager.suggestBranchName(task));
      myChangelistName.setText(taskManager.getChangelistName(task));
      updateFields(true);
    }
    init();
  }

  private void updateFields(boolean initial) {
    if (!initial && myBranchFrom.getItemCount() == 0 && myCreateBranch.isSelected()) {
      Messages.showWarningDialog(myPanel, "Can't create branch if no commit exists.\nCreate a commit first.", "Cannot Create Branch");
      myCreateBranch.setSelected(false);
    }
    myBranchName.setEnabled(myCreateBranch.isSelected());
    myFromLabel.setEnabled(myCreateBranch.isSelected());
    myBranchFrom.setEnabled(myCreateBranch.isSelected());
    myChangelistName.setEnabled(myCreateChangelist.isSelected());
  }


  @Override
  protected void doOKAction() {
    createTask();
    super.doOKAction();
  }

  public void createTask() {
    final TaskManagerImpl taskManager = (TaskManagerImpl)TaskManager.getManager(myProject);

    taskManager.getState().markAsInProgress = isMarkAsInProgress();
    taskManager.getState().createChangelist = myCreateChangelist.isSelected();
    taskManager.getState().createBranch = myCreateBranch.isSelected();

    TaskRepository repository = myTask.getRepository();
    if (isMarkAsInProgress() && repository != null) {
      try {
        repository.setTaskState(myTask, TaskState.IN_PROGRESS);
      }
      catch (Exception ex) {
        Messages.showErrorDialog(myProject, ex.getMessage(), "Cannot Set State For Issue");
        LOG.warn(ex);
      }
    }
    final LocalTask activeTask = taskManager.getActiveTask();
    final LocalTask localTask = taskManager.activateTask(myTask, isClearContext());
    if (myCreateChangelist.isSelected()) {
      taskManager.createChangeList(localTask, myChangelistName.getText());
    }
    if (myCreateBranch.isSelected()) {
      VcsTaskHandler.TaskInfo item = (VcsTaskHandler.TaskInfo)myBranchFrom.getSelectedItem();
      Runnable createBranch = new Runnable() {
        @Override
        public void run() {
          taskManager.createBranch(localTask, activeTask, myBranchName.getText());
        }
      };
      if (item != null && !item.equals(myVcsTaskHandler.getActiveTask())) {
        myVcsTaskHandler.switchToTask(item, createBranch);
      }
      else {
        createBranch.run();
      }
    }
    if (myTask.getType() == TaskType.EXCEPTION && AnalyzeTaskStacktraceAction.hasTexts(myTask)) {
      AnalyzeTaskStacktraceAction.analyzeStacktrace(myTask, myProject);
    }
  }

  @Nullable
  @Override
  protected ValidationInfo doValidate() {
    if (myCreateBranch.isSelected()) {
      String branchName = myBranchName.getText().trim();
      if (branchName.isEmpty()) {
        return new ValidationInfo("Branch name should not be empty", myBranchName);
      }
      else if (branchName.contains(" ")) {
        return new ValidationInfo("Branch name should not contain spaces");
      }
      else {
        return null;
      }
    }
    if (myCreateChangelist.isSelected()) {
      if (myChangelistName.getText().trim().isEmpty()) {
        return new ValidationInfo("Changelist name should not be empty");
      }
    }
    return null;
  }

  private boolean isClearContext() {
    return myClearContext.isSelected();
  }

  private boolean isMarkAsInProgress() {
    return myMarkAsInProgressBox.isSelected() && myMarkAsInProgressBox.isVisible();
  }

  @NonNls
  protected String getDimensionServiceKey() {
    return "SimpleOpenTaskDialog";
  }

  @Override
  public JComponent getPreferredFocusedComponent() {
    if (myCreateBranch.isSelected()) {
      return myBranchName;
    }
    else if (myCreateChangelist.isSelected()) {
      return myChangelistName;
    }
    else {
      return null;
    }
  }

  protected JComponent createCenterPanel() {
    return myPanel;
  }
}