View Javadoc

1   /**
2    * Copyright (C) 2008 Atlassian
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * 
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    * 
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.atlassian.theplugin.idea.action.crucible;
18  
19  import com.atlassian.theplugin.commons.crucible.CrucibleServerFacadeImpl;
20  import com.atlassian.theplugin.commons.crucible.api.model.PermId;
21  import com.atlassian.theplugin.idea.IdeaHelper;
22  import com.atlassian.theplugin.idea.crucible.CruciblePatchAddWorker;
23  import com.atlassian.theplugin.idea.crucible.ReviewData;
24  import com.intellij.openapi.actionSystem.AnAction;
25  import com.intellij.openapi.actionSystem.AnActionEvent;
26  import com.intellij.openapi.actionSystem.DataKeys;
27  import com.intellij.openapi.application.ApplicationManager;
28  import com.intellij.openapi.application.ModalityState;
29  import com.intellij.openapi.project.Project;
30  import com.intellij.openapi.vcs.changes.ChangeList;
31  
32  public class AddPatchToReviewAction extends AnAction {
33  
34      public void actionPerformed(AnActionEvent event) {
35          final ChangeList[] changes = DataKeys.CHANGE_LISTS.getData(event.getDataContext());
36          final Project project = DataKeys.PROJECT.getData(event.getDataContext());
37          final PermId permId = IdeaHelper.getCrucibleToolWindowPanel(event).getSelectedReviewId();
38  
39          new Thread(new Runnable() {
40              public void run() {
41                  ApplicationManager.getApplication().invokeAndWait(
42                          new CruciblePatchAddWorker(CrucibleServerFacadeImpl.getInstance(), permId, project, changes),
43                          ModalityState.defaultModalityState());
44              }
45          }).start();
46      }
47  
48      public void update(AnActionEvent event) {
49          super.update(event);
50          final ChangeList[] changes = DataKeys.CHANGE_LISTS.getData(event.getDataContext());
51          if (changes != null && changes.length == 1) {
52              event.getPresentation().setEnabled(true);
53  
54              if (IdeaHelper.getCrucibleToolWindowPanel(event) != null) {
55                  if (event.getPresentation().isEnabled()) {
56                      if (IdeaHelper.getCrucibleToolWindowPanel(event).getSelectedReview() == null) {
57                          event.getPresentation().setEnabled(false);
58                      } else {
59                          ReviewData rd = IdeaHelper.getCrucibleToolWindowPanel(event).getSelectedReview();
60                          event.getPresentation().setEnabled(rd.getCreator().getUserName().equals(rd.getServer().getUsername()));
61                      }
62                  }
63              } else {
64                  event.getPresentation().setEnabled(false);
65              }
66          } else {
67              event.getPresentation().setEnabled(false);
68          }
69      }
70  }